equational-reasoning

Haskell - How to transform map sum (map (x:) xss) to map (x+) (map sum xss)

空扰寡人 提交于 2020-02-21 12:05:33
问题 Reading "Thinking Functionally with Haskell" I came across a part of a program calculation that required that map sum (map (x:) xss) be rewritten as map (x+) (map sum xss) Intuitively I know that it makes sense ... if you have some lists that you are going to sum but, before summing, to those same lists you are also going to add one element 'x', then that is the same as taking a list of sums of the origninal lists and adding x's value to each of them. But I would like to know how to transform

Haskell - How to transform map sum (map (x:) xss) to map (x+) (map sum xss)

ⅰ亾dé卋堺 提交于 2020-02-21 12:05:08
问题 Reading "Thinking Functionally with Haskell" I came across a part of a program calculation that required that map sum (map (x:) xss) be rewritten as map (x+) (map sum xss) Intuitively I know that it makes sense ... if you have some lists that you are going to sum but, before summing, to those same lists you are also going to add one element 'x', then that is the same as taking a list of sums of the origninal lists and adding x's value to each of them. But I would like to know how to transform

Is it possible to use church encodings without breaking equational reasoning?

风格不统一 提交于 2019-12-04 16:43:08
问题 Mind this program: {-# LANGUAGE RankNTypes #-} import Prelude hiding (sum) type List h = forall t . (h -> t -> t) -> t -> t sum_ :: (Num a) => List a -> a sum_ = \ list -> list (+) 0 toList :: [a] -> List a toList = \ list cons nil -> foldr cons nil list sum :: (Num a) => [a] -> a -- sum = sum_ . toList -- does not work sum = \ a -> sum_ (toList a) -- works main = print (sum [1,2,3]) Both definitions of sum are identical up to equational reasoning. Yet, compiling the second definition of

Is it possible to use church encodings without breaking equational reasoning?

左心房为你撑大大i 提交于 2019-12-03 10:36:57
Mind this program: {-# LANGUAGE RankNTypes #-} import Prelude hiding (sum) type List h = forall t . (h -> t -> t) -> t -> t sum_ :: (Num a) => List a -> a sum_ = \ list -> list (+) 0 toList :: [a] -> List a toList = \ list cons nil -> foldr cons nil list sum :: (Num a) => [a] -> a -- sum = sum_ . toList -- does not work sum = \ a -> sum_ (toList a) -- works main = print (sum [1,2,3]) Both definitions of sum are identical up to equational reasoning. Yet, compiling the second definition of works, but the first one doesn't, with this error: tmpdel.hs:17:14: Couldn't match type ‘(a -> t0 -> t0) ->