Skip to content
Rizo Isrof edited this page Jan 31, 2017 · 16 revisions

This document is work in progress!

This document is the official reference for Fold programming language.

Syntax Elements

The language is made of expressions and the main elements of the expressions are:

  • Atoms – simple values
  • Forms – compounds of atoms

Core Syntax

The core syntax of Fold can be seen as a simplified Lisp.

expression =
  | atom
  | atom expression
  | '(' expression ')'

atom =
  | <bool>
  | <char>
  | <float>
  | <int>
  | <string>
  | <id>
  | <symbol>

Forms

  • Arithmetic Forms
  • Binding Forms
  • Lambda Forms

Binding Terms

Binding forms are syntactic elements that give names to values.

x = 5

Lambda Forms

x y -> x + y

sum : int -> int -> int
sum x y = x + y

## Primitives

Boolean values and logical operators.

```haskell
-> True
:: Bool = True

-> False
:: Bool = False

-> True && not (True || False)
:: Bool = False

Note: the lines that start with an arrow, ->, are treated as the input of the interpreter, followed by the inferred type and the evaluated result.

Numeric values with diferent radixes are supported. Underscores in the numeric values can improve readability and are ignored by the compiler.

-> 123456789
:: Int = 123456789

-> 100_000_000
:: Int = 100000000

-> 0b0101010
:: Int = 42

-> 0xBADC0DE
:: Int = 195936478

-> 0o377
:: Int = 255

-> 3.1415926
:: Float = 3.1415926

-> 10.0 * 2 - 10.0 / 2
:: Float = 15.0

Strings and characters and multi-line strings.

-> 'a'
:: Char = 'a'

-> "hello"
:: String = "hello"

-> """
   Multi-line strings can span multiple lines and
   don't require escapes for "quotation marks"
   """
:: String = "Multi-line strings can span multiple lines and\ndon't require escapes for \"quotation marks\""

Unit – a special value that is used for expressions with no value. Unit has its own type () and only one value (). Mainly used as a return type for effectful functions with no return value.

Note: Unit values are always omitted from the interpreter's output.

-> ()

Regular expressions. The interpreter shows examples of matched strings.

-> /^[a-z0-9_.+-]+@[a-z0-9-]+\.[a-z]+$/
:: Regex = /^[a-z0-9_.+-]+@[a-z0-9-]+\.[a-z]+$/
-- Examples:
--   - "[email protected]"
--   - "[email protected]"
-- Bindings can be used to name values.
-> a = 42
a :: Int = 42

-- Use blocks to group multiple expressions.
-> do
     a = 2
     b = 2
     a + b
   end
:: Int = 4

-- Binding blocks introduce variables in a delimited scope.
-> let
     a = 2
     b = 2
   in
     a + b
   end
:: Int = 4

Collections

-- Tuples / Named Tuples (aka Records)
()
(1, "Hello")
(name: "Alan", age: 23)

-- Linked Lists
[]
[1, 2, 3, 4, 5]

-- Hash-maps
{"x": 89, "y": 32}

-- Arrays
([1, 2, 3, 4, 5] : Array Int)

Definitions

  • =, let, where
name = value

let name = value
    name = value
    ...
in
  expression
end

expression where
  name = value
  name = value
  ...
end
  • function, macro
function name arg args...
  ...
end

macro name arg args...
  ...
end
  • module, interface
module Name
  ...
end

module Name(Arg : M)
  ...
end

interface Name
  ...
end

interface Name (Arg :: M)
  ...
end
  • type
type name = t1
type name = t1, t2, ...

type name = A