blob: d5b331642bbbc8fa3f4ca7dae74809b3edd475dc (
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
|
(;module:
lux
(lux (control [monad #+ do Monad]
[eq]
["p" parser])
(data [text "text/" Monoid<Text>]
text/format
(coll [list "L/" Monad<List>]
[vector]
[array]
[queue]
[set]
[seq]
[dict #+ Dict]
(tree [rose]))
[number "nat/" Codec<Text,Nat>]
[product]
[bool]
[maybe])
(time ["du" duration]
["da" date]
["i" instant])
[macro #+ Monad<Lux> with-gensyms]
(macro [code]
[syntax #+ syntax: Syntax]
(syntax [common])
[poly #+ poly:])
[type]
(type [unit])
))
## [Derivers]
(poly: #export Eq<?>
(with-expansions
[<basic> (do-template [<matcher> <eq>]
[(do @
[_ <matcher>]
(wrap (` (: (~ (@Eq inputT))
<eq>))))]
[poly;unit (function [(~ g!_) (~ g!_)] true)]
[poly;bool bool;Eq<Bool>]
[poly;nat number;Eq<Nat>]
[poly;int number;Eq<Int>]
[poly;deg number;Eq<Deg>]
[poly;frac number;Eq<Frac>]
[poly;text text;Eq<Text>])
<time> (do-template [<type> <eq>]
[(do @
[_ (poly;this <type>)]
(wrap (` (: (~ (@Eq inputT))
<eq>))))]
[du;Duration du;Eq<Duration>]
[i;Instant i;Eq<Instant>]
[da;Date da;Eq<Date>]
[da;Day da;Eq<Day>]
[da;Month da;Eq<Month>])
<composites> (do-template [<name> <eq>]
[(do @
[[_ argC] (poly;apply (p;seq (poly;this <name>)
Eq<?>))]
(wrap (` (: (~ (@Eq inputT))
(<eq> (~ argC))))))]
[;Maybe maybe;Eq<Maybe>]
[;List list;Eq<List>]
[vector;Vector vector;Eq<Vector>]
[array;Array array;Eq<Array>]
[queue;Queue queue;Eq<Queue>]
[set;Set set;Eq<Set>]
[seq;Seq seq;Eq<Seq>]
[rose;Tree rose;Eq<Tree>]
)]
(do @
[#let [g!_ (code;local-symbol "\u0000_")]
*env* poly;env
inputT poly;peek
#let [@Eq (: (-> Type Code)
(function [type]
(` (eq;Eq (~ (poly;to-ast *env* type))))))]]
($_ p;either
## Primitive types
<basic>
## Composite types
<composites>
(do @
[[_ _ valC] (poly;apply ($_ p;seq
(poly;this dict;Dict)
poly;any
Eq<?>))]
(wrap (` (: (~ (@Eq inputT))
(dict;Eq<Dict> (~ valC))))))
## Models
<time>
(do @
[_ (poly;apply (p;seq (poly;this unit;Qty)
poly;any))]
(wrap (` (: (~ (@Eq inputT))
unit;Eq<Qty>))))
## Variants
(do @
[members (poly;variant (p;many Eq<?>))
#let [g!left (code;local-symbol "\u0000left")
g!right (code;local-symbol "\u0000right")]]
(wrap (` (: (~ (@Eq inputT))
(function [(~ g!left) (~ g!right)]
(case [(~ g!left) (~ g!right)]
(~@ (L/join (L/map (function [[tag g!eq]]
(list (` [((~ (code;nat tag)) (~ g!left))
((~ (code;nat tag)) (~ g!right))])
(` ((~ g!eq) (~ g!left) (~ g!right)))))
(list;enumerate members))))
(~ g!_)
false))))))
## Tuples
(do @
[g!eqs (poly;tuple (p;many Eq<?>))
#let [indices (|> (list;size g!eqs) n.dec (list;n.range +0))
g!lefts (L/map (|>. nat/encode (text/append "left") code;local-symbol) indices)
g!rights (L/map (|>. nat/encode (text/append "right") code;local-symbol) indices)]]
(wrap (` (: (~ (@Eq inputT))
(function [[(~@ g!lefts)] [(~@ g!rights)]]
(and (~@ (|> (list;zip3 g!eqs g!lefts g!rights)
(L/map (function [[g!eq g!left g!right]]
(` ((~ g!eq) (~ g!left) (~ g!right)))))))))))))
## Type recursion
(do @
[[g!self bodyC] (poly;recursive Eq<?>)]
(wrap (` (: (~ (@Eq inputT))
(eq;rec (;function [(~ g!self)]
(~ bodyC)))))))
poly;recursive-self
## Type applications
(do @
[[funcC argsC] (poly;apply (p;seq Eq<?> (p;many Eq<?>)))]
(wrap (` ((~ funcC) (~@ argsC)))))
## Bound type-vars
poly;bound
## Polymorphism
(do @
[[funcC varsC bodyC] (poly;polymorphic Eq<?>)]
(wrap (` (: (All [(~@ varsC)]
(-> (~@ (L/map (|>. (~) eq;Eq (`)) varsC))
(eq;Eq ((~ (poly;to-ast *env* inputT)) (~@ varsC)))))
(function (~ funcC) [(~@ varsC)]
(~ bodyC))))))
poly;recursive-call
## If all else fails...
(|> poly;any
(:: @ map (|>. %type (format "Cannot create Eq for: ") p;fail))
(:: @ join))
))))
|