Commit c62600f8 authored by Job Cuppen's avatar Job Cuppen

ex 8.3 rest with copying gEq

parent f64c8750
......@@ -10,12 +10,11 @@ module genericMap
import StdEnv, StdGeneric
generic gMap a b :: a -> b
gMap{|Int|} x = x
gMap{|Real|} x = x
gMap{|UNIT|} x = x
gMap{|PAIR|} f g (PAIR x y) = PAIR (f x) (g y)
gMap{|Int|} x = x
gMap{|Real|} x = x
gMap{|UNIT|} x = x
gMap{|PAIR|} f g (PAIR x y) = PAIR (f x) (g y)
gMap{|EITHER|} f g (LEFT x) = LEFT (f x)
gMap{|EITHER|} f g (RIGHT x) = RIGHT (g x)
gMap{|CONS|} f (CONS x) = CONS (f x)
......@@ -41,3 +40,36 @@ tuplize n = (n, fac n)
// apply the factorial function to all integers in (l, t).
//Start = gMap{|*->*->*|} (gMap{|*->*|} fac) (gMap{|*->*|} fac) (l,t)
generic gEq a :: !a !a -> Bool
gEq{|Int|} x y = x == y
gEq{|Char|} x y = x == y
gEq{|Bool|} x y = x == y
gEq{|Real|} x y = x == y
gEq{|String|} x y = x == y
gEq{|UNIT|} UNIT UNIT = True
gEq{|PAIR|} fx fy (PAIR x1 y1) (PAIR x2 y2) = fx x1 x2 && fy y1 y2
gEq{|EITHER|} fl fr (LEFT x) (LEFT y) = fl x y
gEq{|EITHER|} fl fr (LEFT _) (RIGHT _) = False
gEq{|EITHER|} fl fr (RIGHT x) (RIGHT y) = fr x y
gEq{|EITHER|} fl fr (RIGHT _) (LEFT _) = False
gEq{|CONS|} f (CONS x) (CONS y) = f x y
gEq{|RECORD|} f (RECORD x) (RECORD y) = f x y
gEq{|FIELD|} f (FIELD x) (FIELD y) = f x y
gEq{|OBJECT|} f (OBJECT x) (OBJECT y) = f x y
derive gEq [], (), (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
// compute the equality of [1,2] and [1,2];
//Start = gEq{|*->*|} gEq{|*|} [1,2] [1,2]
// compute the equality of [1,2] and [2,3];
//Start = gEq{|*->*|} gEq{|*|} [1,2] [2,3]
// compute the equality of [1,2] and [2,3]
// where you use the less-then operator <
// for the elements of the list.
// This last item is only to show you the
// possibilities of the generic mechanism,
// it is not necessarily a recommended way of working.
//Start = gEq{|*->*|} (\x y-> not (x<y) && not (y<x)) [1,2] [2,3]
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment