lawspec 0.25.0 → 0.26.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 +43 -1
- package/README.md +5 -4
- package/api.mjs +0 -8
- package/bin/lawspec.mjs +14 -52
- package/build.json +77 -105
- package/core.wasm +2 -2
- package/core_jsffi.js +5 -0
- 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/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 +16 -15
- 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 +48 -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 +6 -5
- 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 +159 -158
- 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 +4 -6
- package/explain.mjs +6 -4
- package/expression-presentation.mjs +8 -8
- package/files.mjs +1 -1
- package/index.d.ts +4 -527
- package/launcher.mjs +9 -2
- package/lsp.mjs +10 -6
- package/native-examples.mjs +2 -2
- package/package.json +1 -1
- package/starter-rc1.lawspec +6 -5
- package/starter.lawspec +16 -15
- package/templates.mjs +4 -2
- 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
|
@@ -16,82 +16,82 @@ import lawspec.network
|
|
|
16
16
|
encoded :: Text -> UInt64 -> Int32 -> Int32 -> List Text
|
|
17
17
|
roundTrips :: Text -> UInt64 -> Int32 -> Int32 -> Bool
|
|
18
18
|
|
|
19
|
-
law
|
|
20
|
-
definition
|
|
21
|
-
|
|
22
|
-
end
|
|
23
|
-
example
|
|
19
|
+
law "|integers on the wire| is
|
|
20
|
+
definition :
|
|
21
|
+
for all (seed :: UInt64) => prelude.length (encoded "(int Integer _ _)" seed 4 4) = 4 end for all
|
|
22
|
+
end definition
|
|
23
|
+
example "|seed 7| :
|
|
24
24
|
seed = 7
|
|
25
25
|
expect encoded "(int Integer _ _)" seed 4 4 = ["9ddc4f", "eefb17", "8fc703", "8ee709"]
|
|
26
26
|
expect encoded "(int Int8 -128 127)" 42 4 5 = ["f901", "28", "00", "48", "5c"]
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
law
|
|
31
|
-
definition
|
|
32
|
-
|
|
33
|
-
end
|
|
34
|
-
example
|
|
27
|
+
end example
|
|
28
|
+
end law
|
|
29
|
+
|
|
30
|
+
law "|text, lists, Maybe and Either on the wire| is
|
|
31
|
+
definition :
|
|
32
|
+
for all (seed :: UInt64) => roundTrips "(list (maybe (either (int UInt8 0 255) (text))))" seed 4 8 = true end for all
|
|
33
|
+
end definition
|
|
34
|
+
example "|vectors| :
|
|
35
35
|
seed = 3
|
|
36
36
|
expect encoded "(text)" seed 4 3 = ["037b5b2c", "0134", "024d54"]
|
|
37
37
|
expect encoded "(list (maybe (bool)))" 5 3 3 = ["02000101", "0100", "010100"]
|
|
38
38
|
expect encoded "(either (int UInt8 0 255) (text))" 9 3 4 = ["00ec02", "0000", "00d201", "0100"]
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
law
|
|
43
|
-
definition
|
|
44
|
-
|
|
45
|
-
end
|
|
46
|
-
example
|
|
39
|
+
end example
|
|
40
|
+
end law
|
|
41
|
+
|
|
42
|
+
law "|data types on the wire| is
|
|
43
|
+
definition :
|
|
44
|
+
for all (seed :: UInt64) => roundTrips "(data Shape (ctor Shape::Circle (int Int32 0 100)) (ctor Shape::Box (int Int32 0 100) (int Int32 0 100)) (ctor Shape::Group (list (ref Shape))))" seed 4 8 = true end for all
|
|
45
|
+
end definition
|
|
46
|
+
example "|vectors| :
|
|
47
47
|
seed = 11
|
|
48
48
|
expect encoded "(data Shape (ctor Shape::Circle (int Int32 0 100)) (ctor Shape::Box (int Int32 0 100) (int Int32 0 100)) (ctor Shape::Group (list (ref Shape))))" seed 3 3 = ["0030", "020201b80100015ac801", "014808"]
|
|
49
|
-
end
|
|
50
|
-
end
|
|
49
|
+
end example
|
|
50
|
+
end law
|
|
51
51
|
|
|
52
52
|
-- A checked definition that another node evaluates by its content hash.
|
|
53
|
-
|
|
53
|
+
function shifted (x :: Int32) -> Int64 is x + 1000 end function
|
|
54
54
|
|
|
55
55
|
-- Calls that cross a network wait for it, so they are asynchronous.
|
|
56
56
|
async remoteShifted :: Int32 -> Int64
|
|
57
57
|
|
|
58
|
-
law
|
|
59
|
-
definition
|
|
60
|
-
end
|
|
58
|
+
law "|a definition evaluated on another node| is
|
|
59
|
+
definition : for all (x :: Int32) => remoteShifted x = shifted x end for all end definition
|
|
60
|
+
end law
|
|
61
61
|
|
|
62
62
|
-- An actor served on one node and called from another.
|
|
63
|
-
type Tally is
|
|
63
|
+
type Tally is a record{ count :: Int64} end type
|
|
64
64
|
|
|
65
65
|
openTally :: Unit -> Tally
|
|
66
66
|
add :: Tally -> UInt8 -> Pair Int64 Tally
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
function modelAdd (n :: UInt8) (total :: Int64 where total >= 0 && total <= 1000000) -> Pair Int64 Int64 is
|
|
69
69
|
Pair (total + n) (total + n)
|
|
70
|
-
end
|
|
70
|
+
end function
|
|
71
71
|
|
|
72
72
|
actor tally :: Tally by Int64 is
|
|
73
73
|
start openTally by 0
|
|
74
74
|
on add by modelAdd
|
|
75
|
-
end
|
|
75
|
+
end actor
|
|
76
76
|
|
|
77
77
|
async remoteAdds :: UInt8 -> Int64
|
|
78
78
|
|
|
79
|
-
law
|
|
80
|
-
definition
|
|
81
|
-
end
|
|
79
|
+
law "|two calls from another node| is
|
|
80
|
+
definition : for all (x :: UInt8) => remoteAdds x = 2 * x end for all end definition
|
|
81
|
+
end law
|
|
82
82
|
|
|
83
83
|
-- A protocol between two nodes: the first end sends a number and receives
|
|
84
84
|
-- its double.
|
|
85
85
|
protocol Doubling is
|
|
86
86
|
send Int32
|
|
87
87
|
receive Int64
|
|
88
|
-
end
|
|
88
|
+
end protocol
|
|
89
89
|
|
|
90
90
|
async remoteDoubling :: Int32 -> Int64
|
|
91
91
|
|
|
92
|
-
law
|
|
93
|
-
definition
|
|
94
|
-
end
|
|
92
|
+
law "|a conversation between nodes| is
|
|
93
|
+
definition : for all (x :: Int32) => remoteDoubling x = 2 * x end for all end definition
|
|
94
|
+
end law
|
|
95
95
|
|
|
96
96
|
-- A mailbox offered on one node and sent to from another: each send waits
|
|
97
97
|
-- until the mailbox has the message.
|
|
@@ -99,22 +99,22 @@ mailbox ledger of Int64
|
|
|
99
99
|
|
|
100
100
|
async remoteLedger :: Int32 -> Int64
|
|
101
101
|
|
|
102
|
-
law
|
|
103
|
-
definition
|
|
104
|
-
end
|
|
102
|
+
law "|messages reach a mailbox on another node| is
|
|
103
|
+
definition : for all (x :: Int32) => remoteLedger x = 2 * x end for all end definition
|
|
104
|
+
end law
|
|
105
105
|
|
|
106
106
|
-- A channel end sent to another node. A local end stays where it is and
|
|
107
107
|
-- the sending node relays its conversation, so the end works on its new
|
|
108
108
|
-- node as it did on the old one.
|
|
109
109
|
protocol Handoff is
|
|
110
110
|
send Doubling
|
|
111
|
-
end
|
|
111
|
+
end protocol
|
|
112
112
|
|
|
113
113
|
async remoteHandoff :: Int32 -> Int64
|
|
114
114
|
|
|
115
|
-
law
|
|
116
|
-
definition
|
|
117
|
-
end
|
|
115
|
+
law "|a channel end handed to another node| is
|
|
116
|
+
definition : for all (x :: Int32) => remoteHandoff x = 2 * x end for all end definition
|
|
117
|
+
end law
|
|
118
118
|
|
|
119
119
|
-- An end that is already between nodes moves: its state goes to the new
|
|
120
120
|
-- node, with any values that reached it, and its peer is told the new
|
|
@@ -125,17 +125,17 @@ end
|
|
|
125
125
|
protocol Answering is
|
|
126
126
|
receive Int32
|
|
127
127
|
send Int64
|
|
128
|
-
end
|
|
128
|
+
end protocol
|
|
129
129
|
|
|
130
130
|
protocol Passing is
|
|
131
131
|
send Answering
|
|
132
|
-
end
|
|
132
|
+
end protocol
|
|
133
133
|
|
|
134
134
|
async remoteHandoffOnward :: Int32 -> Int64
|
|
135
135
|
|
|
136
|
-
law
|
|
137
|
-
definition
|
|
138
|
-
end
|
|
136
|
+
law "|a channel end moved on twice| is
|
|
137
|
+
definition : for all (x :: Int32) => remoteHandoffOnward x = 2 * x end for all end definition
|
|
138
|
+
end law
|
|
139
139
|
|
|
140
140
|
-- The network is secure by default. Each node has an ML-DSA-65 identity;
|
|
141
141
|
-- the first frame between two nodes waits for a handshake (a signed
|
|
@@ -144,9 +144,9 @@ end
|
|
|
144
144
|
-- it; no other transport can.
|
|
145
145
|
async sealedOnTheWire :: Int32 -> Bool
|
|
146
146
|
|
|
147
|
-
law
|
|
148
|
-
definition
|
|
149
|
-
end
|
|
147
|
+
law "|frames cross sealed by default| is
|
|
148
|
+
definition : for all (x :: Int32) => sealedOnTheWire x = true end for all end definition
|
|
149
|
+
end law
|
|
150
150
|
|
|
151
151
|
-- Every target computes the same handshake: from these seeds and this
|
|
152
152
|
-- ciphertext, the same hashes of the hello and welcome, the same session
|
|
@@ -156,6 +156,7 @@ end
|
|
|
156
156
|
-- key and sealed record (dev/handshake-vector.py makes them).
|
|
157
157
|
handshakeAgrees :: Text -> Bool
|
|
158
158
|
|
|
159
|
-
law
|
|
160
|
-
definition
|
|
161
|
-
end
|
|
159
|
+
law "|the secure handshake agrees across targets| is
|
|
160
|
+
definition : handshakeAgrees "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f 404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f 808182838485868788898a8b8c8d8e8f tcp://10.0.0.1:7000 tcp://10.0.0.2:7000 36ed31b7f151b4412fa0898659e567a9fe899394069c31ec9007f98bd63282609999cc959fe9a01b4e9a21d61a866da30cdfb2db3d533e3614a170941f668dbe14b0ed47843bd401f8c76d5c8bc030e33c25cabf09b8fd4e3bdb84fc0fe2d8f1b7fea58f9124cd315d94970a9a9677160ffc00cd22875a598904ddd45c5e94ebea8db52eddd181d50f89a577de6f2cb21836a9da82e68ccf61d7aa72576a9ed6355577af27522b4006af2148510c5e8c901aafca088990811e31791a4c71abb7d96c522ee2de83431dc6db6d13e6b8a8cf8656cb64cf8368b691344d07f3ab75814960f85671890e8ccf6a2cf6b82667ff9a4a09eb59f0168453bc45ade0fa1bf747db6c2179dc1173689897f1dd582f2737f3bc8b6f3b84fa5bda5f204ae845ee5124363cad0ad7a3827c46f3ba55458215b32e748fb075e146880c93e97ca8f0f6b5768ac08d19127a2038889352893f19daccb8a01607805492a3c429df462ee941a73fd396de512d8873e901764b2636e7b03c5562097db59e8e00b08a65667873fdfcff99c805aaf49871d282744ee26118c43186d408ac5a0105c80e2b640d0ba44250887721535d865b1425e193777f4e0967a4d74e17fb14c54befd35d7baadb2caad627bb88e739e8d87616a38ac38bdea36c8b0e6dca981dd7cd15846d4187e2e841d1f6ce704369fddf5957b8827a80c80b1f34f44bab0b48ef26a30486f228c2c023e50ec6fb61c1ced87ab11b41b1e30a7f1a30e2c67e78bffead095b3dfc69205965bf9133a8f3dcc7e074ebaed77fd9f009906228f216a12af663d248c871a3a4b728da3d9987e53416c492da03f9c8f3c6c702f082cfdf8a326268a8e7d3c59f54405eef85c6104207bec5685dab59fdfa94b88cd22a00c8b89a85f71fb09d500b62422fdfac9fc7112aa8f756c3a723d211eeb2081d8858e87b1f480fb4cf80d194527f883be6ff89e648884d62c5df6af29de4119efe1fd5546a120902daa9a087143233432942b828871a1f8ec50081312a3e5363315b5198a5f71849cd9541b4aaaeb956f30a1be583532b1810687f6900c85d05e6e9d066570306c5ced181d90807f85634f1cbed272e8614caa69ddb359862eec8ea23b8e3a741e45e730e1ac9f7832bf4fee8f0a41f77015d0e95394838964bb43032b085cf8af0abe20701c011086474b610fa25185b33e24ea7493f28cc60fd78e5598a612f5c62d14344fa6a08d23231b78830d92c1d816ce3d51f33316c76b7896d789fda34cae5e095e592e73b7c300e9fbeb87aef276c5b0d53e3c120bf204bf8a3c5823e7318bf3e7da8e1abb590f0012a10882f49be91c496b000c28a067014ff1e6734f556b4918f2661faeef5783fbea9ba50340b6532edabf4d89b4113c72e6f92e085fb499a93335d03f616a1ecb5258713e0903c475e043486c78d16d0a4014ec1a065a87ac940e909df94d53109d109bd5b235b0c05b5b81fe202cebabd7cec50e434300eff7b75d910471f8bd400f04a7ffb814051c8b15fe7b7 c8c9cacbcccdcecfd0d1d2d3 046368616e05656e642d33137463703a2f2f31302e302e302e313a373030300e0300022a da31284386e2b951348bda30557cf4b5abb4ad9a1ac15582e99c3b938573a254 9c78ba836e9dcaf2845dceaa7a02c9aad0fe0fd29bb9a5b948635c65aa4b2676 c595a58a85aebe6fb8009a0ca6afc9eb614c7ab4711c08c4f7d34c38420c4abd 4c53010310808182838485868788898a8b8c8d8e8f0040c8c9cacbcccdcecfd0d1d2d314713b317e0cd4b10f4d5b885bc8bc1fa7aae4f824a30442ed569d3eab22fcce44c274bd139e84e8a9960d14a17722544ee187c1" = true end definition
|
|
161
|
+
end law
|
|
162
|
+
end unit
|
|
@@ -3,16 +3,16 @@ unit example.ordering
|
|
|
3
3
|
-- Constrained primitives. Each wrapper is a distinct type whose constructor
|
|
4
4
|
-- checks the value, so an out-of-range quantity cannot be constructed,
|
|
5
5
|
-- generated or decoded from native code. valueOf<Name> unwraps a value.
|
|
6
|
-
wrapper UnitQuantity is Int32 where value >= 1 && value <= 1000 end
|
|
7
|
-
wrapper OrderId is Text where prelude.length value > 0 end
|
|
8
|
-
wrapper NonEmptyList (a :: Type) is List a where prelude.length value > 0 end
|
|
6
|
+
wrapper UnitQuantity is Int32 where value >= 1 && value <= 1000 end wrapper
|
|
7
|
+
wrapper OrderId is Text where prelude.length value > 0 end wrapper
|
|
8
|
+
wrapper NonEmptyList (a :: Type) is List a where prelude.length value > 0 end wrapper
|
|
9
9
|
|
|
10
10
|
-- Each workflow state is its own type. A ValidatedOrder can only hold checked
|
|
11
11
|
-- values, so no later step needs to validate again.
|
|
12
|
-
type UnvalidatedOrder is
|
|
13
|
-
type ValidatedOrder is
|
|
14
|
-
type PricedOrder is
|
|
15
|
-
type OrderError is | InvalidOrderId | InvalidQuantity | PriceTooHigh end
|
|
12
|
+
type UnvalidatedOrder is a record{ id :: Text quantity :: Int32} end type
|
|
13
|
+
type ValidatedOrder is a record{ id :: OrderId quantity :: UnitQuantity} end type
|
|
14
|
+
type PricedOrder is a record{ id :: OrderId quantity :: UnitQuantity total :: Int64} end type
|
|
15
|
+
type OrderError is one of | InvalidOrderId | InvalidQuantity | PriceTooHigh end type
|
|
16
16
|
|
|
17
17
|
-- The compiler checks that each step accepts the previous step's state and
|
|
18
18
|
-- generates placeOrder on every target: the steps run in order and the first
|
|
@@ -22,55 +22,56 @@ type OrderError is | InvalidOrderId | InvalidQuantity | PriceTooHigh end
|
|
|
22
22
|
workflow placeOrder :: UnvalidatedOrder -> Either OrderError PricedOrder is
|
|
23
23
|
validateOrder :: UnvalidatedOrder -> Either OrderError ValidatedOrder
|
|
24
24
|
priceOrder :: ValidatedOrder -> Either OrderError PricedOrder
|
|
25
|
-
end
|
|
25
|
+
end workflow
|
|
26
26
|
|
|
27
27
|
firstLine :: NonEmptyList Int32 -> Int32
|
|
28
28
|
|
|
29
29
|
-- An order is placed when it is valid and its total is at most 20000.
|
|
30
|
-
law
|
|
31
|
-
definition
|
|
32
|
-
|
|
33
|
-
prelude.isRight (placeOrder input) = (
|
|
34
|
-
|
|
30
|
+
law "|placing an order prices it| is
|
|
31
|
+
definition :
|
|
32
|
+
for all (input :: UnvalidatedOrder) =>
|
|
33
|
+
prelude.isRight (placeOrder input) = (first matching case of input :
|
|
34
|
+
when UnvalidatedOrder record{ id = id , quantity = quantity} then
|
|
35
35
|
prelude.length id > 0 && quantity >= 1 && quantity <= 800
|
|
36
|
-
end)
|
|
37
|
-
end
|
|
38
|
-
example
|
|
39
|
-
input = UnvalidatedOrder "A-1" 100
|
|
36
|
+
end case) end for all
|
|
37
|
+
end definition
|
|
38
|
+
example "|a hundred units| :
|
|
39
|
+
input = UnvalidatedOrder record{ id = "A-1" , quantity = 100}
|
|
40
40
|
expect prelude.isRight (placeOrder input) = true
|
|
41
|
-
end
|
|
42
|
-
end
|
|
41
|
+
end example
|
|
42
|
+
end law
|
|
43
43
|
|
|
44
|
-
law
|
|
45
|
-
definition
|
|
46
|
-
|
|
47
|
-
(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
end) = (
|
|
51
|
-
|
|
44
|
+
law "|validation accepts exactly the valid orders| is
|
|
45
|
+
definition :
|
|
46
|
+
for all (input :: UnvalidatedOrder) =>
|
|
47
|
+
(first matching case of validateOrder input :
|
|
48
|
+
when Left e then false
|
|
49
|
+
when Right v then true
|
|
50
|
+
end case) = (first matching case of input :
|
|
51
|
+
when UnvalidatedOrder record{ id = id , quantity = quantity} then
|
|
52
52
|
prelude.length id > 0 && quantity >= 1 && quantity <= 1000
|
|
53
|
-
end)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
53
|
+
end case) end for all
|
|
54
|
+
end definition
|
|
55
|
+
end law
|
|
56
56
|
|
|
57
|
-
law
|
|
58
|
-
definition
|
|
59
|
-
|
|
60
|
-
valueOfUnitQuantity q >= 1 && valueOfUnitQuantity q <= 1000
|
|
61
|
-
end
|
|
62
|
-
example
|
|
57
|
+
law "|quantities are always in range| is
|
|
58
|
+
definition :
|
|
59
|
+
for all (q :: UnitQuantity) =>
|
|
60
|
+
valueOfUnitQuantity q >= 1 && valueOfUnitQuantity q <= 1000 end for all
|
|
61
|
+
end definition
|
|
62
|
+
example "|largest quantity| :
|
|
63
63
|
q = UnitQuantity 1000
|
|
64
64
|
expect valueOfUnitQuantity q = 1000
|
|
65
|
-
end
|
|
66
|
-
end
|
|
65
|
+
end example
|
|
66
|
+
end law
|
|
67
67
|
|
|
68
|
-
law
|
|
69
|
-
definition
|
|
70
|
-
|
|
71
|
-
(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
end) = true
|
|
75
|
-
end
|
|
76
|
-
end
|
|
68
|
+
law "|a non-empty list has a first element| is
|
|
69
|
+
definition :
|
|
70
|
+
for all (xs :: NonEmptyList Int32) =>
|
|
71
|
+
(first matching case of valueOfNonEmptyList xs :
|
|
72
|
+
when Nil then false
|
|
73
|
+
when Cons head tail then firstLine xs = head
|
|
74
|
+
end case) = true end for all
|
|
75
|
+
end definition
|
|
76
|
+
end law
|
|
77
|
+
end unit
|
|
@@ -7,57 +7,58 @@ unit example.durations
|
|
|
7
7
|
-- fail, as division by zero does.
|
|
8
8
|
|
|
9
9
|
-- A checked definition: the totality audit proves the result in range.
|
|
10
|
-
|
|
10
|
+
function timeoutFor (retries :: Int32 where retries >= 0 && retries <= 5) -> Duration is
|
|
11
11
|
2s + 500ms * retries
|
|
12
|
-
end
|
|
12
|
+
end function
|
|
13
13
|
|
|
14
14
|
-- An adapter: the time left of a budget, never negative.
|
|
15
15
|
remaining :: Duration -> Duration -> Duration
|
|
16
16
|
|
|
17
|
-
law
|
|
18
|
-
definition
|
|
19
|
-
|
|
20
|
-
end
|
|
21
|
-
end
|
|
17
|
+
law "|units relate| is
|
|
18
|
+
definition :
|
|
19
|
+
for all (b :: Bool) => (1us * 1000 = 1ms && 1000ms = 1s && 60s = 1min && 60min = 1h && 24h = 1d) = true end for all
|
|
20
|
+
end definition
|
|
21
|
+
end law
|
|
22
22
|
|
|
23
|
-
law
|
|
24
|
-
definition
|
|
25
|
-
|
|
23
|
+
law "|subtraction undoes addition| is
|
|
24
|
+
definition :
|
|
25
|
+
for all (n :: Int32 where n >= 0) (m :: Int32 where m >= 0) =>
|
|
26
26
|
prelude.milliseconds (prelude.Integer n) + prelude.milliseconds (prelude.Integer m) - prelude.milliseconds (prelude.Integer m) =
|
|
27
|
-
prelude.milliseconds (prelude.Integer n)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
law
|
|
32
|
-
definition
|
|
33
|
-
|
|
34
|
-
end
|
|
35
|
-
example
|
|
27
|
+
prelude.milliseconds (prelude.Integer n) end for all
|
|
28
|
+
end definition
|
|
29
|
+
end law
|
|
30
|
+
|
|
31
|
+
law "|timeouts grow by half a second| is
|
|
32
|
+
definition :
|
|
33
|
+
for all (r :: Int32 where r >= 0 && r <= 4) => timeoutFor (r + 1) - timeoutFor r = 500ms end for all
|
|
34
|
+
end definition
|
|
35
|
+
example "|no retries| :
|
|
36
36
|
r = 0
|
|
37
37
|
expect prelude.toMicroseconds (timeoutFor r) = 2000000
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
law
|
|
42
|
-
definition
|
|
43
|
-
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
law
|
|
48
|
-
definition
|
|
49
|
-
|
|
38
|
+
end example
|
|
39
|
+
end law
|
|
40
|
+
|
|
41
|
+
law "|spending everything leaves nothing| is
|
|
42
|
+
definition :
|
|
43
|
+
for all (d :: Duration) => remaining d d = 0us and remaining d 0us = d end for all
|
|
44
|
+
end definition
|
|
45
|
+
end law
|
|
46
|
+
|
|
47
|
+
law "|what remains of a budget| is
|
|
48
|
+
definition :
|
|
49
|
+
for all (budget :: Duration) (spent :: Duration) =>
|
|
50
50
|
prelude.toMicroseconds (remaining budget spent) =
|
|
51
|
-
prelude.select (spent <= budget) (prelude.toMicroseconds budget - prelude.toMicroseconds spent) 0
|
|
52
|
-
end
|
|
53
|
-
example
|
|
51
|
+
prelude.select (spent <= budget) (prelude.toMicroseconds budget - prelude.toMicroseconds spent) 0 end for all
|
|
52
|
+
end definition
|
|
53
|
+
example "|part of a second| :
|
|
54
54
|
budget = 1s
|
|
55
55
|
spent = 250ms
|
|
56
56
|
expect remaining budget spent = 750ms
|
|
57
|
-
end
|
|
58
|
-
example
|
|
57
|
+
end example
|
|
58
|
+
example "|an overrun leaves nothing| :
|
|
59
59
|
budget = 1s
|
|
60
60
|
spent = 2s
|
|
61
61
|
expect remaining budget spent = 0us
|
|
62
|
-
end
|
|
63
|
-
end
|
|
62
|
+
end example
|
|
63
|
+
end law
|
|
64
|
+
end unit
|
|
@@ -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
|