FCO.hs 8.7 KB
Newer Older
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module FCO
  ( Env(..)
  , HNat(..)
  , Coercion(..)
  , coerciontypeSubstitute
  , freeVariablesCoercion
  , coercionshiftplus
  , coercionshiftminus
  , Type(..)
  , typetypeSubstitute
  , freeVariablesType
  , typeshiftplus
  , typeshiftminus
  , Term(..)
  , termtermSubstitute
  , termtypeSubstitute
  , freeVariablesTerm
  , termshiftplus
  , termshiftminus
  , generateHnatTypeVar
  , generateHnatTermVar
  ) where

import           Data.List

data Coercion
  = CoId
  | CoTrans Coercion
            Coercion
  | CoTop Type
  | CoBot Type
  | CoArrow Coercion
            Coercion
  | CoTuple Coercion
            Coercion
  | CoProj1 Type
  | CoProj2 Type
  | CoAll Coercion
  | CoDistArrow
  | CoTopArrow
  | CoTopAll
  | CoDistAll
  deriving (Show, Eq)

data Type
  = TyVar HNat
  | TyArr Type
          Type
  | TyAll Type
  | TyInt
  | TyTop
  | TyProd Type
           Type
  deriving (Show, Eq)

data Term
  = TmVar HNat
  | TmAbs Term
          Type
  | TmApp Term
          Term
  | TmTApp Term
           Type
  | TmTAbs Term
  | TmTop
  | TmTuple Term
            Term
  | TmInt Int
  | TmCast Coercion
           Term
  deriving (Show, Eq)

data HNat
  = Z
  | STypeVar HNat
  | STermVar HNat
  deriving (Show, Eq)

plus x1 (STypeVar x2) = STypeVar (plus x1 x2)
plus x1 (STermVar x2) = STermVar (plus x1 x2)
plus Z h              = h
plus h Z              = h

instance Ord HNat where
  compare (STypeVar h1) (STypeVar h2) = compare h1 h2
  compare (STypeVar h1) (STermVar h2) =
    error "differing namespace found in compare "
  compare (STermVar h1) (STypeVar h2) =
    error "differing namespace found in compare "
  compare (STermVar h1) (STermVar h2) = compare h1 h2
  compare Z Z = EQ
  compare Z _ = LT
  compare _ Z = GT

minus (STypeVar h1) (STypeVar h2) = minus h1 h2
minus (STypeVar h1) (STermVar h2) = error "differing namespace found in minus "
minus (STermVar h1) (STypeVar h2) = error "differing namespace found in minus "
minus (STermVar h1) (STermVar h2) = minus h1 h2
minus Z Z = Z
minus Z _ = error " You cannot substract zero with a positive number "
minus result Z = result

data Env
  = Nil
  | ETypeVar Env
  | ETermVar Type
             Env
  deriving (Show, Eq)

generateHnatTypeVar 0 c = c
generateHnatTypeVar n c = STypeVar (generateHnatTypeVar (n - 1) c)

generateHnatTermVar 0 c = c
generateHnatTermVar n c = STermVar (generateHnatTermVar (n - 1) c)

coercionmap :: (HNat -> Type -> Type) -> HNat -> Coercion -> Coercion
coercionmap onTypeVar c (CoId) = CoId
coercionmap onTypeVar c (CoTrans co1 co2) =
  CoTrans (coercionmap onTypeVar c co1) (coercionmap onTypeVar c co2)
coercionmap onTypeVar c (CoTop ty) = CoTop (typemap onTypeVar c ty)
coercionmap onTypeVar c (CoBot ty) = CoBot (typemap onTypeVar c ty)
coercionmap onTypeVar c (CoArrow co1 co2) =
  CoArrow (coercionmap onTypeVar c co1) (coercionmap onTypeVar c co2)
coercionmap onTypeVar c (CoTuple co1 co2) =
  CoTuple (coercionmap onTypeVar c co1) (coercionmap onTypeVar c co2)
