-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSyntaxTranslate.hs
More file actions
158 lines (117 loc) · 4.16 KB
/
Copy pathSyntaxTranslate.hs
File metadata and controls
158 lines (117 loc) · 4.16 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{-# LANGUAGE GADTs, RankNTypes, TypeFamilies,DataKinds, KindSignatures,TypeOperators, NoMonomorphismRestriction #-}
module SyntaxTranslate where
import KeyM
import FKeyMap
import KExp
import HList
import PFunctor
import Control.Applicative hiding (empty)
import Prelude hiding (lookup, id, fst,snd, curry, uncurry)
data Phoas v a where
PVar :: v a -> Phoas v a
PLam :: (v a -> Phoas v b) -> Phoas v (a -> b)
PApp :: Phoas v (a -> b) -> Phoas v a -> Phoas v b
instance Hoas (Phoas f) where
lam f = PLam (f . PVar)
app = PApp
type ClosedPHoas a = forall f. Phoas f a
data Bruijn l a where
BVar :: Index l a -> Bruijn l a
BLam :: Bruijn (a ': l) b -> Bruijn l (a -> b)
BApp :: Bruijn l (a -> b) -> Bruijn l a -> Bruijn l b
phoasToKey :: (forall v. Phoas v a) -> (forall s. KeyM s (KExp s a))
phoasToKey v = getExp (go v) where
go :: Phoas (HoasKey s) a -> HoasKey s a
go (PVar v) = v
go (PLam f) = lam (go . f)
go (PApp f x) = app (go f) (go x)
phoasToBruijn :: (forall v. Phoas v x) -> Bruijn '[] x
phoasToBruijn p =
runKeyM (keyToBruijn <$> phoasToKey p)
extend :: Key s h -> FKeyMap s (Index t) ->
FKeyMap s (Index (h ': t))
extend k m = insert k Head (pfmap Tail m)
keyToBruijn :: KExp s a -> Bruijn '[] a
keyToBruijn = go empty where
go :: FKeyMap s (Index l) -> KExp s x -> Bruijn l x
go e (KVar v) = BVar (e ! v)
go e (KLam k b) = BLam (go (extend k e) b)
go e (KApp f x) = BApp (go e f) (go e x)
{-
makeMorePrecise :: (forall s. KeyM s (KeyLam s x)) -> NSyn '[] x
makeMorePrecise m = runKeyM (toNSyn <$> m)
toNSyn :: KeyLam s a -> NSyn '[] a
toNSyn = translate empty
type FKeyMap = HFMap
extend :: Key s h -> FKeyMap s (Index t) -> FKeyMap s (Index (h ': t))
extend k m = insert k Head (pfmap Tail m)
translate :: HFMap s (Index l) -> KeyLam s a -> NSyn l a
translate e (KVar v) = NVar (e ! v)
translate e (KLam k b) = NLam (translate (extend k e) b)
translate e (KApp f x) = NApp (translate e f) (translate e x)
-}
{-
toNominal :: Hoas h => PHoas h a -> KeyM s (Nominal s a)
toNominal (Var x) = pure $ KVar x
toNominal (Lam f) = do k <- newKey
b <- toNominal (f k)
pure (KLam k b)
toNominal (App f x) = KApp <$> toNominal f <*> toNominal x
-}
{-
weakenNas :: Nas l a -> Nas (Snoc l a) a
weakenNas (NVar i) = NVar ( i)
-}
--weakenNas (NLam b) = NLam (weakenNas b)
--weakenNas (NApp f x) = NApp (weakenNas f) (weakenNas x)
-- translate :: Nominal s a ->
{-
translate :: ClosedPHoas a -> ClosedNHoas a
translate t = runKeyM (go empty t) where
go :: HFMap s (HIndex l) -> PHoas (Key s) a -> KeyM s (NSyn l a)
go e (Var x) =
pure $ NVar (e ! x)
go e (Lam f) =
do k <- newKey
let e' = insert k HHead (hfmapmap HTail e)
b <- go e' (f k)
return (NLam b)
go e (App f x) = NApp <$> go e f <*> go e x
-}
{-}
type Index = HIndex
data NSyn l a where
NVar :: HIndex l a -> NSyn l a
NLam :: NSyn (a ': l) b -> NSyn l (a -> b)
NApp :: NSyn l (a -> b) -> NSyn l a -> NSyn l b
toClosed :: CCC c => NSyn '[] (x -> y) -> c () (x -> y)
toClosed p = go p TNil where
go :: CCC c => NSyn l y -> TList (c x) l -> c x y
go (NVar x) e = findex e x
go (NLam b) e = curry $ go b (TCons snd (tmap (. fst) e))
go (NApp f x) e = uncurry (go f e) . prod id (go x e)
app :: CCC c => c x (a -> b) -> c x a -> c x b
app f x = uncurry f . (prod id x)
class Category c => CCC c where
prod :: c x a -> c x b -> c x (a,b)
fst :: c (a,b) a
snd :: c (a,b) b
curry :: c (a,b) x -> c a (b -> x)
uncurry :: c a (b -> x) -> c (a,b) x
data TList f l where
TNil :: TList f '[]
TCons :: f h -> TList f t -> TList f (h ': t)
tindex :: TList f l -> Index l x -> f x
tindex (TCons h _) HHead = h
tindex (TCons _ t) (HTail i) = tindex t i
tmap :: (forall x. f x -> g x) -> TList f l -> TList g l
tmap f TNil = TNil
tmap f (TCons h t) = TCons (f h) (tmap f t)
-}
type ClosedHoas a = forall f. Hoas f => f a
toPHoas :: ClosedHoas a -> ClosedPHoas a
toPHoas v = v
--doesNotOccur :: HIndex l a -> Nas l a -> Maybe (forall a. HIndex l a -> )
-- eta :: Nas l (a -> b) -> Nas l (a -> b)
--eta (NLam (NApp x HHead)) = x
test = lam $ \x -> lam $ \y -> x