2

I am a new learner in Haskell, the notion of Mutually recursive types confused me a lot.

Here is an example:

data BoolExpr
     = BoolConst Bool
     | BoolOp BoolOp BoolExpr BoolExpr
     | ConpOp CompOp IntExpr IntExpr

Why here are two BoolOp and CompOp?

I know that the first BoolOp is a data constructor, but how about the second BoolOp?

2
  • 1
    Where is this code from? It'll be easier to explain with more context. Commented Aug 21, 2012 at 1:01
  • 1
    I don't see mutually recursive types; I just see a single recursive data type, BoolExpr. Am I missing something? Commented Aug 21, 2012 at 1:09

1 Answer 1

7

You're right that the first BoolOp is a constructor; the second says that the constructor takes a value of type :: BoolOp. The full type is:

BoolOp :: BoolOp -> BoolExpr -> BoolExpr -> BoolExpr

So I'd expect there's some code like this lying around:

data BoolOp = BoolAnd  |  BoolOr  |  BoolXor

So to use this constructor, you'd need to give it a BoolOp and two BoolExprs:

myBoolExpr :: BoolExpr
myBoolExpr = BoolOp  BoolOr  (BoolConst True)  (BoolConst False)
Sign up to request clarification or add additional context in comments.

3 Comments

@Kevin quick, say it fives times fast while holding your tongue!
@MattFenwick so i can change the name of BoolOp, can i ? like: data BoolExpr = a BoolOp BoolExpr BoolExpr. is that correct?
@user1611996 you can change it, but constructors have to start with a capital letter, so a won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.