forked from gelisam/klister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives-documentation.kl
268 lines (254 loc) · 10.4 KB
/
primitives-documentation.kl
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
#lang "prelude.kl"
(import "pair-datatype.kl")
-- This file acts as a bare minimum form of documentation for all the
-- primitives from "prelude.kl". May we one day have nice prose explaining all
-- of it!
--
-- Until then, values are listed as (example (pair "expr" expr)) so that the
-- "primitives-documentation.golden" file lists both their name and their type
-- signature.
-- For types, their kind is given in a comment.
--
-- Macros don't have types, but sometimes behave as if they have one, in the
-- sense that using them properly requires giving them a certain number of
-- arguments of the right type, and doing so will result in a value of a given
-- output type. A common example is data constructors, which are macros in Klister.
-- In those cases, the type is given in a comment, with the arguments tupled-up
-- to indicate the lack of currying.
-- Otherwise, using the macro properly requires an input which matches a
-- particular shape, so an example is given.
-- primitive module macros
--
-- #%module : Macro (#%module
-- (define x 42)
-- (define y 43))
-- primitive declaration macros
--
-- define : Macro (define x 42)
-- datatype : Macro (datatype (List A)
-- (nil)
-- (:: A (List A)))
-- define-macros : Macro (define-macros
-- ([my-macro1
-- (lambda (stx)
-- (pure '42))]
-- [my-macro2
-- (lambda (stx)
-- (pure '43))]))
-- example : Macro (example 42)
-- run : Macro (run (write stdout "hello world!\n"))
-- import : Macro (import "list.kl")
-- (import (shift "prelude.kl" 1))
-- (import (rename (shift "prelude.kl" 1)
-- [#%module prelude-module]
-- [define prelude-define]))
-- export : Macro (export List nil ::)
-- (export (rename ([List ConsList]
-- [:: cons])
-- List nil ::))
-- meta : Macro (meta
-- (define x 42)
-- (define y 43))
-- group : Macro (group
-- (define x 42)
-- (define y 43))
-- primitive types
--
-- Syntax : Type
(example (pair "open-syntax" open-syntax))
(example (pair "close-syntax" close-syntax))
--
-- -> : Type -> Type -> Type
--
-- Integer : Type
(example (pair "+" +))
(example (pair "-" -))
(example (pair "*" *))
(example (pair "/" /))
(example (pair "abs" abs))
(example (pair "negate" negate))
(example (pair ">" >))
(example (pair ">=" >=))
(example (pair "<" <))
(example (pair "<=" <=))
(example (pair "=" =))
(example (pair "/=" /=))
(example (pair "integer->string" integer->string))
--
-- Macro : Type -> Type
--
-- Type : Type
--
-- String : Type
(example (pair "string-append" string-append))
(example (pair "substring" substring))
(example (pair "string-length" string-length))
(example (pair "string=?" string=?))
(example (pair "string/=?" string/=?))
(example (pair "string<?" string<?))
(example (pair "string<=?" string<=?))
(example (pair "string>?" string>?))
(example (pair "string>=?" string>=?))
(example (pair "string-upcase" string-upcase))
(example (pair "string-downcase" string-downcase))
(example (pair "string-titlecase" string-titlecase))
(example (pair "string-foldcase" string-foldcase))
--
-- IO : Type -> Type
(example (pair "pure-IO" pure-IO))
(example (pair "bind-IO" bind-IO))
-- primitive datatypes
--
-- ScopeAction : Type
(example (flip))
(example (add))
(example (remove))
--
-- Unit : Type
(example unit)
--
-- Bool : Type
(example true)
(example false)
--
-- Problem : Type
(example (module))
(example (declaration))
(example (type))
-- expression : Type -> Problem
(example (pattern))
(example (type-pattern))
--
-- Maybe : Type -> Type
(example nothing)
-- just : A -> Maybe A
--
-- List : Type -> Type
(example (nil))
-- :: : (A, List A) -> List A
--
-- Syntax-Contents : Type -> Type
-- list-contents : List A -> Syntax-Contents A
-- integer-contents : Integer -> Syntax-Contents A
-- string-contents : String -> Syntax-Contents A
-- identifier-contents : String -> Syntax-Contents A
-- primitive expression macros
--
-- error : Syntax -> A
-- the : ((A : Type), A) -> A
-- let : Macro (let [x 42]
-- (+ x x))
-- flet : Macro (flet [fact (n)
-- (if (= n 0)
-- 1
-- (* n (fact (- n 1))))]
-- (fact 10))
-- lambda : Macro (lambda (x) (+ x 1))
-- #%app : Macro (#%app + 2 2)
-- #%integer-literal : Integer -> Integer
-- #%string-literal : String -> String
-- pure : A -> Macro A
-- >>= : (Macro A, A -> Macro B) -> Macro B
-- syntax-error : Syntax -> Macro A
-- bound-identifier=? : (Syntax, Syntax) -> Macro Bool
-- free-identifier=? : (Syntax, Syntax) -> Macro Bool
-- quote : Macro '(+ 2 2)
-- ident-syntax : Macro (ident-syntax 'bar 'loc)
-- empty-list-syntax : Syntax -> Syntax
-- cons-list-syntax : Macro (cons-list-syntax '1 '(2 3 4) 'loc)
-- list-syntax : Macro (list-syntax ('1 '2 '3 '4) 'loc))
-- integer-syntax : (Integer, Syntax) -> Syntax
-- string-syntax : (String, Syntax) -> Syntax
-- replace-loc : (Syntax, Syntax) -> Syntax
-- syntax-case : Macro (syntax-case 'bar
-- [(ident x)
-- (list 'foo x 'baz)])
-- (syntax-case '2
-- [(integer x)
-- (list 1 x 3)])
-- (syntax-case '"bar"
-- [(string x)
-- (list "foo" x "baz")])
-- (syntax-case '(2 3 5)
-- [(list (x y z))
-- (list '1 x y '4 z '6)])
-- (syntax-case '(2 4 5)
-- [(cons x xs)
-- (cons-list-syntax '1
-- (cons-list-syntax x
-- (cons-list-syntax '3
-- xs
-- 'loc)
-- 'loc)
-- 'loc)])
-- (syntax-case '(1 2 3 4)
-- [()
-- 'zero]
-- [(list (_))
-- 'one]
-- [(list (_ _))
-- 'two]
-- [_
-- 'more])
-- let-syntax : Macro (let-syntax [my-macro
-- (lambda (stx)
-- (pure '2))]
-- (+ (my-macro a b c) 2))
-- log : Syntax -> Macro Unit
(example (make-introducer))
(example (which-problem))
-- case : Macro (case (list 1 2 3)
-- [(nil)
-- (nothing)]
-- [(:: x _)
-- (just x)])
-- (case (list 1 2 3)
-- [(list x _ _)
-- x])
-- type-case : Macro (>>= (which-problem)
-- (lambda (problem)
-- (case problem
-- [(expression tp)
-- (type-case tp
-- [String
-- (pure "string")]
-- [(-> A B)
-- (pure "arrow")])])))
-- primitive patterns
--
-- else : Pattern (case (list 1 2 3 4)
-- [(nil)
-- (nil)]
-- [(list x)
-- (list x)]
-- [(list x y)
-- (list x y)]
-- [(else x)
-- x])
-- primitive universal macros
--
-- with-unknown-type : Macro (with-unknown-type [A]
-- (the (-> A A)
-- (lambda (x) x)))
-- non-primitive declaration macros
--
-- defun : Macro (defun fact (n)
-- (if (= n 0)
-- 1
-- (* n (fact (- n 1)))))
-- non-primitive expression macros
--
-- unquote : Macro `(1 2 3 ,(integer-syntax (+ 2 2) 'loc))
-- quasiquote : Macro `(1 2 3 4)
-- quasiquote/loc : Macro (quasiquote/loc 'loc (1 2 3 4))
-- non-primitive expressions
--
-- if : Macro (if true 1 0)
(example (pair "id" id))
(example (pair "const" const))
(example (pair "compose" compose))
-- IO primitives
--
-- Output-Port : Type
(example (pair "stdout" stdout))
(example (pair "write" write))