aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/synthesis/expression.lux
blob: 52ea3380526a5e6c7c2286f4e3a73c7a7e4e81bb (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
(.module:
  [lux #- primitive]
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data [maybe]
             (coll [list "list/" Functor<List>]
                   (dictionary ["dict" unordered #+ Dict]))))
  [///reference]
  [///compiler "operation/" Monad<Operation>]
  [///analysis #+ Analysis]
  [///extension #+ Extension]
  [// #+ Synthesis]
  [//function]
  [//case])

(exception: #export (unknown-synthesis-extension {name Text})
  name)

(def: (primitive analysis)
  (-> ///analysis.Primitive //.Primitive)
  (case analysis
    #///analysis.Unit
    (#//.Text //.unit)
    
    (^template [<analysis> <synthesis>]
      (<analysis> value)
      (<synthesis> value))
    ([#///analysis.Bool #//.Bool]
     [#///analysis.Frac #//.F64]
     [#///analysis.Text #//.Text])

    (^template [<analysis> <synthesis>]
      (<analysis> value)
      (<synthesis> (.i64 value)))
    ([#///analysis.Nat #//.I64]
     [#///analysis.Int #//.I64]
     [#///analysis.Deg #//.I64])))

(def: #export (synthesizer extensions)
  (-> (Extension ///extension.Synthesis) //.Synthesizer)
  (function (synthesize analysis)
    (case analysis
      (#///analysis.Primitive analysis')
      (operation/wrap (#//.Primitive (..primitive analysis')))

      (#///analysis.Structure composite)
      (case (///analysis.variant analysis)
        (#.Some variant)
        (do ///compiler.Monad<Operation>
          [valueS (synthesize (get@ #///analysis.value variant))]
          (wrap (#//.Structure (#//.Variant (set@ #///analysis.value valueS variant)))))

        _
        (do ///compiler.Monad<Operation>
          [tupleS (monad.map @ synthesize (///analysis.tuple analysis))]
          (wrap (#//.Structure (#//.Tuple tupleS)))))

      (#///analysis.Apply _)
      (//function.apply (|>> synthesize //.indirectly) analysis)

      (#///analysis.Function environmentA bodyA)
      (//function.function synthesize environmentA bodyA)

      (#///analysis.Extension name args)
      (case (dict.get name extensions)
        #.None
        (///compiler.throw unknown-synthesis-extension name)
        
        (#.Some extension)
        (extension (|>> synthesize //.indirectly) args))

      (#///analysis.Reference reference)
      (case reference
        (#///reference.Constant constant)
        (operation/wrap (#//.Reference reference))

        (#///reference.Variable var)
        (do ///compiler.Monad<Operation>
          [resolver //.resolver]
          (case var
            (#///reference.Local register)
            (do @
              [arity //.scope-arity]
              (wrap (if (//function.nested? arity)
                      (if (n/= +0 register)
                        (|> (dec arity)
                            (list.n/range +1)
                            (list/map (|>> //.variable/local))
                            [(//.variable/local +0)]
                            //.function/apply)
                        (#//.Reference (#///reference.Variable (//function.adjust arity false var))))
                      (#//.Reference (#///reference.Variable var)))))
            
            (#///reference.Foreign register)
            (wrap (|> resolver (dict.get var) (maybe.default var) #///reference.Variable #//.Reference)))))

      (#///analysis.Case inputA branchesAB+)
      (//case.synthesize (|>> synthesize //.indirectly) inputA branchesAB+)
      )))