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
287
288
289
290
291
292
293
294
295
|
module Inky
import Collie
import Control.App
import Control.App.Console
import Control.App.FileIO
import Flap.Decidable
import Flap.Decidable.Maybe
import Flap.Parser
import Inky.Term
import Inky.Term.Checks
import Inky.Term.Compile
import Inky.Term.Desugar
import Inky.Term.Parser
import Inky.Term.Pretty
import Inky.Term.Pretty.Error
import Inky.Type.Pretty
import System.File.Mode
import System.File.ReadWrite
import System.File.Virtual
import Text.Lexer
import Text.PrettyPrint.Prettyprinter
import Text.PrettyPrint.Prettyprinter.Render.Terminal
%default partial
%hide Collie.Modifiers.infix.(::=)
record Erased (a : Type) where
constructor Forget
0 value : a
-- Actions ---------------------------------------------------------------------
lexInkyString : HasErr String es => String -> App es (List (WithBounds InkyToken))
lexInkyString file = do
let (tokens, _, _, "") = lex tokenMap file
| (_, line, col, rest) => throw "\{show (1 + line)}:\{show col}: unexpected character"
pure (filter (\x => relevant x.val.kind) tokens)
parseType : HasErr String es => List (WithBounds InkyToken) -> App es (Ty [<])
parseType toks = do
let Ok res [] _ = parse @{EqI} OpenType toks
| Ok res (x :: _) _ => throw "\{x.bounds}: unexpected token \{x.val.kind}, expected end of input"
| Err msg => throw msg
let Right a = res.try [<]
| Left msg => throw msg
pure a
parseTerm : HasErr String es => List (WithBounds InkyToken) -> App es (Term Sugar Bounds [<] [<])
parseTerm toks = do
let Ok res [] _ = parse @{EqI} OpenTerm toks
| Ok res (x :: _) _ => throw "\{x.bounds}: unexpected token \{x.val.kind}, expected end of input"
| Err msg => throw msg
let Right t = res.try [<] [<]
| Left msg => throw msg
pure t
checkType : HasErr String es => (a : Ty [<]) -> App es (Erased $ WellFormed a)
checkType a = do
let True `Because` wf = wellFormed a
| False `Because` bad => throw "type ill-formed"
pure (Forget wf)
synthTerm :
(t : Term mode m [<] [<]) ->
HasErr (NotSynths [<] [<] t) es =>
App es (Subset (Ty [<]) (Synths [<] [<] t))
synthTerm t = do
let Just a `Because` prf = synths [<] [<] t
| Nothing `Because` contra => throw contra
pure (Element a prf)
printType :
PrimIO es => {default defaultLayoutOptions opts : LayoutOptions} ->
Ty [<] -> App es ()
printType a = do
primIO $ renderIO $ layoutSmart opts $ prettyType a Open
printTerm :
PrimIO es => {default defaultLayoutOptions opts : LayoutOptions} ->
Term mode m [<] [<] -> App es ()
printTerm a = do
primIO $ renderIO $ layoutSmart opts $ prettyTerm a Open
printSynthError :
PrimIO es => {default defaultLayoutOptions opts : LayoutOptions} ->
{t : Term mode Bounds [<] [<]} ->
NotSynths [<] [<] t -> App es ()
printSynthError contra =
primIO $ renderIO $ layoutSmart opts $
concatWith (surround hardline) $
map (\(bounds, msg) => group $ align $ hang 2 $ pretty "\{bounds}:" <+> line <+> msg) $
prettyNotSynths contra
compileTerm :
PrimIO es => {default defaultLayoutOptions opts : LayoutOptions} ->
{t : Term mode m [<] [<]} ->
(0 prf : Synths [<] [<] t a) ->
App es ()
compileTerm prf =
primIO $ renderIO $ layoutSmart opts $
pretty "(use-modules (ice-9 match))" <+> line <+>
parens (hang 1 $ pretty "define main" <+> line <+> group (compileSynths [<] [<] prf))
readFile : FileIO es => File -> App es String
readFile f = do
content <- read [<] f
pure (fastConcat $ content <>> [])
where
read : SnocList String -> File -> App es (SnocList String)
read acc f = do
False <- fEOF f
| True => pure acc
str <- fGetStr f
read (acc :< str) f
readFileOrStdin : FileIO es => HasErr IOError es => Maybe FilePath -> App es String
readFileOrStdin Nothing = readFile stdin
readFileOrStdin (Just path) = withFile path Read throw readFile
-- Arguments -------------------------------------------------------------------
Inky : Command "inky"
Inky = MkCommand
{ description = """
tool suite for Primrose programs
"""
, subcommands =
[ "--help" ::= basic "print this help text" none
, "format" ::= MkCommand
{ description = """
pretty print data
"""
, subcommands =
[ "type" ::= basic "print a type" filePath
, "term" ::= MkCommand
{ description = "print a term"
, subcommands = []
, modifiers = ["--desugar" ::= flag "desugar the term"]
, arguments = filePath
}
]
, modifiers = []
, arguments = none
}
, "check" ::= MkCommand
{ description = """
check well-formedness
"""
, subcommands =
[ "type" ::= basic "check a type" filePath
, "term" ::= MkCommand
{ description = "check a term"
, subcommands = []
, modifiers = ["--type" ::= option "type to check against" filePath]
, arguments = filePath
}
]
, modifiers = []
, arguments = none
}
, "compile" ::= MkCommand
{ description = """
compile to scheme
"""
, subcommands =
[ "term" ::= MkCommand
{ description = "compile a term"
, subcommands = []
, modifiers = ["--type" ::= option "type to check against" filePath]
, arguments = filePath
}
]
, modifiers = []
, arguments = none
}
]
, modifiers = []
, arguments = none
}
-- Driver ----------------------------------------------------------------------
parseArgs :
(cmd : Command nm) ->
App (String :: Init) (ParseTree cmd)
parseArgs cmd = do
Right args <- primIO cmd.parseArgs
| Left msg => throw msg
let Pure args = ParsedTree.finalising args
| Fail errs => throw (show $ the (Error ()) $ Fail errs)
pure args
abortErr : PrimIO es => String -> App es a
abortErr msg = do
primIO $ () <$ fPutStrLn stderr msg
primIO exitFailure
showHelp : Console es => App es ()
showHelp = putStrLn "Usage: \{Inky .usage}"
main : IO ()
main = run {l = MayThrow} $ do
args <- handle {err = String} (parseArgs Inky) pure abortErr
Collie.handle {cmd = ("inky" ** Inky)} args
[ const showHelp
, "--help" ::= [ const showHelp ]
, "format" ::=
[ const showHelp
, "type" ::=
[ \cmd => do
let path = cmd.arguments
txt <- handle {err = IOError} (readFileOrStdin path) pure (abortErr . show)
toks <- handle {err = String} (lexInkyString txt) pure abortErr
a <- handle {err = String} (parseType toks) pure abortErr
printType a
pure ()
]
, "term" ::=
[ \cmd => do
let path = cmd.arguments
txt <- handle {err = IOError} (readFileOrStdin path) pure (abortErr . show)
toks <- handle {err = String} (lexInkyString txt) pure abortErr
t <- handle {err = String} (parseTerm toks) pure abortErr
let [noSugar] = cmd.modifiers.content
case noSugar of
True => do
let Just t = maybeDesugar t
| Nothing => abortErr "failed to desugar term"
printTerm t
False => printTerm t
]
]
, "check" ::=
[ const showHelp
, "type" ::=
[ \cmd => do
let path = cmd.arguments
txt <- handle {err = IOError} (readFileOrStdin path) pure (abortErr . show)
toks <- handle {err = String} (lexInkyString txt) pure abortErr
a <- handle {err = String} (parseType toks) pure abortErr
handle {err = String} (checkType a) (const $ pure ()) abortErr
]
, "term" ::=
[ \cmd => do
let path = cmd.arguments
txt <- handle {err = IOError} (readFileOrStdin path) pure (abortErr . show)
toks <- handle {err = String} (lexInkyString txt) pure abortErr
t <- handle {err = String} (parseTerm toks) pure abortErr
let [type] = cmd.modifiers.content
t <-
maybe (pure t)
(\path => do
txt <- handle {err = IOError} (readFileOrStdin $ Just path) pure (abortErr . show)
toks <- handle {err = String} (lexInkyString txt) pure abortErr
a <- handle {err = String} (parseType toks) pure abortErr
handle {err = String} (checkType a) (const $ pure ()) abortErr
pure (Annot (MkBounds (-1) (-1) (-1) (-1)) t a))
type
handle {err = NotSynths [<] [<] t} (synthTerm t) (const $ pure ())
(\contra => do printSynthError contra; primIO exitFailure)
]
]
, "compile" ::=
[ const showHelp
, "term" ::=
[ \cmd => do
let path = cmd.arguments
txt <- handle {err = IOError} (readFileOrStdin path) pure (abortErr . show)
toks <- handle {err = String} (lexInkyString txt) pure abortErr
t <- handle {err = String} (parseTerm toks) pure abortErr
let [type] = cmd.modifiers.content
t <-
maybe (pure t)
(\path => do
txt <- handle {err = IOError} (readFileOrStdin $ Just path) pure (abortErr . show)
toks <- handle {err = String} (lexInkyString txt) pure abortErr
a <- handle {err = String} (parseType toks) pure abortErr
handle {err = String} (checkType a) (const $ pure ()) abortErr
pure (Annot (MkBounds (-1) (-1) (-1) (-1)) t a))
type
Element _ prf <-
handle {err = NotSynths [<] [<] t} (synthTerm t) pure
(\contra => do printSynthError contra; primIO exitFailure)
compileTerm prf
]
]
]
|