Skip to content
Rizo Isrof edited this page Dec 19, 2016 · 15 revisions

Language Guide

This document is work in progress!

This page will introduce you to the main concepts of the Fold programming language. Then it will teach you how to effectively use the tools for building, testing and running your Fold programs.

Hello World

-- Here is a classical program which first implementation
-- dates to 1974, written by Brian Kernighan at Bell Labs.

print "hello, world!"

If saved in a file called hello.fold it can be run by simply invoking it by name.

$ fold hello
hello, world!

Alternatively you can start an interactive session and open the program from there. Which we suggest, since this way you can test all the examples in this guide.

$ fold
-> open hello
hello, world
 = ()

At the first glance it seems that we can't learn much from this small example, but in fact, it shows as some important though invisible aspects of the language. The first one is that there are no special requirements for programs to start doing things. No namespace declaration and no main function. Just a pretty nostalgic greeting.

Literals

...

Variable Bindings

Values may happily exist by themselves but for us it is more useful if we keep track of them by binding them to variables.

x = 42
str_1 = "This is a string"

Blocks

A block is a sequence of expressions that has a value of the last one.

x = do
    2 + 2
    42
end

x == 42

Blocks can be used anywhere in Fold, even in the middle of simple arithmetic expressions:

40 + do
    print "Hey, we're in the middle of the block!"
    2
end

x == 42

Blocks are so useful that actually they are used by almost all core functions in Fold. Even the very function that creates functions uses blocks.

-- Next we will show how some important language
-- constructs _desugar_ into elementary expressions.
fun sum x y
    x + y
end

-- Is transformed into:
sum = x y -> do
    x + y
end

Functions

Functions are also first-class values. Which means we may bind them to a variable, pass as an argument to another function, or store in a collection.

You may have noticed in our hello world program, which used print function, that the syntax for function invocation differs from the common mathematical notation – it omits the parentheses. Why do we need them at all? And even if we wanted we could add the parentheses and in many cases we actually have to.

(print "Hello, world")

Modules and Signatures

Modules in Fold are a fundamental concept that enable flexible code reuse, advanced abstractions and composability.

What modules can:

  • Organize functions, values, type and other modules in hierarchical logical units (like namespaces).
  • Manage visibility by defining public and private values.
  • Implement concrete behaviours by complying to interfaces.
  • Act as functions from modules to modules to abstract over implementation details and produce generic modules.
mod Option
sig Animal
  type Self

  new :: String -> Self

  name :: Self -> String

  noise :: Self -> String

  -- Signatures can provide default method definitions.
  talk self =
    print ("%s says %s" % (name self) (noise self))
end