lawspec 0.24.0 → 0.25.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.
Files changed (87) hide show
  1. package/CHANGELOG.md +67 -1
  2. package/README.md +29 -22
  3. package/UNICODE-LICENSE.txt +39 -0
  4. package/api.mjs +14 -23
  5. package/bin/lawspec.mjs +186 -65
  6. package/build.json +255 -159
  7. package/builtin-data-names.mjs +73 -0
  8. package/core.wasm +2 -2
  9. package/examples/native-payments/clojure/lawspec.json +13 -2
  10. package/examples/native-payments/csharp/lawspec.json +13 -2
  11. package/examples/native-payments/elixir/lawspec.json +14 -3
  12. package/examples/native-payments/erlang/lawspec.json +14 -3
  13. package/examples/native-payments/fsharp/lawspec.json +14 -3
  14. package/examples/native-payments/gleam/lawspec.json +14 -3
  15. package/examples/native-payments/go/lawspec.json +14 -3
  16. package/examples/native-payments/groovy/lawspec.json +14 -3
  17. package/examples/native-payments/haskell/lawspec.json +14 -3
  18. package/examples/native-payments/java/lawspec.json +14 -3
  19. package/examples/native-payments/javascript/lawspec.json +14 -3
  20. package/examples/native-payments/kotlin/lawspec.json +14 -3
  21. package/examples/native-payments/python/lawspec.json +14 -3
  22. package/examples/native-payments/rust/lawspec.json +14 -3
  23. package/examples/native-payments/scala/lawspec.json +14 -3
  24. package/examples/native-payments/swift/lawspec.json +14 -3
  25. package/examples/native-payments/typescript/lawspec.json +14 -3
  26. package/examples/packages/orders/lawspec.json +25 -5
  27. package/examples/packages/orders/orders.lawspec +36 -34
  28. package/examples/packages/shop-domain/src/domain.lawspec +23 -21
  29. package/examples/packages/shop-tax-1/src/rates.lawspec +4 -4
  30. package/examples/packages/shop-tax-2/src/rates.lawspec +5 -4
  31. package/examples/rc1/authorization-snapshots.lawspec +112 -0
  32. package/examples/rc1/banner-filenames.lawspec +63 -0
  33. package/examples/rc1/bit-codec.lawspec +56 -0
  34. package/examples/rc1/bounded-witness-search.lawspec +144 -0
  35. package/examples/rc1/conflicting-requirements.lawspec +30 -0
  36. package/examples/rc1/constructive-witnesses.lawspec +113 -0
  37. package/examples/rc1/documentary-review.lawspec +42 -0
  38. package/examples/rc1/empty-domains.lawspec +46 -0
  39. package/examples/rc1/finite-integer-domains.lawspec +75 -0
  40. package/examples/rc1/law-metadata.lawspec +29 -0
  41. package/examples/rc1/native-payments.lawspec +107 -0
  42. package/examples/rc1/nesting/rrr.lawspec +33 -0
  43. package/examples/rc1/nesting/rru.lawspec +56 -0
  44. package/examples/rc1/nesting/rur.lawspec +57 -0
  45. package/examples/rc1/nesting/ruu.lawspec +77 -0
  46. package/examples/rc1/nesting/urr.lawspec +52 -0
  47. package/examples/rc1/nesting/uru.lawspec +72 -0
  48. package/examples/rc1/nesting/uur.lawspec +73 -0
  49. package/examples/rc1/nesting/uuu.lawspec +95 -0
  50. package/examples/rc1/order-workflow.lawspec +126 -0
  51. package/examples/rc1/parcel-measurements.lawspec +92 -0
  52. package/examples/rc1/picture-records.lawspec +51 -0
  53. package/examples/rc1/picture-tuples.lawspec +47 -0
  54. package/examples/rc1/record-payloads.lawspec +56 -0
  55. package/examples/rc1/recursive-records.lawspec +64 -0
  56. package/examples/rc1/refund-conformance.lawspec +44 -0
  57. package/examples/rc1/refund-policy.lawspec +88 -0
  58. package/examples/rc1/requirement-composition.lawspec +89 -0
  59. package/examples/rc1/structural-records.lawspec +53 -0
  60. package/examples/rc1-authoring.lawspec +56 -0
  61. package/examples-command.mjs +11 -5
  62. package/explain.mjs +112 -0
  63. package/expression-presentation.mjs +333 -0
  64. package/files.mjs +16 -0
  65. package/index.d.ts +929 -11
  66. package/lsp.mjs +231 -0
  67. package/migrate.mjs +135 -0
  68. package/migration-bindings.mjs +86 -0
  69. package/migration-cases.mjs +45 -0
  70. package/migration-collections.mjs +54 -0
  71. package/migration-correspondence.mjs +275 -0
  72. package/migration-generation.mjs +99 -0
  73. package/migration-laws.mjs +102 -0
  74. package/migration-records.mjs +166 -0
  75. package/migration-source.mjs +247 -0
  76. package/migration-write.mjs +156 -0
  77. package/native-evidence.mjs +187 -0
  78. package/native-examples.mjs +8 -1
  79. package/node-events-reporter.mjs +23 -0
  80. package/package.json +8 -1
  81. package/positional-fields.mjs +13 -0
  82. package/proof.d.ts +35 -0
  83. package/proof.mjs +174 -0
  84. package/starter-rc1.lawspec +25 -0
  85. package/templates.mjs +2 -2
  86. package/test-command.mjs +107 -35
  87. package/typed-presentation.mjs +165 -0
