aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/function/polymorphism/context.lux
blob: ce9d5a149633cb62656a20202d56c98be977f8dc (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
198
199
200
201
202
203
(.require
 [library
  [lux (.except def with)
   [abstract
    [monad (.only do)]
    ["[0]" hash]]
   [control
    [reader (.only Reader)]
    ["?" parser (.use "[1]#[0]" monad)]
    [function
     ["[0]" predicate (.only Predicate)]]]
   [data
    ["[0]" product]
    ["[0]" text (.use "[1]#[0]" equivalence)]
    [collection
     ["[0]" set (.only Set)]
     ["[0]" list (.use "[1]#[0]" functor)]]]
   ["[0]" meta (.only)
    ["[0]" symbol]
    ["[0]" static]
    ["[0]" code (.only)
     ["?[1]" \\parser (.only Parser)]]
    ["[0]" macro (.only with_symbols)
     ["[0]" context]
     [syntax (.only syntax)
      ["[0]" export]]]
    [type
     [primitive (.except #name)]]]]]
 [///
  ["//" mixin]])

(.def .public (altered alteration scope)
  (All (_ context value)
    (-> (-> context context) (Reader context value)
        (Reader context value)))
  (function (_ context)
    (scope (alteration context))))

(with_expansions [<representation> Symbol]
  (primitive .public Layer
    <representation>

    (.def .public layer
      (syntax (_ [[export_policy name] (export.parser ?code.local)])
        (do meta.monad
          [@ meta.current_module_name]
          (in (list (` (.def (, export_policy) (, (code.local name))
                         Layer
                         (<| (as Layer)
                             (is <representation>)
                             [(, (code.text @))
                              (, (code.text name))]))))))))

    (type .public Context
      (Set Layer))

    (.def .public empty
      Context
      (set.empty (at hash.functor each (|>> representation) symbol.hash)))
    ))

(with_template [<name> <change>]
  [(.def .public (<name> layer scope)
     (All (_ value)
       (-> Layer (Reader Context value)
           (Reader Context value)))
     (function (_ context)
       (scope (<change> layer context))))]

  [with set.has]
  [without set.lacks]
  )

(.def .public (active? layer)
  (All (_ value)
    (-> Layer (Predicate Context)))
  (function (_ context)
    (set.member? context layer)))

(.def .public inactive?
  (All (_ value)
    (-> Layer (Predicate Context)))
  (|>> active? predicate.complement))

(type Polymorphism
  (Record
   [#function Text
    #quantifications (List Code)
    #context Code
    #inputs (List Code)
    #output Code
    #default Code
    #export_policy Code
    #scenarios (List Code)]))

(context.def [stack expression declaration] Polymorphism)

(type Signature
  (Record
   [#name Text
    #next Text
    #parameters (List Code)]))

(.def signature
  (Parser Signature)
  (?code.form
   (all ?.and
        ?code.local
        ?code.local
        (?.many ?code.any))))

(.def (quoted it)
  (-> Code Code)
  (` ((,' .,') (, it))))

(.def .public def
  (syntax (_ [[export_policy signature] (export.parser ..signature)
              quantifications (?code.tuple (?.some ?code.any))
              context ?code.any
              inputs (?code.tuple (?.many ?code.any))
              output ?code.any
              default ?code.any
              methods (?.some ?code.any)])
    (<| (with_symbols [g!self g!_ g!scenarios g!scenario g!mixin])
        (..declaration [#function (the #name signature)
                        #quantifications quantifications
                        #context context
                        #inputs inputs
                        #output output
                        #default default
                        #export_policy export_policy
                        #scenarios (list)])
        (let [name (quoted (code.local (the #name signature)))
              next (quoted (code.local (the #next signature)))
              parameters (list#each quoted (the #parameters signature))
              [@ _] (symbol .._)

              g!self (quoted g!self)])
        (` (these (,* methods)

                  (static.expansion
                   (do meta.monad
                     [[(, g!_) (, g!_) (, g!_) (, g!_) (, g!_) (, g!_) (, g!_) (, g!scenarios)]
                      (context.search' (|>> product.left
                                            (at text.equivalence (,' =) (, (code.text (the #name signature)))))
                                       [(.in_module# (, (code.text @)) ..stack)
                                        (symbol ..stack)])]
                     (at meta.monad (,' in)
                         (list (` (.def (, export_policy) (, name)
                                    (, (quoted (` (<| (,* quantifications)
                                                      (-> (,* inputs) (Reader (, context) (, output)))))))
                                    (let [(, (quoted g!mixin)) (is (, (quoted (` (<| (,* quantifications)
                                                                                     (-> [(,* inputs)] (Reader (, context) (, output)))))))
                                                                   (//.fixed (all //.mixed
                                                                                  ((,' .,*) (, g!scenarios))
                                                                                  (is (, (quoted (` (<| (,* quantifications)
                                                                                                        (//.Mixin [(,* inputs)] (Reader (, context) (, output)))))))
                                                                                      (function ((, g!self) (, next) (, name) [(,* parameters)])
                                                                                        (, (quoted default))))
                                                                                  )))]
                                      (, (when (the #parameters signature)
                                           (list _)
                                           (quoted g!mixin)

                                           _
                                           (` (function ((, name) (,* parameters))
                                                ((, (quoted g!mixin)) [(,* parameters)]))))))))))))
                  )))))

(.def .public method
  (syntax (_ [signature ..signature
              predicate ?code.any
              body ?code.any])
    (do [! meta.monad]
      [.let [criterion (is (Predicate Polymorphism)
                           (|>> (the #function)
                                (text#= (the #name signature))))]
       it (context.search criterion ..stack)]
      (with_symbols [g!self g!predicate g!parameters g!context g!_ g!next g!again]
        (do !
          [_ (context.revised {.#Some criterion}
                              (revised #scenarios (|>> {.#Item (` (let [(, g!predicate) (is (<| (,* (the #quantifications it))
                                                                                                (Predicate (, (the #context it))))
                                                                                            (, predicate))]
                                                                    (is (<| (,* (the #quantifications it))
                                                                            (//.Mixin [(,* (the #inputs it))]
                                                                                      (Reader (, (the #context it))
                                                                                              (, (the #output it)))))
                                                                        (function ((, g!_) (, g!next) (, g!again) (, g!parameters) (, g!context))
                                                                          (if ((, g!predicate) (, g!context))
                                                                            ((, g!self) (, g!next) (, g!again) (, g!parameters) (, g!context))
                                                                            ((, g!next) (, g!parameters) (, g!context)))))))}))
                              ..stack)]
          (in (list (` (.def (, (the #export_policy it)) (, g!self)
                         (<| (,* (the #quantifications it))
                             (//.Mixin [(,* (the #inputs it))]
                                       (Reader (, (the #context it))
                                               (, (the #output it)))))
                         (function ((, g!self)
                                    (, (code.local (the #next signature)))
                                    (, (code.local (the #name signature)))
                                    [(,* (the #parameters signature))])
                           (, body)))))))))))