Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining logic operators in Haskell

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.

like image 965
Carlos Martinez Avatar asked Dec 27 '22 10:12

Carlos Martinez


2 Answers

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

like image 159
John F. Miller Avatar answered Jan 04 '23 23:01

John F. Miller


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.

like image 39
Tikhon Jelvis Avatar answered Jan 05 '23 01:01

Tikhon Jelvis