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

(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)
          (:: ///.monad 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 "Synthesis" 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+])))))))
    ))