|
| 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