Marigold Language Documentation

website·Indexed 30 days ago

https://marigold-language.vercel.app

A Lambda Calculus DSL designed for a simple, quick coding experience. Documentation covers installation via pipx, language fundamentals including variables, booleans, numbers, lists, and pairs, as well as advanced concepts like pipelines, guards, recursive functions, and anonymous functions/lambdas.

Tokens
3.8K
Snippets
30
Records
41
Agent score
75%

What's inside Marigold

  1. Overview of the Marigold Lambda Calculus DSL

    Marigold is a Domain Specific Language (DSL) built on $\lambda$-Calculus. In Marigold, every element—including numbers, booleans, and other datatypes—is represented as a function. The language implements pure lambda calculus patterns while applying modifications to improve code readability.
  2. Overview of Marigold language design goals

    Marigold is designed as a supporting language for lambda calculus, aiming to preserve core functional patterns while remaining accessible to humans. Its primary design pillars include:

    • Faithfulness to Lambda Calculus: Preserves essential patterns such as fixed point recursion, Church encodings, and the fundamental logic of lambda calculus.
    • Human-Friendliness: Prioritizes readability over strict adherence to lambda calculus notation. For example, it implements operators like addition in a way that is easier to read and write than the standard function argument argument pattern.
    • Lightweight Syntax: Minimizes boilerplate and avoids excessive keywords to provide a streamlined coding experience.
  3. Represent numbers in Marigold

    Numbers in Marigold are represented as Church Numerals, which represent natural numbers (positive integers only, no decimals). A number n is represented as a lambda that takes a function f and a value x, applying f to x exactly n times.
  4. Understand variable behavior in Marigold

    In Marigold, variables do not store data; they act as nicknames for expressions. Because they are not storage containers, referencing a variable multiple times will re-evaluate the underlying expression each time. For example, if a variable is assigned the result of an ask command and used twice, the user will be prompted twice. All variables in Marigold are immutable, meaning there are no in-place operations. To achieve behavior similar to mutation, use the -> operator.
  5. Use pipelines for data flow in Marigold

    Marigold uses the | operator to create pipelines, allowing values to be passed as the first argument to a function. This reduces the need for deeply nested function applications and creates a cleaner data flow.

    • A single pipe a | b c d is equivalent to b a c d.
    • Chained pipes a | b | c are equivalent to c (b a).
    // Equivalent to b a c d
    a | b c d
    
    // Equivalent to c (b a)
    a | b | c
  6. Create and use Pairs in Marigold

    Pairs are datatypes in Marigold used to hold two values. They are particularly useful for recursive functions and folds as a more efficient alternative to two-item lists.
    ([item 1],[item 2])
    
    // Or using builtin function notation (primarily for pipelines)
    PAIR [item 1] [item 2]
  7. Define and use functions in Marigold

    Functions in Marigold are defined using the def keyword. Although all functions are technically unary, the compiler automatically curries arguments, allowing for functions of any arity. A function returns the last value evaluated; this is typically the last line of the function, but if statements and pipelines also act as return values.
    def [function_name]([comma seperated arguments]) { [multiline expression] }
  8. Install Marigold CLI from source

    Marigold is installed globally using pipx. Before proceeding, ensure pipx is installed on your system. To install Marigold, clone the repository, install the local directory via pipx, and inject the lark dependency.
    git clone https://github.com/CaseyMasonek/marigold.git
    cd marigold
    pipx install .
    pipx inject marigold lark
  9. Prevent function nesting using the guard keyword

    The guard keyword in Marigold acts as an early return mechanism. It wraps the subsequent lines of a function in an implicit if statement, allowing developers to handle edge cases or invalid conditions early and avoid excessive nesting of the main logic.
    guard ([condition]) [value];
  10. Implement conditional logic using Booleans

    Because booleans in Marigold are functions that select one of two arguments, you can create a conditional by placing the condition before the two possible outcomes:

    [condition] [value if true] [value if false]

    If the condition is true, it returns the first value; if false, it returns the second.

  11. Implement multiple conditions using elif in Marigold

    The elif (else if) keyword provides a shorthand for nesting multiple if statements. It allows for a chain of conditions before a final else value.
    // Shorthand version
    if (condition1) value1 elif (condition2) value2 else value3
    
    // Equivalent to nested version:
    if (condition1) value1 else { if (condition2) value2 else value3 }
  12. Perform Right Folds on lists

    Marigold uses Church list encoding, which forces a right fold. A fold reduces a list to a single value using an accumulation function and an initial value. In a right fold, the accumulation starts from the last item of the list.

    Structure: [list] [accumulation function] [initial r value]

    Accumulation Function: Typically takes the head h and the accumulator r, returning a new value for r for the next item.

    // Example: Summing a list
    // Accumulation function: @hr.h+r
    // Initial value: 0