aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/analyser/struct.lux
blob: 0ca3c9f630eebe3e5e1441e3e00ab0f9d6fda018 (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
(;module:
  lux
  (lux (control monad
                pipe)
       [io #- run]
       (concurrency ["A" atom])
       (data [text "T/" Eq<Text>]
             text/format
             (coll [list "L/" Fold<List> Monoid<List> Monad<List>]
                   ["D" dict])
             [number]
             [product])
       [macro #+ Monad<Lux>]
       [type]
       (type ["TC" check]))
  (luxc ["&" base]
        (lang ["la" analysis #+ Analysis])
        ["&;" module]
        ["&;" env]
        (analyser ["&;" common])))

## [Analysers]
(def: (analyse-typed-tuple analyse members)
  (-> &;Analyser (List Code) (Lux Analysis))
  (do Monad<Lux>
    [expected macro;expected-type]
    (let [member-types (type;flatten-tuple expected)
          num-types (list;size member-types)
          num-members (list;size members)]
      (cond (n.= num-types num-members)
            (do @
              [=tuple (: (Lux (List Analysis))
                         (mapM @
                               (function [[expected member]]
                                 (&;with-expected-type expected
                                   (analyse member)))
                               (list;zip2 member-types members)))]
              (wrap (#la;Tuple =tuple)))

            (n.< num-types num-members)
            (do @
              [#let [[head-ts tail-ts] (list;split (n.- +2 num-members)
                                                   member-types)]
               =prevs (mapM @
                            (function [[expected member]]
                              (&;with-expected-type expected
                                (analyse member)))
                            (list;zip2 head-ts members))
               =last (&;with-expected-type (type;tuple tail-ts)
                       (analyse (default (undefined)
                                  (list;last members))))]
              (wrap (#la;Tuple (L/append =prevs (list =last)))))

            ## (n.> num-types num-members)
            (do @
              [#let [[head-xs tail-xs] (list;split (n.- +2 num-types)
                                                   members)]
               =prevs (mapM @
                            (function [[expected member]]
                              (&;with-expected-type expected
                                (analyse member)))
                            (list;zip2 member-types head-xs))
               =last (&;with-expected-type (default (undefined)
                                             (list;last member-types))
                       (analyse-typed-tuple analyse tail-xs))]
              (wrap (#la;Tuple (L/append =prevs (list =last)))))
            ))))

(def: #export (analyse-tuple analyse members)
  (-> &;Analyser (List Code) (Lux Analysis))
  (do Monad<Lux>
    [expected macro;expected-type]
    (&;with-stacked-errors
      (function [_] (format "Invalid type for tuple: " (%type expected)))
      (case expected
        (#;Product _)
        (analyse-typed-tuple analyse members)

        (#;Named name unnamedT)
        (&;with-expected-type unnamedT
          (analyse-tuple analyse members))

        (#;Var id)
        (do @
          [bound? (&;within-type-env
                   (TC;bound? id))]
          (if bound?
            (do @
              [expected' (&;within-type-env
                          (TC;read-var id))]
              (&;with-expected-type expected'
                (analyse-tuple analyse members)))
            (do @
              [=members (mapM @ (|>. analyse &common;with-unknown-type)
                              members)
               #let [tuple-type (type;tuple (L/map product;left =members))]
               _ (&;within-type-env
                  (TC;check expected tuple-type))]
              (wrap (#la;Tuple (L/map product;right =members))))))

        (#;UnivQ _)
        (do @
          [[var-id var] (&;within-type-env
                         TC;existential)]
          (&;with-expected-type (assume (type;apply-type expected var))
            (analyse-tuple analyse members)))

        (#;ExQ _)
        (&common;with-var
          (function [[var-id var]]
            (&;with-expected-type (assume (type;apply-type expected var))
              (analyse-tuple analyse members))))
        
        _
        (&;fail "")
        ))))

(def: #export (analyse-variant analyse tag value)
  (-> &;Analyser Nat Code (Lux Analysis))
  (do Monad<Lux>
    [expected macro;expected-type]
    (&;with-stacked-errors
      (function [_] (format "Invalid type for variant: " (%type expected)))
      (case expected
        (#;Sum _)
        (let [flat (type;flatten-variant expected)
              type-size (list;size flat)]
          (if (n.< type-size tag)
            (do @
              [#let [last? (n.= tag (n.dec type-size))
                     variant-type (default (undefined)
                                    (list;nth tag flat))]
               =value (&;with-expected-type variant-type
                        (analyse value))]
              (wrap (#la;Variant tag last? =value)))
            (&;fail (format "Trying to create variant with tag beyond type's limitations." "\n"
                            "      Tag: " (%n tag) "\n"
                            "Type size: " (%n type-size) "\n"
                            "     Type: " (%type expected) "\n"))))

        (#;Named name unnamedT)
        (&;with-expected-type unnamedT
          (analyse-variant analyse tag value))

        (#;Var id)
        (do @
          [bound? (&;within-type-env
                   (TC;bound? id))]
          (if bound?
            (do @
              [expected' (&;within-type-env
                          (TC;read-var id))]
              (&;with-expected-type expected'
                (analyse-variant analyse tag value)))
            (&;fail (format "Invalid type for variant: " (%type expected)))))

        (#;UnivQ _)
        (do @
          [[var-id var] (&;within-type-env
                         TC;existential)]
          (&;with-expected-type (assume (type;apply-type expected var))
            (analyse-variant analyse tag value)))

        (#;ExQ _)
        (&common;with-var
          (function [[var-id var]]
            (&;with-expected-type (assume (type;apply-type expected var))
              (analyse-variant analyse tag value))))
        
        _
        (&;fail "")))))