Functions Reference
This section documents the keywords used to define functions and computations in L4. Functions represent individual rules.
Overview
L4 functions are defined using a combination of keywords that specify parameters, return types, and the function body. The typical structure is:
GIVEN parameter IS A Type -- Parameters (optional)
GIVETH A ReturnType -- Return type (optional)
DECIDE name MEANS expression -- Definition
Keywords
| Keyword | Purpose | Required |
|---|---|---|
| GIVEN | Declares function parameters with types | For functions with inputs |
| GIVETH / GIVES | Specifies the return type | Optional (can be inferred) |
| DECIDE | Defines a named value or function | Yes (or use bare MEANS) |
| MEANS | Connects the function name to its body | Yes |
| YIELD | Creates anonymous functions (lambdas) | For inline functions |
| WHERE | Adds local definitions after an expression | Optional |
| LET | Adds local definitions before an expression | Optional |
| AKA | Creates aliases for definitions | Optional |
Quick Examples
Simple Function
GIVEN x IS A NUMBER
double x MEANS x TIMES 2
Fully Annotated Function
GIVEN n IS A NUMBER
GIVETH A NUMBER
DECIDE factorial n MEANS
IF n EQUALS 0
THEN 1
ELSE n TIMES factorial (n MINUS 1)
With Local Definitions
circleArea radius MEANS
pi TIMES radius TIMES radius
WHERE
pi MEANS 3.14159
With Aliases
`total cost` AKA `final price` MEANS
basePrice PLUS tax
Anonymous Functions (Lambdas)
Use GIVEN ... YIELD for inline functions:
GIVEN numbers IS A LIST OF NUMBER
`all positive` MEANS all (GIVEN n YIELD n > 0) numbers
Function Definition Patterns
Named Constants
No parameters needed:
DECIDE pi IS 3.14159
DECIDE greeting IS "Hello, World!"
Single Parameter
GIVEN x IS A NUMBER
square x MEANS x TIMES x
Multiple Parameters
GIVEN a IS A NUMBER
b IS A NUMBER
add a b MEANS a PLUS b
Boolean Decisions
Use DECIDE ... IF for boolean-returning functions:
GIVEN age IS A NUMBER
DECIDE `is adult` IF age >= 18
Mixfix Function Definitions
L4 functions do not have to put the function name first. In a mixfix definition, the parameters declared with GIVEN are interleaved with the identifier words of the function name, so calls read like natural language:
GIVEN mom IS A STRING
dad IS A STRING
kid IS A STRING
GIVES A STRING
mom and dad `have a baby named` kid MEANS
CONCAT "mother: ", mom, ", father: ", dad, ", child: ", kid
The call site uses the same word order:
#EVAL "alice" and "bob" `have a baby named` "carol"
Key points:
- Parameters may appear before, between, or after the function's identifier words — enabling infix (
x plus y) and postfix (n squared) conventions as well. - Backtick-quoted identifiers may contain spaces, so the "name words" can be multi-word legal phrases (
`is entitled to`). - The words of the pattern that are not parameters act as mixfix keywords. The typechecker records each mixfix definition — its written pattern and checked type signature — in a mixfix registry, which it uses to resolve call sites where arguments and keywords are interleaved.
- Tooling reuses the registry: the document renderer derives prose headings for mixfix functions from the registered pattern, rendering each parameter slot in the author's original word order (so even functions that are never called elsewhere get proper headings).
For when to reach for mixfix and more worked examples, see Advanced Patterns: Mixfix Function Definitions.
Local Bindings
Two styles for introducing local definitions:
WHERE (definitions after)
result MEANS x PLUS y
WHERE
x MEANS 10
y MEANS 20
LET (definitions before)
result MEANS
LET x IS 10, y IS 20
IN x PLUS y
See Also
- Types Reference - Available types for parameters and returns
- Operators Reference - Operators for expressions
- Control Flow Reference - IF, CONSIDER, and other constructs