Calculemus


Intro Module

A simple algebraic expressions example to demonstrate the basic concepts of abstract syntax tree, symbolic computation, parsing and prettyprinting.

Table of contents

Types

Type Description

expression

Abstract syntax tree of algebraic expressions.

Symbolic computation

Functions and values

Function or value Description

simplify expr

Full Usage: simplify expr

Parameters:
Returns: expression The simplified expression if simplifiable; otherwise the input itself.

Simplifies an algebraic expression completely.

Completes the work of Intro.simplify1. Recursively simplifies any immediate sub-expressions as much as possible, then applies Intro.simplify1 to the result.

expr : expression

The input expression.

Returns: expression

The simplified expression if simplifiable; otherwise the input itself.

Example

 Mul (Add(Const 0, Const 1), Add(Const 0, Const 0)) |> simplify
Evaluates to Const 0.

Example

 Add (Mul (Add (Mul (Const 0, Var "x"), Const 1), Const 3), Const 12)
 |> simplify
Evaluates to Const 15.

simplify1 expr

Full Usage: simplify1 expr

Parameters:
Returns: expression The simplified expression if simplifiable; otherwise the input itself.

Simplifies an algebraic expression at the first level.

This is an example of symbolic computation. It applies the following transformation rules

  • Const 0 * Var x, Var x * Const 0 \(\longrightarrow\) Const 0
  • Const 0 + Var x, Var x + Const 0, Const 1 * Var x, Var x * Const 1 \(\longrightarrow\) Var x
  • Const m + Const n \(\longrightarrow\) Const (m+n)
  • Const m * Const n \(\longrightarrow\) Const (m*n)
This function applies the rules only if they are applicable directly at the first level of the expression's structure. It is an auxiliary function used to define the complete function Intro.simplify that applies the rules at every level of the expression.

expr : expression

The input expression.

Returns: expression

The simplified expression if simplifiable; otherwise the input itself.

Example

 Add(Const 0, Const 1) |> simplify1
Evaluates to Const 1.

Example

 Mul (Add(Const 0, Const 1), Add(Const 0, Const 0)) |> simplify1
Evaluates to Mul (Add(Const 0, Const 1), Add(Const 0, Const 0)).

The input is returned unchanged, because even if the rules are applicable to its sub-expressions, they cannot be applied directly to the expression itself.

Parsing

Functions and values

Function or value Description

parse_atom i

Full Usage: parse_atom i

Parameters:
    i : string list - The tokenized string list to be parsed.

Returns: expression * string list The pair consisting of the parsed expression tree together with any unparsed input.

Parses an atomic expression.

Implements the atoms part of the expression's recursive descent parsing: \begin{eqnarray*} atoms & \longrightarrow & (expression) \\ & | & constant \\ & | & variable \end{eqnarray*} An atomic expression is either a constant, a variable or an arbitrary expression enclosed in brackets. See also: Intro.parse_expression; Intro.parse_product.

i : string list

The tokenized string list to be parsed.

Returns: expression * string list

The pair consisting of the parsed expression tree together with any unparsed input.

Exception Thrown with message Expected an expression at end of input, when applied to an empty list.
Exception Thrown with message Expected closing bracket, when applied to a list with an initial opening bracket but without a closing one.
Example

Parsing of a variable:

 parse_atom ["x"; "+"; "3"]
Evaluates to (Var "x", ["+"; "3"]).

Example

Parsing of a constant:

 parse_atom ["12"; "+"; "3"]
Evaluates to (Const 12, ["+"; "3"]).

Example

Parsing of an expression enclosed in brackets:

 parse_atom ["(";"12"; "+"; "3";")"]
Evaluates to (Add (Const 12, Const 3), []).

Example

 parse_atom []
Throws System.Exception: Expected an expression at end of input.

Example

 parse_atom ["(";"12"; "+"; "3"]
Throws System.Exception: Expected closing bracket.

parse_exp s

Full Usage: parse_exp s

Parameters:
    s : string - The input string to be parsed.

Returns: expression The expression corresponding to the input string.

Parses a string into an expression.

See also: Intro.parse_atom; Intro.parse_product; Intro.parse_expression

s : string

The input string to be parsed.

Returns: expression

The expression corresponding to the input string.

Exception Thrown with message Expected an expression at end of input, when applied to an incomplete string expression.
Exception Thrown with message Expected closing bracket, when applied to a string with an initial opening bracket but without a closing one.
Example

 parse_exp "(x1 + x2 + x3) * (1 + 2 + 3 * x + y)"
Evaluates to
 Mul
     (Add (Var "x1", Add (Var "x2", Var "x3")),
      Add (Const 1, Add (Const 2, Add (Mul (Const 3, Var "x"), Var "y"))))

Example

 parse_exp "x +"
Throws System.Exception: Expected an expression at end of input.

Example

 parse_exp "(12 + 3"
Throws System.Exception: Expected closing bracket.

parse_expression i

Full Usage: parse_expression i

Parameters:
    i : string list - The tokenized string list to be parsed.

Returns: expression * string list The pair consisting of the parsed expression tree together with any unparsed input.

Parses an expression.

Implements the addition part of the expression's recursive descent parsing: \begin{eqnarray*} expression & \longrightarrow & product \\ & | & product + expression \\ \end{eqnarray*} An expression is a sequence of 'product expressions' (see Intro.parse_product) separated by +. See also: Intro.parse_atom.

i : string list

The tokenized string list to be parsed.

Returns: expression * string list

The pair consisting of the parsed expression tree together with any unparsed input.