coercionmap onTypeVar c (CoProj1 ty2) = CoProj1 (typemap onTypeVar c ty2)
coercionmap onTypeVar c (CoProj2 ty1) = CoProj2 (typemap onTypeVar c ty1)
coercionmap onTypeVar c (CoAll co1) = CoAll (coercionmap onTypeVar c co1)
coercionmap onTypeVar c (CoDistArrow) = CoDistArrow
coercionmap onTypeVar c (CoTopArrow) = CoTopArrow
coercionmap onTypeVar c (CoTopAll) = CoTopAll
coercionmap onTypeVar c (CoDistAll) = CoDistAll

typemap :: (HNat -> Type -> Type) -> HNat -> Type -> Type
typemap onTypeVar c (TyVar hnat) = onTypeVar c (TyVar hnat)
typemap onTypeVar c (TyArr t1 t2) =
  TyArr (typemap onTypeVar c t1) (typemap onTypeVar c t2)
typemap onTypeVar c (TyAll t1) = TyAll (typemap onTypeVar (STypeVar c) t1)
typemap onTypeVar c (TyInt) = TyInt
typemap onTypeVar c (TyTop) = TyTop
typemap onTypeVar c (TyProd t1 t2) =
  TyProd (typemap onTypeVar c t1) (typemap onTypeVar c t2)

termmap ::
     (HNat -> Term -> Term) -> (HNat -> Type -> Type) -> HNat -> Term -> Term
termmap onTermVar onTypeVar c (TmVar hnat) = onTermVar c (TmVar hnat)
termmap onTermVar onTypeVar c (TmAbs x t) =
  TmAbs (termmap onTermVar onTypeVar (STermVar c) x) (typemap onTypeVar c t)
termmap onTermVar onTypeVar c (TmApp t1 t2) =
  TmApp (termmap onTermVar onTypeVar c t1) (termmap onTermVar onTypeVar c t2)
termmap onTermVar onTypeVar c (TmTApp t1 t) =
  TmTApp (termmap onTermVar onTypeVar c t1) (typemap onTypeVar c t)
termmap onTermVar onTypeVar c (TmTAbs t1) =
  TmTAbs (termmap onTermVar onTypeVar (STypeVar c) t1)
termmap onTermVar onTypeVar c (TmTop) = TmTop
termmap onTermVar onTypeVar c (TmTuple e1 e2) =
  TmTuple (termmap onTermVar onTypeVar c e1) (termmap onTermVar onTypeVar c e2)
termmap onTermVar onTypeVar c (TmInt int0) = TmInt int0
termmap onTermVar onTypeVar c (TmCast co e) =
  TmCast (coercionmap onTypeVar c co) (termmap onTermVar onTypeVar c e)

typeshiftHelpplus d c (TyVar hnat)
  | hnat >= c = TyVar (plus hnat d)
  | otherwise = TyVar hnat

termshiftHelpplus d c (TmVar hnat)
  | hnat >= c = TmVar (plus hnat d)
  | otherwise = TmVar hnat

coercionshiftplus :: HNat -> Coercion -> Coercion
coercionshiftplus d t = coercionmap (typeshiftHelpplus d) Z t

typeshiftplus :: HNat -> Type -> Type
typeshiftplus d t = typemap (typeshiftHelpplus d) Z t

termshiftplus :: HNat -> Term -> Term
termshiftplus d t = termmap (termshiftHelpplus d) (typeshiftHelpplus d) Z t

typeshiftHelpminus d c (TyVar hnat)
  | hnat >= c = TyVar (minus hnat d)
  | otherwise = TyVar hnat

termshiftHelpminus d c (TmVar hnat)
  | hnat >= c = TmVar (minus hnat d)
  | otherwise = TmVar hnat

coercionshiftminus :: HNat -> Coercion -> Coercion
coercionshiftminus d t = coercionmap (typeshiftHelpminus d) Z t

typeshiftminus :: HNat -> Type -> Type
typeshiftminus d t = typemap (typeshiftHelpminus d) Z t

termshiftminus :: HNat -> Term -> Term
termshiftminus d t = termmap (termshiftHelpminus d) (typeshiftHelpminus d) Z t

196
197
typeSubstituteHelp sub orig c (TyVar hnat)
  | hnat == plus orig c = typeshiftplus c sub
198
199
  | otherwise = TyVar hnat

