4

In my homework I have to define the logic operators as follows:
Using this data structure:

data MyBool = Cierto|Falso deriving (Show,Eq) -- Cierto = True and Falso = False
data PQR = A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z deriving (Show,Eq)
data Formula = VarProp PQR 
             |Neg Formula -- logic not
             |Formula :|: Formula -- logic or
             |Formula :&: Formula -- logic and... etc
             |Formula :->: Formula
             |Formula :<->: Formula deriving (Show,Eq)

And I have to define functions that tell me if a given formula is True or False, so for example if I write (Cierto :&: Falso) the answer has to be: Falso.

According to my teacher the function has to be called in this case :&: and has to receive MyBool types so I tried to implemented like this:

infixr 3 :&:
(:&:) :: MyBool -> MyBool -> MyBool
Cierto :&: x = x
Falso :&: x = Falso

but when I try to load it it says:

Invalid type signature

I don't know what I doing wrong here.

1
  • Hmm, why did you undo my edit? I didn't change any of the code; I merely reformatted it. The <code> tag doesn't work on this site; you should preface each line in a code block with four spaces. Commented Dec 5, 2011 at 6:40

2 Answers 2

7

Your professor is forcing you to create an Abstract Syntax Tree(AST) for boolean operators. if you have not heard the words "Abstract Syntax Tree" prior to this, it is time to ask someone what the heck is going on.

What you in fact want to do is write a function (called say eval) with the type Formula -> Formula. In looking at the definition, I also believe you have left a line out of data Formula that should be something like VarLiteral MyBool. It looks like you AST is a way of writing programs which operate on MyBool's and supports the typical boolean operations along with :->: assign (?).

I've written a few AST evaluators in Haskell (though nothing this corny :) ) and it feels like there are a few pieces missing in your question. My best advice for you, given what I have in front of me, is to say that this assignment is one level more abstract than you think it is.

Best of Luck

Sign up to request clarification or add additional context in comments.

2 Comments

at first I thought that to, that I need it to write a function called eval or something like that with the type Formula -> Formula but someone asked that cuestion to the prosfessor and he sayed that no the function is called :&: and the type is MyBool -> MyBool and that I shoud write it as an infix operator.
As I said in my (edited) answer, :&: is a type constructor. You got it for free when you defined data Formula = ... Formual :&: Formula.... Now you just have to add a way to turn a single MyBool into a Formula (the VarLiteral MyBool in John's answer).
6

The issue is that a : at the front of a function denotes a data constructor; it's like starting it with a capital letter. You should rename :&: to something like |&|.

Edit: Never mind, I just realized what you're actually trying to accomplish.

The :&: is supposed to take two MyBools and create a Formula rather than another MyBool. You tried to implement the :&: as a function; it is a data constructor. You've already declared it in the data Forumla = ... expression.

You do not need the function declaration at all. Delete the following block of code completely:

infixr 3 :&:
(:&:) :: MyBool -> MyBool -> MyBool
Cierto :&: x = x
Falso :&: x = Falso

You should then be able to use :&: to take two MyBools and create a Formula without adding any other code.

However, having :&: actually act on MyBools is not general enough. We want to be able to and together both expressions and booleans. Thus, you actually want :&: to combine Formulas. This is what your code already does. What you are missing is a constructor like Literal that takes a MyBool and returns a Formula representing that boolean.

7 Comments

Isn't it supposed tobea type constructor? It looks like one of the cases of Formula to me
As I understood it, the :&: is supposed to be logical and. The same as && for normal booleans.
Could be. I guessed that the need was a function to evaluate a Formula to a MyBool. PQR is a mystery to me.
If I had to guess, PQR is somehow supposed to represent variables in predicate calculus.
:&: at the value level is a "data constructor" not a "type constructor".
|

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.