aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/lang/synthesis/expression.lux
blob: d556048b360f9349d02e2a23c9c44a7720090072 (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]))))
  [///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])))

(open: "operation/" //.Operation@Monad)

(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 //.Operation@Monad
          [valueS (synthesize (get@ #///analysis.value variant))]
          (wrap (#//.Structure (#//.Variant (set@ #///analysis.value valueS variant)))))

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

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

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

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

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

        (#///analysis.Variable var)
        (do //.Operation@Monad
          [resolver //.resolver]
          (case var
            (#///analysis.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 (#///analysis.Variable (//function.adjust arity false var))))
                      (#//.Reference (#///analysis.Variable var)))))
            
            (#///analysis.Foreign register)
            (wrap (|> resolver (dict.get var) (maybe.default var) #///analysis.Variable #//.Reference)))))

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