-
Notifications
You must be signed in to change notification settings - Fork 0
/
e3.mana
51 lines (38 loc) · 1.64 KB
/
e3.mana
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
------------------------------------------------------------------------------
--
-- Generic Expression Gadt
--
------------------------------------------------------------------------------
namespace E3
import Core
------------------------------------------------------------------------------
-- E - expressions
------------------------------------------------------------------------------
type E : Type -> Type where
EX : a -> E a
EApply : E (a -> b) -> E a -> E b
------------------------------------------------------------------------------
--
------------------------------------------------------------------------------
lift1 : (a -> b) -> E a -> E b
lift1 f e = EApply (EX f) e
lift2 : (a -> b -> c) -> E a -> E b -> E c
lift2 f e d = EApply (EApply (EX f) e) d
instance FromInteger (E Int) where fromInteger = EX . fromInteger
instance Add (E Int) where (+) = lift2 (+)
instance Multiply (E Int) where (*) = lift2 (*)
(===) = lift2 (== : Int -> Int -> Bool)
------------------------------------------------------------------------------
--
------------------------------------------------------------------------------
eval : E a -> a
eval (EX x ) = x
eval (EApply f x) = eval f (eval x)
------------------------------------------------------------------------------
--
------------------------------------------------------------------------------
main = do
print <| eval <| (5 + 3 : E Int)
print <| eval <| 5 === 3
print <| eval <| lift1 not (5 === 3)
print <| eval <| lift2 (+) (EX "hello ") (EX "world")