Exception Thrown with message Expected an expression at end of input, when applied to an incomplete expression.
Exception Thrown with message Expected closing bracket, when applied to a list with an initial opening bracket but without a closing one.
Note

The grammar is right-associative (\(product + expression \)). So repeated operations that lacks disambiguation are interpreted as right-associative: \(x + y + z = x + (y + z)\) .

Example

 parse_expression ["x"; "+"; "3"]
Evaluates to (Add (Var "x", Const 3), []).

Example

 parse_expression ["x"; "+";]
Throws System.Exception: Expected an expression at end of input.

Example

 parse_expression ["(";"12"; "+"; "3"]
Throws System.Exception: Expected closing bracket.

parse_product i

Full Usage: parse_product i

Parameters:
    i : string list - The tokenized string list to be parsed.

Returns: expression * string list The pair consisting of the parsed expression tree together with any unparsed input.

Parses a product expression.

Implements the products part of the expression's recursive descent parsing: \begin{eqnarray*} product & \longrightarrow & atom \\ & | & atom * product \\ \end{eqnarray*} A product expression is a sequence of 'atomic expressions' (see Intro.parse_atom) separated by *. See also: Intro.parse_expression.

i : string list

The tokenized string list to be parsed.

Returns: expression * string list

The pair consisting of the parsed expression tree together with any unparsed input.

Exception Thrown with message Expected an expression at end of input, when applied to an empty list.
Exception Thrown with message Expected closing bracket, when applied to a list with an initial opening bracket but without a closing one.
Note

The grammar is right-associative (\(atom * product \)). So repeated operations that lacks disambiguation are interpreted as right-associative: \(x * y * z = x * (y * z)\) .

Example

It parses just the first atom if applied to an addition:

 parse_product ["x"; "+"; "3"]
Evaluates to (Var "x", ["+"; "3"]).

Example

It parses a product completely:

 parse_product ["x"; "*"; "3"]
Evaluates to (Mul (Var "x", Const 3), []).

Example

It parses expressions enclosed in brackets:

 parse_product ["(";"12"; "+"; "3";")"]
Evaluates to (Add (Const 12, Const 3), []).

Example

 parse_product []
Throws System.Exception: Expected an expression at end of input.

Example

 parse_product ["(";"12"; "+"; "3"]
Throws System.Exception: Expected closing bracket.

Prettyprinting

Functions and values

Function or value Description

print_exp e

Full Usage: print_exp e

Parameters:
    e : expression - The expression to be translated.

Prints to the stdout the concrete syntax representation of an expression.

Calculates the concrete syntax of an expression e removing unnecessary brackets and writes it to the stdout. It omits the outermost brackets, and those that are implicit in rules for precedence or associativity. Seealso: Intro.string_of_exp; Intro.sprint_exp.

e : expression

The expression to be translated.

Example
 Mul (Add(Const 0, Const 1), Add(Const 0, Const 0))
 |> print_exp
After evaluation the text "<<(0 + 1) * (0 + 0)>>" is written to the stdout.

sprint_exp e

Full Usage: sprint_exp e

Parameters:
    e : expression - The expression to be translated.

Returns: string The expression's concrete syntax representation.

Returns the concrete syntax representation of an expression.

Returns a string of the concrete syntax of an expression e removing unnecessary brackets. It omits the outermost brackets, and those that are implicit in rules for precedence or associativity. Seealso: Intro.string_of_exp; Intro.print_exp.

e : expression

The expression to be translated.

Returns: string

The expression's concrete syntax representation.

Example

 Mul (Add(Const 0, Const 1), Add(Const 0, Const 0))
 |> sprint_exp
Evaluates to "<<(0 + 1) * (0 + 0)>>"

string_of_exp pr e

Full Usage: string_of_exp pr e

Parameters:
    pr : int - The precedence level of the operator of which the expression is an immediate sub-expression .
    e : expression - The expression to be translated.

Returns: string The expression's concrete syntax representation with additional brackets based on pr.

Returns a concrete syntax representation of an expression considering the precedence level of the operator of which the expression is an immediate sub-expression.

It is an auxiliary function used to define Intro.sprint_exp and Intro.print_exp. and to calculate whether or not additional brackets can be omitted.

The allocated precedences are as follows:

  • 2 to addition;
  • 4 to multiplication;
  • 0 at the outermost level.

pr : int

The precedence level of the operator of which the expression is an immediate sub-expression .

e : expression

The expression to be translated.

Returns: string

The expression's concrete syntax representation with additional brackets based on pr.

Example

 "x + 1"
 |> parse_exp
 |> string_of_exp 2
Evaluates to "x + 1"

Example

 "x + 1"
 |> parse_exp
 |> string_of_exp 3
Evaluates to "(x + 1)"

string_of_exp_naive e

Full Usage: string_of_exp_naive e

Parameters:
    e : expression - The expression to be translated.

Returns: string The expression's concrete syntax representation.

Returns a naive concrete syntax representation of an expression.

Reverses transformation, from abstract to concrete syntax keeping brackets. This is a naive version that puts brackets uniformly round each instance of a binary operator, which is perfectly correct but sometimes looks cumbersome to a human. Seealso: Intro.sprint_exp; Intro.print_exp; for clever versions.

e : expression

The expression to be translated.

Returns: string

The expression's concrete syntax representation.

Example

 Mul (Add(Const 0, Const 1), Add(Const 0, Const 0))
 |> string_of_exp_naive
Evaluates to "((0 + 1) * (0 + 0))"