7. Haskell type-classes

1. Enroll the type Extended shown below, in class Eq:

data Extended = Infinity | Value Integer 

2. Enroll the type Formula a in class Eq:

data Formula a = Atom a |
                 Or (Formula a) (Formula a) |
                 And (Formula a) (Formula a) |
                 Not (Formula a)

3. Enroll the type Set a, defined below, in class Num. Implement operation (+) as set reunion and (*) as set intersection. Implement also fromInteger which takes an integer x and returns the set {x}.

data Set a = F (a->Bool)

4. In the last lab, we have worked with the following datatypes and functions:

data PExpr = Val Integer |
             Var String  |
             PExpr :+: PExpr 
 
eval_pexpr :: Dict -> PExpr -> Integer
 
data BExpr = PExpr :==: PExpr | 
            PExpr :<: PExpr |
            Not BExpr |
            BExpr :&&: BExpr 
 
eval_bexpr :: Dict -> BExpr -> Bool
 
data Prog = PlusPlus Var |       
            Var :=: PExpr |     
            DeclareInt Var |     
            Begin Prog Prog |     
            While BExpr Prog |     
            If BExpr Prog Prog      
 
eval :: Dict -> Prog -> Dict                         

We have three different datatypes and three evaluation methods, one for each type.

  • The first type models arithmetic expressions and it's evaluation produces an integer
  • The second type models boolean expressions (comparisons) and evaluates to a boolean
  • The third type models programs and evaluates to a dictionary, holding the final values for each variable

Design a class called Eval which contains a unique eval method. How should the class parameters be?

Enrol types PExpr, BExpr and Prog in this class, thus eliminating the need for multiple evaluation function names.