aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/synthesis.lux
blob: c5152ff6a473f2c97a1afd3dd35cb9257cca42c3 (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
(.module:
  [lux (#- primitive)
   [control
    ["." monad (#+ do)]
    [pipe (#+ case>)]]
   [data
    ["." maybe]
    ["." error]
    [collection
     ["." list ("#/." functor)]
     ["." dictionary (#+ Dictionary)]]]]
  ["." / #_
   ["#." function]
   ["#." case]
   ["#/" // ("#/." monad)
    ["#." extension]
    ["#/" // #_
     ["#." analysis (#+ Analysis)]
     ["/" synthesis (#+ Synthesis Phase)]]]])

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

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

(def: #export (phase analysis)
  Phase
  (case analysis
    (#///analysis.Primitive analysis')
    (///wrap (#/.Primitive (..primitive analysis')))

    (#///analysis.Structure structure)
    (case structure
      (#///analysis.Variant variant)
      (do //.monad
        [valueS (phase (get@ #///analysis.value variant))]
        (wrap (/.variant (set@ #///analysis.value valueS variant))))

      (#///analysis.Tuple tuple)
      (|> tuple
          (monad.map //.monad phase)
          (///map (|>> /.tuple))))
    
    (#///analysis.Reference reference)
    (///wrap (#/.Reference reference))

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

    (^ (///analysis.no-op value))
    (phase value)

    (#///analysis.Apply _)
    (/function.apply phase analysis)

    (#///analysis.Function environmentA bodyA)
    (/function.abstraction phase environmentA bodyA)

    (#///analysis.Extension name args)
    (function (_ state)
      (|> (//extension.apply phase [name args])
          (//.run' state)
          (case> (#error.Success output)
                 (#error.Success output)
                 
                 (#error.Failure error)
                 (<| (//.run' state)
                     (do //.monad
                       [argsS+ (monad.map @ phase args)]
                       (wrap (#/.Extension [name argsS+])))))))

    _
    (///wrap (undefined))
    ))