aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/type/quotient.lux
blob: 63d022a31952de15b24e2177eeaf5aa80f0b3d6b (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
(.module:
  [lux (#- type)
   [control
    [monad (#+ do)]
    ["p" parser]]
   [data
    ["e" error (#+ Error)]]
   [language
    ["." type]]
   [type
    abstract]
   ["." macro
    ["s" syntax (#+ syntax:)]
    ["." poly]]])

(abstract: #export (Class t c q)
  {}

  (-> t c)

  (def: #export class
    (All [t c]
      (Ex [q]
        (-> (-> t c) (Class t c q))))
    (|>> :abstraction))

  (def: expose
    (All [t c q] (-> (Class t c q) (-> t c)))
    (|>> :representation))
  )

(abstract: #export (Quotient t c q)
  {}

  {#value t
   #label c}

  (def: #export (quotient class value)
    (All [t c q]
      (-> (Class t c q) t
          (Quotient t c q)))
    (:abstraction {#value value
                   #label ((expose class) value)}))

  (do-template [<name> <output> <slot>]
    [(def: #export <name>
       (All [t c q] (-> (Quotient t c q) <output>))
       (|>> :representation (get@ <slot>)))]

    [value t #value]
    [label c #label]
    )
  )

(def: (quotient-type constructor-type)
  (-> Type (Error Type))
  (<| (poly.run constructor-type)
      (do p.Monad<Parser>
        [[valueT classT quotient-ex] (<| poly.apply (p.after (poly.exactly ..Class))
                                         ($_ p.seq poly.any poly.any poly.existential))]
        (wrap (.type (..Quotient valueT classT (:~ (#.Ex quotient-ex))))))))

(syntax: #export (type {quotient s.symbol})
  (do @
    [constructorT (macro.find-type quotient)
     quotientT (case (quotient-type constructorT)
                 (#e.Success quotientT)
                 (wrap quotientT)

                 (#e.Error error)
                 (p.fail error))]
    (wrap (list (type.to-code quotientT)))))