Common L4 Patterns
Reference of frequently used basic L4 patterns with working examples: type declarations, predicates, list operations, conditionals, and simple regulative rules. For more advanced structures (progressive brackets, mixfix, statutory isomorphism, nested scoping), see Advanced Patterns.
Type Patterns
Simple Record
DECLARE Person
HAS name IS A STRING
age IS A NUMBER
email IS A STRING
Record with Nested Type
DECLARE Address
HAS street IS A STRING
city IS A STRING
country IS A STRING
DECLARE Person
HAS name IS A STRING
address IS AN Address
Enumeration (Fixed Choices)
DECLARE Status IS ONE OF
Active
Suspended
Terminated
Enumeration with Data
DECLARE PaymentStatus IS ONE OF
Pending
Paid HAS date IS A DATE
amount IS A NUMBER
Rejected HAS reason IS A STRING
Union Type (Either/Or)
DECLARE LegalEntity IS ONE OF
Individual HAS person IS A Person
Corporation HAS company IS A Company
Function Patterns
Simple Boolean Predicate
GIVEN person IS A Person
GIVETH A BOOLEAN
DECIDE `is adult` IF person's age >= 18
Boolean with Multiple Conditions
GIVEN person IS A Person
GIVETH A BOOLEAN
DECIDE `is eligible` IF
person's age >= 18
AND person's hasValidID
AND NOT person's isBanned
Computation Function
GIVEN principal IS A NUMBER
rate IS A NUMBER
years IS A NUMBER
GIVETH A NUMBER
`compound interest` MEANS principal * (EXPONENT (1 + rate) years)
Function with WHERE
GIVEN loan IS A Loan
GIVETH A NUMBER
`monthly payment` MEANS
payment
WHERE
p MEANS loan's principal
r MEANS loan's rate / 12
n MEANS loan's months
payment MEANS p * (r * factor) / (factor - 1)
factor MEANS EXPONENT (1 + r) n
Pattern Matching Function
GIVEN status IS A Status
GIVETH A STRING
`describe status` MEANS
CONSIDER status
WHEN Active THEN "Currently active"
WHEN Suspended THEN "Temporarily suspended"
WHEN Terminated THEN "No longer active"
Pattern Matching with Data Extraction
GIVEN payment IS A PaymentStatus
GIVETH A STRING
`payment description` MEANS
CONSIDER payment
WHEN Pending THEN "Awaiting payment"
WHEN Paid d a THEN CONCAT "Paid ", TOSTRING a, " on ", TOSTRING d
WHEN Rejected r THEN CONCAT "Rejected: ", r
List Patterns
Create a List
numbers MEANS LIST 1, 2, 3, 4, 5
names MEANS LIST "Alice", "Bob", "Charlie"
`no numbers` MEANS EMPTY
Check if List is Empty
GIVEN items IS A LIST OF Item
GIVETH A BOOLEAN
`has items` MEANS NOT null items
Get First Element
GIVEN items IS A LIST OF Item
GIVETH A MAYBE Item
`first item` MEANS
CONSIDER items
WHEN EMPTY THEN NOTHING
WHEN x FOLLOWED BY rest THEN JUST x
Check if Element is in List
GIVEN item IS AN Item
items IS A LIST OF Item
GIVETH A BOOLEAN
`contains item` MEANS elem item items
Check All Elements
GIVEN people IS A LIST OF Person
GIVETH A BOOLEAN
`all adults` MEANS all (GIVEN p YIELD p's age >= 18) people
Check Any Element
GIVEN people IS A LIST OF Person
GIVETH A BOOLEAN
`any minor` MEANS any (GIVEN p YIELD p's age < 18) people
Transform All Elements
GIVEN numbers IS A LIST OF NUMBER
GIVETH A LIST OF NUMBER
`double all` MEANS map (GIVEN n YIELD n * 2) numbers
Filter Elements
GIVEN people IS A LIST OF Person
GIVETH A LIST OF Person
`only adults` MEANS filter (GIVEN p YIELD p's age >= 18) people
Conditional Patterns
Simple If-Then-Else
GIVEN age IS A NUMBER
GIVETH A STRING
`age category` MEANS
IF age < 18 THEN "minor"
ELSE "adult"
Multi-Way Conditions
Prefer BRANCH over nested IF/ELSE chains:
GIVEN score IS A NUMBER
GIVETH A STRING
`grade` MEANS
BRANCH
IF score >= 90 THEN "A"
IF score >= 80 THEN "B"
IF score >= 70 THEN "C"
IF score >= 60 THEN "D"
OTHERWISE "F"
Conditional in Context
DECIDE amount IS
IF isVIP THEN baseAmount * 0.9
ELSE baseAmount
Regulative Rule Patterns
Simple Obligation
GIVETH A DEONTIC Party Action
`delivery obligation` MEANS
PARTY Seller
MUST `deliver goods`
WITHIN 14
HENCE FULFILLED
LEST BREACH
Permission
GIVETH A DEONTIC Party Action
`inspection right` MEANS
PARTY Buyer
MAY `inspect goods`
HENCE FULFILLED
Prohibition
GIVETH A DEONTIC Party Action
`confidentiality clause` MEANS
PARTY Employee
SHANT `disclose confidential information`
HENCE FULFILLED
LEST BREACH
Chained Obligations
GIVETH A DEONTIC Party Action
`sale contract` MEANS
PARTY Seller
MUST `deliver goods`
WITHIN 14
HENCE
PARTY Buyer
MUST `pay invoice`
WITHIN 30
HENCE FULFILLED
LEST BREACH BY Buyer
LEST BREACH BY Seller
Conditional Obligation
GIVETH A DEONTIC Party Action
`conditional payment` MEANS
PARTY Buyer
MUST `pay` amount PROVIDED amount > 0
WITHIN 30
HENCE FULFILLED
LEST BREACH
Parallel Obligations (RAND)
GIVETH A DEONTIC Party Action
`mutual obligations` MEANS
(PARTY Seller MUST `deliver` WITHIN 14 HENCE FULFILLED LEST BREACH)
RAND
(PARTY Seller MUST `provide documentation` WITHIN 14 HENCE FULFILLED LEST BREACH)
Alternative Paths (ROR)
GIVETH A DEONTIC Party Action
`delivery options` MEANS
(PARTY Seller MUST `ship goods` WITHIN 14 HENCE FULFILLED)
ROR
(PARTY Seller MUST `arrange pickup` WITHIN 7 HENCE FULFILLED)
LEST BREACH
Money and Currency Patterns
Money Type
DECLARE Money
HAS amount IS A NUMBER
currency IS A STRING
Currency Constructors
GIVEN n IS A NUMBER
GIVETH A Money
USD MEANS Money n "USD"
GIVEN n IS A NUMBER
GIVETH A Money
EUR MEANS Money n "EUR"
Money Comparison
GIVEN m1 IS A Money
m2 IS A Money
GIVETH A BOOLEAN
`money equals` MEANS
m1's amount = m2's amount
AND m1's currency EQUALS m2's currency
See also the currency library for ISO 4217 currency handling.
Date Patterns
Using the daydate Library
IMPORT daydate
-- Create a date (day, month, year)
expiryDate MEANS Date 31 12 2025
-- Compare dates
GIVEN d1 IS A DATE
d2 IS A DATE
GIVETH A BOOLEAN
`comes first` MEANS d1 `is before` d2
Days Between Dates
GIVEN start IS A DATE
end IS A DATE
GIVETH A NUMBER
`days between` MEANS Day end MINUS Day start
See the daydate library for the full date toolkit.
Testing Patterns
Unit Test with #EVAL
#EVAL `is adult` (Person "Test" 18 "test@example.com") -- TRUE
#EVAL `is adult` (Person "Test" 17 "test@example.com") -- FALSE
Integration Test with #TRACE
#TRACE `sale contract` AT 0 WITH
PARTY Seller DOES `deliver goods` AT 10
PARTY Buyer DOES `pay invoice` AT 25
-- Expected: FULFILLED
Boundary Testing
-- Test boundaries
#EVAL `is adult` (Person "Edge" 18 "e@example.com") -- TRUE (exactly at boundary)
#EVAL `is adult` (Person "Edge" 17 "e@example.com") -- FALSE (just below)
#EVAL `is adult` (Person "Edge" 19 "e@example.com") -- TRUE (just above)
Anti-Patterns (What NOT to Do)
❌ Missing Parentheses with Field Access
-- ❌ Wrong
length charity's governors > 0
-- ✅ Right
length (charity's governors) > 0
❌ Forgetting ELSE Branch
-- ❌ Wrong (missing ELSE)
DECIDE result IS IF condition THEN "yes"
-- ✅ Right
DECIDE result IS IF condition THEN "yes" ELSE "no"
❌ Missing GIVETH
-- ❌ Wrong
GIVEN n IS A NUMBER
square MEANS n * n
-- ✅ Right
GIVEN n IS A NUMBER
GIVETH A NUMBER
square MEANS n * n
❌ Inconsistent Indentation
-- ❌ Wrong (misaligned fields)
DECLARE Person
HAS name IS A STRING
age IS A NUMBER
email IS A STRING
-- ✅ Right (all fields aligned)
DECLARE Person
HAS name IS A STRING
age IS A NUMBER
email IS A STRING
Quick Reference
| Pattern | When to Use |
|---|---|
DECLARE ... HAS |
Define a record type |
DECLARE ... IS ONE OF |
Define an enumeration |
GIVEN ... GIVETH ... DECIDE |
Define a function |
CONSIDER ... WHEN |
Pattern matching |
IF ... THEN ... ELSE |
Conditional expression |
BRANCH |
Multi-way conditions |
PARTY ... MUST |
Create obligation |
PARTY ... MAY |
Create permission |
PARTY ... SHANT |
Create prohibition |
RAND |
Parallel obligations |
ROR |
Alternative obligations |
#EVAL |
Test expression |
#TRACE |
Test regulative rule |
See Also
- Advanced Patterns - Progressive brackets, mixfix, statutory isomorphism, nested scoping
- Functions Reference - GIVEN, GIVETH, MEANS, DECIDE, WHERE
- Types Reference - DECLARE, HAS, IS ONE OF, LIST, MAYBE
- Regulative Reference - PARTY, MUST, MAY, SHANT, HENCE, LEST
- Reference Home - Complete language reference