200
201
termSubstituteHelp sub orig c (TmVar hnat)
  | hnat == plus orig c = termshiftplus c sub
202
203
204
  | otherwise = TmVar hnat

coerciontypeSubstitute :: Type -> HNat -> Coercion -> Coercion
205
206
coerciontypeSubstitute sub orig t =
  coercionmap (typeSubstituteHelp sub orig) Z t
207
208

typetypeSubstitute :: Type -> HNat -> Type -> Type
209
typetypeSubstitute sub orig t = typemap (typeSubstituteHelp sub orig) Z t
210
211
212

termtermSubstitute :: Term -> HNat -> Term -> Term
termtermSubstitute sub orig t =
213
  termmap (termSubstituteHelp sub orig) (\c x -> x) Z t
214
215
216

termtypeSubstitute :: Type -> HNat -> Term -> Term
termtypeSubstitute sub orig t =
217
  termmap (\c x -> x) (typeSubstituteHelp sub orig) Z t
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

freeVariablesCoercion :: HNat -> Coercion -> [HNat]
freeVariablesCoercion c (CoId) = nub ([])
freeVariablesCoercion c (CoTrans co1 co2) =
  nub ((freeVariablesCoercion c co1) ++ (freeVariablesCoercion c co2))
freeVariablesCoercion c (CoTop ty) = nub ((freeVariablesType c ty))
freeVariablesCoercion c (CoBot ty) = nub ((freeVariablesType c ty))
freeVariablesCoercion c (CoArrow co1 co2) =
  nub ((freeVariablesCoercion c co1) ++ (freeVariablesCoercion c co2))
freeVariablesCoercion c (CoTuple co1 co2) =
  nub ((freeVariablesCoercion c co1) ++ (freeVariablesCoercion c co2))
freeVariablesCoercion c (CoProj1 ty2) = nub ((freeVariablesType c ty2))
freeVariablesCoercion c (CoProj2 ty1) = nub ((freeVariablesType c ty1))
freeVariablesCoercion c (CoAll co1) = nub ((freeVariablesCoercion c co1))
freeVariablesCoercion c (CoDistArrow) = nub ([])
freeVariablesCoercion c (CoTopArrow) = nub ([])
freeVariablesCoercion c (CoTopAll) = nub ([])
freeVariablesCoercion c (CoDistAll) = nub ([])

freeVariablesType :: HNat -> Type -> [HNat]
freeVariablesType c (TyVar hnat)
  | hnat >= c = [minus hnat c]
  | otherwise = []
freeVariablesType c (TyArr t1 t2) =
  nub ((freeVariablesType c t1) ++ (freeVariablesType c t2))
freeVariablesType c (TyAll t1) = nub ((freeVariablesType (STypeVar c) t1))
freeVariablesType c (TyInt) = nub ([])
freeVariablesType c (TyTop) = nub ([])
freeVariablesType c (TyProd t1 t2) =
  nub ((freeVariablesType c t1) ++ (freeVariablesType c t2))

freeVariablesTerm :: HNat -> Term -> [HNat]
freeVariablesTerm c (TmVar hnat)
  | hnat >= c = [minus hnat c]
  | otherwise = []
freeVariablesTerm c (TmAbs x t) =
  nub ((freeVariablesTerm (STermVar c) x) ++ (freeVariablesType c t))
freeVariablesTerm c (TmApp t1 t2) =
  nub ((freeVariablesTerm c t1) ++ (freeVariablesTerm c t2))
freeVariablesTerm c (TmTApp t1 t) =
  nub ((freeVariablesTerm c t1) ++ (freeVariablesType c t))
freeVariablesTerm c (TmTAbs t1) = nub ((freeVariablesTerm (STypeVar c) t1))
freeVariablesTerm c (TmTop) = nub ([])
freeVariablesTerm c (TmTuple e1 e2) =
  nub ((freeVariablesTerm c e1) ++ (freeVariablesTerm c e2))
freeVariablesTerm c (TmInt _) = nub ([])
freeVariablesTerm c (TmCast co e) =
  nub ((freeVariablesCoercion c co) ++ (freeVariablesTerm c e))