@@ -0,0 +1,72 @@
1
+ unit example.delivery.uru
2
+
3
+ type File is one of
4
+ | Pdf of type record{fileName of type Text
5
+ byteCount of type Whole}
6
+ | PlainText of type record{fileName of type Text}
7
+ end type
8
+
9
+ type Delivery is one of
10
+ | Email of type record{recipient of type Text
11
+ document of type record{number of type Text
12
+ file of type File}}
13
+ | Download of type record{document of type record{number of type Text
14
+ file of type File}}
15
+ end type
16
+
17
+ function "|email invoice PDF file name|
18
+ (delivery of type Delivery) -> Maybe Text is
19
+ first matching case of delivery:
20
+ when Email record{document = record{file = Pdf record{fileName = name}}}
21
+ then Just name
22
+ otherwise Nothing
23
+ end case
24
+ end function
25
+
26
+ function emailPdfInvoice (name of type Text) -> Delivery is
27
+ Email (record{recipient = "accounts@example.com", document =
28
+ record{number = "INV-2026-10", file = (Pdf (record{fileName
29
+ = name, byteCount = 1200}))}})
30
+ end function
31
+
32
+ law "|emailPdfInvoice result| is
33
+ description:
34
+ "PDF invoices sent by email retain their file names."
35
+ end
36
+ definition:
37
+ "|email invoice PDF file name| (emailPdfInvoice "invoice.pdf") must be Just
38
+ "invoice.pdf"
39
+ end
40
+ end
41
+
42
+ function emailPlainTextInvoice (name of type Text) -> Delivery is
43
+ Email (record{recipient = "accounts@example.com", document =
44
+ record{number = "INV-2026-10", file = (PlainText (record{fileName =
45
+ name}))}})
46
+ end function
47
+
48
+ law "|emailPlainTextInvoice result| is
49
+ description:
50
+ "Plain-text invoices have no PDF file name."
51
+ end
52
+ definition:
53
+ "|email invoice PDF file name| (emailPlainTextInvoice "invoice.pdf") must be
54
+ Nothing
55
+ end
56
+ end
57
+
58
+ function downloadPdfInvoice (name of type Text) -> Delivery is
59
+ Download (record{document = record{number =
60
+ "INV-2026-10", file = (Pdf (record{fileName = name, byteCount =
61
+ 1200}))}})
62
+ end function
63
+
64
+ law "|downloadPdfInvoice result| is
65
+ description:
66
+ "Downloads have no email invoice PDF file name."
67
+ end
68
+ definition:
69
+ "|email invoice PDF file name| (downloadPdfInvoice "invoice.pdf") must be
70
+ Nothing
71
+ end
72
+ end
@@ -0,0 +1,73 @@
1
+ unit example.delivery.uur
2
+
3
+ type Document is one of
4
+ | Invoice of type record{number of type Text
5
+ file of type record{fileName of type Text
6
+ byteCount of type Whole}}
7
+ | Letter of type record{subject of type Text
8
+ file of type record{fileName of type Text
9
+ byteCount of type Whole}}
10
+ end type
11
+
12
+ type Delivery is one of
13
+ | Email of type record{recipient of type Text
14
+ document of type Document}
15
+ | Download of type record{document of type Document}
16
+ end type
17
+
18
+ function "|email invoice PDF file name|
19
+ (delivery of type Delivery) -> Maybe Text is
20
+ first matching case of delivery:
21
+ when Email record{document = Invoice record{file = record{fileName = name}}}
22
+ then Just name
23
+ otherwise Nothing
24
+ end case
25
+ end function
26
+
27
+ function emailPdfInvoice (name of type Text) -> Delivery is
28
+ Email (record{recipient = "accounts@example.com", document =
29
+ (Invoice (record{number = "INV-2026-10", file = record{fileName = name,
30
+ byteCount = 1200}}))})
31
+ end function
32
+
33
+ law "|emailPdfInvoice result| is
34
+ description:
35
+ "PDF invoices sent by email retain their file names."
36
+ end
37
+ definition:
38
+ "|email invoice PDF file name| (emailPdfInvoice "invoice.pdf") must be Just
39
+ "invoice.pdf"
40
+ end
41
+ end
42
+
43
+ function emailPdfLetter (name of type Text) -> Delivery is
44
+ Email (record{recipient = "accounts@example.com", document =
45
+ (Letter (record{subject = "Receipt", file = record{fileName = name,
46
+ byteCount = 1200}}))})
47
+ end function
48
+
49
+ law "|emailPdfLetter result| is
50
+ description:
51
+ "Letters have no invoice PDF file name."
52
+ end
53
+ definition:
54
+ "|email invoice PDF file name| (emailPdfLetter "invoice.pdf") must be
55
+ Nothing
56
+ end
57
+ end
58
+
59
+ function downloadPdfInvoice (name of type Text) -> Delivery is
60
+ Download (record{document = (Invoice (record{number = "INV-2026-10", file =
61
+ record{fileName = name,
62
+ byteCount = 1200}}))})
63
+ end function
64
+
65
+ law "|downloadPdfInvoice result| is
66
+ description:
67
+ "Downloads have no email invoice PDF file name."
68
+ end
69
+ definition:
70
+ "|email invoice PDF file name| (downloadPdfInvoice "invoice.pdf") must be
71
+ Nothing
72
+ end
73
+ end
@@ -0,0 +1,95 @@
1
+ unit example.delivery.uuu
2
+
3
+ type File is one of
4
+ | Pdf of type record{fileName of type Text
5
+ byteCount of type Whole}
6
+ | PlainText of type record{fileName of type Text}
7
+ end type
8
+
9
+ type Document is one of
10
+ | Invoice of type record{number of type Text
11
+ file of type File}
12
+ | Letter of type record{subject of type Text
13
+ file of type File}
14
+ end type
15
+
16
+ type Delivery is one of
17
+ | Email of type record{recipient of type Text
18
+ document of type Document}
19
+ | Download of type record{document of type Document}
20
+ end type
21
+
22
+ function "|email invoice PDF file name|
23
+ (delivery of type Delivery) -> Maybe Text is
24
+ first matching case of delivery:
25
+ when Email record{document = Invoice record{file = Pdf record{fileName =
26
+ name}}}
27
+ then Just name
28
+ otherwise Nothing
29
+ end case
30
+ end function
31
+
32
+ function emailPdfInvoice (name of type Text) -> Delivery is
33
+ Email (record{recipient = "accounts@example.com", document =
34
+ (Invoice (record{number = "INV-2026-10", file = (Pdf
35
+ (record{fileName = name, byteCount = 1200}))}))})
36
+ end function
37
+
38
+ law "|emailPdfInvoice result| is
39
+ description:
40
+ "PDF invoices sent by email retain their file names."
41
+ end
42
+ definition:
43
+ "|email invoice PDF file name| (emailPdfInvoice "invoice.pdf") must be Just
44
+ "invoice.pdf"
45
+ end
46
+ end
47
+
48
+ function emailPlainTextInvoice (name of type Text) -> Delivery is
49
+ Email (record{recipient = "accounts@example.com", document =
50
+ (Invoice (record{number = "INV-2026-10", file = (PlainText
51
+ (record{fileName = name}))}))})
52
+ end function
53
+
54
+ law "|emailPlainTextInvoice result| is
55
+ description:
56
+ "Plain-text invoices have no PDF file name."
57
+ end
58
+ definition:
59
+ "|email invoice PDF file name| (emailPlainTextInvoice "invoice.pdf") must be
60
+ Nothing
61
+ end
62
+ end
63
+
64
+ function emailPdfLetter (name of type Text) -> Delivery is
65
+ Email (record{recipient = "accounts@example.com", document =
66
+ (Letter (record{subject = "Receipt", file = (Pdf (record{fileName = name,
67
+ byteCount = 1200}))}))})
68
+ end function
69
+
70
+ law "|emailPdfLetter result| is
71
+ description:
72
+ "Letters have no invoice PDF file name."
73
+ end
74
+ definition:
75
+ "|email invoice PDF file name| (emailPdfLetter "invoice.pdf") must be
76
+ Nothing
77
+ end
78
+ end
79
+
80
+ function downloadPdfInvoice (name of type Text) -> Delivery is
81
+ Download (record{document = (Invoice (record{number = "INV-2026-10", file =
82
+ (Pdf (record{fileName =
83
+ name,
84
+ byteCount = 1200}))}))})
85
+ end function
86
+
87
+ law "|downloadPdfInvoice result| is
88
+ description:
89
+ "Downloads have no email invoice PDF file name."
90
+ end
91
+ definition:
92
+ "|email invoice PDF file name| (downloadPdfInvoice "invoice.pdf") must be
93
+ Nothing
94
+ end
95
+ end
@@ -0,0 +1,126 @@
1
+ unit example.orders
2
+
3
+ type OrderState is one of
4
+ | Pending
5
+ | Authorized
6
+ | Shipped
7
+ | Cancelled
8
+ end type
9
+
10
+ type OrderAction is one of
11
+ | Authorize
12
+ | Dispatch
13
+ | Cancel
14
+ end type
15
+
16
+ function nextState (state of type OrderState)
17
+ (action of type OrderAction) -> OrderState is
18
+ first matching case of action:
19
+ when Authorize then
20
+ if state = Pending then Authorized else state end if
21
+ when Dispatch then
22
+ if state = Authorized then Shipped else state end if
23
+ when Cancel then
24
+ if state = Shipped then Shipped else Cancelled end if
25
+ end case
26
+ end function
27
+
28
+ ability OrderStore is
29
+ current of type OrderState
30
+ change of type OrderState -> ()
31
+ end
32
+
33
+ handler inMemory for OrderStore with state order of type OrderState
34
+ start Pending is
35
+ current is order end
36
+ change updated is ~order := updated; () end
37
+ end
38
+
39
+ procedure perform (action of type OrderAction) -> OrderState uses OrderStore is
40
+ let updated = nextState current action in
41
+ change updated; current
42
+ end procedure
43
+
44
+ procedure process (actions of type List OrderAction) -> OrderState
45
+ uses OrderStore is
46
+ first matching case of actions:
47
+ when Nil then current
48
+ when Cons action remaining then
49
+ perform action; process remaining
50
+ end case
51
+ end procedure
52
+
53
+ function runOrder (actions of type List OrderAction) -> OrderState is
54
+ handle process actions with inMemory end
55
+ end function
56
+
57
+ function modelRun (state of type OrderState)
58
+ (actions of type List OrderAction) -> OrderState is
59
+ first matching case of actions:
60
+ when Nil then state
61
+ when Cons action remaining then
62
+ modelRun (nextState state action) remaining
63
+ end case
64
+ end function
65
+
66
+ law "|order transitions follow the business lifecycle| is
67
+ definition:
68
+ for all (state of type OrderState) (action of type OrderAction) =>
69
+ (state = Shipped implies nextState state action must be Shipped)
70
+ and (state = Cancelled implies nextState state action must be Cancelled)
71
+ and (state = Pending and action = Authorize implies
72
+ nextState state action must be Authorized)
73
+ and (state = Pending and action = Dispatch implies
74
+ nextState state action must be Pending)
75
+ and (state = Authorized and action = Authorize implies
76
+ nextState state action must be Authorized)
77
+ and (state = Authorized and action = Dispatch implies
78
+ nextState state action must be Shipped)
79
+ and ((state = Pending or state = Authorized) and action = Cancel implies
80
+ nextState state action must be Cancelled)
81
+ end
82
+ end
83
+ expect nextState state action = expected
84
+ in table (state, action, expected):
85
+ row Pending, Authorize, Authorized
86
+ row Pending, Dispatch, Pending
87
+ row Pending, Cancel, Cancelled
88
+ row Authorized, Authorize, Authorized
89
+ row Authorized, Dispatch, Shipped
90
+ row Authorized, Cancel, Cancelled
91
+ row Shipped, Authorize, Shipped
92
+ row Shipped, Dispatch, Shipped
93
+ row Shipped, Cancel, Shipped
94
+ row Cancelled, Authorize, Cancelled
95
+ row Cancelled, Dispatch, Cancelled
96
+ row Cancelled, Cancel, Cancelled
97
+ end
98
+ end
99
+
100
+ law "|processing retains each completed transition| is
101
+ description:
102
+ "Every action observes the state left by the previous action."
103
+ end
104
+ definition:
105
+ for all (actions of type List OrderAction) =>
106
+ runOrder actions must be modelRun Pending actions
107
+ end
108
+ end
109
+ expect runOrder actions = expected
110
+ in table (actions, expected):
111
+ row [], Pending
112
+ row [Dispatch], Pending
113
+ row [Authorize], Authorized
114
+ row [Authorize, Dispatch], Shipped
115
+ row [Cancel, Authorize, Dispatch], Cancelled
116
+ row [Authorize, Cancel, Dispatch], Cancelled
117
+ row [Authorize, Dispatch, Cancel], Shipped
118
+ row [Authorize, Dispatch, Authorize], Shipped
119
+ end
120
+ end
121
+
122
+ law "|independent orders start pending| is
123
+ definition:
124
+ runOrder [Authorize] must be Authorized and runOrder [] must be Pending
125
+ end
126
+ end
@@ -0,0 +1,92 @@
1
+ unit example.refinedPayloads
2
+
3
+ type Measurement (a of type Type) is a record{
4
+ reading of type record{value of type a}
5
+ }
6
+ end type
7
+
8
+ type Parcel is a record{
9
+ details of type record{
10
+ weight of type (measurement of type Measurement Integer
11
+ where measurement.reading.value > 0)
12
+ }
13
+ }
14
+ end type
15
+
16
+ type DeliveryLabel is a record{
17
+ code of type UInt8
18
+ recipient of type Text
19
+ }
20
+ end type
21
+
22
+ type Delivery (a of type Type) is a record{
23
+ contents of type record{value of type a}
24
+ }
25
+ end type
26
+
27
+ function sampleParcel (unused of type ()) -> Parcel is
28
+ Parcel record{details = record{
29
+ weight = Measurement record{reading = record{value = 1}}
30
+ }}
31
+ end function
32
+
33
+ function weightOf (parcel of type record{
34
+ weight of type (measurement of type Measurement Integer
35
+ where measurement.reading.value > 0)
36
+ }) -> Integer is
37
+ parcel.weight.reading.value
38
+ end function
39
+
40
+ function recipient (delivery of type Delivery DeliveryLabel) -> Text is
41
+ delivery.contents.value.recipient
42
+ end function
43
+
44
+ law sampleWeight is
45
+ description:
46
+ "A sample parcel weighs one gram."
47
+ end
48
+ definition:
49
+ sampleParcel () must be Parcel record{details = record{
50
+ weight = Measurement record{reading = record{value = 1}}
51
+ }}
52
+ end
53
+ end
54
+
55
+ law positiveWeights is
56
+ description:
57
+ "Every accepted parcel measurement has a positive weight."
58
+ end
59
+ definition:
60
+ for all (parcel of type record{
61
+ weight of type (measurement of type Measurement Integer
62
+ where measurement.reading.value > 0)
63
+ }) =>
64
+ weightOf parcel > 0
65
+ end
66
+ end
67
+ expect weightOf parcel = expected
68
+ in table (parcel, expected):
69
+ row record{weight = Measurement record{reading = record{value = 1}}}, 1
70
+ row record{weight = Measurement record{reading = record{value = 3}}}, 3
71
+ end
72
+ end
73
+
74
+ law recipientLabels is
75
+ description:
76
+ "Deliveries preserve the recipient written on their label."
77
+ end
78
+ definition:
79
+ for all (delivery of type Delivery DeliveryLabel) =>
80
+ recipient delivery = recipient delivery
81
+ end
82
+ end
83
+ expect recipient delivery = expected
84
+ in table (delivery, expected):
85
+ row Delivery record{contents = record{value = DeliveryLabel record{
86
+ recipient = "Asha", code = 7
87
+ }}}, "Asha"
88
+ row Delivery record{contents = record{value = DeliveryLabel record{
89
+ code = 8, recipient = "Bo"
90
+ }}}, "Bo"
91
+ end
92
+ end
@@ -0,0 +1,51 @@
1
+ unit pictures.records
2
+
3
+ type Orientation is one of
4
+ | Landscape
5
+ | Portrait
6
+ | Square
7
+ end type
8
+
9
+ type Picture is a record{
10
+ fileName of type Text
11
+ dimensions of type record{
12
+ width of type Nat
13
+ height of type Nat
14
+ }
15
+ }
16
+ end type
17
+
18
+ function orientation
19
+ (record{dimensions = record{width = width, height = height}} of type Picture)
20
+ -> Orientation is
21
+ first matching case of width:
22
+ when it > height then Landscape
23
+ when it < height then Portrait
24
+ otherwise Square
25
+ end case
26
+ end function
27
+
28
+ law "|named dimensions determine orientation| is
29
+ description:
30
+ "Dimension labels preserve their meaning in either field order."
31
+ end
32
+ definition:
33
+ for all (picture of type Picture) =>
34
+ orientation picture must be
35
+ if picture.dimensions.width > picture.dimensions.height then Landscape
36
+ else if picture.dimensions.width < picture.dimensions.height then Portrait
37
+ else Square
38
+ end if
39
+ end if
40
+ end
41
+ end
42
+ expect orientation picture = expected
43
+ in table (picture, expected):
44
+ row Picture record{fileName = "wide.jpg", dimensions = record{width = 12,
45
+ height = 8}}, Landscape
46
+ row Picture record{fileName = "tall.jpg", dimensions = record{height = 12,
47
+ width = 8}}, Portrait
48
+ row Picture record{fileName = "square.jpg", dimensions = record{width = 10,
49
+ height = 10}}, Square
50
+ end
51
+ end
@@ -0,0 +1,47 @@
1
+ unit pictures.tuples
2
+
3
+ type Orientation is one of
4
+ | Landscape
5
+ | Portrait
6
+ | Square
7
+ end type
8
+
9
+ type Picture is a record{
10
+ fileName of type Text
11
+ dimensions of type (Nat, Nat)
12
+ }
13
+ end type
14
+
15
+ function orientation
16
+ (record{dimensions = (width, height)} of type Picture) -> Orientation is
17
+ first matching case of width:
18
+ when it > height then Landscape
19
+ when it < height then Portrait
20
+ otherwise Square
21
+ end case
22
+ end function
23
+
24
+ law "|tuple dimensions determine orientation| is
25
+ description:
26
+ "Width comes first and height second in a picture's dimensions."
27
+ end
28
+ definition:
29
+ for all (picture of type Picture) =>
30
+ orientation picture must be
31
+ first matching case of picture.dimensions:
32
+ when (width, height) then
33
+ if width > height then Landscape
34
+ else if width < height then Portrait
35
+ else Square
36
+ end if
37
+ end if
38
+ end case
39
+ end
40
+ end
41
+ expect orientation picture = expected
42
+ in table (picture, expected):
43
+ row Picture record{fileName = "wide.jpg", dimensions = (12, 8)}, Landscape
44
+ row Picture record{fileName = "tall.jpg", dimensions = (8, 12)}, Portrait
45
+ row Picture record{fileName = "square.jpg", dimensions = (10, 10)}, Square
46
+ end
47
+ end
@@ -0,0 +1,56 @@
1
+ unit example.payloads
2
+ type Picture is a record{
3
+ fileName of type Text
4
+ dimensions of type record{width of type Nat height of type Nat}
5
+ } end type
6
+ type Delivery is one of
7
+ | Download of type record{document of type Text}
8
+ | Unavailable
9
+ end type
10
+ type Envelope (a of type Type) is a record{payload of type record{value of type
11
+ a copy of type a}} end type
12
+ type Box (a of type Type) is one of | Box of type record{value of type a}
13
+ end type
14
+ function width (picture of type Picture) -> Nat is picture.dimensions.width
15
+ end function
16
+ function label (delivery of type Delivery) -> Text is
17
+ first matching case of delivery:
18
+ when Download record{document = name} then name
19
+ when Unavailable then "unavailable"
20
+ end case
21
+ end function
22
+ function extract (envelope of type Envelope a) -> a is envelope.payload.value
23
+ end function
24
+ function unpack (envelope of type Envelope a) -> record{value of type a copy
25
+ of type a} is envelope.payload end function
26
+ function boxedPayload (box of type Box a) -> record{value of type a} is
27
+ first matching case of box: when Box value then value end case end function
28
+ law pictureWidth is definition:
29
+ width (Picture record{fileName = "photo", dimensions = record{height = 3,
30
+ width = 2}}) must be 2
31
+ end end
32
+ law downloadLabel is definition: label (Download record{document = "paper"})
33
+ must be "paper" end end
34
+ law wrappedText is definition: extract (Envelope record{payload = record{value =
35
+ "note", copy = "other"}}) must be "note" end end
36
+ law wrappedBoolean is definition: extract (Envelope record{payload =
37
+ record{value = true, copy = false}}) must be true end end
38
+ law wholeRecord is definition: unpack (Envelope record{payload = record{value =
39
+ true, copy = false}}) = record{copy = false, value = true} end end
40
+ law boxedResult is definition: boxedPayload (Box record{value = true}) =
41
+ record{value = true} end end
42
+ law boxedRows is definition: for all (box of type Box Text) => boxedPayload box
43
+ = boxedPayload box end end
44
+ expect (boxedPayload box).value = expected in table (box, expected):
45
+ row Box record{value = "note"}, "note"
46
+ row Box record{value = "paper"}, "paper"
47
+ end end
48
+ law labels is
49
+ definition: for all (delivery of type Delivery) => label delivery = label
50
+ delivery end end
51
+ expect label delivery = expected
52
+ in table (delivery, expected):
53
+ row Download record{document = "paper"}, "paper"
54
+ row Unavailable, "unavailable"
55
+ end
56
+ end
@@ -0,0 +1,64 @@
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
39
+ end
40
+ end
41
+
42
+ law "|mutually dependent snapshots have no first value| is
43
+ definition:
44
+ for all (snapshot of type FirstSnapshot) =>
45
+ false
46
+ end
47
+ end
48
+ end
49
+
50
+ law "|a completed chain has no remaining approval| is
51
+ definition:
52
+ chainLength (ApprovalChain record{next = Nothing}) must be 0
53
+ end
54
+ end
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
64
+ end