lawspec 0.25.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +109 -1
- package/NORMALIZATION-LICENSES.txt +296 -0
- package/README.md +13 -4
- package/api.mjs +0 -8
- package/bin/lawspec.mjs +114 -79
- package/build.json +99 -117
- package/core.wasm +2 -2
- package/core_jsffi.js +5 -0
- package/doctor.mjs +14 -4
- package/examples/native-payments/clojure/lawspec.json +1 -1
- package/examples/native-payments/csharp/lawspec.json +1 -1
- package/examples/native-payments/elixir/lawspec.json +1 -1
- package/examples/native-payments/erlang/lawspec.json +1 -1
- package/examples/native-payments/fsharp/lawspec.json +1 -1
- package/examples/native-payments/gleam/lawspec.json +1 -1
- package/examples/native-payments/go/lawspec.json +1 -1
- package/examples/native-payments/groovy/lawspec.json +1 -1
- package/examples/native-payments/haskell/lawspec.json +1 -1
- package/examples/native-payments/java/lawspec.json +1 -1
- package/examples/native-payments/javascript/lawspec.json +1 -1
- package/examples/native-payments/kotlin/lawspec.json +1 -1
- package/examples/native-payments/prolog/lawspec.json +20 -0
- package/examples/native-payments/prolog/src/domain/payments.pl +23 -0
- package/examples/native-payments/prolog/tests/domain/payment_generators.pl +17 -0
- package/examples/native-payments/python/lawspec.json +1 -1
- package/examples/native-payments/rust/lawspec.json +1 -1
- package/examples/native-payments/scala/lawspec.json +1 -1
- package/examples/native-payments/swift/lawspec.json +1 -1
- package/examples/native-payments/typescript/lawspec.json +1 -1
- package/examples/packages/orders/lawspec.json +1 -1
- package/examples/packages/orders/orders.lawspec +27 -25
- package/examples/packages/shop-domain/src/domain.lawspec +15 -14
- package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-1/src/rates.lawspec +1 -0
- package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
- package/examples/packages/shop-tax-2/src/rates.lawspec +1 -0
- package/examples/rc1/authorization-snapshots.lawspec +24 -23
- package/examples/rc1/banner-filenames.lawspec +7 -6
- package/examples/rc1/bit-codec.lawspec +12 -11
- package/examples/rc1/bounded-witness-search.lawspec +52 -45
- package/examples/rc1/conflicting-requirements.lawspec +8 -7
- package/examples/rc1/constructive-witnesses.lawspec +40 -33
- package/examples/rc1/documentary-review.lawspec +9 -8
- package/examples/rc1/empty-domains.lawspec +16 -15
- package/examples/rc1/finite-integer-domains.lawspec +24 -23
- package/examples/rc1/law-metadata.lawspec +6 -5
- package/examples/rc1/native-payments.lawspec +17 -16
- package/examples/rc1/nesting/rrr.lawspec +4 -3
- package/examples/rc1/nesting/rru.lawspec +7 -6
- package/examples/rc1/nesting/rur.lawspec +7 -6
- package/examples/rc1/nesting/ruu.lawspec +10 -9
- package/examples/rc1/nesting/urr.lawspec +7 -6
- package/examples/rc1/nesting/uru.lawspec +10 -9
- package/examples/rc1/nesting/uur.lawspec +10 -9
- package/examples/rc1/nesting/uuu.lawspec +13 -12
- package/examples/rc1/order-workflow.lawspec +18 -17
- package/examples/rc1/parcel-measurements.lawspec +14 -13
- package/examples/rc1/picture-records.lawspec +6 -5
- package/examples/rc1/picture-tuples.lawspec +6 -5
- package/examples/rc1/record-payloads.lawspec +12 -11
- package/examples/rc1/recursive-records.lawspec +11 -10
- package/examples/rc1/refund-conformance.lawspec +7 -6
- package/examples/rc1/refund-policy.lawspec +15 -14
- package/examples/rc1/requirement-composition.lawspec +26 -25
- package/examples/rc1/structural-records.lawspec +13 -12
- package/examples/rc1-authoring.lawspec +6 -5
- package/examples/specs/abilities.lawspec +67 -65
- package/examples/specs/ability_imports.lawspec +58 -57
- package/examples/specs/actors.lawspec +23 -22
- package/examples/specs/algebra.lawspec +142 -141
- package/examples/specs/approvals.lawspec +19 -18
- package/examples/specs/async_bindings.lawspec +10 -9
- package/examples/specs/async_fetch.lawspec +12 -11
- package/examples/specs/atoi_codec.lawspec +16 -15
- package/examples/specs/boolean_flags.lawspec +10 -9
- package/examples/specs/builtins.lawspec +73 -72
- package/examples/specs/canonical_url.lawspec +14 -13
- package/examples/specs/collections.lawspec +153 -154
- package/examples/specs/composition.lawspec +70 -66
- package/examples/specs/concurrent_collections.lawspec +8 -7
- package/examples/specs/consistency.lawspec +4 -3
- package/examples/specs/contracts.lawspec +80 -79
- package/examples/specs/crypto.lawspec +26 -25
- package/examples/specs/ctc.lawspec +148 -0
- package/examples/specs/currying.lawspec +32 -31
- package/examples/specs/data_types.lawspec +50 -49
- package/examples/specs/distribution.lawspec +57 -56
- package/examples/specs/domain_modeling.lawspec +68 -47
- package/examples/specs/durations.lawspec +39 -38
- package/examples/specs/equivalent.lawspec +25 -24
- package/examples/specs/failures.lawspec +64 -63
- package/examples/specs/finite_data.lawspec +32 -31
- package/examples/specs/flow_stack.lawspec +64 -63
- package/examples/specs/gadt_expressions.lawspec +50 -49
- package/examples/specs/generation.lawspec +115 -114
- package/examples/specs/handler_bindings.lawspec +42 -40
- package/examples/specs/handles.lawspec +2 -1
- package/examples/specs/harness.lawspec +45 -43
- package/examples/specs/harness_testing.lawspec +18 -13
- package/examples/specs/indexed_arithmetic.lawspec +401 -126
- package/examples/specs/indexed_families.lawspec +182 -66
- package/examples/specs/keywords.lawspec +22 -21
- package/examples/specs/limits.lawspec +7 -6
- package/examples/specs/list_contracts.lawspec +121 -120
- package/examples/specs/list_refinements.lawspec +43 -42
- package/examples/specs/lists.lawspec +45 -44
- package/examples/specs/matchers.lawspec +79 -78
- package/examples/specs/matching.lawspec +43 -42
- package/examples/specs/mixed_inputs.lawspec +25 -24
- package/examples/specs/models.lawspec +44 -43
- package/examples/specs/parse_port.lawspec +28 -27
- package/examples/specs/payments.lawspec +55 -54
- package/examples/specs/railway.lawspec +59 -58
- package/examples/specs/records.lawspec +149 -148
- package/examples/specs/records_inner.lawspec +5 -4
- package/examples/specs/recursive_refinements.lawspec +30 -29
- package/examples/specs/refined_definitions.lawspec +24 -23
- package/examples/specs/refinements.lawspec +58 -57
- package/examples/specs/resilience.lawspec +161 -160
- package/examples/specs/resources.lawspec +61 -60
- package/examples/specs/scalar_adapters.lawspec +60 -59
- package/examples/specs/scalar_catalog.lawspec +165 -164
- package/examples/specs/scalars.lawspec +78 -77
- package/examples/specs/scheduling_order.lawspec +8 -7
- package/examples/specs/scheduling_overlap.lawspec +5 -4
- package/examples/specs/sequences.lawspec +66 -65
- package/examples/specs/sessions.lawspec +13 -12
- package/examples/specs/slug.lawspec +16 -15
- package/examples/specs/sum_refinements.lawspec +42 -41
- package/examples/specs/tables.lawspec +29 -28
- package/examples/specs/total_functions.lawspec +71 -70
- package/examples/specs/workflows.lawspec +101 -100
- package/examples-command.mjs +7 -7
- package/explain.mjs +6 -4
- package/expression-presentation.mjs +8 -8
- package/files.mjs +1 -1
- package/index.d.ts +10 -528
- package/launcher.mjs +9 -2
- package/lsp.mjs +10 -6
- package/native-evidence.mjs +25 -12
- package/native-examples.mjs +3 -3
- package/package.json +2 -1
- package/starter-rc1.lawspec +6 -5
- package/starter.lawspec +16 -15
- package/templates.mjs +88 -5
- package/test-command.mjs +40 -4
- package/migrate.mjs +0 -135
- package/migration-bindings.mjs +0 -86
- package/migration-cases.mjs +0 -45
- package/migration-collections.mjs +0 -54
- package/migration-correspondence.mjs +0 -275
- package/migration-generation.mjs +0 -99
- package/migration-laws.mjs +0 -102
- package/migration-records.mjs +0 -166
- package/migration-source.mjs +0 -247
- package/migration-write.mjs +0 -156
|
@@ -5,187 +5,188 @@ import example.records.inner as inner
|
|
|
5
5
|
-- Records: a type with one constructor has fields that code reads by name
|
|
6
6
|
-- (account.balance) and replaces by name (account with balance = 0).
|
|
7
7
|
|
|
8
|
-
type Account is
|
|
9
|
-
|
|
8
|
+
type Account is a record{
|
|
9
|
+
|
|
10
10
|
owner :: Text
|
|
11
11
|
balance :: Int64
|
|
12
|
-
holds :: List Int64
|
|
13
|
-
end
|
|
12
|
+
holds :: List Int64}
|
|
13
|
+
end type
|
|
14
|
+
|
|
15
|
+
type Ledger is a record{
|
|
14
16
|
|
|
15
|
-
type Ledger is
|
|
16
|
-
Ledger
|
|
17
17
|
account :: Account
|
|
18
|
-
entries :: Int32
|
|
19
|
-
end
|
|
18
|
+
entries :: Int32}
|
|
19
|
+
end type
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
if a.balance >= 0 && a.balance <= 1000000000 then a with balance = prelude.Int64 (a.balance + amount) else a
|
|
23
|
-
end
|
|
21
|
+
function deposit (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) -> Account is
|
|
22
|
+
if a.balance >= 0 && a.balance <= 1000000000 then a with balance = prelude.Int64 (a.balance + amount) else a end if
|
|
23
|
+
end function
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
function rename (l :: Ledger) (name :: Text) -> Ledger is
|
|
26
26
|
l with account = (l.account with owner = name), entries = 0
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
law
|
|
30
|
-
definition
|
|
31
|
-
|
|
32
|
-
(deposit (Account owner start []) amount).balance = start + amount
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
law
|
|
37
|
-
definition
|
|
38
|
-
|
|
39
|
-
(deposit a amount).owner = a.owner and (deposit a amount).holds = a.holds
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
law
|
|
44
|
-
definition
|
|
45
|
-
|
|
46
|
-
end
|
|
47
|
-
end
|
|
27
|
+
end function
|
|
28
|
+
|
|
29
|
+
law "|a deposit adds to the balance| is
|
|
30
|
+
definition :
|
|
31
|
+
for all (owner :: Text) (start :: Int64 where start >= 0 && start <= 1000000000) (amount :: Int64 where amount >= 0 && amount <= 1000000) =>
|
|
32
|
+
(deposit (Account record{ owner = owner , balance = start , holds = []}) amount).balance = start + amount end for all
|
|
33
|
+
end definition
|
|
34
|
+
end law
|
|
35
|
+
|
|
36
|
+
law "|a deposit keeps the owner and holds| is
|
|
37
|
+
definition :
|
|
38
|
+
for all (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) =>
|
|
39
|
+
(deposit a amount).owner = a.owner and (deposit a amount).holds = a.holds end for all
|
|
40
|
+
end definition
|
|
41
|
+
end law
|
|
42
|
+
|
|
43
|
+
law "|renaming reads back| is
|
|
44
|
+
definition :
|
|
45
|
+
for all (l :: Ledger) (name :: Text) => (rename l name).account.owner = name and (rename l name).entries = 0 end for all
|
|
46
|
+
end definition
|
|
47
|
+
end law
|
|
48
48
|
|
|
49
49
|
-- A definition may fail without any ability, and another may attempt it.
|
|
50
|
-
type Refusal is | Overdrawn | Unfunded end
|
|
50
|
+
type Refusal is one of | Overdrawn | Unfunded end type
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
procedure withdraw (a :: Account) (amount :: Int64) -> Account fails with Refusal is
|
|
53
53
|
if amount < 0 then raise Unfunded else
|
|
54
|
-
if amount > a.balance then raise Overdrawn else a with balance = prelude.Int64 (a.balance - amount)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
law
|
|
65
|
-
definition
|
|
66
|
-
|
|
67
|
-
settled (Account owner start []) amount = Right (prelude.Int64 (start - amount))
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
law
|
|
72
|
-
definition
|
|
73
|
-
|
|
74
|
-
settled (Account owner start []) amount = Left Overdrawn and settled (Account owner start []) (0 - amount) = Left Unfunded
|
|
75
|
-
end
|
|
76
|
-
end
|
|
54
|
+
if amount > a.balance then raise Overdrawn else a with balance = prelude.Int64 (a.balance - amount) end if end if
|
|
55
|
+
end procedure
|
|
56
|
+
|
|
57
|
+
function settled (a :: Account) (amount :: Int64) -> Either Refusal Int64 is
|
|
58
|
+
first matching case of prelude.attempt (withdraw a amount) :
|
|
59
|
+
when Left refusal then Left refusal
|
|
60
|
+
when Right after then Right after.balance
|
|
61
|
+
end case
|
|
62
|
+
end function
|
|
63
|
+
|
|
64
|
+
law "|a withdrawal within the balance leaves the rest| is
|
|
65
|
+
definition :
|
|
66
|
+
for all (owner :: Text) (start :: Int64 where start >= 0) (amount :: Int64 where amount >= 0 && amount <= start) =>
|
|
67
|
+
settled (Account record{ owner = owner , balance = start , holds = []}) amount = Right (prelude.Int64 (start - amount)) end for all
|
|
68
|
+
end definition
|
|
69
|
+
end law
|
|
70
|
+
|
|
71
|
+
law "|a withdrawal beyond the balance is refused| is
|
|
72
|
+
definition :
|
|
73
|
+
for all (owner :: Text) (start :: Int64 where start >= 0 && start < 1000) (amount :: Int64 where amount >= 1000) =>
|
|
74
|
+
settled (Account record{ owner = owner , balance = start , holds = []}) amount = Left Overdrawn and settled (Account record{ owner = owner , balance = start , holds = []}) (0 - amount) = Left Unfunded end for all
|
|
75
|
+
end definition
|
|
76
|
+
end law
|
|
77
77
|
|
|
78
78
|
-- A refinement through a definition still guides generation: sample
|
|
79
79
|
-- names come from the pattern, so the law does not starve its filter.
|
|
80
|
-
|
|
81
|
-
prelude.length name
|
|
82
|
-
end
|
|
80
|
+
function validOwner (name :: Text) -> Bool is
|
|
81
|
+
prelude.length name = 9 && name matches regex "acct-[0-9]{4}"
|
|
82
|
+
end function
|
|
83
83
|
|
|
84
|
-
law
|
|
85
|
-
definition
|
|
86
|
-
|
|
87
|
-
end
|
|
88
|
-
end
|
|
84
|
+
law "|a renamed ledger keeps a valid owner| is
|
|
85
|
+
definition :
|
|
86
|
+
for all (l :: Ledger) (name :: Text where validOwner name) => validOwner (rename l name).account.owner end for all
|
|
87
|
+
end definition
|
|
88
|
+
end law
|
|
89
89
|
|
|
90
90
|
-- A literal takes its type from where it is used: 0 is an Int64 here.
|
|
91
|
-
|
|
92
|
-
let start = (
|
|
93
|
-
if start < 0 then 0 else start
|
|
94
|
-
end
|
|
91
|
+
function opening (a :: Maybe Account) -> Int64 is
|
|
92
|
+
let start = (first matching case of a : when Nothing then 0 when Just account then account.balance end case) in
|
|
93
|
+
if start < 0 then 0 else start end if end let
|
|
94
|
+
end function
|
|
95
95
|
|
|
96
|
-
law
|
|
97
|
-
definition
|
|
98
|
-
end
|
|
96
|
+
law "|an absent account opens at zero| is
|
|
97
|
+
definition : opening Nothing = 0 end definition
|
|
98
|
+
end law
|
|
99
99
|
|
|
100
100
|
-- Recursion on an integer that moves toward a bound is total.
|
|
101
|
-
|
|
102
|
-
if n
|
|
103
|
-
end
|
|
101
|
+
function countdown (n :: Whole) -> List Whole is
|
|
102
|
+
if n = 0 then [] else Cons n (countdown (n - 1)) end if
|
|
103
|
+
end function
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
function evensBelow (i :: Int32) (n :: Int32) -> List Int32 is
|
|
106
106
|
if i >= n || i < 0 then [] else
|
|
107
|
-
(if prelude.rem i 2
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
law
|
|
111
|
-
definition
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
law
|
|
115
|
-
definition
|
|
116
|
-
|
|
117
|
-
prelude.length (evensBelow 0 n) = prelude.quot (n + 1) 2
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
law
|
|
122
|
-
definition
|
|
123
|
-
|
|
124
|
-
end
|
|
125
|
-
example
|
|
107
|
+
(if prelude.rem i 2 = 0 then Cons i (evensBelow (prelude.Int32 (i + 1)) n) else evensBelow (prelude.Int32 (i + 1)) n end if) end if
|
|
108
|
+
end function
|
|
109
|
+
|
|
110
|
+
law "|a countdown has n items| is
|
|
111
|
+
definition : for all (n :: Whole where n <= 50) => prelude.length (countdown n) = n end for all end definition
|
|
112
|
+
end law
|
|
113
|
+
|
|
114
|
+
law "|the evens below n are half of the range| is
|
|
115
|
+
definition :
|
|
116
|
+
for all (n :: Int32 where n >= 0 && n <= 40) =>
|
|
117
|
+
prelude.length (evensBelow 0 n) = prelude.quot (n + 1) 2 end for all
|
|
118
|
+
end definition
|
|
119
|
+
end law
|
|
120
|
+
|
|
121
|
+
law "|range counts from the first up to the last| is
|
|
122
|
+
definition :
|
|
123
|
+
for all (n :: Int32 where n >= 0 && n <= 40) => prelude.length (prelude.range 0 n) = n end for all
|
|
124
|
+
end definition
|
|
125
|
+
example "|three| :
|
|
126
126
|
n = 3
|
|
127
127
|
expect prelude.range 0 n = [0, 1, 2]
|
|
128
|
-
end
|
|
129
|
-
end
|
|
128
|
+
end example
|
|
129
|
+
end law
|
|
130
130
|
|
|
131
131
|
-- The prover keeps what guards say about calls, lengths and remainders.
|
|
132
|
-
|
|
133
|
-
if prelude.length xs <= 100 then prelude.Int32 (prelude.length xs) else 100
|
|
134
|
-
end
|
|
132
|
+
function clip (xs :: List Int32) -> Int32 is
|
|
133
|
+
if prelude.length xs <= 100 then prelude.Int32 (prelude.length xs) else 100 end if
|
|
134
|
+
end function
|
|
135
135
|
|
|
136
|
-
|
|
137
|
-
if k >= 1 && p >= 0 then prelude.Int32 (prelude.rem p k) else 0
|
|
138
|
-
end
|
|
136
|
+
function owner (p :: Int32) (k :: Int32) -> Int32 is
|
|
137
|
+
if k >= 1 && p >= 0 then prelude.Int32 (prelude.rem p k) else 0 end if
|
|
138
|
+
end function
|
|
139
139
|
|
|
140
|
-
law
|
|
141
|
-
definition
|
|
142
|
-
end
|
|
140
|
+
law "|a clipped length is at most 100| is
|
|
141
|
+
definition : for all (xs :: List Int32) => (clip xs <= 100 && clip xs >= 0) = true end for all end definition
|
|
142
|
+
end law
|
|
143
143
|
|
|
144
|
-
law
|
|
145
|
-
definition
|
|
146
|
-
|
|
147
|
-
end
|
|
148
|
-
end
|
|
144
|
+
law "|an owner is one of the k| is
|
|
145
|
+
definition :
|
|
146
|
+
for all (p :: Int32 where p >= 0) (k :: Int32 where k >= 1 && k <= 64) => (owner p k >= 0 && owner p k < k) = true end for all
|
|
147
|
+
end definition
|
|
148
|
+
end law
|
|
149
149
|
|
|
150
150
|
-- Arithmetic into a bounded type, by name.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
law
|
|
156
|
-
definition
|
|
157
|
-
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
law
|
|
162
|
-
definition
|
|
163
|
-
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
law
|
|
168
|
-
definition
|
|
169
|
-
|
|
170
|
-
total a b = (if a + b > Int32.max || a + b < Int32.min then Nothing else Just (prelude.Int32 (a + b)))
|
|
171
|
-
end
|
|
172
|
-
end
|
|
151
|
+
function nextEpoch (e :: Int16) -> Int16 is prelude.wrapping (e + 1) end function
|
|
152
|
+
function topUp (level :: UInt8) -> UInt8 is prelude.saturating (level + 10) end function
|
|
153
|
+
function total (a :: Int32) (b :: Int32) -> Maybe Int32 is prelude.bounded (a + b) end function
|
|
154
|
+
|
|
155
|
+
law "|epochs wrap around| is
|
|
156
|
+
definition :
|
|
157
|
+
for all (e :: Int16) => nextEpoch e = (if e = Int16.max then Int16.min else prelude.Int16 (e + 1) end if) end for all
|
|
158
|
+
end definition
|
|
159
|
+
end law
|
|
160
|
+
|
|
161
|
+
law "|a top up stops at the top| is
|
|
162
|
+
definition :
|
|
163
|
+
for all (level :: UInt8) => topUp level = (if level >= 245 then 255 else prelude.UInt8 (level + 10) end if) end for all
|
|
164
|
+
end definition
|
|
165
|
+
end law
|
|
166
|
+
|
|
167
|
+
law "|a total that overflows is Nothing| is
|
|
168
|
+
definition :
|
|
169
|
+
for all (a :: Int32) (b :: Int32) =>
|
|
170
|
+
total a b = (if a + b > Int32.max || a + b < Int32.min then Nothing else Just (prelude.Int32 (a + b)) end if) end for all
|
|
171
|
+
end definition
|
|
172
|
+
end law
|
|
173
173
|
|
|
174
174
|
-- A refinement shapes generation: texts a regex must match are drawn from
|
|
175
175
|
-- the regex, through the definition that says so, not filtered from any
|
|
176
176
|
-- text.
|
|
177
|
-
|
|
177
|
+
function validTopic (name :: Text) -> Bool is
|
|
178
178
|
let n = prelude.length name in
|
|
179
|
-
n >= 1 && n <= 249 && name != "." && name != ".." && name matches regex "[a-zA-Z0-9._-]+"
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
law
|
|
183
|
-
definition
|
|
184
|
-
|
|
185
|
-
(prelude.length t >= 1 && prelude.length t <= 249 && n <= 64) = true
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
law
|
|
190
|
-
definition
|
|
191
|
-
end
|
|
179
|
+
n >= 1 && n <= 249 && name != "." && name != ".." && name matches regex "[a-zA-Z0-9._-]+" end let
|
|
180
|
+
end function
|
|
181
|
+
|
|
182
|
+
law "|a valid topic name keeps to its characters| is
|
|
183
|
+
definition :
|
|
184
|
+
for all (t :: Text where validTopic t) (n :: Int32 where n >= 1 && n <= 64) =>
|
|
185
|
+
(prelude.length t >= 1 && prelude.length t <= 249 && n <= 64) = true end for all
|
|
186
|
+
end definition
|
|
187
|
+
end law
|
|
188
|
+
|
|
189
|
+
law "|a unit may sit above its package's other units| is
|
|
190
|
+
definition : for all (x :: Int32) => inner.doubled x = x + x end for all end definition
|
|
191
|
+
end law
|
|
192
|
+
end unit
|
|
@@ -3,8 +3,9 @@ unit example.records.inner
|
|
|
3
3
|
-- A unit inside another unit's name: on Python, example.records is then its
|
|
4
4
|
-- package's __init__.py, and this unit is example/records/inner.py.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
function doubled (x :: Int32) -> Integer is x * 2 end function
|
|
7
7
|
|
|
8
|
-
law
|
|
9
|
-
definition
|
|
10
|
-
end
|
|
8
|
+
law "|doubling is even| is
|
|
9
|
+
definition : for all (x :: Int32) => prelude.rem (doubled x) 2 = 0 end for all end definition
|
|
10
|
+
end law
|
|
11
|
+
end unit
|
|
@@ -2,40 +2,41 @@ unit example.recursive_refinements
|
|
|
2
2
|
|
|
3
3
|
-- Refining a type argument follows its stored occurrences through recursion.
|
|
4
4
|
-- The fixed Int8 field remains independent of the refined argument.
|
|
5
|
-
type Tree (a :: Type) is
|
|
6
|
-
Leaf value :: a fixed :: Int8
|
|
7
|
-
Fork left :: Tree a right :: Tree a
|
|
8
|
-
end
|
|
5
|
+
type Tree (a :: Type) is one of
|
|
6
|
+
Leaf of type record{ value :: a fixed :: Int8}
|
|
7
|
+
Fork of type record{ left :: Tree a right :: Tree a}
|
|
8
|
+
end type
|
|
9
9
|
|
|
10
|
-
refinement Positive is (value :: Int8 where value > 0) end
|
|
10
|
+
refinement Positive is (value :: Int8 where value > 0) end refinement
|
|
11
11
|
|
|
12
12
|
-- The payload guarantee is available when proving recursive definitions.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
end
|
|
18
|
-
end
|
|
13
|
+
function positiveSum (tree :: Tree Positive) -> (total :: Integer where total > 0) is
|
|
14
|
+
first matching case of tree :
|
|
15
|
+
when Leaf record{ value = value , fixed = fixed} then value + 0
|
|
16
|
+
when Fork record{ left = left , right = right} then positiveSum left + positiveSum right
|
|
17
|
+
end case
|
|
18
|
+
end function
|
|
19
19
|
|
|
20
|
-
law
|
|
21
|
-
definition
|
|
22
|
-
|
|
23
|
-
end
|
|
24
|
-
example
|
|
25
|
-
tree = Fork (Leaf 1 -128) (Fork (Leaf 2 0) (Leaf 3 -1))
|
|
20
|
+
law "|positive leaves have a positive total| is
|
|
21
|
+
definition :
|
|
22
|
+
for all (tree :: Tree Positive) => positiveSum tree > 0 end for all
|
|
23
|
+
end definition
|
|
24
|
+
example "|fixed fields may be negative| :
|
|
25
|
+
tree = Fork record{ left = (Leaf record{ value = 1 , fixed = -128}) , right = (Fork record{ left = (Leaf record{ value = 2 , fixed = 0}) , right = (Leaf record{ value = 3 , fixed = -1})})}
|
|
26
26
|
expect positiveSum tree = 6
|
|
27
|
-
end
|
|
28
|
-
end
|
|
27
|
+
end example
|
|
28
|
+
end law
|
|
29
29
|
|
|
30
30
|
-- Dependent predicates retain the enclosing input, even below several forks.
|
|
31
|
-
law
|
|
32
|
-
definition
|
|
33
|
-
|
|
34
|
-
(tree :: Tree (value :: Int8 where value > threshold))
|
|
35
|
-
end
|
|
36
|
-
example
|
|
31
|
+
law "|every stored argument exceeds the threshold| is
|
|
32
|
+
definition :
|
|
33
|
+
for all (threshold :: Int8 where threshold < 127)
|
|
34
|
+
(tree :: Tree (value :: Int8 where value > threshold)) => tree = tree end for all
|
|
35
|
+
end definition
|
|
36
|
+
example "|nested threshold| :
|
|
37
37
|
threshold = 4
|
|
38
|
-
tree = Fork (Leaf 5 -128) (Leaf 6 0)
|
|
39
|
-
expect tree = Fork (Leaf 5 -128) (Leaf 6 0)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
38
|
+
tree = Fork record{ left = (Leaf record{ value = 5 , fixed = -128}) , right = (Leaf record{ value = 6 , fixed = 0})}
|
|
39
|
+
expect tree = Fork record{ left = (Leaf record{ value = 5 , fixed = -128}) , right = (Leaf record{ value = 6 , fixed = 0})}
|
|
40
|
+
end example
|
|
41
|
+
end law
|
|
42
|
+
end unit
|
|
@@ -2,43 +2,44 @@ unit example.refined_definitions
|
|
|
2
2
|
|
|
3
3
|
refinement Nonzero (T :: Type) requires Integer T is
|
|
4
4
|
(value :: T where value != 0)
|
|
5
|
-
end
|
|
5
|
+
end refinement
|
|
6
6
|
|
|
7
7
|
-- The compiler proves division is defined under the input contract.
|
|
8
8
|
-- Native callers are checked before the body runs.
|
|
9
|
-
|
|
9
|
+
function reciprocal (x :: Nonzero a) -> Rational
|
|
10
10
|
requires Integer a
|
|
11
11
|
is
|
|
12
12
|
1 / x
|
|
13
|
-
end
|
|
13
|
+
end function
|
|
14
14
|
|
|
15
15
|
-- Arithmetic promotes before this explicit, proved narrowing conversion.
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
function increment (x :: Int8 where x < 127)
|
|
17
|
+
-> (result :: Int8 where result > x)
|
|
18
18
|
is
|
|
19
19
|
prelude.Int8 (x + 1)
|
|
20
|
-
end
|
|
20
|
+
end function
|
|
21
21
|
|
|
22
22
|
-- Later argument refinements can depend on earlier arguments.
|
|
23
|
-
|
|
23
|
+
function inverseGap (x :: Int8) (y :: Int8 where y > x) -> Rational is
|
|
24
24
|
1 / (y - x)
|
|
25
|
-
end
|
|
25
|
+
end function
|
|
26
26
|
|
|
27
|
-
law
|
|
28
|
-
definition
|
|
29
|
-
example
|
|
30
|
-
end
|
|
27
|
+
law "|exact reciprocal| is
|
|
28
|
+
definition : for all (x :: Nonzero Int8) => reciprocal x = 1 / x end for all end definition
|
|
29
|
+
example "|one half| : x = 2 expect reciprocal x = rational(1, 2) end example
|
|
30
|
+
end law
|
|
31
31
|
|
|
32
|
-
law
|
|
33
|
-
definition
|
|
34
|
-
example
|
|
35
|
-
end
|
|
32
|
+
law "|checked increment| is
|
|
33
|
+
definition : for all (x :: Int8 where x < 127) => increment x = x + 1 end for all end definition
|
|
34
|
+
example "|largest admitted input| : x = 126 expect increment x = 127 end example
|
|
35
|
+
end law
|
|
36
36
|
|
|
37
|
-
law
|
|
38
|
-
definition
|
|
39
|
-
inverseGap x y = 1 / (y - x)
|
|
40
|
-
end
|
|
41
|
-
example
|
|
37
|
+
law "|dependent gap| is
|
|
38
|
+
definition : for all (x :: Int8) (y :: Int8 where y > x) =>
|
|
39
|
+
inverseGap x y = 1 / (y - x) end for all
|
|
40
|
+
end definition
|
|
41
|
+
example "|difference exceeds Int8| :
|
|
42
42
|
x = -128 y = 127 expect inverseGap x y = rational(1, 255)
|
|
43
|
-
end
|
|
44
|
-
end
|
|
43
|
+
end example
|
|
44
|
+
end law
|
|
45
|
+
end unit
|
|
@@ -3,84 +3,85 @@ unit example.refinements
|
|
|
3
3
|
refinement Between (T :: Type) (minimum :: T) (maximum :: T)
|
|
4
4
|
requires Ordered T is
|
|
5
5
|
(value :: T where minimum <= value && value <= maximum)
|
|
6
|
-
end
|
|
6
|
+
end refinement
|
|
7
7
|
|
|
8
8
|
refinement Positive (T :: Type) requires Integer T is
|
|
9
9
|
(value :: T where value > 0)
|
|
10
|
-
end
|
|
10
|
+
end refinement
|
|
11
11
|
|
|
12
12
|
refinement AdditionOverflows (T :: Type) (left :: T)
|
|
13
13
|
requires Integer T Bounded T is
|
|
14
14
|
(right :: T where left + right > T.max)
|
|
15
|
-
end
|
|
15
|
+
end refinement
|
|
16
16
|
|
|
17
17
|
add :: (x :: Int8)
|
|
18
18
|
-> (y :: AdditionOverflows Int8 x)
|
|
19
|
-
-> (result :: Integer where result
|
|
20
|
-
successor :: (x :: Int8) -> (result :: Integer where result
|
|
19
|
+
-> (result :: Integer where result = x + y)
|
|
20
|
+
successor :: (x :: Int8) -> (result :: Integer where result = x + 1)
|
|
21
21
|
count :: (text :: Text where prelude.length text > 0)
|
|
22
|
-
-> (result :: Integer where result
|
|
23
|
-
preserve :: (x :: UInt64) -> (result :: Integer where result
|
|
24
|
-
positive :: (x :: Positive Int8) -> (result :: Positive Int8 where result
|
|
22
|
+
-> (result :: Integer where result = prelude.length text)
|
|
23
|
+
preserve :: (x :: UInt64) -> (result :: Integer where result = x)
|
|
24
|
+
positive :: (x :: Positive Int8) -> (result :: Positive Int8 where result = x)
|
|
25
25
|
|
|
26
|
-
law
|
|
27
|
-
definition
|
|
28
|
-
example
|
|
29
|
-
end
|
|
26
|
+
law "|maximum input| is
|
|
27
|
+
definition : for all (x :: Int8) => successor x = x + 1 end for all end definition
|
|
28
|
+
example "|beyond Int8| : x = 127 expect successor x = 128 end example
|
|
29
|
+
end law
|
|
30
30
|
|
|
31
|
-
law
|
|
32
|
-
definition
|
|
33
|
-
|
|
34
|
-
add x y = x + y
|
|
35
|
-
end
|
|
36
|
-
example
|
|
37
|
-
example
|
|
38
|
-
end
|
|
31
|
+
law "|overflow pairs| is
|
|
32
|
+
definition :
|
|
33
|
+
for all (x :: Int8) (y :: AdditionOverflows Int8 x) =>
|
|
34
|
+
add x y = x + y end for all
|
|
35
|
+
end definition
|
|
36
|
+
example "|smallest overflowing sum| : x = 127 y = 1 expect add x y = 128 end example
|
|
37
|
+
example "|largest sum| : x = 127 y = 127 expect add x y = 254 end example
|
|
38
|
+
end law
|
|
39
39
|
|
|
40
|
-
law
|
|
41
|
-
definition
|
|
42
|
-
example
|
|
43
|
-
end
|
|
40
|
+
law "|bounded input| is
|
|
41
|
+
definition : for all (x :: Between Int8 1 3) => positive x = x end for all end definition
|
|
42
|
+
example "|middle| : x = 2 expect positive x = 2 end example
|
|
43
|
+
end law
|
|
44
44
|
|
|
45
|
-
law
|
|
46
|
-
definition
|
|
47
|
-
example
|
|
48
|
-
end
|
|
45
|
+
law "|large integer| is
|
|
46
|
+
definition : for all (x :: UInt64) => preserve x = x end for all end definition
|
|
47
|
+
example "|maximum UInt64| : x = 18446744073709551615 expect preserve x = 18446744073709551615 end example
|
|
48
|
+
end law
|
|
49
49
|
|
|
50
|
-
abstractEcho :: (x :: Integer) -> (result :: Integer where result
|
|
50
|
+
abstractEcho :: (x :: Integer) -> (result :: Integer where result = x)
|
|
51
51
|
|
|
52
|
-
law
|
|
53
|
-
definition
|
|
54
|
-
end
|
|
52
|
+
law "|nested presence| is
|
|
53
|
+
definition : for all (x :: Optional (Nullable (Positive Int8))) => x = x end for all end definition
|
|
54
|
+
end law
|
|
55
55
|
|
|
56
|
-
law
|
|
57
|
-
definition
|
|
58
|
-
|
|
59
|
-
prelude.isFinite x
|
|
60
|
-
end
|
|
61
|
-
example
|
|
62
|
-
end
|
|
56
|
+
law "|finite positive floats| is
|
|
57
|
+
definition :
|
|
58
|
+
for all (x :: Float32 where prelude.isFinite x && x > 0) =>
|
|
59
|
+
prelude.isFinite x end for all
|
|
60
|
+
end definition
|
|
61
|
+
example "|positive| : x = 0.5 expect x = 0.5 end example
|
|
62
|
+
end law
|
|
63
63
|
|
|
64
|
-
law
|
|
65
|
-
definition
|
|
66
|
-
example
|
|
67
|
-
end
|
|
64
|
+
law "|raw byte length| is
|
|
65
|
+
definition : for all (x :: Bytes where prelude.length x = 3) => prelude.length x = 3 end for all end definition
|
|
66
|
+
example "|arbitrary octets| : x = bytes([0,128,255]) expect prelude.length x = 3 end example
|
|
67
|
+
end law
|
|
68
68
|
|
|
69
|
-
law
|
|
70
|
-
definition
|
|
71
|
-
end
|
|
69
|
+
law "|guarded predicate arithmetic| is
|
|
70
|
+
definition : for all (x :: Int8) (y :: Int8 where x != 0 && y > 10 / x) => y > 10 / x end for all end definition
|
|
71
|
+
end law
|
|
72
72
|
|
|
73
|
-
law
|
|
74
|
-
definition
|
|
75
|
-
end
|
|
73
|
+
law "|machine bound| is
|
|
74
|
+
definition : for all (x :: IntSize where x = IntSize.max) => x = IntSize.max end for all end definition
|
|
75
|
+
end law
|
|
76
76
|
|
|
77
|
-
law
|
|
78
|
-
definition
|
|
79
|
-
|
|
80
|
-
x = symbol("selected", "fixture")
|
|
81
|
-
end
|
|
82
|
-
example
|
|
77
|
+
law "|symbol fixture refinement| is
|
|
78
|
+
definition :
|
|
79
|
+
for all (x :: Symbol where x = symbol("selected", "fixture")) =>
|
|
80
|
+
x = symbol("selected", "fixture") end for all
|
|
81
|
+
end definition
|
|
82
|
+
example "|selected identity| :
|
|
83
83
|
x = symbol("selected", "fixture")
|
|
84
84
|
expect x = symbol("selected", "fixture")
|
|
85
|
-
end
|
|
86
|
-
end
|
|
85
|
+
end example
|
|
86
|
+
end law
|
|
87
|
+
end unit
|