blob: ae910874cc89f5a3c4846904673e35209d8ab3c2 (
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
|
(.module:
[lux #*
[control
[monad (#+ do Monad)]
["eq" equivalence]
["p" parser]]
[data
["." product]
["." bit]
["." maybe]
["." number ("nat/." Codec<Text,Nat>)]
["." text ("text/." Monoid<Text>)
format]
[collection
["." list ("list/." Monad<List>)]
["." row]
["." array]
["." queue]
["." set]
["dict" dictionary (#+ Dictionary)]
[tree
["." rose]]]]
[time
["du" duration]
["da" date]
["i" instant]]
["." macro
["." code]
[syntax (#+ syntax: Syntax)
["." common]]
["." poly (#+ poly:)]]
["." type
["." unit]]])
## [Derivers]
(poly: #export Equivalence<?>
(`` (do @
[#let [g!_ (code.local-symbol "_____________")]
*env* poly.env
inputT poly.peek
#let [@Equivalence (: (-> Type Code)
(function (_ type)
(` (eq.Equivalence (~ (poly.to-code *env* type))))))]]
($_ p.either
## Basic types
(~~ (do-template [<matcher> <eq>]
[(do @
[_ <matcher>]
(wrap (` (: (~ (@Equivalence inputT))
<eq>))))]
[(poly.exactly Any) (function ((~ g!_) (~ g!_) (~ g!_)) #1)]
[(poly.similar Bit) bit.Equivalence<Bit>]
[(poly.similar Nat) number.Equivalence<Nat>]
[(poly.similar Int) number.Equivalence<Int>]
[(poly.similar Rev) number.Equivalence<Rev>]
[(poly.similar Frac) number.Equivalence<Frac>]
[(poly.similar Text) text.Equivalence<Text>]))
## Composite types
(~~ (do-template [<name> <eq>]
[(do @
[[_ argC] (poly.apply (p.seq (poly.exactly <name>)
Equivalence<?>))]
(wrap (` (: (~ (@Equivalence inputT))
(<eq> (~ argC))))))]
[.Maybe maybe.Equivalence<Maybe>]
[.List list.Equivalence<List>]
[row.Row row.Equivalence<Row>]
[.Array array.Equivalence<Array>]
[queue.Queue queue.Equivalence<Queue>]
[set.Set set.Equivalence<Set>]
[rose.Tree rose.Equivalence<Tree>]
))
(do @
[[_ _ valC] (poly.apply ($_ p.seq
(poly.exactly dict.Dictionary)
poly.any
Equivalence<?>))]
(wrap (` (: (~ (@Equivalence inputT))
(dict.Equivalence<Dictionary> (~ valC))))))
## Models
(~~ (do-template [<type> <eq>]
[(do @
[_ (poly.exactly <type>)]
(wrap (` (: (~ (@Equivalence inputT))
<eq>))))]
[du.Duration du.Equivalence<Duration>]
[i.Instant i.Equivalence<Instant>]
[da.Date da.Equivalence<Date>]
[da.Day da.Equivalence<Day>]
[da.Month da.Equivalence<Month>]))
(do @
[_ (poly.apply (p.seq (poly.exactly unit.Qty)
poly.any))]
(wrap (` (: (~ (@Equivalence inputT))
unit.Equivalence<Qty>))))
## Variants
(do @
[members (poly.variant (p.many Equivalence<?>))
#let [g!_ (code.local-symbol "_____________")
g!left (code.local-symbol "_____________left")
g!right (code.local-symbol "_____________right")]]
(wrap (` (: (~ (@Equivalence inputT))
(function ((~ g!_) (~ g!left) (~ g!right))
(case [(~ g!left) (~ g!right)]
(~+ (list/join (list/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!_)
#0))))))
## Tuples
(do @
[g!eqs (poly.tuple (p.many Equivalence<?>))
#let [g!_ (code.local-symbol "_____________")
indices (|> (list.size g!eqs) dec (list.n/range +0))
g!lefts (list/map (|>> nat/encode (text/compose "left") code.local-symbol) indices)
g!rights (list/map (|>> nat/encode (text/compose "right") code.local-symbol) indices)]]
(wrap (` (: (~ (@Equivalence inputT))
(function ((~ g!_) [(~+ g!lefts)] [(~+ g!rights)])
(and (~+ (|> (list.zip3 g!eqs g!lefts g!rights)
(list/map (function (_ [g!eq g!left g!right])
(` ((~ g!eq) (~ g!left) (~ g!right)))))))))))))
## Type recursion
(do @
[[g!self bodyC] (poly.recursive Equivalence<?>)
#let [g!_ (code.local-symbol "_____________")]]
(wrap (` (: (~ (@Equivalence inputT))
(eq.rec (.function ((~ g!_) (~ g!self))
(~ bodyC)))))))
poly.recursive-self
## Type applications
(do @
[[funcC argsC] (poly.apply (p.seq Equivalence<?> (p.many Equivalence<?>)))]
(wrap (` ((~ funcC) (~+ argsC)))))
## Parameters
poly.parameter
## Polymorphism
(do @
[[funcC varsC bodyC] (poly.polymorphic Equivalence<?>)]
(wrap (` (: (All [(~+ varsC)]
(-> (~+ (list/map (|>> (~) eq.Equivalence (`)) varsC))
(eq.Equivalence ((~ (poly.to-code *env* inputT)) (~+ varsC)))))
(function ((~ funcC) (~+ varsC))
(~ bodyC))))))
poly.recursive-call
## If all else fails...
(|> poly.any
(:: @ map (|>> %type (format "Cannot create Equivalence for: ") p.fail))
(:: @ join))
))))
|