-- -- -- Base -- -- -- | Type class for equality class Eq a where (==) (x: a) (y: a) : Bool (/=) (x: a) (y: a) : Bool -- | Eq instances for basic types instance Eq Integer where (==) x y := x = y (/=) x y := not (x == y) instance Eq Float where (==) x y := x = y (/=) x y := not (x == y) instance Eq String where (==) x y := x = y (/=) x y := not (x == y) instance Eq Bool where (==) x y := x = y (/=) x y := not (x == y) instance Eq Char where (==) x y := x = y (/=) x y := not (x == y) instance {Eq a} Eq (Tensor a) where (==) t1 t2 := t1 = t2 (/=) t1 t2 := not (t1 == t2) def eq {Eq a} : Matcher a := matcher | #$val as () with | $tgt -> if val == tgt then [()] else [] | $ as something with | $tgt -> [tgt] def bool : Matcher Bool := eq def char : Matcher Char := eq def integer : Matcher Integer := eq def float : Matcher Float := eq class Num a where (+) (x: a) (y: a) : a (-) (x: a) (y: a) : a (*) (x: a) (y: a) : a (/) (x: a) (y: a) : a --instance Num Integer where -- (+) x y := i.+ x y -- (-) x y := i.- x y -- (*) x y := i.* x y -- (/) x y := i./ x y instance Num MathExpr where (+) x y := plusForMathExpr x y (-) x y := minusForMathExpr x y (*) x y := multForMathExpr x y (/) x y := divForMathExpr x y instance Num Float where (+) x y := f.+ x y (-) x y := f.- x y (*) x y := f.* x y (/) x y := f./ x y -- -- Utility -- def id {a} (x: a) : a := x def fst {a, b} (x: a, _: b) : a := x def snd {a, b} (_: a, y: b) : b := y def ($) {a, b} (f: a -> b) (x: a) : b := f x def compose {a, b, c} (f: a -> b) (g: b -> c) : a -> c := \x -> g (f x) def flip {a, b, c} (fn: a -> b -> c) : b -> a -> c := \x y -> fn y x def eqAs {Eq a} (m: Matcher a) (x: a) (y: a) : Bool := match x as m with | #y -> True | _ -> False def curry {a, b, c} (f: (a, b) -> c) (x: a) (y: b) : c := f (x, y) def uncurry {a, b, c} (f: a -> b -> c) (x: a, y: b) : c := f x y -- -- Boolean -- def (&&) (b1: Bool) (b2: Bool) : Bool := if b1 then b2 else False def (||) (b1: Bool) (b2: Bool) : Bool := if b1 then True else b2 def not (b: Bool) : Bool := if b then False else True -- -- Unordered Pair -- def unorderedPair {a} (m: Matcher a) : Matcher (a, a) := matcher | ($, $) as (m, m) with | ($x, $y) -> [(x, y), (y, x)] | $ as something with | $tgt -> [tgt]