Skip to content

Commit 368d695

Browse files
authored
Merge pull request #815 from smucclaw/thomasgorissen/documentation-v2
feat(documentation): Mixfix/infix/postfix
2 parents 67c9785 + 8d832f4 commit 368d695

File tree

10 files changed

+462
-20
lines changed

10 files changed

+462
-20
lines changed

doc/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ Learn L4 from scratch. No prior programming experience required.
5151

5252
Deep dives for production use.
5353

54-
- Real Regulatory Schemes
55-
- Cross-Cutting Concerns
56-
- Complex Contracts
57-
- Production Patterns
54+
- Module A1: Real Regulatory Schemes
55+
- Module A2: Cross-Cutting Concerns
56+
- Module A3: Complex Contracts
57+
- Module A4: Production Patterns
5858

5959
**Prerequisites:** Foundation Course
6060

doc/concepts/legal-modeling/regulative-rules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ LEST BREACH
245245

246246
```
247247
┌──────────────────────────────────────────────┐
248-
│ CREATED
249-
│ (rule becomes active)
248+
│ CREATED │
249+
│ (rule becomes active) │
250250
└──────────────────────────────────────────────┘
251251
252252

doc/courses/foundation/module-3-control-flow.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ GIVETH A STRING
3333

3434
Every `IF` must have both `THEN` and `ELSE` branches—L4 requires you to handle all cases.
3535

36-
### Nested Conditionals
36+
### Nested Conditionals with BRANCH
3737

3838
```l4
3939
GIVEN score IS A NUMBER
4040
GIVETH A STRING
41-
`the letter grade` MEANS
42-
IF score >= 90 THEN "A"
43-
ELSE IF score >= 80 THEN "B"
44-
ELSE IF score >= 70 THEN "C"
45-
ELSE "F"
41+
`grade` MEANS
42+
BRANCH
43+
IF score >= 90 THEN "A"
44+
IF score >= 80 THEN "B"
45+
IF score >= 70 THEN "C"
46+
IF score >= 60 THEN "D"
47+
OTHERWISE "F"
4648
```
4749

4850
### Conditional in Context

doc/reference/functions/DECIDE.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ DECIDE isEligible IF
9797
- **[WHERE](WHERE.md)** - Local definitions
9898
- **[IF](../control-flow/IF.md)** - Boolean condition form
9999

100-
> Note: IS is part of the DECIDE syntax, not a separate keyword page.
100+
## Tutorials
101101

102-
## See Also
103-
104-
- **[Keywords Reference](README.md)** - All keywords
102+
- **[Using Infix, Postfix, and Mixfix Functions](../../tutorials/natural-language-functions/natural-language-functions.md)** - Define functions that read like natural language

doc/reference/functions/MEANS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ LET y MEAN 10 IN y PLUS 1
8080
- **[DECIDE](DECIDE.md)** - Declares a decision
8181
- **[WHERE](WHERE.md)** - Local definitions
8282

83-
> Note: IS and BE are alternative forms with the same meaning as MEANS.
83+
## Tutorials
84+
85+
- **[Using Infix, Postfix, and Mixfix Functions](../../tutorials/natural-language-functions/natural-language-functions.md)** - Define functions that read like natural language

doc/tutorials/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ New to L4? Start here:
1313

1414
## By Topic
1515

16+
### Natural Language Functions
17+
18+
Write functions that read like legal prose:
19+
20+
- **[Infix, Postfix, and Mixfix Functions](natural-language-functions/natural-language-functions.md)** - Define operators like `` `is eligible for` `` or `` `percent` ``
21+
22+
**Audience:** All L4 users
23+
**Prerequisites:** Basic function syntax
24+
1625
### LLM Integration
1726

1827
Working with AI and language models in L4:

doc/tutorials/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
- [Wedding Vows](getting-started/wedding-vows.md)
1111
- [Common Patterns](getting-started/common-patterns.md)
1212

13+
## Natural Language Functions
14+
15+
- [Using Infix, Postfix, and Mixfix Functions](natural-language-functions/natural-language-functions.md)
16+
1317
## LLM Integration
1418

1519
- [Getting Started with LLM](llm-integration/llm-getting-started.md)

doc/tutorials/getting-started/wedding-vows.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A fun introduction to regulative rules using wedding vows.
55
**Audience:** Anyone new to regulative rules
66
**Prerequisites:** Basic L4 knowledge
77
**Time:** 20 minutes
8-
**Goal:** Understand MUST, MAY, SHANT, HENCE, and LEST through a familiar example
8+
**Goal:** Understand MUST, MAY, SHANT, DO, HENCE, and LEST through a familiar example
99

1010
---
1111

@@ -38,7 +38,7 @@ DECLARE Action IS ONE OF
3838
`have and hold`
3939
`support`
4040
`care for`
41-
abandon
41+
`abandon`
4242
`be unfaithful`
4343
```
4444

