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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
module Inky.Term.Parser
import Data.List1
import Data.String
import Inky.Context
import Inky.Parser
import Inky.Term
import Inky.Thinning
import Inky.Type
import Text.Lexer
-- Lexer -----------------------------------------------------------------------
export
data InkyKind
= TermIdent
| TypeIdent
| Lit
| Suc
| Let
| In
| Case
| Of
| Fold
| By
| Nat
| ParenOpen
| ParenClose
| BracketOpen
| BracketClose
| AngleOpen
| AngleClose
| BraceOpen
| BraceClose
| Arrow
| DoubleArrow
| Bang
| Tilde
| Backslash
| Colon
| Equal
| Comma
| Ignore
export
[EqI] Eq InkyKind where
TermIdent == TermIdent = True
TypeIdent == TypeIdent = True
Lit == Lit = True
Suc == Suc = True
Let == Let = True
In == In = True
Case == Case = True
Of == Of = True
Fold == Fold = True
By == By = True
Nat == Nat = True
ParenOpen == ParenOpen = True
ParenClose == ParenClose = True
BracketOpen == BracketOpen = True
BracketClose == BracketClose = True
AngleOpen == AngleOpen = True
AngleClose == AngleClose = True
BraceOpen == BraceOpen = True
BraceClose == BraceClose = True
Arrow == Arrow = True
DoubleArrow == DoubleArrow = True
Bang == Bang = True
Tilde == Tilde = True
Backslash == Backslash = True
Colon == Colon = True
Equal == Equal = True
Comma == Comma = True
Ignore == Ignore = True
_ == _ = False
export
Interpolation InkyKind where
interpolate TermIdent = "term name"
interpolate TypeIdent = "type name"
interpolate Lit = "number"
interpolate Suc = "'suc'"
interpolate Let = "'let'"
interpolate In = "'in'"
interpolate Case = "'case'"
interpolate Of = "'of'"
interpolate Fold = "'fold'"
interpolate By = "'by'"
interpolate Nat = "'Nat'"
interpolate ParenOpen = "'('"
interpolate ParenClose = "')'"
interpolate BracketOpen = "'['"
interpolate BracketClose = "']'"
interpolate AngleOpen = "'<'"
interpolate AngleClose = "'>'"
interpolate BraceOpen = "'{'"
interpolate BraceClose = "'}'"
interpolate Arrow = "'->'"
interpolate DoubleArrow = "'=>'"
interpolate Bang = "'!'"
interpolate Tilde = "'~'"
interpolate Backslash = "'\\'"
interpolate Colon = "':'"
interpolate Equal = "'='"
interpolate Comma = "','"
interpolate Ignore = ""
TokenKind InkyKind where
TokType TermIdent = String
TokType TypeIdent = String
TokType Lit = Nat
TokType _ = ()
tokValue TermIdent = id
tokValue TypeIdent = id
tokValue Lit = stringToNatOrZ
tokValue Suc = const ()
tokValue Let = const ()
tokValue In = const ()
tokValue Case = const ()
tokValue Of = const ()
tokValue Fold = const ()
tokValue By = const ()
tokValue Nat = const ()
tokValue ParenOpen = const ()
tokValue ParenClose = const ()
tokValue BracketOpen = const ()
tokValue BracketClose = const ()
tokValue AngleOpen = const ()
tokValue AngleClose = const ()
tokValue BraceOpen = const ()
tokValue BraceClose = const ()
tokValue Arrow = const ()
tokValue DoubleArrow = const ()
tokValue Bang = const ()
tokValue Tilde = const ()
tokValue Backslash = const ()
tokValue Colon = const ()
tokValue Equal = const ()
tokValue Comma = const ()
tokValue Ignore = const ()
keywords : List (String, InkyKind)
keywords =
[ ("suc", Suc)
, ("let", Let)
, ("in", In)
, ("case", Case)
, ("of", Of)
, ("fold", Fold)
, ("by", By)
, ("Nat", Nat)
]
export
relevant : InkyKind -> Bool
relevant = (/=) @{EqI} Ignore
public export
InkyToken : Type
InkyToken = Token InkyKind
termIdentifier : Lexer
termIdentifier = lower <+> many (alphaNum <|> exact "_")
typeIdentifier : Lexer
typeIdentifier = upper <+> many (alphaNum <|> exact "_")
export
tokenMap : TokenMap InkyToken
tokenMap =
toTokenMap [(spaces, Ignore)] ++
[(termIdentifier, \s =>
case lookup s keywords of
Just k => Tok k s
Nothing => Tok TermIdent s)
,(typeIdentifier, \s =>
case lookup s keywords of
Just k => Tok k s
Nothing => Tok TypeIdent s)] ++
toTokenMap
[ (digits, Lit)
, (exact "(", ParenOpen)
, (exact ")", ParenClose)
, (exact "[", BracketOpen)
, (exact "]", BracketClose)
, (exact "<", AngleOpen)
, (exact ">", AngleClose)
, (exact "{", BraceOpen)
, (exact "}", BraceClose)
, (exact "->", Arrow)
, (exact "=>", DoubleArrow)
, (exact "!", Bang)
, (exact "~", Tilde)
, (exact "\\", Backslash)
, (exact ":", Colon)
, (exact "=", Equal)
, (exact ",", Comma)
]
-- Parser ----------------------------------------------------------------------
public export
InkyParser : Bool -> Context Type -> Context Type -> (a : Type) -> Type
InkyParser nil locked free a =
Parser InkyKind nil
(map (\a => (False, a)) locked)
(map (\a => (False, a)) free)
a
public export
record TypeFun where
constructor MkTypeFun
try : (ctx : Context ()) -> Either String (Ty ctx ())
public export
TypeParser : Context () -> Context () -> Type
TypeParser locked free =
InkyParser False (map (const TypeFun) locked) (map (const TypeFun) free) TypeFun
Row : InkyParser True [<] [<"openType" :- TypeFun] (List $ Assoc TypeFun)
Row =
sepBy (match Comma)
(mkAssoc <$> Seq [match TypeIdent, match Colon, Var (%% "openType")])
where
mkAssoc : HList [String, (), TypeFun] -> Assoc TypeFun
mkAssoc [x, _, v] = x :- v
tryRow : WithBounds (List $ Assoc TypeFun) -> (ctx : Context ()) -> Either String (Row (Ty ctx ()))
tryRow xs ctx =
foldlM
(\row, xf => do
a <- xf.value.try ctx
let Just row' = extend row (xf.name :- a)
| Nothing => Left "\{xs.bounds}: duplicate name \"\{xf.name}\""
pure row')
[<]
xs.val
OpenOrFixpointType : TypeParser [<] [<"openType" :- ()]
OpenOrFixpointType =
OneOf
[ mkFix <$>
Seq [match Backslash, match TypeIdent, match DoubleArrow, Var (%% "openType")]
, Var (%% "openType")
]
where
mkFix : HList [(), String, (), TypeFun] -> TypeFun
mkFix [_, x, _, a] = MkTypeFun (\ctx => pure $ TFix x !(a.try (ctx :< (x :- ()))))
AtomType : TypeParser [<"openType" :- ()] [<]
AtomType =
OneOf
[
mkVar <$> WithBounds (match TypeIdent)
, MkTypeFun (\_ => pure TNat) <$ match Nat
, mkProd <$> enclose (match AngleOpen) (match AngleClose) (WithBounds Row)
, mkSum <$> enclose (match BracketOpen) (match BracketClose) (WithBounds Row)
, enclose (match ParenOpen) (match ParenClose) OpenOrFixpointType
]
where
mkVar : WithBounds String -> TypeFun
mkVar x =
MkTypeFun
(\ctx => case lookup x.val ctx of
Just (() ** i) => pure (TVar i)
Nothing => Left "\{x.bounds}: unbound name \"\{x.val}\"")
mkProd : WithBounds (List $ Assoc TypeFun) -> TypeFun
mkProd xs = MkTypeFun (\ctx => TProd <$> tryRow xs ctx)
mkSum : WithBounds (List $ Assoc TypeFun) -> TypeFun
mkSum xs = MkTypeFun (\ctx => TSum <$> tryRow xs ctx)
export
OpenType : TypeParser [<] [<]
OpenType =
Fix "openType" $ mkArrow <$> sepBy1 (match Arrow) AtomType
where
mkArrow : List1 TypeFun -> TypeFun
mkArrow = foldr1 (\x, y => MkTypeFun (\ctx => [| TArrow (x.try ctx) (y.try ctx) |]))
%hint
export
OpenTypeWf : So (wellTyped EqI [<] [<] [<] [<] OpenType)
OpenTypeWf = Oh
|