aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/compiler/default/repl/type.lux
blob: b78d1785c86cc3e00e461dc9e280467012793897 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(.module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:]
                ["p" parser]
                pipe)
       (data [error #+ Error]
             text/format
             (format [xml #+ XML]
                     [json #+ JSON])
             (collection [list]))
       (time [instant #+ Instant]
             [duration #+ Duration]
             [date #+ Date])
       [function]
       [macro]
       (macro [code]
              [poly #+ Poly])
       (lang [type])))

(exception: #export (cannot-represent-value {type Type})
  (ex.report ["Type" (%type type)]))

(type: Representation (-> Any Text))

(def: primitive-representation
  (Poly Representation)
  (`` ($_ p.either
          (do p.Monad<Parser>
            [_ (poly.this Any)]
            (wrap (function.constant "[]")))
          
          (~~ (do-template [<type> <formatter>]
                [(do p.Monad<Parser>
                   [_ (poly.like <type>)]
                   (wrap (|>> (:coerce <type>) <formatter>)))]

                [Bool %b]
                [Nat %n]
                [Int %i]
                [Rev %r]
                [Frac %f]
                [Text %t])))))

(def: (special-representation representation)
  (-> (Poly Representation) (Poly Representation))
  (`` ($_ p.either
          (~~ (do-template [<type> <formatter>]
                [(do p.Monad<Parser>
                   [_ (poly.like <type>)]
                   (wrap (|>> (:coerce <type>) <formatter>)))]

                [Type %type]
                [Code %code]
                [Instant %instant]
                [Duration %duration]
                [Date %date]
                [JSON %json]
                [XML %xml]))

          (do p.Monad<Parser>
            [[_ elemT] (poly.apply (p.seq (poly.this List) poly.any))
             elemR (poly.local (list elemT) representation)]
            (wrap (|>> (:coerce (List Any)) (%list elemR))))

          (do p.Monad<Parser>
            [[_ elemT] (poly.apply (p.seq (poly.this Maybe) poly.any))
             elemR (poly.local (list elemT) representation)]
            (wrap (|>> (:coerce (Maybe Any))
                       (case> #.None
                              "#.None"

                              (#.Some elemV)
                              (format "(#.Some " (elemR elemV) ")"))))))))

(def: (record-representation tags representation)
  (-> (List Ident) (Poly Representation) (Poly Representation))
  (do p.Monad<Parser>
    [membersR+ (poly.tuple (p.many representation))
     _ (p.assert "Number of tags does not match record type size."
                 (n/= (list.size tags) (list.size membersR+)))]
    (wrap (function (_ recordV)
            (let [record-body (loop [pairs-left (list.zip2 tags membersR+)
                                     recordV recordV]
                                (case pairs-left
                                  #.Nil
                                  ""

                                  (#.Cons [tag repr] #.Nil)
                                  (format (%code (code.tag tag)) " " (repr recordV))

                                  (#.Cons [tag repr] tail)
                                  (let [[leftV rightV] (:coerce [Any Any] recordV)]
                                    (format (%code (code.tag tag)) " " (repr leftV) " "
                                            (recur tail rightV)))))]
              (format "{" record-body "}"))))))

(def: (variant-representation tags representation)
  (-> (List Ident) (Poly Representation) (Poly Representation))
  (do p.Monad<Parser>
    [casesR+ (poly.variant (p.many representation))
     #let [num-tags (list.size tags)]
     _ (p.assert "Number of tags does not match variant type size."
                 (n/= num-tags (list.size casesR+)))]
    (wrap (function (_ variantV)
            (loop [cases-left (list.zip3 tags
                                         (list.n/range +0 (dec num-tags))
                                         casesR+)
                   variantV variantV]
              (case cases-left
                #.Nil
                ""

                (#.Cons [tag-name tag-idx repr] #.Nil)
                (let [[_tag _last? _value] (:coerce [Nat Text Any] variantV)]
                  (if (n/= tag-idx _tag)
                    (format "(" (%code (code.tag tag-name)) " " (repr _value) ")")
                    (undefined)))

                (#.Cons [tag-name tag-idx repr] tail)
                (let [[_tag _last? _value] (:coerce [Nat Text Any] variantV)]
                  (if (n/= tag-idx _tag)
                    (format "(" (%code (code.tag tag-name)) " " (repr _value) ")")
                    (recur tail variantV)))))))))

(def: (tagged-representation compiler representation)
  (-> Lux (Poly Representation) (Poly Representation))
  (do p.Monad<Parser>
    [[name anonymous] poly.named]
    (case (macro.run compiler (macro.tags-of name))
      (#error.Success ?tags)
      (case ?tags
        (#.Some tags)
        (poly.local (list anonymous)
                    (p.either (record-representation tags representation)
                              (variant-representation tags representation)))
        
        #.None
        representation)
      
      (#error.Error error)
      (p.fail error))))

(def: (tuple-representation representation)
  (-> (Poly Representation) (Poly Representation))
  (do p.Monad<Parser>
    [membersR+ (poly.tuple (p.many representation))]
    (wrap (function (_ tupleV)
            (let [tuple-body (loop [representations membersR+
                                    tupleV tupleV]
                               (case representations
                                 #.Nil
                                 ""
                                 
                                 (#.Cons lastR #.Nil)
                                 (lastR tupleV)
                                 
                                 (#.Cons headR tailR)
                                 (let [[leftV rightV] (:coerce [Any Any] tupleV)]
                                   (format (headR leftV) " " (recur tailR rightV)))))]
              (format "[" tuple-body "]"))))))

(def: (representation compiler)
  (-> Lux (Poly Representation))
  (p.rec
   (function (_ representation)
     ($_ p.either
         primitive-representation
         (special-representation representation)
         (tagged-representation compiler representation)
         (tuple-representation representation)

         (do p.Monad<Parser>
           [[funcT inputsT+] (poly.apply (p.seq poly.any (p.many poly.any)))]
           (case (type.apply inputsT+ funcT)
             (#.Some outputT)
             (poly.local (list outputT) representation)

             #.None
             (p.fail "")))

         (do p.Monad<Parser>
           [[name anonymous] poly.named]
           (poly.local (list anonymous) representation))

         (p.fail "")
         ))))

(def: #export (represent compiler type value)
  (-> Lux Type Any Text)
  (case (poly.run type (representation compiler))
    (#error.Success representation)
    (ex.report ["Type" (%type type)]
               ["Value" (representation value)])

    (#error.Error error)
    (ex.construct cannot-represent-value [type])))