aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/synthesis/simple.lux
blob: 7f2eb7e1152664642fc7f9212e3ac95fa72bee56 (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
(.using
 [library
  [lux "*"
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]]
   [control
    ["[0]" pipe]]
   [data
    ["[0]" bit ("[1]#[0]" equivalence)]
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format]]]
   [macro
    ["^" pattern]]
   [math
    [number
     ["[0]" i64 ("[1]#[0]" equivalence)]
     ["n" nat]
     ["i" int]
     ["f" frac]]]]])

(type: .public Simple
  (Variant
   {#Bit Bit}
   {#I64 I64}
   {#F64 Frac}
   {#Text Text}))

(def: .public (format it)
  (%.Format Simple)
  (case it
    (^.template [<pattern> <format>]
      [{<pattern> value}
       (<format> value)])
    ([#Bit  %.bit]
     [#F64  %.frac]
     [#Text %.text])
    
    {#I64 value}
    (%.int (.int value))))

(implementation: .public equivalence
  (Equivalence Simple)
  
  (def: (= reference sample)
    (case [reference sample]
      (^.template [<tag> <eq> <format>]
        [[{<tag> reference'} {<tag> sample'}]
         (<eq> reference' sample')])
      ([#Bit  bit#=  %.bit]
       [#F64  f.=    %.frac]
       [#Text text#= %.text])

      [{#I64 reference'} {#I64 sample'}]
      (i64#= reference' sample')

      _
      false)))

(implementation: .public hash
  (Hash Simple)

  (def: equivalence ..equivalence)

  (def: hash
    (|>> (pipe.case
           (^.template [<factor> <tag> <hash>]
             [{<tag> value'}
              (n.* <factor>  (# <hash> hash value'))])
           ([2 #Bit  bit.hash]
            [3 #F64  f.hash]
            [5 #Text text.hash]
            [7 #I64  i64.hash])))))