aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/poly/lux/abstract/equivalence.lux
blob: b6cfa2c2c2f1468631aba6cc5bd83ca3d119c9a6 (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
158
159
160
161
162
163
164
165
166
167
168
(.module:
  [lux #*
   [abstract
    [monad (#+ Monad do)]]
   [control
    ["p" parser
     ["<.>" type]
     ["s" code (#+ Parser)]]]
   [data
    ["." product]
    ["." bit]
    ["." maybe]
    [number
     ["." nat ("#\." decimal)]
     ["." int]
     ["." rev]
     ["." frac]]
    ["." text ("#\." monoid)
     ["%" format (#+ format)]]
    [collection
     ["." list ("#\." monad)]
     ["." row]
     ["." array]
     ["." queue]
     ["." set]
     ["." dictionary (#+ Dictionary)]
     ["." tree]]]
   [time
    ["." duration]
    ["." date]
    ["." instant]
    ["." day]
    ["." month]]
   [macro
    ["." code]
    ["." poly (#+ poly:)]
    [syntax (#+ syntax:)
     ["." common]]]
   ["." type
    ["." unit]]]
  {1
   ["." /]})

(poly: #export equivalence
  (`` (do {! p.monad}
        [#let [g!_ (code.local_identifier "_____________")]
         *env* <type>.env
         inputT <type>.peek
         #let [@Equivalence (: (-> Type Code)
                               (function (_ type)
                                 (` ((~! /.Equivalence) (~ (poly.to_code *env* type))))))]]
        ($_ p.either
            ## Basic types
            (~~ (template [<matcher> <eq>]
                  [(do !
                     [_ <matcher>]
                     (wrap (` (: (~ (@Equivalence inputT))
                                 <eq>))))]

                  [(<type>.exactly Any) (function ((~ g!_) (~ g!_) (~ g!_)) #1)]
                  [(<type>.sub Bit)     (~! bit.equivalence)]
                  [(<type>.sub Nat)     (~! nat.equivalence)]
                  [(<type>.sub Int)     (~! int.equivalence)]
                  [(<type>.sub Rev)     (~! rev.equivalence)]
                  [(<type>.sub Frac)    (~! frac.equivalence)]
                  [(<type>.sub Text)    (~! text.equivalence)]))
            ## Composite types
            (~~ (template [<name> <eq>]
                  [(do !
                     [[_ argC] (<type>.apply (p.and (<type>.exactly <name>)
                                                    equivalence))]
                     (wrap (` (: (~ (@Equivalence inputT))
                                 (<eq> (~ argC))))))]

                  [.Maybe      (~! maybe.equivalence)]
                  [.List       (~! list.equivalence)]
                  [row.Row     (~! row.equivalence)]
                  [array.Array (~! array.equivalence)]
                  [queue.Queue (~! queue.equivalence)]
                  [set.Set     (~! set.equivalence)]
                  [tree.Tree   (~! tree.equivalence)]
                  ))
            (do !
              [[_ _ valC] (<type>.apply ($_ p.and
                                            (<type>.exactly dictionary.Dictionary)
                                            <type>.any
                                            equivalence))]
              (wrap (` (: (~ (@Equivalence inputT))
                          ((~! dictionary.equivalence) (~ valC))))))
            ## Models
            (~~ (template [<type> <eq>]
                  [(do !
                     [_ (<type>.exactly <type>)]
                     (wrap (` (: (~ (@Equivalence inputT))
                                 <eq>))))]

                  [duration.Duration duration.equivalence]
                  [instant.Instant instant.equivalence]
                  [date.Date date.equivalence]
                  [day.Day day.equivalence]
                  [month.Month month.equivalence]
                  ))
            (do !
              [_ (<type>.apply (p.and (<type>.exactly unit.Qty)
                                      <type>.any))]
              (wrap (` (: (~ (@Equivalence inputT))
                          unit.equivalence))))
            ## Variants
            (do !
              [members (<type>.variant (p.many equivalence))
               #let [last (dec (list.size members))
                     g!_ (code.local_identifier "_____________")
                     g!left (code.local_identifier "_____________left")
                     g!right (code.local_identifier "_____________right")]]
              (wrap (` (: (~ (@Equivalence inputT))
                          (function ((~ g!_) (~ g!left) (~ g!right))
                            (case [(~ g!left) (~ g!right)]
                              (~+ (list\join (list\map (function (_ [tag g!eq])
                                                         (if (nat.= last tag)
                                                           (list (` [((~ (code.nat (dec tag))) #1 (~ g!left))
                                                                     ((~ (code.nat (dec tag))) #1 (~ g!right))])
                                                                 (` ((~ g!eq) (~ g!left) (~ g!right))))
                                                           (list (` [((~ (code.nat tag)) #0 (~ g!left))
                                                                     ((~ (code.nat tag)) #0 (~ g!right))])
                                                                 (` ((~ g!eq) (~ g!left) (~ g!right))))))
                                                       (list.enumeration members))))
                              (~ g!_)
                              #0))))))
            ## Tuples
            (do !
              [g!eqs (<type>.tuple (p.many equivalence))
               #let [g!_ (code.local_identifier "_____________")
                     indices (list.indices (list.size g!eqs))
                     g!lefts (list\map (|>> nat\encode (text\compose "left") code.local_identifier) indices)
                     g!rights (list\map (|>> nat\encode (text\compose "right") code.local_identifier) indices)]]
              (wrap (` (: (~ (@Equivalence inputT))
                          (function ((~ g!_) [(~+ g!lefts)] [(~+ g!rights)])
                            (and (~+ (|> (list.zip/3 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] (<type>.recursive equivalence)
               #let [g!_ (code.local_identifier "_____________")]]
              (wrap (` (: (~ (@Equivalence inputT))
                          ((~! /.rec) (.function ((~ g!_) (~ g!self))
                                        (~ bodyC)))))))
            <type>.recursive_self
            ## Type applications
            (do !
              [[funcC argsC] (<type>.apply (p.and equivalence (p.many equivalence)))]
              (wrap (` ((~ funcC) (~+ argsC)))))
            ## Parameters
            <type>.parameter
            ## Polymorphism
            (do !
              [[funcC varsC bodyC] (<type>.polymorphic equivalence)]
              (wrap (` (: (All [(~+ varsC)]
                            (-> (~+ (list\map (|>> (~) ((~! /.Equivalence)) (`)) varsC))
                                ((~! /.Equivalence) ((~ (poly.to_code *env* inputT)) (~+ varsC)))))
                          (function ((~ funcC) (~+ varsC))
                            (~ bodyC))))))
            <type>.recursive_call
            ## If all else fails...
            (|> <type>.any
                (\ ! map (|>> %.type (format "Cannot create Equivalence for: ") p.fail))
                (\ ! join))
            ))))