lawspec 0.25.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +109 -1
- package/NORMALIZATION-LICENSES.txt +296 -0
- package/README.md +13 -4
- package/api.mjs +0 -8
- package/bin/lawspec.mjs +114 -79
- package/build.json +99 -117
- package/core.wasm +2 -2
- package/core_jsffi.js +5 -0
- package/doctor.mjs +14 -4
- package/examples/native-payments/clojure/lawspec.json +1 -1
- package/examples/native-payments/csharp/lawspec.json +1 -1
- package/examples/native-payments/elixir/lawspec.json +1 -1
- package/examples/native-payments/erlang/lawspec.json +1 -1
- package/examples/native-payments/fsharp/lawspec.json +1 -1
- package/examples/native-payments/gleam/lawspec.json +1 -1
- package/examples/native-payments/go/lawspec.json +1 -1
- package/examples/native-payments/groovy/lawspec.json +1 -1
- package/examples/native-payments/haskell/lawspec.json +1 -1
- package/examples/native-payments/java/lawspec.json +1 -1
- package/examples/native-payments/javascript/lawspec.json +1 -1
- package/examples/native-payments/kotlin/lawspec.json +1 -1
- package/examples/native-payments/prolog/lawspec.json +20 -0
- package/examples/native-payments/prolog/src/domain/payments.pl +23 -0
- package/examples/native-payments/prolog/tests/domain/payment_generators.pl +17 -0
- package/examples/native-payments/python/lawspec.json +1 -1
- package/examples/native-payments/rust/lawspec.json +1 -1
- package/examples/native-payments/scala/lawspec.json +1 -1
- package/examples/native-payments/swift/lawspec.json +1 -1
- package/examples/native-payments/typescript/lawspec.json +1 -1
- package/examples/packages/orders/lawspec.json +1 -1
- package/examples/packages/orders/orders.lawspec +27 -25
- package/examples/packages/shop-domain/src/domain.lawspec +15 -14
- package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-1/src/rates.lawspec +1 -0
- package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-2/src/rates.lawspec +1 -0
- package/examples/rc1/authorization-snapshots.lawspec +24 -23
- package/examples/rc1/banner-filenames.lawspec +7 -6
- package/examples/rc1/bit-codec.lawspec +12 -11
- package/examples/rc1/bounded-witness-search.lawspec +52 -45
- package/examples/rc1/conflicting-requirements.lawspec +8 -7
- package/examples/rc1/constructive-witnesses.lawspec +40 -33
- package/examples/rc1/documentary-review.lawspec +9 -8
- package/examples/rc1/empty-domains.lawspec +16 -15
- package/examples/rc1/finite-integer-domains.lawspec +24 -23
- package/examples/rc1/law-metadata.lawspec +6 -5
- package/examples/rc1/native-payments.lawspec +17 -16
- package/examples/rc1/nesting/rrr.lawspec +4 -3
- package/examples/rc1/nesting/rru.lawspec +7 -6
- package/examples/rc1/nesting/rur.lawspec +7 -6
- package/examples/rc1/nesting/ruu.lawspec +10 -9
- package/examples/rc1/nesting/urr.lawspec +7 -6
- package/examples/rc1/nesting/uru.lawspec +10 -9
- package/examples/rc1/nesting/uur.lawspec +10 -9
- package/examples/rc1/nesting/uuu.lawspec +13 -12
- package/examples/rc1/order-workflow.lawspec +18 -17
- package/examples/rc1/parcel-measurements.lawspec +14 -13
- package/examples/rc1/picture-records.lawspec +6 -5
- package/examples/rc1/picture-tuples.lawspec +6 -5
- package/examples/rc1/record-payloads.lawspec +12 -11
- package/examples/rc1/recursive-records.lawspec +11 -10
- package/examples/rc1/refund-conformance.lawspec +7 -6
- package/examples/rc1/refund-policy.lawspec +15 -14
- package/examples/rc1/requirement-composition.lawspec +26 -25
- package/examples/rc1/structural-records.lawspec +13 -12
- package/examples/rc1-authoring.lawspec +6 -5
- package/examples/specs/abilities.lawspec +67 -65
- package/examples/specs/ability_imports.lawspec +58 -57
- package/examples/specs/actors.lawspec +23 -22
- package/examples/specs/algebra.lawspec +142 -141
- package/examples/specs/approvals.lawspec +19 -18
- package/examples/specs/async_bindings.lawspec +10 -9
- package/examples/specs/async_fetch.lawspec +12 -11
- package/examples/specs/atoi_codec.lawspec +16 -15
- package/examples/specs/boolean_flags.lawspec +10 -9
- package/examples/specs/builtins.lawspec +73 -72
- package/examples/specs/canonical_url.lawspec +14 -13
- package/examples/specs/collections.lawspec +153 -154
- package/examples/specs/composition.lawspec +70 -66
- package/examples/specs/concurrent_collections.lawspec +8 -7
- package/examples/specs/consistency.lawspec +4 -3
- package/examples/specs/contracts.lawspec +80 -79
- package/examples/specs/crypto.lawspec +26 -25
- package/examples/specs/ctc.lawspec +148 -0
- package/examples/specs/currying.lawspec +32 -31
- package/examples/specs/data_types.lawspec +50 -49
- package/examples/specs/distribution.lawspec +57 -56
- package/examples/specs/domain_modeling.lawspec +68 -47
- package/examples/specs/durations.lawspec +39 -38
- package/examples/specs/equivalent.lawspec +25 -24
- package/examples/specs/failures.lawspec +64 -63
- package/examples/specs/finite_data.lawspec +32 -31
- package/examples/specs/flow_stack.lawspec +64 -63
- package/examples/specs/gadt_expressions.lawspec +50 -49
- package/examples/specs/generation.lawspec +115 -114
- package/examples/specs/handler_bindings.lawspec +42 -40
- package/examples/specs/handles.lawspec +2 -1
- package/examples/specs/harness.lawspec +45 -43
- package/examples/specs/harness_testing.lawspec +18 -13
- package/examples/specs/indexed_arithmetic.lawspec +401 -126
- package/examples/specs/indexed_families.lawspec +182 -66
- package/examples/specs/keywords.lawspec +22 -21
- package/examples/specs/limits.lawspec +7 -6
- package/examples/specs/list_contracts.lawspec +121 -120
- package/examples/specs/list_refinements.lawspec +43 -42
- package/examples/specs/lists.lawspec +45 -44
- package/examples/specs/matchers.lawspec +79 -78
- package/examples/specs/matching.lawspec +43 -42
- package/examples/specs/mixed_inputs.lawspec +25 -24
- package/examples/specs/models.lawspec +44 -43
- package/examples/specs/parse_port.lawspec +28 -27
- package/examples/specs/payments.lawspec +55 -54
- package/examples/specs/railway.lawspec +59 -58
- package/examples/specs/records.lawspec +149 -148
- package/examples/specs/records_inner.lawspec +5 -4
- package/examples/specs/recursive_refinements.lawspec +30 -29
- package/examples/specs/refined_definitions.lawspec +24 -23
- package/examples/specs/refinements.lawspec +58 -57
- package/examples/specs/resilience.lawspec +161 -160
- package/examples/specs/resources.lawspec +61 -60
- package/examples/specs/scalar_adapters.lawspec +60 -59
- package/examples/specs/scalar_catalog.lawspec +165 -164
- package/examples/specs/scalars.lawspec +78 -77
- package/examples/specs/scheduling_order.lawspec +8 -7
- package/examples/specs/scheduling_overlap.lawspec +5 -4
- package/examples/specs/sequences.lawspec +66 -65
- package/examples/specs/sessions.lawspec +13 -12
- package/examples/specs/slug.lawspec +16 -15
- package/examples/specs/sum_refinements.lawspec +42 -41
- package/examples/specs/tables.lawspec +29 -28
- package/examples/specs/total_functions.lawspec +71 -70
- package/examples/specs/workflows.lawspec +101 -100
- package/examples-command.mjs +7 -7
- package/explain.mjs +6 -4
- package/expression-presentation.mjs +8 -8
- package/files.mjs +1 -1
- package/index.d.ts +10 -528
- package/launcher.mjs +9 -2
- package/lsp.mjs +10 -6
- package/native-evidence.mjs +25 -12
- package/native-examples.mjs +3 -3
- package/package.json +2 -1
- package/starter-rc1.lawspec +6 -5
- package/starter.lawspec +16 -15
- package/templates.mjs +88 -5
- package/test-command.mjs +40 -4
- package/migrate.mjs +0 -135
- package/migration-bindings.mjs +0 -86
- package/migration-cases.mjs +0 -45
- package/migration-collections.mjs +0 -54
- package/migration-correspondence.mjs +0 -275
- package/migration-generation.mjs +0 -99
- package/migration-laws.mjs +0 -102
- package/migration-records.mjs +0 -166
- package/migration-source.mjs +0 -247
- package/migration-write.mjs +0 -156
|
@@ -5,43 +5,44 @@ referenceRender :: Int32 -> Text
|
|
|
5
5
|
clamp :: Int32 -> Int32
|
|
6
6
|
referenceClamp :: Int32 -> Int32
|
|
7
7
|
|
|
8
|
-
law
|
|
9
|
-
definition
|
|
10
|
-
|
|
11
|
-
end
|
|
12
|
-
description
|
|
8
|
+
law "|decimal renderers agree| is
|
|
9
|
+
definition :
|
|
10
|
+
apply law "|equivalent| render referenceRender
|
|
11
|
+
end definition
|
|
12
|
+
description :
|
|
13
13
|
"{render} agrees with {referenceRender} on decimal formatting"
|
|
14
|
-
end
|
|
15
|
-
rationale
|
|
14
|
+
end description
|
|
15
|
+
rationale :
|
|
16
16
|
"changing the formatting implementation must preserve its result"
|
|
17
|
-
end
|
|
18
|
-
example
|
|
17
|
+
end rationale
|
|
18
|
+
example "|both renderers produce "-42"| :
|
|
19
19
|
x = -42
|
|
20
20
|
expect render x = "-42"
|
|
21
21
|
expect referenceRender x = "-42"
|
|
22
|
-
end
|
|
23
|
-
example
|
|
22
|
+
end example
|
|
23
|
+
example "|both renderers produce "0"| :
|
|
24
24
|
x = 0
|
|
25
25
|
expect render x = "0"
|
|
26
26
|
expect referenceRender x = "0"
|
|
27
|
-
end
|
|
28
|
-
end
|
|
27
|
+
end example
|
|
28
|
+
end law
|
|
29
29
|
|
|
30
|
-
law
|
|
31
|
-
definition
|
|
32
|
-
|
|
33
|
-
end
|
|
34
|
-
description
|
|
30
|
+
law "|nonnegative clamps agree| is
|
|
31
|
+
definition :
|
|
32
|
+
apply law "|equivalent| clamp referenceClamp
|
|
33
|
+
end definition
|
|
34
|
+
description :
|
|
35
35
|
"{clamp} agrees with {referenceClamp} when clamping negative inputs to zero"
|
|
36
|
-
end
|
|
37
|
-
example
|
|
36
|
+
end description
|
|
37
|
+
example "|both clamps return 0 for -2147483648| :
|
|
38
38
|
x = -2147483648
|
|
39
39
|
expect clamp x = 0
|
|
40
40
|
expect referenceClamp x = 0
|
|
41
|
-
end
|
|
42
|
-
example
|
|
41
|
+
end example
|
|
42
|
+
example "|both clamps return 2147483647 for 2147483647| :
|
|
43
43
|
x = 2147483647
|
|
44
44
|
expect clamp x = 2147483647
|
|
45
45
|
expect referenceClamp x = 2147483647
|
|
46
|
-
end
|
|
47
|
-
end
|
|
46
|
+
end example
|
|
47
|
+
end law
|
|
48
|
+
end unit
|
|
@@ -5,92 +5,93 @@ unit example.failures
|
|
|
5
5
|
-- pattern, where _ matches any field, and may also check the failure's
|
|
6
6
|
-- message.
|
|
7
7
|
|
|
8
|
-
type Decision is
|
|
8
|
+
type Decision is one of
|
|
9
9
|
| Approve
|
|
10
|
-
| Decline
|
|
10
|
+
| Decline of type Text
|
|
11
11
|
| Block
|
|
12
|
-
end
|
|
12
|
+
end type
|
|
13
13
|
|
|
14
|
-
type PaymentError is
|
|
15
|
-
| Declined message :: Text
|
|
14
|
+
type PaymentError is one of
|
|
15
|
+
| Declined of type record{ message :: Text}
|
|
16
16
|
| Blocked
|
|
17
|
-
| TooLarge
|
|
18
|
-
end
|
|
17
|
+
| TooLarge of type Int32
|
|
18
|
+
end type
|
|
19
19
|
|
|
20
20
|
-- The payment provider decides; its native handler is written by hand.
|
|
21
21
|
ability Gateway is
|
|
22
22
|
decide :: Int32 -> Decision
|
|
23
|
-
end
|
|
23
|
+
end ability
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
procedure charge (cents :: Int32) -> Int32 uses Gateway fails with PaymentError is
|
|
26
26
|
if cents > 100000 then raise (TooLarge 100000) else
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
32
|
-
end
|
|
27
|
+
first matching case of decide cents :
|
|
28
|
+
when Approve then cents
|
|
29
|
+
when Decline reason then raise (Declined record{ message = reason})
|
|
30
|
+
when Block then raise Blocked
|
|
31
|
+
end case end if
|
|
32
|
+
end procedure
|
|
33
33
|
|
|
34
|
-
law
|
|
35
|
-
definition
|
|
36
|
-
|
|
37
|
-
end
|
|
38
|
-
end
|
|
34
|
+
law "|large charges fail as too large| is
|
|
35
|
+
definition :
|
|
36
|
+
for all (cents :: Int32 where cents > 100000) => charge cents fails with TooLarge 100000 end for all
|
|
37
|
+
end definition
|
|
38
|
+
end law
|
|
39
39
|
|
|
40
|
-
law
|
|
41
|
-
definition
|
|
42
|
-
|
|
43
|
-
charge cents fails with Declined _ message contains "odd"
|
|
44
|
-
end
|
|
45
|
-
end
|
|
40
|
+
law "|odd amounts are declined, and say why| is
|
|
41
|
+
definition :
|
|
42
|
+
for all (cents :: Int32 where cents >= 0 && cents <= 100000 && prelude.rem cents 2 = 1) =>
|
|
43
|
+
charge cents fails with Declined record{ message = _} message contains "odd" end for all
|
|
44
|
+
end definition
|
|
45
|
+
end law
|
|
46
46
|
|
|
47
|
-
law
|
|
48
|
-
definition
|
|
49
|
-
|
|
50
|
-
end
|
|
51
|
-
example
|
|
47
|
+
law "|negative amounts are blocked| is
|
|
48
|
+
definition :
|
|
49
|
+
for all (cents :: Int32 where cents < 0) => charge cents fails with Blocked end for all
|
|
50
|
+
end definition
|
|
51
|
+
example "|minus one| :
|
|
52
52
|
cents = -1
|
|
53
53
|
expect charge cents fails with Blocked
|
|
54
|
-
expect !(charge cents fails with Declined _)
|
|
55
|
-
end
|
|
56
|
-
end
|
|
54
|
+
expect !(charge cents fails with Declined record{ message = _})
|
|
55
|
+
end example
|
|
56
|
+
end law
|
|
57
57
|
|
|
58
|
-
law
|
|
59
|
-
definition
|
|
60
|
-
|
|
61
|
-
prelude.attempt (charge cents) = Right cents
|
|
62
|
-
end
|
|
63
|
-
end
|
|
58
|
+
law "|even amounts go through| is
|
|
59
|
+
definition :
|
|
60
|
+
for all (cents :: Int32 where cents >= 0 && cents <= 100000 && prelude.rem cents 2 = 0) =>
|
|
61
|
+
prelude.attempt (charge cents) = Right cents end for all
|
|
62
|
+
end definition
|
|
63
|
+
end law
|
|
64
64
|
|
|
65
65
|
-- Native adapters fail too: their native code raises the runtime's Fail
|
|
66
66
|
-- with a PaymentError, synchronously or from an async adapter.
|
|
67
67
|
refund :: Int32 -> Int32 fails with PaymentError
|
|
68
68
|
async settle :: Int32 -> Int32 fails with PaymentError
|
|
69
69
|
|
|
70
|
-
law
|
|
71
|
-
definition
|
|
72
|
-
|
|
73
|
-
end
|
|
74
|
-
end
|
|
70
|
+
law "|refunds over the limit are too large| is
|
|
71
|
+
definition :
|
|
72
|
+
for all (cents :: Int32 where cents > 5000) => refund cents fails with TooLarge 5000 end for all
|
|
73
|
+
end definition
|
|
74
|
+
end law
|
|
75
75
|
|
|
76
|
-
law
|
|
77
|
-
definition
|
|
78
|
-
|
|
79
|
-
end
|
|
80
|
-
end
|
|
76
|
+
law "|refunds within the limit go through| is
|
|
77
|
+
definition :
|
|
78
|
+
for all (cents :: Int32 where cents >= 0 && cents <= 5000) => prelude.attempt (refund cents) = Right cents end for all
|
|
79
|
+
end definition
|
|
80
|
+
end law
|
|
81
81
|
|
|
82
|
-
law
|
|
83
|
-
definition
|
|
84
|
-
|
|
85
|
-
end
|
|
86
|
-
end
|
|
82
|
+
law "|settling a negative amount is blocked| is
|
|
83
|
+
definition :
|
|
84
|
+
for all (cents :: Int32 where cents < 0) => settle cents fails with Blocked end for all
|
|
85
|
+
end definition
|
|
86
|
+
end law
|
|
87
87
|
|
|
88
|
-
law
|
|
89
|
-
definition
|
|
90
|
-
end
|
|
88
|
+
law "|settling nothing is declined, and says why| is
|
|
89
|
+
definition : settle 0 fails with Declined record{ message = _} message contains "nothing" end definition
|
|
90
|
+
end law
|
|
91
91
|
|
|
92
|
-
law
|
|
93
|
-
definition
|
|
94
|
-
|
|
95
|
-
end
|
|
96
|
-
end
|
|
92
|
+
law "|settling a positive amount goes through| is
|
|
93
|
+
definition :
|
|
94
|
+
for all (cents :: Int32 where cents > 0) => prelude.attempt (settle cents) = Right cents end for all
|
|
95
|
+
end definition
|
|
96
|
+
end law
|
|
97
|
+
end unit
|
|
@@ -1,37 +1,38 @@
|
|
|
1
1
|
unit example.finite_data
|
|
2
2
|
|
|
3
|
-
type Choice is Yes | No end
|
|
3
|
+
type Choice is one of Yes | No end type
|
|
4
4
|
|
|
5
|
-
type Pair (a :: Type) (b :: Type) is
|
|
6
|
-
|
|
7
|
-
end
|
|
5
|
+
type Pair (a :: Type) (b :: Type) is a record{
|
|
6
|
+
first :: a second :: b}
|
|
7
|
+
end type
|
|
8
8
|
|
|
9
|
-
law
|
|
10
|
-
definition
|
|
11
|
-
|
|
12
|
-
(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
end) = choice
|
|
16
|
-
end
|
|
17
|
-
example
|
|
18
|
-
end
|
|
9
|
+
law "|sum constructors remain distinct| is
|
|
10
|
+
definition :
|
|
11
|
+
for all (choice :: Choice) =>
|
|
12
|
+
(first matching case of choice :
|
|
13
|
+
when Yes then Yes
|
|
14
|
+
when No then No
|
|
15
|
+
end case) = choice end for all
|
|
16
|
+
end definition
|
|
17
|
+
example "|yes| : choice = Yes expect choice = Yes end example
|
|
18
|
+
end law
|
|
19
19
|
|
|
20
|
-
law
|
|
21
|
-
definition
|
|
22
|
-
|
|
23
|
-
(
|
|
24
|
-
|
|
25
|
-
end) = pair
|
|
26
|
-
end
|
|
27
|
-
example
|
|
28
|
-
pair = Pair true false
|
|
29
|
-
expect pair = Pair true false
|
|
30
|
-
end
|
|
31
|
-
end
|
|
20
|
+
law "|products preserve both fields| is
|
|
21
|
+
definition :
|
|
22
|
+
for all (pair :: Pair Bool Bool) =>
|
|
23
|
+
(first matching case of pair :
|
|
24
|
+
when Pair record{ first = first , second = second} then Pair record{ first = first , second = second}
|
|
25
|
+
end case) = pair end for all
|
|
26
|
+
end definition
|
|
27
|
+
example "|different fields| :
|
|
28
|
+
pair = Pair record{ first = true , second = false}
|
|
29
|
+
expect pair = Pair record{ first = true , second = false}
|
|
30
|
+
end example
|
|
31
|
+
end law
|
|
32
32
|
|
|
33
|
-
law
|
|
34
|
-
definition
|
|
35
|
-
|
|
36
|
-
end
|
|
37
|
-
end
|
|
33
|
+
law "|nested optional products keep presence states| is
|
|
34
|
+
definition :
|
|
35
|
+
for all (value :: Maybe (Pair Choice (Nullable Bool))) => value = value end for all
|
|
36
|
+
end definition
|
|
37
|
+
end law
|
|
38
|
+
end unit
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
unit example.flow
|
|
2
2
|
|
|
3
|
-
type Stack (n ::
|
|
3
|
+
type Stack (n :: Whole) is one of
|
|
4
4
|
| Empty where n = 0
|
|
5
|
-
| Push top :: Int8 rest :: Stack m where n = m + 1
|
|
6
|
-
end
|
|
5
|
+
| Push of type record{ top :: Int8 rest :: Stack m} where n = m + 1
|
|
6
|
+
end type
|
|
7
7
|
|
|
8
8
|
-- A / A' is a flow parameter: the call takes the stack from A to A'. Each
|
|
9
9
|
-- adapter returns its result with the stack it leaves, as a generated
|
|
@@ -13,81 +13,82 @@ pop :: Stack (n + 1) / Stack n -> Int8
|
|
|
13
13
|
peek :: Stack (n + 1) / Stack (n + 1) -> Int8
|
|
14
14
|
|
|
15
15
|
-- A reference definition updates its flow parameter with ~s := e.
|
|
16
|
-
|
|
17
|
-
~s := Push x s
|
|
18
|
-
end
|
|
16
|
+
function pushed (x :: Int8) (s :: Stack n / Stack (n + 1)) -> Unit is
|
|
17
|
+
~s := Push record{ top = x , rest = s}
|
|
18
|
+
end function
|
|
19
19
|
|
|
20
20
|
-- ~s passes s to a flow parameter and rebinds s to the stack the call
|
|
21
21
|
-- leaves; e1; e2 sequences, left to right. pop needs Stack (m + 1): after
|
|
22
22
|
-- push, s is a Stack (n + 1), so the pop below type-checks.
|
|
23
|
-
law
|
|
24
|
-
definition
|
|
25
|
-
|
|
26
|
-
end
|
|
27
|
-
end
|
|
23
|
+
law "|pop returns what push pushed| is
|
|
24
|
+
definition :
|
|
25
|
+
for all (x :: Int8) (s :: Stack n) => (push x ~s; pop ~s) = x end for all
|
|
26
|
+
end definition
|
|
27
|
+
end law
|
|
28
28
|
|
|
29
|
-
law
|
|
30
|
-
definition
|
|
31
|
-
|
|
32
|
-
end
|
|
33
|
-
end
|
|
29
|
+
law "|push grows the stack| is
|
|
30
|
+
definition :
|
|
31
|
+
for all (x :: Int8) (s :: Stack n) => (push x ~s; nOfStack s) = n + 1 end for all
|
|
32
|
+
end definition
|
|
33
|
+
end law
|
|
34
34
|
|
|
35
|
-
law
|
|
36
|
-
definition
|
|
37
|
-
|
|
38
|
-
(push x ~s; push y ~s; pop ~s; pop ~s) = x
|
|
39
|
-
end
|
|
40
|
-
end
|
|
35
|
+
law "|pops come back in reverse| is
|
|
36
|
+
definition :
|
|
37
|
+
for all (x :: Int8) (y :: Int8) (s :: Stack n) =>
|
|
38
|
+
(push x ~s; push y ~s; pop ~s; pop ~s) = x end for all
|
|
39
|
+
end definition
|
|
40
|
+
end law
|
|
41
41
|
|
|
42
42
|
-- The quantifier's index k + 1 lets peek and pop take s.
|
|
43
|
-
law
|
|
44
|
-
definition
|
|
45
|
-
|
|
46
|
-
end
|
|
47
|
-
end
|
|
43
|
+
law "|peek and pop agree| is
|
|
44
|
+
definition :
|
|
45
|
+
for all (s :: Stack (k + 1)) => peek ~s = pop ~s end for all
|
|
46
|
+
end definition
|
|
47
|
+
end law
|
|
48
48
|
|
|
49
|
-
law
|
|
50
|
-
definition
|
|
51
|
-
|
|
52
|
-
end
|
|
53
|
-
end
|
|
49
|
+
law "|pop shrinks the stack| is
|
|
50
|
+
definition :
|
|
51
|
+
for all (s :: Stack (k + 1)) => (pop ~s; nOfStack s) = k end for all
|
|
52
|
+
end definition
|
|
53
|
+
end law
|
|
54
54
|
|
|
55
|
-
law
|
|
56
|
-
definition
|
|
57
|
-
|
|
58
|
-
end
|
|
59
|
-
end
|
|
55
|
+
law "|push agrees with the reference| is
|
|
56
|
+
definition :
|
|
57
|
+
for all (x :: Int8) (s :: Stack n) => (pushed x ~s; pop ~s) = x end for all
|
|
58
|
+
end definition
|
|
59
|
+
end law
|
|
60
60
|
|
|
61
61
|
-- A function may take several flow parameters: pushBoth takes two stacks and
|
|
62
62
|
-- leaves each one higher. A call passes ~a ~b, rebinding each.
|
|
63
|
-
|
|
64
|
-
~a := Push x a; ~b := Push x b
|
|
65
|
-
end
|
|
63
|
+
function pushBoth (x :: Int8) (a :: Stack n / Stack (n + 1)) (b :: Stack m / Stack (m + 1)) -> Unit is
|
|
64
|
+
~a := Push record{ top = x , rest = a}; ~b := Push record{ top = x , rest = b}
|
|
65
|
+
end function
|
|
66
66
|
|
|
67
|
-
law
|
|
68
|
-
definition
|
|
69
|
-
|
|
70
|
-
end
|
|
71
|
-
end
|
|
67
|
+
law "|both stacks get the value| is
|
|
68
|
+
definition :
|
|
69
|
+
for all (x :: Int8) (a :: Stack n) (b :: Stack m) => (pushBoth x ~a ~b; pop ~a; pop ~b) = x end for all
|
|
70
|
+
end definition
|
|
71
|
+
end law
|
|
72
72
|
|
|
73
73
|
-- A flow call may sit in a branch of an if or a match, when every branch
|
|
74
74
|
-- leaves the stack at the same type: here each branch pushes once.
|
|
75
|
-
law
|
|
76
|
-
definition
|
|
77
|
-
|
|
78
|
-
((if x > 0 then push x ~s else push 0 ~s); pop ~s) = (if x > 0 then x else 0)
|
|
79
|
-
end
|
|
80
|
-
end
|
|
75
|
+
law "|a push in each branch| is
|
|
76
|
+
definition :
|
|
77
|
+
for all (x :: Int8) (s :: Stack n) =>
|
|
78
|
+
((if x > 0 then push x ~s else push 0 ~s end if); pop ~s) = (if x > 0 then x else 0 end if) end for all
|
|
79
|
+
end definition
|
|
80
|
+
end law
|
|
81
81
|
|
|
82
|
-
law
|
|
83
|
-
definition
|
|
84
|
-
|
|
85
|
-
((
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
end); pop ~s) = (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
end)
|
|
92
|
-
end
|
|
93
|
-
end
|
|
82
|
+
law "|a push chosen by a match| is
|
|
83
|
+
definition :
|
|
84
|
+
for all (m :: Maybe Int8) (s :: Stack n) =>
|
|
85
|
+
((first matching case of m :
|
|
86
|
+
when Just v then push v ~s
|
|
87
|
+
when Nothing then push 0 ~s
|
|
88
|
+
end case); pop ~s) = (first matching case of m :
|
|
89
|
+
when Just v then v
|
|
90
|
+
when Nothing then prelude.Int8 0
|
|
91
|
+
end case) end for all
|
|
92
|
+
end definition
|
|
93
|
+
end law
|
|
94
|
+
end unit
|
|
@@ -1,76 +1,77 @@
|
|
|
1
1
|
unit example.gadt
|
|
2
2
|
|
|
3
|
-
type Pair (a :: Type) (b :: Type) is
|
|
3
|
+
type Pair (a :: Type) (b :: Type) is a record{ first :: a second :: b} end type
|
|
4
4
|
|
|
5
5
|
-- An expression indexed by the type of its value. Each constructor's `where`
|
|
6
6
|
-- fixes that type, so a value of Expr Bool is never a number literal. The
|
|
7
7
|
-- types b and c of Both are existential: Expr (Pair b c) determines them.
|
|
8
|
-
type Expr (a :: Type) is
|
|
9
|
-
| Number
|
|
10
|
-
| Truth
|
|
11
|
-
| Plus left :: Expr
|
|
12
|
-
| Same left :: Expr
|
|
13
|
-
| Negate
|
|
14
|
-
| Both first :: Expr b second :: Expr c where a = Pair b c
|
|
15
|
-
end
|
|
8
|
+
type Expr (a :: Type) is one of
|
|
9
|
+
| Number of type Integer where a = Integer
|
|
10
|
+
| Truth of type Bool where a = Bool
|
|
11
|
+
| Plus of type record{ left :: Expr Integer right :: Expr Integer} where a = Integer
|
|
12
|
+
| Same of type record{ left :: Expr Integer right :: Expr Integer} where a = Bool
|
|
13
|
+
| Negate of type Expr Bool where a = Bool
|
|
14
|
+
| Both of type record{ first :: Expr b second :: Expr c} where a = Pair b c
|
|
15
|
+
end type
|
|
16
16
|
|
|
17
17
|
-- Matching Number refines a to BigInt in that branch, so v is a value of a.
|
|
18
18
|
-- eval calls itself at other types (Expr BigInt inside Expr Bool): each type
|
|
19
19
|
-- it is used at becomes a checked instance, and unreachable branches vanish.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
end
|
|
29
|
-
end
|
|
20
|
+
function eval (e :: Expr a) -> a is
|
|
21
|
+
first matching case of e :
|
|
22
|
+
when Number v then v
|
|
23
|
+
when Truth b then b
|
|
24
|
+
when Plus record{ left = l , right = r} then eval l + eval r
|
|
25
|
+
when Same record{ left = l , right = r} then eval l = eval r
|
|
26
|
+
when Negate x then !(eval x)
|
|
27
|
+
when Both record{ first = l , second = r} then Pair record{ first = (eval l) , second = (eval r)}
|
|
28
|
+
end case
|
|
29
|
+
end function
|
|
30
30
|
|
|
31
|
-
evalNumber :: Expr
|
|
31
|
+
evalNumber :: Expr Integer -> Integer
|
|
32
32
|
evalTruth :: Expr Bool -> Bool
|
|
33
|
-
evalPair :: Expr (Pair
|
|
33
|
+
evalPair :: Expr (Pair Integer Bool) -> Pair Integer Bool
|
|
34
34
|
-- Constant folding: a single number with the same value.
|
|
35
|
-
fold :: Expr
|
|
35
|
+
fold :: Expr Integer -> Expr Integer
|
|
36
36
|
|
|
37
37
|
-- A shown value's type is existential and only the value says what it is:
|
|
38
38
|
-- each value carries its type, as a witness, to native code.
|
|
39
|
-
type Shown is
|
|
40
|
-
| Shown
|
|
41
|
-
end
|
|
39
|
+
type Shown is one of
|
|
40
|
+
| Shown of type b
|
|
41
|
+
end type
|
|
42
42
|
|
|
43
43
|
-- The name of the type a shown value holds.
|
|
44
44
|
describe :: Shown -> Text
|
|
45
45
|
|
|
46
|
-
law
|
|
47
|
-
definition
|
|
48
|
-
end
|
|
46
|
+
law "|numbers agree with eval| is
|
|
47
|
+
definition : for all (e :: Expr Integer) => evalNumber e = eval e end for all end definition
|
|
48
|
+
end law
|
|
49
49
|
|
|
50
|
-
law
|
|
51
|
-
definition
|
|
52
|
-
end
|
|
50
|
+
law "|truths agree with eval| is
|
|
51
|
+
definition : for all (e :: Expr Bool) => evalTruth e = eval e end for all end definition
|
|
52
|
+
end law
|
|
53
53
|
|
|
54
|
-
law
|
|
55
|
-
definition
|
|
56
|
-
end
|
|
54
|
+
law "|pairs agree with eval| is
|
|
55
|
+
definition : for all (e :: Expr (Pair Integer Bool)) => evalPair e = eval e end for all end definition
|
|
56
|
+
end law
|
|
57
57
|
|
|
58
|
-
law
|
|
59
|
-
definition
|
|
60
|
-
end
|
|
58
|
+
law "|folding keeps the value| is
|
|
59
|
+
definition : for all (e :: Expr Integer) => eval (fold e) = eval e end for all end definition
|
|
60
|
+
end law
|
|
61
61
|
|
|
62
|
-
law
|
|
63
|
-
definition
|
|
64
|
-
end
|
|
62
|
+
law "|a shown number names its type| is
|
|
63
|
+
definition : for all (x :: Int32) => describe (Shown x) = "Int32" end for all end definition
|
|
64
|
+
end law
|
|
65
65
|
|
|
66
|
-
law
|
|
67
|
-
definition
|
|
68
|
-
end
|
|
66
|
+
law "|a shown flag names its type| is
|
|
67
|
+
definition : for all (b :: Bool) => describe (Shown b) = "Bool" end for all end definition
|
|
68
|
+
end law
|
|
69
69
|
|
|
70
|
-
law
|
|
71
|
-
definition
|
|
72
|
-
end
|
|
70
|
+
law "|every shown value is described| is
|
|
71
|
+
definition : for all (s :: Shown) => prelude.length (describe s) > 0 end for all end definition
|
|
72
|
+
end law
|
|
73
73
|
|
|
74
|
-
law
|
|
75
|
-
definition
|
|
76
|
-
end
|
|
74
|
+
law "|two plus three| is
|
|
75
|
+
definition : for all (x :: Integer) => eval (Plus record{ left = (Number 2) , right = (Number 3)}) = 5 end for all end definition
|
|
76
|
+
end law
|
|
77
|
+
end unit
|