@@ -76,7 +76,7 @@ GIVETH DEONTIC Person Action
7676
| ------------------------------ | ------------------------------------------------------------------------ |
7777
| `GIVETH DEONTIC Person Action` | This returns an obligation/permission involving these actors and actions |
7878
| `PARTY Spouse1` | Spouse1 is the one with the obligation |
79-
| `MUST \`exchange vows\`` | They must exchange vows |
79+
| `` `MUST `exchange vows` `` | They must exchange vows |
8080
| `WITHIN 1` | Within 1 day (the wedding day) |
8181
| `HENCE ...` | If they do, then Spouse2 must respond |
8282
| `LEST BREACH` | If they don't, it's a breach |
@@ -181,6 +181,7 @@ GIVETH DEONTIC Person Action
181181
| `MUST` | Required action | Must love and cherish |
182182
| `MAY` | Permitted action | May have pets |
183183
| `SHANT` | Prohibited action | Shall not be unfaithful |
184+
| `DO` | Possible action | enters the premise |
184185

185186
---
186187

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
§ `Natural Language Functions Tutorial`
2+
3+
-- This file demonstrates infix, postfix, and mixfix function patterns
4+
-- that allow L4 code to read like natural language.
5+
6+
-- ============================================================================
7+
-- INFIX FUNCTIONS
8+
-- Pattern: param `keyword` param
9+
-- ============================================================================
10+
11+
-- Simple numeric infix operator
12+
GIVEN a IS A NUMBER, b IS A NUMBER
13+
GIVETH A NUMBER
14+
a `plus` b MEANS a + b
15+
16+
-- Multi-word infix operator
17+
GIVEN person IS A STRING, program IS A STRING
18+
GIVETH A BOOLEAN
19+
person `is eligible for` program MEANS TRUE
20+
21+
-- Test infix calls
22+
#EVAL 3 `plus` 5
23+
#EVAL "Alice" `is eligible for` "Medicare"
24+
25+
-- Infix can also be called with prefix notation
26+
#EVAL `plus` 10 20
27+
28+
-- ============================================================================
29+
-- POSTFIX FUNCTIONS
30+
-- Pattern: param `keyword`
31+
-- ============================================================================
32+
33+
-- Simple postfix operator
34+
GIVEN amount IS A NUMBER
35+
GIVETH A NUMBER
36+
amount `percent` MEANS amount / 100
37+
38+
-- Another postfix example
39+
GIVEN n IS A NUMBER
40+
GIVETH A NUMBER
41+
n squared MEANS n * n
42+
43+
-- Test postfix calls
44+
#EVAL 50 `percent`
45+
#EVAL 75 percent
46+
#EVAL 5 squared
47+
#EVAL 7 `squared`
48+
49+
-- Postfix with expressions
50+
#EVAL (10 + 5) `percent`
51+
52+
-- ============================================================================
53+
-- TERNARY MIXFIX FUNCTIONS
54+
-- Pattern: `keyword` param `keyword` param `keyword` param
55+
-- ============================================================================
56+
57+
-- Custom conditional operator
58+
GIVEN cond IS A BOOLEAN, thenVal IS A NUMBER, elseVal IS A NUMBER
59+
GIVETH A NUMBER
60+
`myif` cond `mythen` thenVal `myelse` elseVal MEANS
61+
IF cond THEN thenVal ELSE elseVal
62+
63+
-- Test ternary mixfix
64+
#EVAL `myif` TRUE `mythen` 42 `myelse` 0
65+
#EVAL `myif` FALSE `mythen` 42 `myelse` 0
66+
#EVAL `myif` (1 = 1) `mythen` (10 + 5) `myelse` (20 - 5)
67+
68+
-- ============================================================================
69+
-- QUATERNARY MIXFIX FUNCTIONS
70+
-- Pattern: param `keyword` param `keyword` param `keyword` param
71+
-- ============================================================================
72+
73+
GIVEN mummy IS A STRING, daddy IS A STRING, baby IS A STRING, nickname IS A STRING
74+
GIVETH A STRING
75+
mummy `and` daddy `had` baby `called` nickname MEANS baby
76+
77+
-- Test quaternary mixfix
78+
#EVAL "Alice" `and` "Bob" `had` "Charlie" `called` "Chuck"
79+
80+
-- Prefix notation also works
81+
#EVAL `and` "Mom" "Dad" "Kid" "Kiddo"
82+
83+
-- ============================================================================
84+
-- ARITY-5 MIXFIX FUNCTIONS
85+
-- ============================================================================
86+
87+
GIVEN a IS A NUMBER, b IS A NUMBER, c IS A NUMBER, d IS A NUMBER, e IS A NUMBER
88+
GIVETH A NUMBER
89+
a `op1` b `op2` c `op3` d `op4` e MEANS a + b + c + d + e
90+
91+
#EVAL 1 `op1` 2 `op2` 3 `op3` 4 `op4` 5
92+
#EVAL (10 - 9) `op1` (1 + 1) `op2` 3 `op3` (2 * 2) `op4` 5
93+
94+
-- ============================================================================
95+
-- RANGE CHECK MIXFIX
96+
-- A practical example for legal conditions
97+
-- ============================================================================
98+
99+
GIVEN lower IS A NUMBER, value IS A NUMBER, upper IS A NUMBER
100+
GIVETH A BOOLEAN
101+
lower `is at most` value `which is at most` upper MEANS
102+
lower <= value AND value <= upper
103+
104+
#EVAL 0 `is at most` 5 `which is at most` 10
105+
#EVAL 0 `is at most` 15 `which is at most` 10
106+
107+
-- ============================================================================
108+
-- PRACTICAL LEGAL EXAMPLE
109+
-- Functions that read like legal prose
110+
-- ============================================================================
111+
112+
DECLARE Person HAS
113+
name IS A STRING
114+
age IS A NUMBER
115+
116+
GIVEN person IS A Person
117+
GIVETH A BOOLEAN
118+
person `is an adult` MEANS person's age >= 18
119+
120+
GIVEN person IS A Person
121+
GIVETH A BOOLEAN
122+
person `is a minor` MEANS person's age < 18
123+
124+
-- Create test data
125+
`test adult` MEANS Person WITH name IS "Alice", age IS 30
126+
`test minor` MEANS Person WITH name IS "Bob", age IS 15
127+
128+
-- Test legal predicates
129+
#EVAL `test adult` `is an adult`
130+
#EVAL `test minor` `is a minor`
131+
132+
-- ============================================================================
133+
-- MIXFIX WITH WHERE CLAUSE
134+
-- Defining helper operators locally
135+
-- ============================================================================
136+
137+
GIVEN radius IS A NUMBER
138+
GIVETH A NUMBER
139+
`circle area for radius` radius MEANS
140+
radius `squared locally` TIMES pi
141+
WHERE
142+
pi MEANS 3.14159
143+
GIVEN r IS A NUMBER
144+
r `squared locally` MEANS r * r
145+
146+
#EVAL `circle area for radius` 10

0 commit comments

Comments
 (0)