aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/tool/compiler/language/lux/phase/synthesis/primitive.lux
blob: b0fd4e5e2e479ac42fcf5e0af43f0527c8a4862c (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
(.using
 [lux (.except primitive)
  [abstract ["[0]" monad (.only do)]]
  [control
   ["[0]" pipe]
   ["[0]" try]]
  [data
   ["%" text/format (.only format)]
   [number
    ["n" nat]]
   [collection
    ["[0]" list]]]
  ["r" math/random (.only Random) (.open: "[1]#[0]" monad)]
  ["_" test (.only Test)]
  [meta
   ["[0]" symbol]]]
 [\\
  ["[0]" /
   ["/[1]" // (.only)
    ["/[1]" //
     [extension
      ["[1][0]" bundle]]
     ["/[1]" // (.only)
      ["[1][0]" analysis (.only Analysis)]
      ["[1][0]" synthesis (.only Synthesis)]
      [///
       ["[0]" phase]
       [meta
        ["[0]" archive]]]]]]]])

(def .public primitive
  (Random Analysis)
  (do r.monad
    [primitive (is (Random ////analysis.Primitive)
                   (all r.or
                        (in [])
                        r.bit
                        r.nat
                        r.int
                        r.rev
                        r.frac
                        (r.unicode 5)))]
    (in {////analysis.#Primitive primitive})))

(def .public (corresponds? analysis synthesis)
  (-> Analysis Synthesis Bit)
  (`` (case [analysis synthesis]
        (~~ (with_template [<analysis> <post_analysis> <synthesis> <post_synthesis>]
              [[{////analysis.#Primitive {<analysis> expected}}
                {////synthesis.#Primitive {<synthesis> actual}}]
               (same? (|> expected <post_analysis>)
                      (|> actual <post_synthesis>))]

              [////analysis.#Unit (as Text) ////synthesis.#Text (|>)]
              [////analysis.#Bit  (|>) ////synthesis.#Bit (|>)]
              [////analysis.#Nat  .i64 ////synthesis.#I64 .i64]
              [////analysis.#Int  .i64 ////synthesis.#I64 .i64]
              [////analysis.#Rev  .i64 ////synthesis.#I64 .i64]
              [////analysis.#Frac (|>) ////synthesis.#F64 (|>)]
              [////analysis.#Text (|>) ////synthesis.#Text (|>)]
              ))

        (pattern [(////analysis.tuple expected)
                  (////synthesis.tuple actual)])
        (and (n.= (list.size expected)
                  (list.size actual))
             (list.every? (function (_ [expected actual])
                            (corresponds? expected actual))
                          (list.zipped_2 expected actual)))
        
        _
        false)))

(def .public test
  Test
  (<| (_.context (%.symbol (symbol ////synthesis.#Primitive)))
      (`` (all _.and
               (~~ (with_template [<analysis> <synthesis> <generator>]
                     [(do r.monad
                        [expected <generator>]
                        (_.property (%.symbol (symbol <synthesis>))
                          (|> {////analysis.#Primitive {<analysis> expected}}
                              (//.phase archive.empty)
                              (phase.result [///bundle.empty ////synthesis.init])
                              (pipe.case
                                {try.#Success {////synthesis.#Primitive {<synthesis> actual}}}
                                (same? expected actual)

                                _
                                false))))]

                     [////analysis.#Unit ////synthesis.#Text (r#in ////synthesis.unit)]
                     [////analysis.#Bit  ////synthesis.#Bit  r.bit]
                     [////analysis.#Nat  ////synthesis.#I64  (r#each .i64 r.nat)]
                     [////analysis.#Int  ////synthesis.#I64  (r#each .i64 r.int)]
                     [////analysis.#Rev  ////synthesis.#I64  (r#each .i64 r.rev)]
                     [////analysis.#Frac ////synthesis.#F64  r.frac]
                     [////analysis.#Text ////synthesis.#Text (r.unicode 5)]))))))