blob: d32ea993b1a11affaa6f2e1691c1cdf1af9e67d5 (
plain)
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
|
## Copyright (c) Eduardo Julian. All rights reserved.
## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
## If a copy of the MPL was not distributed with this file,
## You can obtain one at http://mozilla.org/MPL/2.0/.
(;import lux
(lux (control show
eq
monad)
(data (text #open ("text:" Text/Monoid Text/Eq))
(number/int #open ("int:" Int/Eq Int/Show))
maybe
(list #refer #all #open ("list:" List/Monad)))
))
## [Structures]
(defstruct #export Type/Show (Show Type)
(def (show type)
(case type
(#;DataT name)
($ text:++ "(^ " name ")")
(#;TupleT members)
(case members
#;Nil
"(,)"
_
($ text:++ "(, " (|> members (list:map show) (interpose " ") (foldL text:++ "")) ")"))
(#;VariantT members)
(case members
#;Nil
"(|)"
_
($ text:++ "(| " (|> members (list:map show) (interpose " ") (foldL text:++ "")) ")"))
(#;LambdaT input output)
($ text:++ "(-> " (show input) " " (show output) ")")
(#;VarT id)
($ text:++ "⌈" (int:show id) "⌋")
(#;BoundT idx)
(int:show idx)
(#;ExT id)
($ text:++ "⟨" (int:show id) "⟩")
(#;AppT fun param)
($ text:++ "(" (show fun) " " (show param) ")")
(#;UnivQ env body)
($ text:++ "(All " (show body) ")")
(#;ExQ env body)
($ text:++ "(Ex " (show body) ")")
(#;NamedT [module name] type)
($ text:++ module ";" name)
)))
## (defstruct #export Type/Eq (Eq Type)
## (def (= x y)
## (case [x y]
## [(#;DataT xname) (#;DataT yname)]
## (text:= xname yname)
## (\or [(#;VarT xid) (#;VarT yid)]
## [(#;ExT xid) (#;ExT yid)]
## [(#;BoundT xid) (#;BoundT yid)])
## (int:= xid yid)
## (\or [(#;LambdaT xleft xright) (#;LambdaT yleft yright)]
## [(#;AppT xleft xright) (#;AppT yleft yright)])
## (and (= xleft yleft)
## (= xright yright))
## [(#;NamedT [xmodule xname] xtype) (#;NamedT [ymodule yname] ytype)]
## (and (text:= xmodule ymodule)
## (text:= xname yname)
## (= xtype ytype))
## (\or [(#;TupleT xmembers) (#;TupleT ymembers)]
## [(#;VariantT xmembers) (#;VariantT ymembers)])
## (and (int:= (size xmembers) (size ymembers))
## (foldL (lambda [prev [x y]]
## (and prev (= v y)))
## true
## (zip2 xmembers ymembers)))
## (\or [(#;UnivQ yenv ybody) (#;UnivQ yenv ybody)]
## [(#;ExQ yenv ybody) (#;ExQ yenv ybody)])
## (and (int:= (size xenv) (size yenv))
## (foldL (lambda [prev [x y]]
## (and prev (= v y)))
## (= xbody ybody)
## (zip2 xenv yenv)))
## _
## false
## )))
## [Functions]
(def #export (beta-reduce env type)
(-> (List Type) Type Type)
(case type
(\template [<tag>]
[(<tag> members)
(<tag> (list:map (beta-reduce env) members))])
[[#;VariantT]
[#;TupleT]]
(\template [<tag>]
[(<tag> left right)
(<tag> (beta-reduce env left) (beta-reduce env right))])
[[#;LambdaT]
[#;AppT]]
(\template [<tag>]
[(<tag> env def)
(case env
#;Nil
(<tag> env def)
_
type)])
[[#;UnivQ]
[#;ExQ]]
(#;BoundT idx)
(? type (@ idx env))
(#;NamedT name type)
(beta-reduce env type)
_
type
))
(def #export (apply-type type-fun param)
(-> Type Type (Maybe Type))
(case type-fun
(#;UnivQ env body)
(#;Some (beta-reduce (@list& type-fun param env) body))
(#;AppT F A)
(do Maybe/Monad
[type-fn* (apply-type F A)]
(apply-type type-fn* param))
(#;NamedT name type)
(apply-type type param)
_
#;None))
|