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
|
@@ -8,27 +8,27 @@ label :: Int32 -> Text
|
|
|
8
8
|
|
|
9
9
|
-- Each row is its own example. The column on the right of an expect's =
|
|
10
10
|
-- holds the expected values; the other columns bind the law's inputs.
|
|
11
|
-
law
|
|
12
|
-
definition
|
|
13
|
-
|
|
14
|
-
(kilometres :: Int32 where kilometres >= 0 && kilometres <= 1000)
|
|
15
|
-
shippingCost kilograms kilometres >= 0
|
|
16
|
-
end
|
|
17
|
-
|
|
11
|
+
law "|shipping cost grows with weight and distance| is
|
|
12
|
+
definition :
|
|
13
|
+
for all (kilograms :: Int32 where kilograms >= 0 && kilograms <= 1000)
|
|
14
|
+
(kilometres :: Int32 where kilometres >= 0 && kilometres <= 1000) =>
|
|
15
|
+
shippingCost kilograms kilometres >= 0 end for all
|
|
16
|
+
end definition
|
|
17
|
+
expect shippingCost kilograms kilometres = cost
|
|
18
|
+
in table (kilograms, kilometres, cost) :
|
|
18
19
|
row 0, 0, 0
|
|
19
20
|
row 1, 10, 15
|
|
20
21
|
row 2, 10, 30
|
|
21
22
|
row 10, 100, 1050
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
end
|
|
23
|
+
end table
|
|
24
|
+
end law
|
|
25
25
|
|
|
26
26
|
-- Examples in a description are fenced, and become examples of the law.
|
|
27
|
-
law
|
|
28
|
-
definition
|
|
29
|
-
|
|
30
|
-
end
|
|
31
|
-
description
|
|
27
|
+
law "|a label names the parcel| is
|
|
28
|
+
definition :
|
|
29
|
+
for all (parcel :: Int32 where parcel >= 0) => label parcel starts with "parcel " end for all
|
|
30
|
+
end definition
|
|
31
|
+
description :
|
|
32
32
|
"Every label starts with the word parcel and the parcel's number.
|
|
33
33
|
|
|
34
34
|
```example
|
|
@@ -40,25 +40,26 @@ law `a label names the parcel` is
|
|
|
40
40
|
parcel = 123456
|
|
41
41
|
expect label parcel = \"parcel 123456\"
|
|
42
42
|
```"
|
|
43
|
-
end
|
|
44
|
-
end
|
|
43
|
+
end description
|
|
44
|
+
end law
|
|
45
45
|
|
|
46
46
|
-- A recorded value is stored in the project, under
|
|
47
47
|
-- recorded/example.tables/<name>, in the portable rendering every target
|
|
48
48
|
-- shares. lawspec test --update-recorded records it again; until then, a
|
|
49
49
|
-- changed value fails.
|
|
50
|
-
law
|
|
51
|
-
definition
|
|
50
|
+
law "|the first label is recorded| is
|
|
51
|
+
definition :
|
|
52
52
|
label 1 = recorded "first label"
|
|
53
|
-
end
|
|
54
|
-
end
|
|
53
|
+
end definition
|
|
54
|
+
end law
|
|
55
55
|
|
|
56
|
-
law
|
|
57
|
-
definition
|
|
58
|
-
|
|
59
|
-
end
|
|
60
|
-
example
|
|
56
|
+
law "|labels are short| is
|
|
57
|
+
definition :
|
|
58
|
+
for all (parcel :: Int32 where parcel >= 0) => prelude.length (label parcel) <= 20 end for all
|
|
59
|
+
end definition
|
|
60
|
+
example "|parcel 42| :
|
|
61
61
|
parcel = 42
|
|
62
62
|
expect label parcel = recorded "parcel 42"
|
|
63
|
-
end
|
|
64
|
-
end
|
|
63
|
+
end example
|
|
64
|
+
end law
|
|
65
|
+
end unit
|
|
@@ -2,100 +2,101 @@ unit example.total_functions
|
|
|
2
2
|
|
|
3
3
|
-- These functions are implemented in the specification. LawSpec checks their
|
|
4
4
|
-- bodies and emits reusable source; they do not need adapter implementations.
|
|
5
|
-
|
|
5
|
+
function increment (x :: a) -> Integer
|
|
6
6
|
requires Integer a
|
|
7
7
|
is
|
|
8
8
|
x + 1
|
|
9
|
-
end
|
|
9
|
+
end function
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
end
|
|
16
|
-
end
|
|
11
|
+
function count (xs :: List a) -> Integer is
|
|
12
|
+
first matching case of xs :
|
|
13
|
+
when Nil then 0
|
|
14
|
+
when Cons head tail then 1 + count tail
|
|
15
|
+
end case
|
|
16
|
+
end function
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
end
|
|
23
|
-
end
|
|
18
|
+
function nonempty (xs :: List a) -> Bool is
|
|
19
|
+
first matching case of xs :
|
|
20
|
+
when Nil then false
|
|
21
|
+
when Cons head tail then true
|
|
22
|
+
end case
|
|
23
|
+
end function
|
|
24
24
|
|
|
25
|
-
law
|
|
26
|
-
definition
|
|
27
|
-
|
|
28
|
-
end
|
|
29
|
-
example
|
|
30
|
-
end
|
|
25
|
+
law "|nonempty lists have positive length| is
|
|
26
|
+
definition :
|
|
27
|
+
for all (xs :: List Int8 where nonempty xs) => count xs > 0 end for all
|
|
28
|
+
end definition
|
|
29
|
+
example "|singleton| : xs = [0] expect count xs = 1 end example
|
|
30
|
+
end law
|
|
31
31
|
|
|
32
32
|
-- The exact guard proves that the promoted denominator cannot be zero.
|
|
33
|
-
|
|
33
|
+
function positiveReciprocal (x :: a) -> Bool
|
|
34
34
|
requires Integer a
|
|
35
35
|
is
|
|
36
36
|
x >= 0 && 1 / (x + 1) > 0
|
|
37
|
-
end
|
|
37
|
+
end function
|
|
38
38
|
|
|
39
|
-
law
|
|
40
|
-
definition
|
|
41
|
-
|
|
42
|
-
end
|
|
43
|
-
example
|
|
39
|
+
law "|guarded exact arithmetic| is
|
|
40
|
+
definition :
|
|
41
|
+
for all (x :: Int8) => positiveReciprocal x = (x >= 0) end for all
|
|
42
|
+
end definition
|
|
43
|
+
example "|skip zero denominator| :
|
|
44
44
|
x = -1 expect positiveReciprocal x = false
|
|
45
|
-
end
|
|
46
|
-
example
|
|
45
|
+
end example
|
|
46
|
+
example "|promote before division| :
|
|
47
47
|
x = 127 expect positiveReciprocal x = true
|
|
48
|
-
end
|
|
49
|
-
end
|
|
48
|
+
end example
|
|
49
|
+
end law
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
function guardedNarrow (x :: Int8) -> Bool is
|
|
52
52
|
x < 127 && prelude.Int8 (x + 1) > x
|
|
53
|
-
end
|
|
53
|
+
end function
|
|
54
54
|
|
|
55
|
-
law
|
|
56
|
-
definition
|
|
57
|
-
|
|
58
|
-
end
|
|
59
|
-
example
|
|
60
|
-
example
|
|
61
|
-
end
|
|
55
|
+
law "|narrow only after proving the range| is
|
|
56
|
+
definition :
|
|
57
|
+
for all (x :: Int8) => guardedNarrow x = (x < 127) end for all
|
|
58
|
+
end definition
|
|
59
|
+
example "|largest valid conversion| : x = 126 expect guardedNarrow x = true end example
|
|
60
|
+
example "|overflow branch is skipped| : x = 127 expect guardedNarrow x = false end example
|
|
61
|
+
end law
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
function same (xs :: List a) (ys :: List a) -> Bool
|
|
64
64
|
requires Eq a
|
|
65
65
|
is
|
|
66
|
-
xs
|
|
67
|
-
end
|
|
66
|
+
xs = ys
|
|
67
|
+
end function
|
|
68
68
|
|
|
69
69
|
-- The same checked template produces independent concrete instances.
|
|
70
|
-
law
|
|
71
|
-
definition
|
|
72
|
-
|
|
73
|
-
end
|
|
74
|
-
example
|
|
75
|
-
end
|
|
70
|
+
law "|count also accepts text| is
|
|
71
|
+
definition :
|
|
72
|
+
for all (xs :: List Text) => count xs = prelude.length xs end for all
|
|
73
|
+
end definition
|
|
74
|
+
example "|two words| : xs = ["hello", "world"] expect count xs = 2 end example
|
|
75
|
+
end law
|
|
76
76
|
|
|
77
|
-
law
|
|
78
|
-
definition
|
|
79
|
-
|
|
80
|
-
end
|
|
81
|
-
example
|
|
77
|
+
law "|increment preserves the mathematical result| is
|
|
78
|
+
definition :
|
|
79
|
+
for all (x :: Int8) => increment x = x + 1 end for all
|
|
80
|
+
end definition
|
|
81
|
+
example "|maximum input| :
|
|
82
82
|
x = 127 expect increment x = 128
|
|
83
|
-
end
|
|
84
|
-
end
|
|
83
|
+
end example
|
|
84
|
+
end law
|
|
85
85
|
|
|
86
|
-
law
|
|
87
|
-
definition
|
|
88
|
-
|
|
89
|
-
end
|
|
90
|
-
example
|
|
91
|
-
example
|
|
92
|
-
end
|
|
86
|
+
law "|recursive count agrees with length| is
|
|
87
|
+
definition :
|
|
88
|
+
for all (xs :: List Int8) => count xs = prelude.length xs end for all
|
|
89
|
+
end definition
|
|
90
|
+
example "|empty| : xs = [] expect count xs = 0 end example
|
|
91
|
+
example "|duplicates still count| : xs = [7, 7, 7] expect count xs = 3 end example
|
|
92
|
+
end law
|
|
93
93
|
|
|
94
|
-
law
|
|
95
|
-
definition
|
|
96
|
-
|
|
97
|
-
end
|
|
98
|
-
example
|
|
94
|
+
law "|list equality preserves duplicates and order| is
|
|
95
|
+
definition :
|
|
96
|
+
for all (xs :: List Int8) (ys :: List Int8) => same xs ys = (xs = ys) end for all
|
|
97
|
+
end definition
|
|
98
|
+
example "|different order| :
|
|
99
99
|
xs = [1, 2] ys = [2, 1] expect same xs ys = false
|
|
100
|
-
end
|
|
101
|
-
end
|
|
100
|
+
end example
|
|
101
|
+
end law
|
|
102
|
+
end unit
|
|
@@ -4,32 +4,32 @@ unit example.workflows
|
|
|
4
4
|
-- the composition itself, so a workflow behaves the same everywhere: every
|
|
5
5
|
-- stage below is a keyword, and each has a symbol too (>>=, <$>, <!>, <|>, ??).
|
|
6
6
|
|
|
7
|
-
type Signup is
|
|
8
|
-
type Account is
|
|
9
|
-
type SignupError is | MissingName | TooYoung | NameTooLong | Unavailable end
|
|
7
|
+
type Signup is a record{ name :: Text age :: Int32} end type
|
|
8
|
+
type Account is a record{ name :: Text age :: Int32 level :: Int32} end type
|
|
9
|
+
type SignupError is one of | MissingName | TooYoung | NameTooLong | Unavailable end type
|
|
10
10
|
|
|
11
11
|
-- Checked definitions: LawSpec writes these on every target.
|
|
12
|
-
|
|
12
|
+
function explain (reason :: Text) -> SignupError is TooYoung end function
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
end
|
|
18
|
-
end
|
|
14
|
+
function promote (a :: Account) -> Account is
|
|
15
|
+
first matching case of a :
|
|
16
|
+
when Account record{ name = name , age = age , level = level} then Account record{ name = name , age = age , level = 2}
|
|
17
|
+
end case
|
|
18
|
+
end function
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
end
|
|
24
|
-
end
|
|
20
|
+
function brief (a :: Account) -> Bool is
|
|
21
|
+
first matching case of a :
|
|
22
|
+
when Account record{ name = name , age = age , level = level} then prelude.length name <= 8
|
|
23
|
+
end case
|
|
24
|
+
end function
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
function tooLong (a :: Account) -> SignupError is NameTooLong end function
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
32
|
-
end
|
|
28
|
+
function nameOf (s :: Signup) -> Text is
|
|
29
|
+
first matching case of s :
|
|
30
|
+
when Signup record{ name = name , age = age} then name
|
|
31
|
+
end case
|
|
32
|
+
end function
|
|
33
33
|
|
|
34
34
|
-- Side steps and recovery handlers, also written by hand.
|
|
35
35
|
audit :: Account -> Bool
|
|
@@ -47,7 +47,7 @@ workflow register :: Signup -> Either SignupError Account is
|
|
|
47
47
|
tap audit
|
|
48
48
|
ensure brief else tooLong
|
|
49
49
|
orElse waitlist
|
|
50
|
-
end
|
|
50
|
+
end workflow
|
|
51
51
|
|
|
52
52
|
-- With the error written as _, LawSpec generates GreetError, with one
|
|
53
53
|
-- constructor per step that can fail: GreetCheckNameFailed and GreetCheckAgeFailed.
|
|
@@ -56,120 +56,121 @@ workflow greet :: Signup -> Either _ Text is
|
|
|
56
56
|
>>= checkAge :: Signup -> Either Text Signup
|
|
57
57
|
<$> nameOf
|
|
58
58
|
?? anonymous
|
|
59
|
-
end
|
|
59
|
+
end workflow
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
function anonymous (e :: GreetError) -> Text is "guest" end function
|
|
62
62
|
|
|
63
|
-
law
|
|
64
|
-
definition
|
|
65
|
-
|
|
66
|
-
prelude.isRight (register s) = (
|
|
67
|
-
|
|
63
|
+
law "|registering an account| is
|
|
64
|
+
definition :
|
|
65
|
+
for all (s :: Signup) =>
|
|
66
|
+
prelude.isRight (register s) = (first matching case of s :
|
|
67
|
+
when Signup record{ name = name , age = age} then
|
|
68
68
|
prelude.length name > 0 && age >= 18 && prelude.length name <= 8
|
|
69
|
-
end)
|
|
70
|
-
end
|
|
71
|
-
example
|
|
72
|
-
s = Signup "ada" 30
|
|
73
|
-
expect register s = Right (Account "ada" 30 2)
|
|
74
|
-
end
|
|
75
|
-
example
|
|
76
|
-
s = Signup "" 30
|
|
69
|
+
end case) end for all
|
|
70
|
+
end definition
|
|
71
|
+
example "|a new account is promoted| :
|
|
72
|
+
s = Signup record{ name = "ada" , age = 30}
|
|
73
|
+
expect register s = Right (Account record{ name = "ada" , age = 30 , level = 2})
|
|
74
|
+
end example
|
|
75
|
+
example "|a name is required| :
|
|
76
|
+
s = Signup record{ name = "" , age = 30}
|
|
77
77
|
expect register s = Left MissingName
|
|
78
|
-
end
|
|
79
|
-
example
|
|
80
|
-
s = Signup "bob" 17
|
|
78
|
+
end example
|
|
79
|
+
example "|seventeen is too young| :
|
|
80
|
+
s = Signup record{ name = "bob" , age = 17}
|
|
81
81
|
expect register s = Left TooYoung
|
|
82
|
-
end
|
|
83
|
-
example
|
|
84
|
-
s = Signup "bartholomew" 30
|
|
82
|
+
end example
|
|
83
|
+
example "|a long name is rejected| :
|
|
84
|
+
s = Signup record{ name = "bartholomew" , age = 30}
|
|
85
85
|
expect register s = Left NameTooLong
|
|
86
|
-
end
|
|
87
|
-
example
|
|
88
|
-
s = Signup "taken" 30
|
|
89
|
-
expect register s = Right (Account "waitlist" 18 0)
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
law
|
|
94
|
-
definition
|
|
95
|
-
|
|
96
|
-
end
|
|
97
|
-
example
|
|
98
|
-
s = Signup "ada" 30
|
|
86
|
+
end example
|
|
87
|
+
example "|a taken name joins the waitlist| :
|
|
88
|
+
s = Signup record{ name = "taken" , age = 30}
|
|
89
|
+
expect register s = Right (Account record{ name = "waitlist" , age = 18 , level = 0})
|
|
90
|
+
end example
|
|
91
|
+
end law
|
|
92
|
+
|
|
93
|
+
law "|greeting never fails| is
|
|
94
|
+
definition :
|
|
95
|
+
for all (s :: Signup) => prelude.isRight (greet s) = true end for all
|
|
96
|
+
end definition
|
|
97
|
+
example "|a named greeting| :
|
|
98
|
+
s = Signup record{ name = "ada" , age = 30}
|
|
99
99
|
expect greet s = Right "ada"
|
|
100
|
-
end
|
|
101
|
-
example
|
|
102
|
-
s = Signup "" 30
|
|
100
|
+
end example
|
|
101
|
+
example "|an anonymous greeting| :
|
|
102
|
+
s = Signup record{ name = "" , age = 30}
|
|
103
103
|
expect greet s = Right "guest"
|
|
104
|
-
end
|
|
105
|
-
end
|
|
104
|
+
end example
|
|
105
|
+
end law
|
|
106
106
|
|
|
107
107
|
-- An all group runs its steps on the same state and combines their results.
|
|
108
108
|
-- With accumulate, every failing step's error is reported, in a generated
|
|
109
109
|
-- VetFailures constructor.
|
|
110
|
-
|
|
110
|
+
function firstName (a :: Signup) (b :: Signup) -> Text is nameOf a end function
|
|
111
111
|
|
|
112
112
|
workflow vet :: Signup -> Either _ Text is
|
|
113
113
|
all accumulate
|
|
114
114
|
then checkName
|
|
115
115
|
then checkAge
|
|
116
|
-
end
|
|
116
|
+
end all
|
|
117
117
|
combine firstName
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
law
|
|
121
|
-
definition
|
|
122
|
-
|
|
123
|
-
prelude.isRight (vet s) = (
|
|
124
|
-
|
|
125
|
-
end)
|
|
126
|
-
end
|
|
127
|
-
example
|
|
128
|
-
s = Signup "ada" 30
|
|
118
|
+
end workflow
|
|
119
|
+
|
|
120
|
+
law "|vetting reports every problem| is
|
|
121
|
+
definition :
|
|
122
|
+
for all (s :: Signup) =>
|
|
123
|
+
prelude.isRight (vet s) = (first matching case of s :
|
|
124
|
+
when Signup record{ name = name , age = age} then prelude.length name > 0 && age >= 18
|
|
125
|
+
end case) end for all
|
|
126
|
+
end definition
|
|
127
|
+
example "|no problems| :
|
|
128
|
+
s = Signup record{ name = "ada" , age = 30}
|
|
129
129
|
expect vet s = Right "ada"
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
law
|
|
134
|
-
definition
|
|
135
|
-
|
|
136
|
-
vet (Signup "" (prelude.Int32 age)) = Left (VetFailures [VetCheckNameFailed MissingName, VetCheckAgeFailed "too young"])
|
|
137
|
-
end
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
law
|
|
141
|
-
definition
|
|
142
|
-
|
|
143
|
-
vet (Signup "ada" (prelude.Int32 age)) = Left (VetFailures [VetCheckAgeFailed "too young"])
|
|
144
|
-
end
|
|
145
|
-
end
|
|
130
|
+
end example
|
|
131
|
+
end law
|
|
132
|
+
|
|
133
|
+
law "|vetting a nameless child reports both problems| is
|
|
134
|
+
definition :
|
|
135
|
+
for all (age :: Int8 where age >= 0 && age < 18) =>
|
|
136
|
+
vet (Signup record{ name = "" , age = (prelude.Int32 age)}) = Left (VetFailures [VetCheckNameFailed MissingName, VetCheckAgeFailed "too young"]) end for all
|
|
137
|
+
end definition
|
|
138
|
+
end law
|
|
139
|
+
|
|
140
|
+
law "|vetting a named child reports its age| is
|
|
141
|
+
definition :
|
|
142
|
+
for all (age :: Int8 where age >= 0 && age < 18) =>
|
|
143
|
+
vet (Signup record{ name = "ada" , age = (prelude.Int32 age)}) = Left (VetFailures [VetCheckAgeFailed "too young"]) end for all
|
|
144
|
+
end definition
|
|
145
|
+
end law
|
|
146
146
|
|
|
147
147
|
-- In a workflow with asynchronous steps, an all group runs them at the same
|
|
148
148
|
-- time. Order -1's checks are slow: the stock check takes 400ms and the
|
|
149
149
|
-- credit check 250ms, so run in turn they would take 650ms. Every order with
|
|
150
150
|
-- a negative number fails both checks.
|
|
151
|
-
type Order is
|
|
151
|
+
type Order is a record{ number :: Int64} end type
|
|
152
152
|
|
|
153
153
|
async checkStock :: Order -> Either Text Order
|
|
154
154
|
async checkCredit :: Order -> Either Text Order
|
|
155
155
|
|
|
156
|
-
|
|
156
|
+
function firstOrder (a :: Order) (b :: Order) -> Order is a end function
|
|
157
157
|
|
|
158
158
|
workflow approve :: Order -> Either _ Order is
|
|
159
159
|
all accumulate
|
|
160
160
|
then checkStock
|
|
161
161
|
then checkCredit
|
|
162
|
-
end
|
|
162
|
+
end all
|
|
163
163
|
combine firstOrder
|
|
164
|
-
end
|
|
164
|
+
end workflow
|
|
165
165
|
|
|
166
166
|
-- The failures are in the order the steps are declared, not the order they
|
|
167
167
|
-- finish in.
|
|
168
|
-
law
|
|
169
|
-
definition
|
|
170
|
-
|
|
171
|
-
approve (Order n) = prelude.select (n < 0)
|
|
168
|
+
law "|approval reports failures in declaration order| is
|
|
169
|
+
definition :
|
|
170
|
+
for all (n :: Int64) =>
|
|
171
|
+
approve (Order record{ number = n}) = prelude.select (n < 0)
|
|
172
172
|
(Left (ApproveFailures [ApproveCheckStockFailed "no stock", ApproveCheckCreditFailed "no credit"]))
|
|
173
|
-
(Right (Order n))
|
|
174
|
-
end
|
|
175
|
-
end
|
|
173
|
+
(Right (Order record{ number = n})) end for all
|
|
174
|
+
end definition
|
|
175
|
+
end law
|
|
176
|
+
end unit
|
package/examples-command.mjs
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
import { readFile, readdir, mkdir } from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { createCompiler } from "./api.mjs";
|
|
5
|
-
import { targets } from "./templates.mjs";
|
|
5
|
+
import { programTargets as targets, artifactTargets } from "./templates.mjs";
|
|
6
6
|
import { safePath, planWrites, applyWrites } from "./files.mjs";
|
|
7
7
|
import { exportNativePayments } from "./native-examples.mjs";
|
|
8
8
|
|
|
9
9
|
export async function generateExamples(options) {
|
|
10
10
|
const selected = options.target ? [options.target] : targets;
|
|
11
|
+
if (selected.some(t => artifactTargets.includes(t)))
|
|
12
|
+
throw new Error("examples exports program targets; use init and generate for artifact targets");
|
|
11
13
|
if (selected.some((t) => !targets.includes(t)))
|
|
12
14
|
throw new Error(`Unknown target: ${options.target}`);
|
|
13
15
|
if (options.example !== undefined) {
|
|
@@ -30,11 +32,9 @@ export async function generateExamples(options) {
|
|
|
30
32
|
const compiler = await createCompiler();
|
|
31
33
|
const artifacts = [];
|
|
32
34
|
for (const target of selected) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
schemaVersion: 5, from: '0.24', includeGeneration: true,
|
|
37
|
-
languageProfile: '1.0-rc.1', verification: {layers: ['proof','exhaustive','property','examples','runtime','model']},
|
|
35
|
+
const result = await compiler.planGeneration({
|
|
36
|
+
schemaVersion: 5,
|
|
37
|
+
languageProfile: '1.0-rc.2', verification: {layers: ['proof','exhaustive','property','examples','runtime','model']},
|
|
38
38
|
sources,
|
|
39
39
|
target,
|
|
40
40
|
machineBits: options.machineBits ?? 64,
|
|
@@ -58,7 +58,7 @@ export async function generateExamples(options) {
|
|
|
58
58
|
await applyWrites(plans);
|
|
59
59
|
return plans.map((plan, i) => ({
|
|
60
60
|
target: selected[i],
|
|
61
|
-
|
|
61
|
+
languageProfile: '1.0-rc.2',
|
|
62
62
|
directory: plan.root,
|
|
63
63
|
files: artifacts[i].map((f) => ({ path: f.path, ownership: f.ownership, placement: f.placement })),
|
|
64
64
|
changes: plan.changes.length,
|
package/explain.mjs
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
import { createTypePresentation } from './typed-presentation.mjs';
|
|
5
5
|
|
|
6
6
|
export const explanationViews = ['developer', 'business', 'formal', 'evidence'];
|
|
7
|
+
const exampleName = name => name.startsWith('"|') && name.endsWith('|') ? name
|
|
8
|
+
: '"|' + name.replace(/\\/gu, '\\\\').replace(/\|/gu, '\\|') + '|';
|
|
7
9
|
const presentationProposition = ({ typeName, expressionText }, inputs) => function proposition(assertion, business = false, scope = inputs) {
|
|
8
10
|
const term = expression => expressionText(expression, scope);
|
|
9
11
|
const recurse = (body, wording = business) => proposition(body, wording, scope);
|
|
@@ -34,7 +36,7 @@ const presentationProposition = ({ typeName, expressionText }, inputs) => functi
|
|
|
34
36
|
const restrictions = assertion.predicates.map(p => expressionText(p, nested));
|
|
35
37
|
const restricted = restrictions.length ? ` where ${restrictions.join(' and ')}` : '';
|
|
36
38
|
if (business) return `${universal ? 'For every' : 'There must exist'} ${binder}${restricted}, ${proposition(assertion.body, true, nested)}`;
|
|
37
|
-
return `${universal ? 'for all' : 'there exists'} (${binder}${restricted}) ${universal ? '=>' : 'such that'}\n${proposition(assertion.body, false, nested)}\nend`;
|
|
39
|
+
return `${universal ? 'for all' : 'there exists'} (${binder}${restricted}) ${universal ? '=>' : 'such that'}\n${proposition(assertion.body, false, nested)}\nend ${universal ? 'for all' : 'exists'}`;
|
|
38
40
|
}
|
|
39
41
|
default: throw new Error(`Unsupported typed assertion ${assertion.kind}`);
|
|
40
42
|
}
|
|
@@ -70,7 +72,7 @@ export function explainLaw(law, result, view = 'developer') {
|
|
|
70
72
|
proposition(law.assertion, true), law.rationale ? `Reason: ${law.rationale}` : ''].filter(Boolean).join('\n');
|
|
71
73
|
else if (view === 'formal') text = [quantifiers.length ? `for all (${quantifiers.join(') (')}) =>` : '',
|
|
72
74
|
restrictions.length ? `(${restrictions.join(' and ')}) implies` : '',
|
|
73
|
-
proposition(law.assertion), ...(quantifiers.length ? ['end'] : [])].filter(Boolean).join('\n');
|
|
75
|
+
proposition(law.assertion), ...(quantifiers.length ? ['end for all'] : [])].filter(Boolean).join('\n');
|
|
74
76
|
else text = [law.id, ...documentary, ...law.trace, ...strategies,
|
|
75
77
|
...(quantifiers.length ? [`Inputs: ${quantifiers.join('; ')}`] : []),
|
|
76
78
|
...(restrictions.length ? [`Restrictions: ${restrictions.join(' and ')}`] : []), proposition(law.assertion),
|
|
@@ -78,9 +80,9 @@ export function explainLaw(law, result, view = 'developer') {
|
|
|
78
80
|
`Examples: ${examples.length}`,
|
|
79
81
|
...examples.flatMap(example => {
|
|
80
82
|
const locals = new Map([...scope, ...example.bindings.map(binding => [binding.id, binding.name])]);
|
|
81
|
-
return [`example ${
|
|
83
|
+
return [`example ${exampleName(example.name)}:`,
|
|
82
84
|
...example.bindings.map(binding => ` ${binding.name} = ${valueText(binding.value, locals)}`),
|
|
83
|
-
...example.expectations.map(expectation => ` expect ${proposition(expectation, false, locals)}`)];
|
|
85
|
+
...example.expectations.map(expectation => ` expect ${proposition(expectation, false, locals)}`), 'end example'];
|
|
84
86
|
})].join('\n');
|
|
85
87
|
if(view==='developer')for(const item of evidence)if(item.nativeEvidence)text+='\n'+nativeEvidenceText(item);
|
|
86
88
|
if (view === 'evidence' && strategies.length) text += '\n' + strategies.join('\n');
|