Skip to main content

Func

rascal-0.34.0

Synopsis

Func is a tiny functional language; We present several interpreters for it.

Description

Func is a functional language with the following features:

  • A program consists of a number of function declarations.
  • A function declaration consists of a name, zero or more formal parameter names and an expression.
  • An expression can be one of:
    • an integer constant.
    • a variable.
    • arithmetic operators +, -, * and /.
    • comparison operators <, \<=, > and >=.
    • a call of a function.
    • an if expression.
    • a sequence of expressions (;).
    • an assignment (:=).
    • a let expression to introduce new bindings for local variables.
    • an address of a variables (denoted by &).
    • dereference of a variable (denoted by *).

Some features add more complexity to an interpreter, therefore we present four interpreters Eval0, Eval1, Eval2 and Eval2 that implement increasingly complex features:

FeatureEval0Eval1Eval2Eval3
function declarationyyyy
integer constantyyyy
variableyyyy
arithmetic operatorsyyyy
comparison operatorsyyyy
callyyyy
ifyyyy
letyyy
sequenceyy
assignmentyy
address operatory
dereference operatory

Examples

Here are several versions of the factorial function that use more and more features of the Func language:

F0.func:

fact(n) = if n <= 1 then
1
else
n * fact(n-1)
end

F1.func:

fact(n) = let
x = n
in
if x <= 1 then
x
else
x * fact(x-1)
end
end

F2.func:

fact(n) = if n <= 1 then 
n := 1
else
n := n * fact(n-1)
end;
n

F3.func:

swap(a, b) =
let
temp = *a
in
*a := *b;
*b := temp
end

fact(n) = let
x = 1,
y = 0
in
if n <= 1 then
x := 1
else
x := n * fact(n-1)
end;
swap(&x, &y);
y
end

For convenience, we use two versions of these examples for each Fᵢ:

  • A file Fᵢ.func that contains the code as shown above.
  • A file Fᵢ.rsc a Rascal file that declares a string variable Fᵢ with the same content.

For instance, F0.rsc looks like this

module demo::lang::Func::programs::F0

public str F0 =
"fact(n) = if n \<= 1 then
1
else
n * fact(n-1)
end";

NOTE: Note the escaped < character in \\<=. This is necessary since < and > are used in strings to enclose interpolations (insertion of the value of a Rascal expression). Both symbols need to be escaped when used as literal symbol and not as interpolation.