aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/math/constructive.lux
blob: 762e15e3147f3a808aa43f5a74df05531373572e (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]
                ["p" parser "parser/" Monad<Parser>]
                ["ex" exception #+ exception:])
       (data [product]
             ["e" error]
             text/format
             (coll [list "list/" Functor<List>]))
       (lang [type])
       (type abstract)
       [macro]
       (macro [code]
              ["s" syntax #+ syntax:]
              (syntax (common ["scr" reader]
                              ["scw" writer]))
              [poly])))

(abstract: #export (Witness t w)
  {}

  t

  (.def: #export !
    (.All [t]
      (.Ex [w]
        (-> t (Witness t w))))
    (.|>> @abstraction))

  (.def: #export ?
    (.All [t w]
      (-> (Witness t w) t))
    (.|>> @representation))
  )

(syntax: #export (@ [name s.symbol])
  (do @
    [witnessT (macro.find-type name)]
    (.case (.<| (poly.run witnessT)
                poly.apply
                (p.after (poly.this Witness))
                (p.after poly.any)
                poly.any)
      (#e.Success identityT)
      (wrap (.list (type.to-code identityT)))
      
      (#e.Error error)
      (macro.fail error))))

(syntax: #export (proof: [export? scr.export]
                   [name s.local-symbol]
                   type
                   proof)
  (do @
    [g!w (macro.gensym "w")]
    (wrap (.list (.` (.def: (~+ (scw.export export?))
                       (~ (code.local-symbol name))
                       (..! (.: (~ type)
                                (~ proof)))))))))

(.def: type-vars
  (s.Syntax (.List .Text))
  (p.default (.list)
             (s.tuple (p.some s.local-symbol))))

(.type: Input'
  {#input-name .Text
   #input-type .Code})

(.type: Input
  (#Identified Input')
  (#Anonymous Input'))

(.do-template [<name> <tag> <type>]
  [(.def: (<name> input)
     (-> Input <type>)
     (.case input
       (#Identified input')
       (.get@ <tag> input')
       
       (#Anonymous input')
       (.get@ <tag> input')))]

  [input-name #input-name .Text]
  [input-type #input-type .Code]
  )

(.def: (identified? input)
  (-> Input .Bool)
  (.case input
    (#Identified input')
    true
    
    (#Anonymous input')
    false))

(.def: input
  (s.Syntax Input)
  (p.alt (s.record (p.seq s.local-symbol s.any))
         (s.tuple (p.seq s.local-symbol s.any))))

(.def: theorem-declaration
  (s.Syntax [.Text (.List Input)])
  (s.form (p.seq s.local-symbol (p.some input))))

(.def: proposition-declaration
  (s.Syntax [.Text (.List .Text)])
  (s.form (p.seq s.local-symbol (p.many s.local-symbol))))

(syntax: #export (proposition: [export? scr.export]
                   [[name elements] proposition-declaration]
                   [meaning (p.maybe s.any)])
  (.case meaning
    #.None
    (wrap (.list (.` ((~! abstract:) (~+ (scw.export export?))
                      ((~ (code.local-symbol name))
                       (~+ (list/map code.local-symbol elements)))

                      {}

                      .Unit))))

    (#.Some meaning)
    (wrap (.list (.` (.type: (~+ (scw.export export?))
                       ((~ (code.local-symbol name))
                        (~+ (list/map code.local-symbol elements)))
                       (~ meaning)))))))

(.def: (theorem-type type-vars inputs meaning)
  (-> (.List .Text) (.List Input) .Code .Code)
  (.let [g!universals (list/map code.local-symbol
                                type-vars)
         g!identities (.|> inputs
                           (list.filter identified?)
                           (list/map (.|>> input-name code.local-symbol)))
         g!requisites (list/map (.function [input]
                                  (.case input
                                    (#Identified input')
                                    (.` (..Witness (~ (.get@ #input-type input'))
                                                   (~ (code.local-symbol (input-name input)))))
                                    
                                    (#Anonymous input')
                                    (.get@ #input-type input')))
                                inputs)]
    (.` (.All [(~+ g!universals)]
          (.Ex [(~+ g!identities)]
            (-> (~+ g!requisites) (~ meaning)))))))

(syntax: #export (axiom [description (p.default "" s.text)])
  (wrap (.list (.` (.:!! [])))))

(syntax: #export (theorem [type-vars type-vars]
                          [[name inputs] theorem-declaration]
                          outputT
                          meaning)
  (.let [inputs-names (list/map (.|>> input-name code.local-symbol)
                                inputs)]
    (wrap (.list (.` (.: (~ (theorem-type type-vars inputs outputT))
                         (.function (~ (code.local-symbol name))
                           [(~+ inputs-names)]
                           (~ meaning))))))))

(.def: (input-code input)
  (-> Input .Code)
  (.case input
    (#Identified [name type])
    (.` {(~ (code.local-symbol name)) (~ type)})
    
    (#Anonymous [name type])
    (.` [(~ (code.local-symbol name)) (~ type)])))

(syntax: #export (theorem: [export? scr.export]
                   [type-vars type-vars]
                   [[name inputs] theorem-declaration]
                   outputT
                   meaning)
  (wrap (.list (.` (..proof: (~+ (scw.export export?))
                     (~ (code.local-symbol name))
                     (~ (theorem-type type-vars inputs outputT))
                     (..theorem [(~+ (list/map code.local-symbol type-vars))]
                                ((~ (code.local-symbol name)) (~+ (list/map input-code inputs)))
                                (~ outputT)
                                (~ meaning)))))))

(.type: #export (not p)
  (-> p .Bottom))

(.type: #export (Test p)
  (#True p)
  (#False (not p)))

(exception: #export Absurdity)

(.def: #export absurdity
  (.All [p] (-> p .Bottom))
  (.function [proof]
    (.error! (ex.construct Absurdity []))))