Haskell Trees Mapping -


i've been trying write simple mapping function using haskell, , came following problem:

data tree = leaf | node (tree a) (tree a) deriving(show) let g (leaf l) = leaf(l+1) let map g (leaf l) = g(l) let lf = (leaf 2) 

for g(lf) output leaf 3 excepted, map g lf i'm getting following error :

    couldn't match type `integer' `tree a0' expected type: tree (tree a0)   actual type: tree integer in second argument of `map', namely `lf' in expression: map g lf in equation `it': = map g lf 

i don't know why error , i'd grateful if point me solution.

this type system showing logic of program doesn't work out. can track details adding lot of type information in hand see if our intuition matches details of checker.

data tree = leaf | node (tree a) (tree a) deriving (show)  g :: num => tree -> tree g (leaf l) = leaf (l + 1)  map :: (a -> b) -> tree -> b map g (leaf l) = g(l)  lf :: num => tree lf = (leaf 2) 

now @ failing expression

(map :: (a -> b) -> tree -> b)   (g :: num c => tree c -> tree c)   (lf :: num d => tree d) 

we'll notice (a -> b) has match (num c => tree c -> tree c) giving a tree c , b.

(map g :: tree (tree c) -> tree c)   (lf :: num d => tree d) 

and notice d must match tree c giving us

map g lf :: num (tree c) => tree (tree c) -> tree c 

now know we're sunk because trees aren't numbers, error got different. it's saying tree c isn't integer.

this occurs because haskell, when don't provide type signatures, tries helpful , guess concrete types might dealing with. when this, picks less polymorphic ones strictly possible. in case, decided lf :: tree integer instead of lf :: num d => tree d former more concrete.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -