aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension.lux
blob: 02d8b32decad0d6b7b001862f3ac8f4516a313e1 (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
(.module:
  [library
   [lux {"-" Name}
    [abstract
     [equivalence {"+" Equivalence}]
     [hash {"+" Hash}]
     ["[0]" monad {"+" do}]]
    [control
     ["[0]" function]
     ["[0]" try {"+" Try}]
     ["[0]" exception {"+" exception:}]]
    [data
     ["[0]" product]
     ["[0]" text ("[1]#[0]" order)
      ["%" format {"+" Format format}]]
     [collection
      ["[0]" list]
      ["[0]" dictionary {"+" Dictionary}]]]]]
  [/////
   ["//" phase]
   [meta
    [archive {"+" Archive}]]])

(type: .public Name
  Text)

(type: .public (Extension a)
  [Name (List a)])

(def: .public equivalence
  (All (_ a) (-> (Equivalence a) (Equivalence (Extension a))))
  (|>> list.equivalence
       (product.equivalence text.equivalence)))

(def: .public hash
  (All (_ a) (-> (Hash a) (Hash (Extension a))))
  (|>> list.hash
       (product.hash text.hash)))

(with_expansions [<Bundle> (as_is (Dictionary Name (Handler s i o)))]
  (type: .public (Handler s i o)
    (-> Name
        (//.Phase [<Bundle> s] i o)
        (//.Phase [<Bundle> s] (List i) o)))

  (type: .public (Bundle s i o)
    <Bundle>))

(def: .public empty
  Bundle
  (dictionary.empty text.hash))

(type: .public (State s i o)
  (Record
   [#bundle (Bundle s i o)
    #state s]))

(type: .public (Operation s i o v)
  (//.Operation (State s i o) v))

(type: .public (Phase s i o)
  (//.Phase (State s i o) i o))

(exception: .public (cannot_overwrite [name Name])
  (exception.report
   ["Extension" (%.text name)]))

(exception: .public (incorrect_arity [name Name
                                      arity Nat
                                      args Nat])
  (exception.report
   ["Extension" (%.text name)]
   ["Expected" (%.nat arity)]
   ["Actual" (%.nat args)]))

(exception: .public [a] (invalid_syntax [name Name
                                         %format (Format a)
                                         inputs (List a)])
  (exception.report
   ["Extension" (%.text name)]
   ["Inputs" (exception.listing %format inputs)]))

(exception: .public [s i o] (unknown [name Name
                                      bundle (Bundle s i o)])
  (exception.report
   ["Extension" (%.text name)]
   ["Available" (|> bundle
                    dictionary.keys
                    (list.sorted text#<)
                    (exception.listing %.text))]))

(type: .public (Extender s i o)
  (-> Any (Handler s i o)))

(def: .public (install extender name handler)
  (All (_ s i o)
    (-> (Extender s i o) Text (Handler s i o) (Operation s i o Any)))
  (function (_ [bundle state])
    (case (dictionary.value name bundle)
      {.#None}
      {try.#Success [[(dictionary.has name (extender handler) bundle) state]
                     []]}

      _
      (exception.except ..cannot_overwrite name))))

(def: .public (with extender extensions)
  (All (_ s i o)
    (-> Extender (Bundle s i o) (Operation s i o Any)))
  (|> extensions
      dictionary.entries
      (monad.mix //.monad
                 (function (_ [extension handle] output)
                   (..install extender extension handle))
                 [])))

(def: .public (apply archive phase [name parameters])
  (All (_ s i o)
    (-> Archive (Phase s i o) (Extension i) (Operation s i o o)))
  (function (_ (^@ stateE [bundle state]))
    (case (dictionary.value name bundle)
      {.#Some handler}
      (((handler name phase) archive parameters)
       stateE)

      {.#None}
      (exception.except ..unknown [name bundle]))))

(def: .public (localized get set transform)
  (All (_ s s' i o v)
    (-> (-> s s') (-> s' s s) (-> s' s')
        (-> (Operation s i o v) (Operation s i o v))))
  (function (_ operation)
    (function (_ [bundle state])
      (let [old (get state)]
        (case (operation [bundle (set (transform old) state)])
          {try.#Success [[bundle' state'] output]}
          {try.#Success [[bundle' (set old state')] output]}

          {try.#Failure error}
          {try.#Failure error})))))

(def: .public (temporary transform)
  (All (_ s i o v)
    (-> (-> s s)
        (-> (Operation s i o v) (Operation s i o v))))
  (function (_ operation)
    (function (_ [bundle state])
      (case (operation [bundle (transform state)])
        {try.#Success [[bundle' state'] output]}
        {try.#Success [[bundle' state] output]}

        {try.#Failure error}
        {try.#Failure error}))))

(def: .public (with_state state)
  (All (_ s i o v)
    (-> s (-> (Operation s i o v) (Operation s i o v))))
  (..temporary (function.constant state)))

(def: .public (read get)
  (All (_ s i o v)
    (-> (-> s v) (Operation s i o v)))
  (function (_ [bundle state])
    {try.#Success [[bundle state] (get state)]}))

(def: .public (update transform)
  (All (_ s i o)
    (-> (-> s s) (Operation s i o Any)))
  (function (_ [bundle state])
    {try.#Success [[bundle (transform state)] []]}))

(def: .public (lifted action)
  (All (_ s i o v)
    (-> (//.Operation s v)
        (//.Operation [(Bundle s i o) s] v)))
  (function (_ [bundle state])
    (case (action state)
      {try.#Success [state' output]}
      {try.#Success [[bundle state'] output]}

      {try.#Failure error}
      {try.#Failure error})))