lawspec 0.24.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 +109 -1
- package/README.md +34 -26
- package/UNICODE-LICENSE.txt +39 -0
- package/api.mjs +6 -23
- package/bin/lawspec.mjs +148 -65
- package/build.json +245 -177
- package/builtin-data-names.mjs +73 -0
- package/core.wasm +2 -2
- package/core_jsffi.js +5 -0
- package/examples/native-payments/clojure/lawspec.json +13 -2
- package/examples/native-payments/csharp/lawspec.json +13 -2
- package/examples/native-payments/elixir/lawspec.json +14 -3
- package/examples/native-payments/erlang/lawspec.json +14 -3
- package/examples/native-payments/fsharp/lawspec.json +14 -3
- package/examples/native-payments/gleam/lawspec.json +14 -3
- package/examples/native-payments/go/lawspec.json +14 -3
- package/examples/native-payments/groovy/lawspec.json +14 -3
- package/examples/native-payments/haskell/lawspec.json +14 -3
- package/examples/native-payments/java/lawspec.json +14 -3
- package/examples/native-payments/javascript/lawspec.json +14 -3
- package/examples/native-payments/kotlin/lawspec.json +14 -3
- package/examples/native-payments/python/lawspec.json +14 -3
- package/examples/native-payments/rust/lawspec.json +14 -3
- package/examples/native-payments/scala/lawspec.json +14 -3
- package/examples/native-payments/swift/lawspec.json +14 -3
- package/examples/native-payments/typescript/lawspec.json +14 -3
- package/examples/packages/orders/lawspec.json +25 -5
- package/examples/packages/orders/orders.lawspec +57 -53
- package/examples/packages/shop-domain/src/domain.lawspec +36 -33
- package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-1/src/rates.lawspec +5 -4
- package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-2/src/rates.lawspec +6 -4
- package/examples/rc1/authorization-snapshots.lawspec +113 -0
- package/examples/rc1/banner-filenames.lawspec +64 -0
- package/examples/rc1/bit-codec.lawspec +57 -0
- package/examples/rc1/bounded-witness-search.lawspec +151 -0
- package/examples/rc1/conflicting-requirements.lawspec +31 -0
- package/examples/rc1/constructive-witnesses.lawspec +120 -0
- package/examples/rc1/documentary-review.lawspec +43 -0
- package/examples/rc1/empty-domains.lawspec +47 -0
- package/examples/rc1/finite-integer-domains.lawspec +76 -0
- package/examples/rc1/law-metadata.lawspec +30 -0
- package/examples/rc1/native-payments.lawspec +108 -0
- package/examples/rc1/nesting/rrr.lawspec +34 -0
- package/examples/rc1/nesting/rru.lawspec +57 -0
- package/examples/rc1/nesting/rur.lawspec +58 -0
- package/examples/rc1/nesting/ruu.lawspec +78 -0
- package/examples/rc1/nesting/urr.lawspec +53 -0
- package/examples/rc1/nesting/uru.lawspec +73 -0
- package/examples/rc1/nesting/uur.lawspec +74 -0
- package/examples/rc1/nesting/uuu.lawspec +96 -0
- package/examples/rc1/order-workflow.lawspec +127 -0
- package/examples/rc1/parcel-measurements.lawspec +93 -0
- package/examples/rc1/picture-records.lawspec +52 -0
- package/examples/rc1/picture-tuples.lawspec +48 -0
- package/examples/rc1/record-payloads.lawspec +57 -0
- package/examples/rc1/recursive-records.lawspec +65 -0
- package/examples/rc1/refund-conformance.lawspec +45 -0
- package/examples/rc1/refund-policy.lawspec +89 -0
- package/examples/rc1/requirement-composition.lawspec +90 -0
- package/examples/rc1/structural-records.lawspec +54 -0
- package/examples/rc1-authoring.lawspec +57 -0
- 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 +8 -4
- package/explain.mjs +114 -0
- package/expression-presentation.mjs +333 -0
- package/files.mjs +16 -0
- package/index.d.ts +406 -11
- package/launcher.mjs +9 -2
- package/lsp.mjs +235 -0
- package/native-evidence.mjs +187 -0
- package/native-examples.mjs +8 -1
- package/node-events-reporter.mjs +23 -0
- package/package.json +8 -1
- package/positional-fields.mjs +13 -0
- package/proof.d.ts +35 -0
- package/proof.mjs +174 -0
- package/starter-rc1.lawspec +26 -0
- package/starter.lawspec +16 -15
- package/templates.mjs +6 -4
- package/test-command.mjs +107 -35
- package/typed-presentation.mjs +165 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
unit example.rc1.recursiveRecords
|
|
2
|
+
|
|
3
|
+
type UnissuedApproval is one of
|
|
4
|
+
| Unissued of type UnissuedApproval
|
|
5
|
+
end type
|
|
6
|
+
|
|
7
|
+
type Box (item of type Type) is a record{
|
|
8
|
+
contents of type item
|
|
9
|
+
}
|
|
10
|
+
end type
|
|
11
|
+
|
|
12
|
+
type FirstSnapshot is a record{
|
|
13
|
+
next of type Box SecondSnapshot
|
|
14
|
+
}
|
|
15
|
+
end type
|
|
16
|
+
|
|
17
|
+
type SecondSnapshot is a record{
|
|
18
|
+
previous of type FirstSnapshot
|
|
19
|
+
}
|
|
20
|
+
end type
|
|
21
|
+
|
|
22
|
+
type ApprovalChain is a record{
|
|
23
|
+
next of type Maybe ApprovalChain
|
|
24
|
+
}
|
|
25
|
+
end type
|
|
26
|
+
|
|
27
|
+
function chainLength (chain of type ApprovalChain) -> Whole is
|
|
28
|
+
first matching case of chain:
|
|
29
|
+
when record{next = Nothing} then 0
|
|
30
|
+
when record{next = Just rest} then 1 + chainLength rest
|
|
31
|
+
end case
|
|
32
|
+
end function
|
|
33
|
+
|
|
34
|
+
law "|an unissued approval has no finite construction| is
|
|
35
|
+
definition:
|
|
36
|
+
for all (approval of type UnissuedApproval) =>
|
|
37
|
+
false
|
|
38
|
+
end for all
|
|
39
|
+
end definition
|
|
40
|
+
end law
|
|
41
|
+
|
|
42
|
+
law "|mutually dependent snapshots have no first value| is
|
|
43
|
+
definition:
|
|
44
|
+
for all (snapshot of type FirstSnapshot) =>
|
|
45
|
+
false
|
|
46
|
+
end for all
|
|
47
|
+
end definition
|
|
48
|
+
end law
|
|
49
|
+
|
|
50
|
+
law "|a completed chain has no remaining approval| is
|
|
51
|
+
definition:
|
|
52
|
+
chainLength (ApprovalChain record{next = Nothing}) must be 0
|
|
53
|
+
end definition
|
|
54
|
+
end law
|
|
55
|
+
|
|
56
|
+
law "|a pending chain counts each remaining approval| is
|
|
57
|
+
definition:
|
|
58
|
+
chainLength (ApprovalChain record{
|
|
59
|
+
next = Just (ApprovalChain record{
|
|
60
|
+
next = Just (ApprovalChain record{next = Nothing})
|
|
61
|
+
})
|
|
62
|
+
}) must be 2
|
|
63
|
+
end definition
|
|
64
|
+
end law
|
|
65
|
+
end unit
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
unit example.refundConformance
|
|
2
|
+
|
|
3
|
+
import example.refunds as policy
|
|
4
|
+
|
|
5
|
+
nativeRefund of type policy.RefundRequest -> policy.RefundDecision
|
|
6
|
+
|
|
7
|
+
law "|native refunds conform to the policy| is
|
|
8
|
+
description:
|
|
9
|
+
"The application's refund decision agrees with the checked policy."
|
|
10
|
+
end description
|
|
11
|
+
definition:
|
|
12
|
+
for all (request of type policy.RefundRequest) =>
|
|
13
|
+
nativeRefund request must be policy.refund request
|
|
14
|
+
end for all
|
|
15
|
+
end definition
|
|
16
|
+
|
|
17
|
+
references:
|
|
18
|
+
ref function policy.refund
|
|
19
|
+
end references
|
|
20
|
+
|
|
21
|
+
expect nativeRefund request = expected
|
|
22
|
+
in table (request, expected):
|
|
23
|
+
row policy.RefundRequest record{capturedAmount = 0, requestedAmount = 0,
|
|
24
|
+
flagged = false}, policy.Approved
|
|
25
|
+
row policy.RefundRequest record{capturedAmount = 0, requestedAmount = 0,
|
|
26
|
+
flagged = true}, policy.Approved
|
|
27
|
+
row policy.RefundRequest record{capturedAmount = 100, requestedAmount = 0,
|
|
28
|
+
flagged = true}, policy.Approved
|
|
29
|
+
row policy.RefundRequest record{capturedAmount = 0, requestedAmount = 1,
|
|
30
|
+
flagged = false}, policy.Rejected
|
|
31
|
+
row policy.RefundRequest record{capturedAmount = 100, requestedAmount = 101,
|
|
32
|
+
flagged = true}, policy.Rejected
|
|
33
|
+
row policy.RefundRequest record{capturedAmount = 100, requestedAmount = 100,
|
|
34
|
+
flagged = true}, policy.ReviewRequired
|
|
35
|
+
row policy.RefundRequest record{capturedAmount = 100, requestedAmount = 1,
|
|
36
|
+
flagged = true}, policy.ReviewRequired
|
|
37
|
+
row policy.RefundRequest record{capturedAmount = 100, requestedAmount = 100,
|
|
38
|
+
flagged = false}, policy.Approved
|
|
39
|
+
row policy.RefundRequest record{capturedAmount = 100, requestedAmount = 99,
|
|
40
|
+
flagged = false}, policy.Approved
|
|
41
|
+
row policy.RefundRequest record{capturedAmount = 9007199254740992,
|
|
42
|
+
requestedAmount = 9007199254740993, flagged = false}, policy.Rejected
|
|
43
|
+
end table
|
|
44
|
+
end law
|
|
45
|
+
end unit
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
unit example.refunds
|
|
2
|
+
|
|
3
|
+
type RefundDecision is one of
|
|
4
|
+
| Approved
|
|
5
|
+
| ReviewRequired
|
|
6
|
+
| Rejected
|
|
7
|
+
end type
|
|
8
|
+
|
|
9
|
+
type RefundRequest is a record{
|
|
10
|
+
capturedAmount of type Whole
|
|
11
|
+
requestedAmount of type Whole
|
|
12
|
+
flagged of type Bool
|
|
13
|
+
}
|
|
14
|
+
end type
|
|
15
|
+
|
|
16
|
+
function refund (request of type RefundRequest) -> RefundDecision is
|
|
17
|
+
first matching case of request:
|
|
18
|
+
when record{requestedAmount = 0} then Approved
|
|
19
|
+
when it.requestedAmount > it.capturedAmount then Rejected
|
|
20
|
+
when it.flagged then ReviewRequired
|
|
21
|
+
otherwise Approved
|
|
22
|
+
end case
|
|
23
|
+
end function
|
|
24
|
+
|
|
25
|
+
law "|a zero refund is approved| is
|
|
26
|
+
description:
|
|
27
|
+
"A zero request transfers nothing and needs no review."
|
|
28
|
+
end description
|
|
29
|
+
definition:
|
|
30
|
+
for all (capturedAmount of type Whole) (flagged of type Bool) =>
|
|
31
|
+
refund (RefundRequest record{
|
|
32
|
+
capturedAmount = capturedAmount, requestedAmount = 0, flagged = flagged
|
|
33
|
+
}) must be Approved
|
|
34
|
+
end for all
|
|
35
|
+
end definition
|
|
36
|
+
end law
|
|
37
|
+
|
|
38
|
+
law "|a refund cannot exceed the captured amount| is
|
|
39
|
+
definition:
|
|
40
|
+
for all (request of type RefundRequest) =>
|
|
41
|
+
request.requestedAmount > request.capturedAmount implies
|
|
42
|
+
refund request must be Rejected
|
|
43
|
+
end for all
|
|
44
|
+
end definition
|
|
45
|
+
end law
|
|
46
|
+
|
|
47
|
+
law "|an eligible flagged refund requires review| is
|
|
48
|
+
definition:
|
|
49
|
+
for all (request of type RefundRequest) =>
|
|
50
|
+
request.requestedAmount > 0 and
|
|
51
|
+
request.requestedAmount <= request.capturedAmount and request.flagged
|
|
52
|
+
implies refund request must be ReviewRequired
|
|
53
|
+
end for all
|
|
54
|
+
end definition
|
|
55
|
+
end law
|
|
56
|
+
|
|
57
|
+
law "|an eligible unflagged refund is approved| is
|
|
58
|
+
definition:
|
|
59
|
+
for all (request of type RefundRequest) =>
|
|
60
|
+
request.requestedAmount <= request.capturedAmount and not request.flagged
|
|
61
|
+
implies refund request must be Approved
|
|
62
|
+
end for all
|
|
63
|
+
end definition
|
|
64
|
+
|
|
65
|
+
expect refund request = expected
|
|
66
|
+
in table (request, expected):
|
|
67
|
+
row RefundRequest record{capturedAmount = 0, requestedAmount = 0, flagged =
|
|
68
|
+
false}, Approved
|
|
69
|
+
row RefundRequest record{capturedAmount = 0, requestedAmount = 0, flagged =
|
|
70
|
+
true}, Approved
|
|
71
|
+
row RefundRequest record{capturedAmount = 100, requestedAmount = 0, flagged
|
|
72
|
+
= true}, Approved
|
|
73
|
+
row RefundRequest record{capturedAmount = 0, requestedAmount = 1, flagged =
|
|
74
|
+
false}, Rejected
|
|
75
|
+
row RefundRequest record{capturedAmount = 100, requestedAmount = 101,
|
|
76
|
+
flagged = true}, Rejected
|
|
77
|
+
row RefundRequest record{capturedAmount = 100, requestedAmount = 100,
|
|
78
|
+
flagged = true}, ReviewRequired
|
|
79
|
+
row RefundRequest record{capturedAmount = 100, requestedAmount = 1, flagged
|
|
80
|
+
= true}, ReviewRequired
|
|
81
|
+
row RefundRequest record{capturedAmount = 100, requestedAmount = 100,
|
|
82
|
+
flagged = false}, Approved
|
|
83
|
+
row RefundRequest record{capturedAmount = 100, requestedAmount = 99, flagged
|
|
84
|
+
= false}, Approved
|
|
85
|
+
row RefundRequest record{capturedAmount = 9007199254740992, requestedAmount
|
|
86
|
+
= 9007199254740993, flagged = false}, Rejected
|
|
87
|
+
end table
|
|
88
|
+
end law
|
|
89
|
+
end unit
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
unit example.rc1.requirementComposition
|
|
2
|
+
|
|
3
|
+
import example.rc1.authorizationSnapshots as snapshot
|
|
4
|
+
(Payment, Authorization, AuthorizationState, active, authorizes)
|
|
5
|
+
|
|
6
|
+
law "|each item has a match| (relation of type a -> b -> Bool) is
|
|
7
|
+
definition:
|
|
8
|
+
for all (item of type b) =>
|
|
9
|
+
there exists (witness of type a) such that
|
|
10
|
+
relation witness item
|
|
11
|
+
end exists
|
|
12
|
+
end for all
|
|
13
|
+
end definition
|
|
14
|
+
end law
|
|
15
|
+
|
|
16
|
+
function revokedMatch (authorization of type Authorization)
|
|
17
|
+
(payment of type Payment) -> Bool is
|
|
18
|
+
active AllRevoked authorization and authorizes authorization payment
|
|
19
|
+
end function
|
|
20
|
+
|
|
21
|
+
law "|matching and readiness agree| is
|
|
22
|
+
definition:
|
|
23
|
+
(apply law "|each item has a match| authorizes)
|
|
24
|
+
and
|
|
25
|
+
(for all (payment of type Payment) =>
|
|
26
|
+
there exists (authorization of type Authorization
|
|
27
|
+
where active Ready authorization) such that
|
|
28
|
+
authorizes authorization payment
|
|
29
|
+
end exists
|
|
30
|
+
end for all)
|
|
31
|
+
end definition
|
|
32
|
+
|
|
33
|
+
description:
|
|
34
|
+
"Every payment has a matching identity and an active approval when ready."
|
|
35
|
+
end description
|
|
36
|
+
end law
|
|
37
|
+
|
|
38
|
+
law "|revocation or matching has a complete explanation| is
|
|
39
|
+
definition:
|
|
40
|
+
(must not apply law "|each item has a match| revokedMatch)
|
|
41
|
+
or apply law "|each item has a match| authorizes
|
|
42
|
+
end definition
|
|
43
|
+
|
|
44
|
+
description:
|
|
45
|
+
"Either revoked approvals cannot cover every payment, or matching succeeds."
|
|
46
|
+
end description
|
|
47
|
+
end law
|
|
48
|
+
|
|
49
|
+
law "|matching implies ready approval coverage| is
|
|
50
|
+
definition:
|
|
51
|
+
(apply law "|each item has a match| authorizes)
|
|
52
|
+
implies
|
|
53
|
+
(for all (payment of type Payment) =>
|
|
54
|
+
there exists (authorization of type Authorization
|
|
55
|
+
where active Ready authorization) such that
|
|
56
|
+
authorizes authorization payment
|
|
57
|
+
end exists
|
|
58
|
+
end for all)
|
|
59
|
+
end definition
|
|
60
|
+
end law
|
|
61
|
+
|
|
62
|
+
law "|payment and approval identities have separate scopes| is
|
|
63
|
+
definition:
|
|
64
|
+
(for all (value of type Payment) =>
|
|
65
|
+
value = FirstPayment or value = SecondPayment
|
|
66
|
+
end for all)
|
|
67
|
+
and
|
|
68
|
+
(for all (value of type Authorization) =>
|
|
69
|
+
value = FirstApproval or value = SecondApproval
|
|
70
|
+
end for all)
|
|
71
|
+
end definition
|
|
72
|
+
end law
|
|
73
|
+
|
|
74
|
+
law "|an empty condition does not hide required coverage| is
|
|
75
|
+
definition:
|
|
76
|
+
(for all (authorization of type Authorization where false) =>
|
|
77
|
+
false
|
|
78
|
+
end for all)
|
|
79
|
+
and
|
|
80
|
+
(there exists (authorization of type Authorization
|
|
81
|
+
where active Ready authorization) such that
|
|
82
|
+
authorizes authorization FirstPayment
|
|
83
|
+
end exists)
|
|
84
|
+
end definition
|
|
85
|
+
|
|
86
|
+
rationale:
|
|
87
|
+
"A vacuous first clause leaves the approval requirement in force."
|
|
88
|
+
end rationale
|
|
89
|
+
end law
|
|
90
|
+
end unit
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
unit example.records
|
|
2
|
+
|
|
3
|
+
function double
|
|
4
|
+
(value of type record{
|
|
5
|
+
count of type Integer
|
|
6
|
+
}) -> record{
|
|
7
|
+
count of type Integer
|
|
8
|
+
} is
|
|
9
|
+
record{count = value.count + value.count}
|
|
10
|
+
end function
|
|
11
|
+
|
|
12
|
+
function wrap (value of type a) -> record{wrapped of type a} is
|
|
13
|
+
record{wrapped = value}
|
|
14
|
+
end function
|
|
15
|
+
|
|
16
|
+
law doubles is
|
|
17
|
+
definition:
|
|
18
|
+
for all (value of type record{count of type Integer}) =>
|
|
19
|
+
(double value).count must be value.count + value.count
|
|
20
|
+
end for all
|
|
21
|
+
end definition
|
|
22
|
+
|
|
23
|
+
expect double value = expected
|
|
24
|
+
in table (value, expected):
|
|
25
|
+
row record{count = -1}, record{count = -2}
|
|
26
|
+
row record{count = 0}, record{count = 0}
|
|
27
|
+
row record{count = 1}, record{count = 2}
|
|
28
|
+
end table
|
|
29
|
+
end law
|
|
30
|
+
|
|
31
|
+
law wraps is
|
|
32
|
+
definition:
|
|
33
|
+
(wrap true).wrapped must be true
|
|
34
|
+
end definition
|
|
35
|
+
end law
|
|
36
|
+
|
|
37
|
+
law ordered is
|
|
38
|
+
definition:
|
|
39
|
+
record{a = 1, b = true} must be record{b = true, a = 1}
|
|
40
|
+
end definition
|
|
41
|
+
end law
|
|
42
|
+
|
|
43
|
+
law nested is
|
|
44
|
+
definition:
|
|
45
|
+
record{inner = record{count = 4}}.inner.count must be 4
|
|
46
|
+
end definition
|
|
47
|
+
end law
|
|
48
|
+
|
|
49
|
+
law empty is
|
|
50
|
+
definition:
|
|
51
|
+
record{} must be record{}
|
|
52
|
+
end definition
|
|
53
|
+
end law
|
|
54
|
+
end unit
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
unit example.rc1
|
|
2
|
+
|
|
3
|
+
function "|adds the values| (a of type Integer) (b of type Integer) -> Integer is
|
|
4
|
+
a + b
|
|
5
|
+
end function
|
|
6
|
+
|
|
7
|
+
law "|addition preserves the inputs| is
|
|
8
|
+
description:
|
|
9
|
+
"Adding two amounts gives their mathematical sum."
|
|
10
|
+
end description
|
|
11
|
+
|
|
12
|
+
definition:
|
|
13
|
+
for all (a of type Integer) (b of type Integer) =>
|
|
14
|
+
a `|adds the values| b must be a + b
|
|
15
|
+
end for all
|
|
16
|
+
end definition
|
|
17
|
+
|
|
18
|
+
expect "|adds the values| a b = expected
|
|
19
|
+
in table (a, b, expected):
|
|
20
|
+
row -1, 1, 0
|
|
21
|
+
row 0, 0, 0
|
|
22
|
+
row 1, 2, 3
|
|
23
|
+
end table
|
|
24
|
+
end law
|
|
25
|
+
|
|
26
|
+
type Picture is a record{
|
|
27
|
+
fileName of type Text
|
|
28
|
+
width of type Nat
|
|
29
|
+
}
|
|
30
|
+
end type
|
|
31
|
+
|
|
32
|
+
function picture (width of type Nat) -> Picture is
|
|
33
|
+
Picture record{fileName = "photo.jpg", width = width}
|
|
34
|
+
end function
|
|
35
|
+
|
|
36
|
+
function pictureWidth (picture of type Picture) -> Nat is
|
|
37
|
+
picture.width
|
|
38
|
+
end function
|
|
39
|
+
|
|
40
|
+
type Direction is one of
|
|
41
|
+
| North
|
|
42
|
+
| South
|
|
43
|
+
end type
|
|
44
|
+
|
|
45
|
+
mapping directionText of type Direction -> Text is
|
|
46
|
+
row North, "north"
|
|
47
|
+
row South, "south"
|
|
48
|
+
end mapping
|
|
49
|
+
|
|
50
|
+
function "|picture category| (width of type Nat) -> Text is
|
|
51
|
+
first matching case of width:
|
|
52
|
+
when it < 100 then "small"
|
|
53
|
+
when it < 1000 then "medium"
|
|
54
|
+
otherwise "large"
|
|
55
|
+
end case
|
|
56
|
+
end function
|
|
57
|
+
end unit
|
|
@@ -5,9 +5,9 @@ unit example.abilities
|
|
|
5
5
|
-- Gateway says so, and every law about it holds for every lawful handler:
|
|
6
6
|
-- the native one written by hand, and each spec handler below.
|
|
7
7
|
|
|
8
|
-
type Payment is | Approved
|
|
9
|
-
type Receipt is
|
|
10
|
-
type PaymentError is | Refused | TooLarge end
|
|
8
|
+
type Payment is one of | Approved of type Int32 | Declined end type
|
|
9
|
+
type Receipt is a record{ cents :: Int32} end type
|
|
10
|
+
type PaymentError is one of | Refused | TooLarge end type
|
|
11
11
|
|
|
12
12
|
-- A payment gateway. Its laws are obligations on every handler.
|
|
13
13
|
ability Gateway is
|
|
@@ -15,89 +15,91 @@ ability Gateway is
|
|
|
15
15
|
capture :: Int32 -> Receipt
|
|
16
16
|
fee :: Int32
|
|
17
17
|
laws
|
|
18
|
-
law
|
|
19
|
-
definition
|
|
20
|
-
end
|
|
21
|
-
law
|
|
22
|
-
definition
|
|
23
|
-
|
|
24
|
-
(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
end) = true
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
law
|
|
31
|
-
definition
|
|
32
|
-
end
|
|
33
|
-
end
|
|
18
|
+
law "|a receipt is for the amount captured| is
|
|
19
|
+
definition : for all (cents :: Int32) => capture cents = Receipt record{ cents = cents} end for all end definition
|
|
20
|
+
end law
|
|
21
|
+
law "|an approval is for the amount asked| is
|
|
22
|
+
definition :
|
|
23
|
+
for all (cents :: Int32) =>
|
|
24
|
+
(first matching case of authorize cents :
|
|
25
|
+
when Approved approved then approved = cents
|
|
26
|
+
when Declined then true
|
|
27
|
+
end case) = true end for all
|
|
28
|
+
end definition
|
|
29
|
+
end law
|
|
30
|
+
law "|the fee is never negative| is
|
|
31
|
+
definition : fee >= 0 end definition
|
|
32
|
+
end law
|
|
33
|
+
end laws
|
|
34
|
+
end ability
|
|
34
35
|
|
|
35
36
|
-- A fake for tests: it approves small amounts.
|
|
36
37
|
handler fakeGateway for Gateway is
|
|
37
|
-
authorize cents is if cents <= 10000 then Approved cents else Declined end
|
|
38
|
-
capture cents is Receipt cents end
|
|
39
|
-
fee is 30 end
|
|
40
|
-
end
|
|
38
|
+
operation authorize cents is if cents <= 10000 then Approved cents else Declined end if end operation
|
|
39
|
+
operation capture cents is Receipt record{ cents = cents} end operation
|
|
40
|
+
operation fee is 30 end operation
|
|
41
|
+
end handler
|
|
41
42
|
|
|
42
43
|
-- A handler with state: it keeps every amount it captured.
|
|
43
44
|
handler keepingGateway for Gateway with state captured :: List Int32 start [] is
|
|
44
|
-
authorize cents is Approved cents end
|
|
45
|
-
capture cents is ~captured := Cons cents captured; Receipt cents end
|
|
46
|
-
fee is 0 end
|
|
47
|
-
end
|
|
45
|
+
operation authorize cents is Approved cents end operation
|
|
46
|
+
operation capture cents is ~captured := Cons cents captured; Receipt record{ cents = cents} end operation
|
|
47
|
+
operation fee is 0 end operation
|
|
48
|
+
end handler
|
|
48
49
|
|
|
49
50
|
-- A checked definition that uses Gateway. Its row, {Gateway}, is inferred.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
end
|
|
55
|
-
end
|
|
51
|
+
procedure checkout (cents :: Int32) -> Bool is
|
|
52
|
+
first matching case of authorize cents :
|
|
53
|
+
when Approved amount then (first matching case of capture amount : when Receipt record{ cents = paid} then paid = cents end case)
|
|
54
|
+
when Declined then false
|
|
55
|
+
end case
|
|
56
|
+
end procedure
|
|
56
57
|
|
|
57
58
|
-- A native adapter that uses Gateway: it gets a handler as its first
|
|
58
59
|
-- argument.
|
|
59
60
|
charge :: Int32 -> Bool uses Gateway
|
|
60
61
|
|
|
61
62
|
-- A definition that can fail says what it fails with.
|
|
62
|
-
|
|
63
|
+
procedure pay (cents :: Int32) -> Receipt uses Gateway fails with PaymentError is
|
|
63
64
|
if cents > 100000 then raise TooLarge else
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
end
|
|
68
|
-
end
|
|
65
|
+
first matching case of authorize cents :
|
|
66
|
+
when Approved amount then capture amount
|
|
67
|
+
when Declined then raise Refused
|
|
68
|
+
end case end if
|
|
69
|
+
end procedure
|
|
69
70
|
|
|
70
71
|
-- Counting calls is part of the claim, so the law names a recording
|
|
71
72
|
-- handler. Which gateway it records is left to the harness: each lawful one.
|
|
72
|
-
law
|
|
73
|
-
definition
|
|
74
|
-
|
|
75
|
-
(if checkout cents then calls of capture with (cents)
|
|
76
|
-
end
|
|
77
|
-
end
|
|
73
|
+
law "|checkout captures once, and only when it succeeds| using recording Gateway is
|
|
74
|
+
definition :
|
|
75
|
+
for all (cents :: Int32) =>
|
|
76
|
+
(if checkout cents then calls of capture with (cents) = 1 else calls of capture = 0 end if) = true end for all
|
|
77
|
+
end definition
|
|
78
|
+
end law
|
|
78
79
|
|
|
79
|
-
law
|
|
80
|
-
definition
|
|
81
|
-
|
|
82
|
-
(if charge cents then calls of capture with (cents)
|
|
83
|
-
end
|
|
84
|
-
end
|
|
80
|
+
law "|charge captures once, and only when it succeeds| using recording Gateway is
|
|
81
|
+
definition :
|
|
82
|
+
for all (cents :: Int32) =>
|
|
83
|
+
(if charge cents then calls of capture with (cents) = 1 else calls of capture = 0 end if) = true end for all
|
|
84
|
+
end definition
|
|
85
|
+
end law
|
|
85
86
|
|
|
86
87
|
-- This law needs the fake's limit, so the fake is part of the claim.
|
|
87
|
-
law
|
|
88
|
-
definition
|
|
89
|
-
|
|
90
|
-
end
|
|
91
|
-
end
|
|
88
|
+
law "|large payments fail| using fakeGateway is
|
|
89
|
+
definition :
|
|
90
|
+
for all (cents :: Int32 where cents > 100000) => prelude.attempt (pay cents) = Left TooLarge end for all
|
|
91
|
+
end definition
|
|
92
|
+
end law
|
|
92
93
|
|
|
93
|
-
law
|
|
94
|
-
definition
|
|
95
|
-
|
|
96
|
-
prelude.attempt (pay cents) = Left Refused
|
|
97
|
-
end
|
|
98
|
-
end
|
|
94
|
+
law "|a declined payment is refused| using fakeGateway is
|
|
95
|
+
definition :
|
|
96
|
+
for all (cents :: Int32 where cents > 10000 && cents <= 100000) =>
|
|
97
|
+
prelude.attempt (pay cents) = Left Refused end for all
|
|
98
|
+
end definition
|
|
99
|
+
end law
|
|
99
100
|
|
|
100
101
|
-- Without `using`, a law runs under each lawful handler in turn.
|
|
101
|
-
law
|
|
102
|
-
definition
|
|
103
|
-
end
|
|
102
|
+
law "|the fee is small| is
|
|
103
|
+
definition : fee <= 100 end definition
|
|
104
|
+
end law
|
|
105
|
+
end unit
|