aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/poly/lux/abstract/equivalence.lux
blob: b311d97ec26edfabe5afe74f013e17917f1d0746 (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:
  [library
   [lux #*
    [abstract
     [monad (#+ Monad do)]]
    [control
     ["." maybe]
     ["<>" parser
      ["<.>" type]]]
    [data
     ["." product]
     ["." bit]
     ["." text ("#\." monoid)
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." monad)]
      ["." row]
      ["." array]
      ["." queue]
      ["." set]
      ["." dictionary (#+ Dictionary)]
      ["." tree]]]
    [macro
     [syntax (#+ syntax:)]
     ["." code]]
    [math
     [number
      ["." nat ("#\." decimal)]
      ["." int]
      ["." rev]
      ["." frac]]]
    [time
     ["." duration]
     ["." date]
     ["." instant]
     ["." day]
     ["." month]]
    ["." type
     ["." poly (#+ poly:)]
     ["." unit]]]]
  [\\library
   ["." /]])

(poly: .public equivalence
  (`` (do {! <>.monad}
        [.let [g!_ (code.local_identifier "_____________")]
         *env* <type>.env
         inputT <type>.next
         .let [@Equivalence (: (-> Type Code)
                               (function (_ type)
                                 (` ((~! /.Equivalence) (~ (poly.code *env* type))))))]]
        ($_ <>.either
            ... Basic types
            (~~ (template [<matcher> <eq>]
                  [(do !
                     [_ <matcher>]
                     (in (` (: (~ (@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>.applied (<>.and (<type>.exactly <name>)
                                                       equivalence))]
                     (in (` (: (~ (@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>.applied ($_ <>.and
                                              (<type>.exactly dictionary.Dictionary)
                                              <type>.any
                                              equivalence))]
              (in (` (: (~ (@Equivalence inputT))
                        ((~! dictionary.equivalence) (~ valC))))))
            ... Models
            (~~ (template [<type> <eq>]
                  [(do !
                     [_ (<type>.exactly <type>)]
                     (in (` (: (~ (@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>.applied (<>.and (<type>.exactly unit.Qty)
                                         <type>.any))]
              (in (` (: (~ (@Equivalence inputT))
                        unit.equivalence))))
            ... Variants
            (do !
              [members (<type>.variant (<>.many equivalence))
               .let [last (dec (list.size members))
                     g!_ (code.local_identifier "_____________")
                     g!left (code.local_identifier "_____________left")
                     g!right (code.local_identifier "_____________right")]]
              (in (` (: (~ (@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 (<>.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)]]
              (in (` (: (~ (@Equivalence inputT))
                        (function ((~ g!_) [(~+ g!lefts)] [(~+ g!rights)])
                          (and (~+ (|> (list.zipped/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 "_____________")]]
              (in (` (: (~ (@Equivalence inputT))
                        ((~! /.rec) (.function ((~ g!_) (~ g!self))
                                      (~ bodyC)))))))
            <type>.recursive_self
            ... Type applications
            (do !
              [[funcC argsC] (<type>.applied (<>.and equivalence (<>.many equivalence)))]
              (in (` ((~ funcC) (~+ argsC)))))
            ... Parameters
            <type>.parameter
            ... Polymorphism
            (do !
              [[funcC varsC bodyC] (<type>.polymorphic equivalence)]
              (in (` (: (All [(~+ varsC)]
                          (-> (~+ (list\map (|>> (~) ((~! /.Equivalence)) (`)) varsC))
                              ((~! /.Equivalence) ((~ (poly.code *env* inputT)) (~+ varsC)))))
                        (function ((~ funcC) (~+ varsC))
                          (~ bodyC))))))
            <type>.recursive_call
            ... If all else fails...
            (|> <type>.any
                (\ ! map (|>> %.type (format "Cannot create Equivalence for: ") <>.failure))
                (\ ! join))
            ))))