-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnbe-hoas3.hs
61 lines (46 loc) · 1.16 KB
/
nbe-hoas3.hs
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
52
53
54
55
56
57
58
59
60
-- starting from nbe-hoas3.agda
--module nbe-hoas3 where
{-# LANGUAGE Rank2Types #-}
{-
class TM a where
lam :: (a -> a) -> a
app :: a -> a -> a
-- Open HOAS terms
type OTm a = TM a => a
-}
type OTm a = ((a -> a) -> a) -> (a -> a -> a) -> a
-- Close HOAS terms
type Tm = forall a. OTm a
data Sem v
= L (Sem v -> Sem v)
| A (Sem v) (Sem v)
| V v
eval :: Tm -> Sem v
eval f = f L app' where
app' (L f) x = f x
app' f x = A f x
reify :: Sem exp -> OTm exp
reify s lam app = go s where
go (L f) = lam $ go . f . V
go (A n d) = app (go n) (go d)
go (V v) = v
norm :: Tm -> Tm
norm = reify . eval
idTm :: Tm
idTm lam app = lam $ \x -> x
apTm :: Tm
apTm lam app = lam $ \x -> lam $ \y -> x `app` y
t1 :: Tm
t1 lam app = apTm lam app `app` idTm lam app
t2 :: Tm
t2 lam app = lam $ \z -> ((lam $ \x -> lam $ \y -> x `app` y) `app` z) `app` (lam $ \x -> lam $ \y -> x `app` y)
data DB a
= Var a
| App (DB a) (DB a)
| Lam (DB (Maybe a))
deriving (Show)
data H = H
h = h
newtype D = D { unD :: forall a. a -> DB a }
db :: Tm -> DB ()
db t = unD (t (\g -> D $ \x -> Lam $ unD (g (D Var)) Nothing) (\(D t) (D u) -> D (\x -> App (t x) (u x)))) ()