lawspec 0.17.5 → 0.19.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.
@@ -0,0 +1,100 @@
1
+ unit example.actors
2
+
3
+ -- An actor owns a state and handles one message at a time, in the order
4
+ -- they arrive. Its handlers are plain functions from the state (and the
5
+ -- message's arguments) to a reply and the next state; LawSpec runs them
6
+ -- inside an actor and checks every reply against the model, in sequence and
7
+ -- from several callers at once. Runs also crash the actor between
8
+ -- messages: it restarts from its last state by reopen, and the model's state
9
+ -- follows modelReopen.
10
+ type Account is Account balance :: Int64 end
11
+
12
+ openAccount :: Unit -> Account
13
+ deposit :: Account -> UInt8 -> Pair Int64 Account
14
+ withdrawAll :: Account -> Pair Int64 Account
15
+ balance :: Account -> Pair Int64 Account
16
+ close :: Account -> Account
17
+ -- After a crash, the account reopens with the balance it had.
18
+ reopen :: Account -> Account
19
+
20
+ definition modelDeposit (amount :: UInt8) (n :: Int64 where n >= 0 && n <= 1000000) :: Pair Int64 Int64 is
21
+ Pair (n + amount) (n + amount)
22
+ end
23
+
24
+ definition modelWithdrawAll (n :: Int64) :: Pair Int64 Int64 is Pair n 0 end
25
+
26
+ definition modelBalance (n :: Int64) :: Pair Int64 Int64 is Pair n n end
27
+ definition modelClose (n :: Int64) :: Int64 is 0 end
28
+ definition modelReopen (n :: Int64) :: Int64 is n end
29
+ definition solvent (n :: Int64) :: Bool is n >= 0 end
30
+
31
+ actor account :: Account by Int64 is
32
+ start openAccount by 0
33
+ restart from reopen by modelReopen
34
+ on deposit by modelDeposit
35
+ on withdrawAll by modelWithdrawAll
36
+ on balance by modelBalance
37
+ on close by modelClose
38
+ invariant solvent
39
+ end
40
+
41
+ -- Implementation code uses the generated typed actor: depositTwice starts an
42
+ -- account and deposits from two callers at once.
43
+ depositTwice :: UInt8 -> Int64
44
+
45
+ law `two callers' deposits both land` is
46
+ definition is `for all` (x :: UInt8) . depositTwice x = 2 * x end
47
+ end
48
+
49
+ -- A supervisor starts its actors and restarts them after a crash: one for
50
+ -- one restarts only the actor that crashed. The account restarts from its
51
+ -- last balance (restart from reopen).
52
+ supervisor bank is
53
+ one for one
54
+ at most 3 restarts in 5s
55
+ permanent account
56
+ end
57
+
58
+ -- survivesCrash starts the bank, deposits, crashes the account, and reads
59
+ -- its balance through the same handle.
60
+ survivesCrash :: UInt8 -> Int64
61
+
62
+ law `a crashed account keeps its balance` is
63
+ definition is `for all` (x :: UInt8) . survivesCrash x = x end
64
+ end
65
+
66
+ -- A scenario runs the actor's messages from processes that talk over
67
+ -- channels. Some runs crash one process at a random point: a receive from a
68
+ -- process that failed runs its or else instead of waiting forever.
69
+ protocol Receipt is
70
+ send Int64
71
+ end
72
+
73
+ scenario `a deposit's receipt, or the balance if the teller fails` in account is
74
+ channel receipt :: Receipt
75
+ par
76
+ n <- deposit 5
77
+ send receipt n
78
+ with
79
+ receive receipt m or else
80
+ balance
81
+ end
82
+ expect m = 5
83
+ end
84
+ end
85
+
86
+ -- A mailbox has many senders and one receiver: two tellers report their
87
+ -- deposits to one auditor, who receives both reports.
88
+ scenario `two tellers report to one auditor` in account is
89
+ mailbox reports of Int64
90
+ par
91
+ n <- deposit 3
92
+ send reports n
93
+ with
94
+ m <- deposit 4
95
+ send reports m
96
+ with
97
+ receive reports first
98
+ receive reports second
99
+ end
100
+ end
@@ -0,0 +1,48 @@
1
+ unit example.concurrent
2
+
3
+ -- Concurrent collections checked against the built-in ones they behave
4
+ -- like. Each adapter uses its target's own concurrent structures; LawSpec
5
+ -- runs the commands in sequence and at the same time, and every history
6
+ -- must be linearizable. Sets and maps are checked key by key.
7
+
8
+ type WorkQueue is WorkQueue id :: Int32 end
9
+
10
+ newQueue :: Unit -> WorkQueue
11
+ async offer :: WorkQueue -> Int32 -> Unit
12
+ async poll :: WorkQueue -> Maybe Int32
13
+ async queueSize :: WorkQueue -> Int64
14
+
15
+ model queue :: shared WorkQueue behaves like Queue Int32 is
16
+ start newQueue
17
+ offer as offer
18
+ poll as poll
19
+ queueSize as size
20
+ end
21
+
22
+ type Tags is Tags id :: Int32 end
23
+
24
+ newTags :: Unit -> Tags
25
+ async tag :: Tags -> Int32 -> Bool
26
+ async untag :: Tags -> Int32 -> Bool
27
+ async tagged :: Tags -> Int32 -> Bool
28
+
29
+ model tags :: shared Tags behaves like Set Int32 is
30
+ start newTags
31
+ tag as add
32
+ untag as remove
33
+ tagged as contains
34
+ end
35
+
36
+ type Cache is Cache id :: Int32 end
37
+
38
+ newCache :: Unit -> Cache
39
+ async store :: Cache -> Int8 -> Int64 -> Maybe Int64
40
+ async fetch :: Cache -> Int8 -> Maybe Int64
41
+ async evict :: Cache -> Int8 -> Maybe Int64
42
+
43
+ model cache :: shared Cache behaves like KeyVal Int8 Int64 is
44
+ start newCache
45
+ store as put
46
+ fetch as get
47
+ evict as remove
48
+ end
@@ -0,0 +1,21 @@
1
+ unit example.consistency
2
+
3
+ -- Weaker consistency for replicated state. A page-view counter keeps one
4
+ -- replica per caller: a hit counts on the caller's replica and returns what
5
+ -- that replica has seen, so callers see stale totals, and the total is the
6
+ -- sum of the replicas. Its histories are not linearizable, but once every
7
+ -- hit is done the total must count them all: it is eventually consistent.
8
+ type Views is Views id :: Int32 end
9
+
10
+ newViews :: Unit -> Views
11
+ hit :: Views -> Int64
12
+ total :: Views -> Int64
13
+
14
+ definition modelHit (n :: Int64 where n >= 0 && n <= 1000000) :: Pair Int64 Int64 is Pair (n + 1) (n + 1) end
15
+
16
+ model views :: shared Views by Int64 is
17
+ consistency eventual
18
+ start newViews ~ 0
19
+ hit ~ modelHit
20
+ abstract total
21
+ end
@@ -0,0 +1,110 @@
1
+ unit example.distribution
2
+
3
+ -- Distribution. Nodes reach each other's actors, channels and checked
4
+ -- definitions over a transport: in memory (with faults, for testing), TCP or
5
+ -- HTTP. Values cross in the canonical wire encoding, the same bytes on every
6
+ -- target, so a node on one target can talk to a node on another.
7
+
8
+ -- The wire encoding's conformance vectors: values generated from a type
9
+ -- descriptor and a seed, then encoded, in hexadecimal.
10
+ encoded :: Text -> UInt64 -> Int32 -> Int32 -> List Text
11
+ roundTrips :: Text -> UInt64 -> Int32 -> Int32 -> Bool
12
+
13
+ law `integers on the wire` is
14
+ definition is
15
+ `for all` (seed :: UInt64) . prelude.length (encoded "(int Integer _ _)" seed 4 4) = 4
16
+ end
17
+ example `seed 7` is
18
+ seed = 7
19
+ expect encoded "(int Integer _ _)" seed 4 4 = ["9ddc4f", "eefb17", "8fc703", "8ee709"]
20
+ expect encoded "(int Int8 -128 127)" 42 4 5 = ["f901", "28", "00", "48", "5c"]
21
+ end
22
+ end
23
+
24
+ law `text, lists, Maybe and Either on the wire` is
25
+ definition is
26
+ `for all` (seed :: UInt64) . roundTrips "(list (maybe (either (int UInt8 0 255) (text))))" seed 4 8 = true
27
+ end
28
+ example `vectors` is
29
+ seed = 3
30
+ expect encoded "(text)" seed 4 3 = ["037b5b2c", "0134", "024d54"]
31
+ expect encoded "(list (maybe (bool)))" 5 3 3 = ["02000101", "0100", "010100"]
32
+ expect encoded "(either (int UInt8 0 255) (text))" 9 3 4 = ["00ec02", "0000", "00d201", "0100"]
33
+ end
34
+ end
35
+
36
+ law `data types on the wire` is
37
+ definition is
38
+ `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
39
+ end
40
+ example `vectors` is
41
+ seed = 11
42
+ 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"]
43
+ end
44
+ end
45
+
46
+ -- A checked definition that another node evaluates by its content hash.
47
+ definition shifted (x :: Int32) :: Int64 is x + 1000 end
48
+
49
+ -- Calls that cross a network wait for it, so they are asynchronous.
50
+ async remoteShifted :: Int32 -> Int64
51
+
52
+ law `a definition evaluated on another node` is
53
+ definition is `for all` (x :: Int32) . remoteShifted x = shifted x end
54
+ end
55
+
56
+ -- An actor served on one node and called from another.
57
+ type Tally is Tally count :: Int64 end
58
+
59
+ openTally :: Unit -> Tally
60
+ add :: Tally -> UInt8 -> Pair Int64 Tally
61
+
62
+ definition modelAdd (n :: UInt8) (total :: Int64 where total >= 0 && total <= 1000000) :: Pair Int64 Int64 is
63
+ Pair (total + n) (total + n)
64
+ end
65
+
66
+ actor tally :: Tally by Int64 is
67
+ start openTally by 0
68
+ on add by modelAdd
69
+ end
70
+
71
+ async remoteAdds :: UInt8 -> Int64
72
+
73
+ law `two calls from another node` is
74
+ definition is `for all` (x :: UInt8) . remoteAdds x = 2 * x end
75
+ end
76
+
77
+ -- A protocol between two nodes: the first end sends a number and receives
78
+ -- its double.
79
+ protocol Doubling is
80
+ send Int32
81
+ receive Int64
82
+ end
83
+
84
+ async remoteDoubling :: Int32 -> Int64
85
+
86
+ law `a conversation between nodes` is
87
+ definition is `for all` (x :: Int32) . remoteDoubling x = 2 * x end
88
+ end
89
+
90
+ -- A mailbox offered on one node and sent to from another: each send waits
91
+ -- until the mailbox has the message.
92
+ mailbox ledger of Int64
93
+
94
+ async remoteLedger :: Int32 -> Int64
95
+
96
+ law `messages reach a mailbox on another node` is
97
+ definition is `for all` (x :: Int32) . remoteLedger x = 2 * x end
98
+ end
99
+
100
+ -- A channel end sent to another node: the node that sends it relays the
101
+ -- conversation, so the end works on its new node as it did on the old one.
102
+ protocol Handoff is
103
+ send Doubling
104
+ end
105
+
106
+ async remoteHandoff :: Int32 -> Int64
107
+
108
+ law `a channel end handed to another node` is
109
+ definition is `for all` (x :: Int32) . remoteHandoff x = 2 * x end
110
+ end
@@ -14,9 +14,11 @@ type ValidatedOrder is ValidatedOrder id :: OrderId quantity :: UnitQuantity end
14
14
  type PricedOrder is PricedOrder id :: OrderId quantity :: UnitQuantity total :: Int64 end
15
15
  type OrderError is | InvalidOrderId | InvalidQuantity | PriceTooHigh end
16
16
 
17
- -- The compiler checks that each step accepts the previous step's state and that
18
- -- all failures share one error type. It adds a law that placeOrder is the
19
- -- railway composition of the steps: the first Left is the workflow's result.
17
+ -- The compiler checks that each step accepts the previous step's state and
18
+ -- generates placeOrder on every target: the steps run in order and the first
19
+ -- Left is the workflow's result. Only the steps are written by hand. The steps
20
+ -- share OrderError, so it is the workflow's error type; laws say the workflow
21
+ -- succeeds when every step does and stops at the first failing step.
20
22
  workflow placeOrder :: UnvalidatedOrder -> Either OrderError PricedOrder is
21
23
  validateOrder :: UnvalidatedOrder -> Either OrderError ValidatedOrder
22
24
  priceOrder :: ValidatedOrder -> Either OrderError PricedOrder
@@ -24,6 +26,21 @@ end
24
26
 
25
27
  firstLine :: NonEmptyList Int32 -> Int32
26
28
 
29
+ -- An order is placed when it is valid and its total is at most 20000.
30
+ law `placing an order prices it` is
31
+ definition is
32
+ `for all` (input :: UnvalidatedOrder) .
33
+ prelude.isRight (placeOrder input) = (match input with
34
+ | UnvalidatedOrder id quantity ->
35
+ prelude.length id > 0 && quantity >= 1 && quantity <= 800
36
+ end)
37
+ end
38
+ example `a hundred units` is
39
+ input = UnvalidatedOrder "A-1" 100
40
+ expect prelude.isRight (placeOrder input) = true
41
+ end
42
+ end
43
+
27
44
  law `validation accepts exactly the valid orders` is
28
45
  definition is
29
46
  `for all` (input :: UnvalidatedOrder) .
@@ -0,0 +1,63 @@
1
+ unit example.durations
2
+
3
+ -- A Duration is a whole number of microseconds, from 0 to about 146 years.
4
+ -- Literals name a unit (us, ms, s, min, h, d), prelude.milliseconds and its
5
+ -- siblings build one from a number, and prelude.toMicroseconds reads one.
6
+ -- +, -, comparisons, * and prelude.quot by whole numbers stay in range or
7
+ -- fail, as division by zero does.
8
+
9
+ -- A checked definition: the totality audit proves the result in range.
10
+ definition timeoutFor (retries :: Int32 where retries >= 0 && retries <= 5) :: Duration is
11
+ 2s + 500ms * retries
12
+ end
13
+
14
+ -- An adapter: the time left of a budget, never negative.
15
+ remaining :: Duration -> Duration -> Duration
16
+
17
+ law `units relate` is
18
+ definition is
19
+ `for all` (b :: Bool) . (1us * 1000 == 1ms && 1000ms == 1s && 60s == 1min && 60min == 1h && 24h == 1d) = true
20
+ end
21
+ end
22
+
23
+ law `subtraction undoes addition` is
24
+ definition is
25
+ `for all` (n :: Int32 where n >= 0) (m :: Int32 where m >= 0) .
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 `timeouts grow by half a second` is
32
+ definition is
33
+ `for all` (r :: Int32 where r >= 0 && r <= 4) . timeoutFor (r + 1) - timeoutFor r = 500ms
34
+ end
35
+ example `no retries` is
36
+ r = 0
37
+ expect prelude.toMicroseconds (timeoutFor r) = 2000000
38
+ end
39
+ end
40
+
41
+ law `spending everything leaves nothing` is
42
+ definition is
43
+ `for all` (d :: Duration) . remaining d d = 0us and remaining d 0us = d
44
+ end
45
+ end
46
+
47
+ law `what remains of a budget` is
48
+ definition is
49
+ `for all` (budget :: Duration) (spent :: Duration) .
50
+ prelude.toMicroseconds (remaining budget spent) =
51
+ prelude.select (spent <= budget) (prelude.toMicroseconds budget - prelude.toMicroseconds spent) 0
52
+ end
53
+ example `part of a second` is
54
+ budget = 1s
55
+ spent = 250ms
56
+ expect remaining budget spent = 750ms
57
+ end
58
+ example `an overrun leaves nothing` is
59
+ budget = 1s
60
+ spent = 2s
61
+ expect remaining budget spent = 0us
62
+ end
63
+ end
@@ -0,0 +1,142 @@
1
+ unit example.generation
2
+
3
+ -- Conformance of every target's portable generator with the same vectors:
4
+ -- the values generated from a type descriptor and a SplitMix64 seed, and the
5
+ -- shrink candidates of the first, rendered canonically.
6
+
7
+ generated :: Text -> UInt64 -> Int32 -> Int32 -> List Text
8
+ shrunk :: Text -> UInt64 -> Int32 -> List Text
9
+
10
+ law `int8` is
11
+ definition is
12
+ `for all` (seed :: UInt64) . prelude.length (generated "(int Int8 -128 127)" seed 4 5) = 5
13
+ end
14
+ example `seed 42` is
15
+ seed = 42
16
+ expect generated "(int Int8 -128 127)" seed 4 5 = ["-125", "20", "0", "36", "46"]
17
+ expect shrunk "(int Int8 -128 127)" seed 4 = ["0", "-63", "-124"]
18
+ end
19
+ end
20
+
21
+ law `unbounded integers` is
22
+ definition is
23
+ `for all` (seed :: UInt64) . prelude.length (generated "(int Integer _ _)" seed 4 4) = 4
24
+ end
25
+ example `seed 7` is
26
+ seed = 7
27
+ expect generated "(int Integer _ _)" seed 4 4 = ["-653071", "196343", "-29128", "80327"]
28
+ expect shrunk "(int Integer _ _)" seed 4 = ["0", "-326536", "-653070"]
29
+ end
30
+ end
31
+
32
+ law `a lower bound` is
33
+ definition is
34
+ `for all` (seed :: UInt64) . prelude.length (generated "(int Int32 5 _)" seed 4 4) = 4
35
+ end
36
+ example `seed 7` is
37
+ seed = 7
38
+ expect generated "(int Int32 5 _)" seed 4 4 = ["346934", "1196348", "970877", "1080332"]
39
+ expect shrunk "(int Int32 5 _)" seed 4 = ["5", "173470", "346933"]
40
+ end
41
+ end
42
+
43
+ law `the full UInt64 range` is
44
+ definition is
45
+ `for all` (seed :: UInt64) . prelude.length (generated "(int UInt64 0 18446744073709551615)" seed 4 3) = 3
46
+ end
47
+ example `seed 1` is
48
+ seed = 1
49
+ expect generated "(int UInt64 0 18446744073709551615)" seed 4 3 = ["13757245211066428519", "1", "0"]
50
+ expect shrunk "(int UInt64 0 18446744073709551615)" seed 4 = ["0", "6878622605533214260", "13757245211066428518"]
51
+ end
52
+ end
53
+
54
+ law `booleans` is
55
+ definition is
56
+ `for all` (seed :: UInt64) . prelude.length (generated "(bool)" seed 4 6) = 6
57
+ end
58
+ example `seed 3` is
59
+ seed = 3
60
+ expect generated "(bool)" seed 4 6 = ["true", "true", "true", "true", "false", "true"]
61
+ expect shrunk "(bool)" seed 4 = ["false"]
62
+ end
63
+ end
64
+
65
+ law `text` is
66
+ definition is
67
+ `for all` (seed :: UInt64) . prelude.length (generated "(text)" seed 6 3) = 3
68
+ end
69
+ example `seed 9` is
70
+ seed = 9
71
+ expect generated "(text)" seed 6 3 = ["\"I(\"", "\"\"", "\"4<*6F\""]
72
+ expect shrunk "(text)" seed 6 = ["\"\"", "\"I\"", "\"(\""]
73
+ end
74
+ end
75
+
76
+ law `unit` is
77
+ definition is
78
+ `for all` (seed :: UInt64) . prelude.length (generated "(unit)" seed 1 2) = 2
79
+ end
80
+ example `seed 1` is
81
+ seed = 1
82
+ expect generated "(unit)" seed 1 2 = ["()", "()"]
83
+ expect shrunk "(unit)" seed 1 = []
84
+ end
85
+ end
86
+
87
+ law `lists` is
88
+ definition is
89
+ `for all` (seed :: UInt64) . prelude.length (generated "(list (int Int32 0 _))" seed 4 3) = 3
90
+ end
91
+ example `seed 42` is
92
+ seed = 42
93
+ expect generated "(list (int Int32 0 _))" seed 4 3 = ["[0, 1745563, 275752]", "[1325093, 742426, 493279]", "[2000000]"]
94
+ expect shrunk "(list (int Int32 0 _))" seed 4 = ["[]", "[0]", "[1745563, 275752]", "[0, 275752]", "[0, 1745563]", "[0, 0, 275752]", "[0, 872782, 275752]", "[0, 1745562, 275752]", "[0, 1745563, 0]", "[0, 1745563, 137876]", "[0, 1745563, 275751]"]
95
+ end
96
+ end
97
+
98
+ law `maybe` is
99
+ definition is
100
+ `for all` (seed :: UInt64) . prelude.length (generated "(maybe (bool))" seed 4 4) = 4
101
+ end
102
+ example `seed 5` is
103
+ seed = 5
104
+ expect generated "(maybe (bool))" seed 4 4 = ["Just(false)", "Just(true)", "Just(false)", "Just(true)"]
105
+ expect shrunk "(maybe (bool))" seed 4 = ["Nothing"]
106
+ end
107
+ end
108
+
109
+ law `either` is
110
+ definition is
111
+ `for all` (seed :: UInt64) . prelude.length (generated "(either (text) (int Int8 -128 127))" seed 4 4) = 4
112
+ end
113
+ example `seed 11` is
114
+ seed = 11
115
+ expect generated "(either (text) (int Int8 -128 127))" seed 4 4 = ["Right(-19)", "Left(\"\\\"{U\")", "Left(\"j\")", "Right(1)"]
116
+ expect shrunk "(either (text) (int Int8 -128 127))" seed 4 = ["Right(0)", "Right(-10)", "Right(-18)"]
117
+ end
118
+ end
119
+
120
+ law `a recursive tree` is
121
+ definition is
122
+ `for all` (seed :: UInt64) . prelude.length (generated "(data Tree (ctor t::type::Tree::Leaf) (ctor t::type::Tree::Node (ref Tree) (int Int8 -128 127) (ref Tree))) (ref Tree)" seed 3 2) = 2
123
+ end
124
+ example `seed 13` is
125
+ seed = 13
126
+ expect generated "(data Tree (ctor t::type::Tree::Leaf) (ctor t::type::Tree::Node (ref Tree) (int Int8 -128 127) (ref Tree))) (ref Tree)" seed 3 2 = ["Node(Node(Leaf, -29, Leaf), -44, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Leaf"]
127
+ expect shrunk "(data Tree (ctor t::type::Tree::Leaf) (ctor t::type::Tree::Node (ref Tree) (int Int8 -128 127) (ref Tree))) (ref Tree)" seed 3 = ["Leaf", "Node(Leaf, -29, Leaf)", "Node(Node(Leaf, -110, Leaf), 58, Leaf)", "Node(Leaf, -44, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Node(Node(Leaf, 0, Leaf), -44, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Node(Node(Leaf, -15, Leaf), -44, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Node(Node(Leaf, -28, Leaf), -44, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Node(Node(Leaf, -29, Leaf), 0, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Node(Node(Leaf, -29, Leaf), -22, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Node(Node(Leaf, -29, Leaf), -43, Node(Node(Leaf, -110, Leaf), 58, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Leaf)", "Node(Node(Leaf, -29, Leaf), -44, Node(Leaf, -110, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Node(Leaf, 58, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Node(Node(Leaf, 0, Leaf), 58, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Node(Node(Leaf, -55, Leaf), 58, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Node(Node(Leaf, -109, Leaf), 58, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Node(Node(Leaf, -110, Leaf), 0, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Node(Node(Leaf, -110, Leaf), 29, Leaf))", "Node(Node(Leaf, -29, Leaf), -44, Node(Node(Leaf, -110, Leaf), 57, Leaf))"]
128
+ end
129
+ end
130
+
131
+ -- A refinement's bounds narrow generation: an amount from 1 to 1000 is
132
+ -- drawn from that range, not filtered out of every Int32, alongside a value
133
+ -- of a data type.
134
+ type Purse is Purse coins :: Int64 end
135
+
136
+ definition spend (p :: Purse) (amount :: Int32 where amount >= 1 && amount <= 1000) :: Int32 is amount end
137
+
138
+ law `refined amounts are drawn from their range` is
139
+ definition is
140
+ `for all` (p :: Purse) (amount :: Int32 where amount >= 1 && amount <= 1000) . spend p amount >= 1
141
+ end
142
+ end
@@ -0,0 +1,20 @@
1
+ unit example.handles
2
+
3
+ -- A handle is a value only adapters create, such as a library's concurrent
4
+ -- queue. LawSpec passes it along unopened: it never generates, builds or
5
+ -- looks inside one, and two handles are equal only when they are the same.
6
+ -- Here the queue is the target's own concurrent queue, bound directly where
7
+ -- the target has one (see each target's bindings) and wrapped elsewhere.
8
+ handle Jobs
9
+
10
+ newJobs :: Unit -> Jobs
11
+ submit :: Jobs -> Int32 -> Unit
12
+ take :: Jobs -> Maybe Int32
13
+ pending :: Jobs -> Int32
14
+
15
+ model jobs :: shared Jobs behaves like Queue Int32 is
16
+ start newJobs
17
+ submit as offer
18
+ take as poll
19
+ pending as size
20
+ end
@@ -0,0 +1,33 @@
1
+ unit example.keywords
2
+
3
+ -- A name that is a keyword of a target language is emitted there with a
4
+ -- leading underscore: short is _short in Java, lambda is _lambda in Python,
5
+ -- and class is _class wherever class is reserved. Other targets keep the name.
6
+
7
+ definition short (x :: BigInt) :: Bool is x < 10 end
8
+ definition double (x :: BigInt) :: BigInt is x + x end
9
+ definition lambda (x :: BigInt) :: BigInt is x + 1 end
10
+ definition fun (x :: BigInt) :: BigInt is x + 2 end
11
+ definition loop (x :: BigInt) :: BigInt is x + 3 end
12
+ definition deriving (x :: BigInt) :: BigInt is x + 4 end
13
+ definition delete (x :: BigInt) :: BigInt is x + 5 end
14
+
15
+ -- An adapter, written by hand on each target as _class or class.
16
+ class :: BigInt -> BigInt
17
+
18
+ law `keyword names are callable` is
19
+ definition is
20
+ `for all` (x :: BigInt) .
21
+ double x - x + lambda x + fun x + loop x + deriving x + delete x = 6 * x + 15
22
+ end
23
+ example `short` is
24
+ x = 3
25
+ expect short x = true
26
+ end
27
+ end
28
+
29
+ law `the class adapter doubles` is
30
+ definition is
31
+ `for all` (x :: BigInt) . class x = double x
32
+ end
33
+ end
@@ -0,0 +1,43 @@
1
+ unit example.limits
2
+
3
+ -- A workflow with a rate limit, for the resilience suite: two calls per
4
+ -- second, rejecting the rest. Its own unit, so the suite's checks can call it
5
+ -- on every target (a Haskell workflow imports its steps' module).
6
+ type Ticket is Ticket number :: Int64 end
7
+
8
+ admitTicket :: Ticket -> Either Text Ticket
9
+
10
+ workflow limited :: Ticket -> Either _ Ticket is
11
+ admitTicket :: Ticket -> Either Text Ticket
12
+ rateLimit tokenBucket 2 1s reject
13
+ end
14
+
15
+ -- A workflow that compensates: when charging fails, the reserved seat is
16
+ -- released.
17
+ reserveSeat :: Ticket -> Either Text Ticket
18
+ chargeCard :: Ticket -> Either Text Ticket
19
+ releaseSeat :: Ticket -> Bool
20
+
21
+ workflow book :: Ticket -> Either _ Ticket is
22
+ reserveSeat :: Ticket -> Either Text Ticket
23
+ compensate releaseSeat
24
+ chargeCard :: Ticket -> Either Text Ticket
25
+ end
26
+
27
+ -- An asynchronous step within a timeout: the quote for ticket -1 is slow
28
+ -- (600ms), so its stage times out.
29
+ async fetchQuote :: Ticket -> Either Text Ticket
30
+
31
+ workflow quoted :: Ticket -> Either _ Ticket is
32
+ fetchQuote :: Ticket -> Either Text Ticket
33
+ timeout 200ms
34
+ end
35
+
36
+ -- An asynchronous step with a hedge: ticket -2's first quote stalls (600ms),
37
+ -- so a second starts after 50ms and answers first.
38
+ async hedgeQuote :: Ticket -> Either Text Ticket
39
+
40
+ workflow hedged :: Ticket -> Either _ Ticket is
41
+ hedgeQuote :: Ticket -> Either Text Ticket
42
+ hedge 50ms
43
+ end