Convert Either to Tree - Occurs check
hello, i want convert "Either" to a tree. Example: Either ( Either 1 2 ) ( Either 3 4) ---- > Branch ( Branch (Leafl 1) (Leafr2) ) ( Branch (Leafl 3) (Leafr4)) ) Code: data TreeE a b = Empty |Leafl a | Leafr b | Branch (TreeE a b ) (TreeE a b) deriving Show f3 (Right b) (Branch l r) =case( isRight(b) || isLeft(b) )of true -> Branch l (f3 b r) false -> Branch l (Leafl b) f3 (Left b) (Branch l r) = case( isRight(b) || isLeft(b) )of true -> Branch (f3 b l) r false -> Branch (Leafl b) r Error: Occurs check: cannot construct the infinite type: b = Either a b Expected type: Either a b -> TreeE t t1 -> TreeE t t1 Inferred type: Either a (Either a b) -> TreeE t t1 -> t2 In the second argument of `Branch', namely `(f3 b r)' In the expression: Branch l (f3 b r) I don't understand why this happen... Can anyone help me?
André Batista Martins asked:
i want convert "Either" to a tree. Example: Either ( Either 1 2 ) ( Either 3 4) ---- > Branch ( Branch (Leafl 1) (Leafr2) ) ( Branch (Leafl 3) (Leafr4)) )
Before writing the function to convert your data structure, why not try writing down the precise type signature you expect it to have. In fact, before that, try writing down the precise type signature of the thing you call an "Either". -- Dan
Tks for the answer,
the data structure of Either is:
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)
one example of what i want convert is:
Left(Right(Left(Left())))
No dia 22 de Outubro de 2010 04:58, Dan Piponi
André Batista Martins asked:
i want convert "Either" to a tree. Example: Either ( Either 1 2 ) ( Either 3 4) ---- > Branch ( Branch (Leafl 1) (Leafr2) ) ( Branch (Leafl 3) (Leafr4)) )
Before writing the function to convert your data structure, why not try writing down the precise type signature you expect it to have.
In fact, before that, try writing down the precise type signature of the thing you call an "Either". -- Dan
On 22/10/10 09:23, André Batista Martins wrote:
Tks for the answer, the data structure of Either is:
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)
one example of what i want convert is: Left(Right(Left(Left())))
Hi, The problem here is that the type of Left () is:
Either () a
The type of Left (Left ()) is:
Either (Either () a) b
The type of Right (Left (Left ())) is:
Either c (Either (Either () a) b)
and finally, the type of Left (Right (Left (Left ()))) is:
Either (Either c (Either (Either () a) b)) d
That is, each level in the tree must have a different type. For this reason, you can't sensibly use Either for tree types of varying depth (a type-class would help, but I doubt it's what you want). A sensible type for a tree is the one you gave in your original post, TreeE. So why do you want to encode the tree with Either (not really possible) and then convert to your TreeE type? Why not just start out with the values in your tree type? Thanks, Neil.
Tks for the answser,
I need to continue a work that has already been developed. In this work i
have Eithers and i want convert to another datatype with more information,
because I want to generate Eithers, which will contain the information from
the first but with positions exchanged.
No dia 22 de Outubro de 2010 11:30, Neil Brown
On 22/10/10 09:23, André Batista Martins wrote:
Tks for the answer, the data structure of Either is:
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)
one example of what i want convert is: Left(Right(Left(Left())))
Hi,
The problem here is that the type of Left () is:
Either () a
The type of Left (Left ()) is:
Either (Either () a) b
The type of Right (Left (Left ())) is:
Either c (Either (Either () a) b)
and finally, the type of Left (Right (Left (Left ()))) is:
Either (Either c (Either (Either () a) b)) d
That is, each level in the tree must have a different type. For this reason, you can't sensibly use Either for tree types of varying depth (a type-class would help, but I doubt it's what you want). A sensible type for a tree is the one you gave in your original post, TreeE. So why do you want to encode the tree with Either (not really possible) and then convert to your TreeE type? Why not just start out with the values in your tree type?
Thanks,
Neil.
participants (3)
-
André Batista Martins -
Dan Piponi -
Neil Brown