Mathemagix source programs |
Mathemagix source programs are of type MMX
and correspond to parse trees of the original Mathemagix source files.
Mathemagix programs can be highly ambiguous. During the first phase of
the compilation, they are therefore transformed into typed programs of
type PRG, each of which
has a unique type. The file mmx_syntax.mmx
contains several utility routines for the manipulation of MMX programs. These utility routines are
usually prefixed by $ or mmx$. The
type MMXS is an
abbreviation for Vector
MMX.
Internal representation of Mathemagix
source programs
Internally, an MMX
program is encoded by an expression tree of type GEN (which equals Generic), each subtree being either of
type Literal or Compound. The private
conversion routines between the types MMX
and GEN are postfix .v and mmx.
The public routines for the syntactic manipulation of MMX programs are
mmx$literal: Literal ->
MMX
mmx$literal: String ->
MMX
literal?: MMX ->
Boolean
convert: MMX ->
Literal
Creation of a leaf, testing whether an MMX
program is a leaf and converting a leaf to a literal.
postfix (): (MMX, Tuple MMX) ->
MMX
compound?: MMX ->
Boolean
prefix #: MMX ->
Int
postfix []: (MMX, Int) ->
MMX
Creation of a compound tree, testing whether an MMX program is a compound tree,
obtaining the arity of a compound tree and accessing a child of a
compound tree.
Syntactic utility functions
postfix .components: MMX ->
MMXS
postfix .args: MMX ->
MMXS
postfix .initial: MMX ->
MMXS
postfix .initial: MMXS ->
MMXS
postfix .last: MMX ->
MMX
postfix .last: MMXS ->
MMX
Commodity accessors for the determination of all components of a
compound tree (postfix
.components), of all arguments of a primitive or
function call (postfix
.args), of all but its last arguments (postfix .initial), and its last
argument (postfix
.args).
subst (p: MMX, what: MMXS, by: MMXS):
MMX
Given vectors what = [w1, …,
wn] and by = [b1,
…, bn] of the same length n, return the syntactic substitution of
each wi by bi in p.
Declarations
The following syntactic constructors can be used for building
declarations:
$forall (ql: MMXS, decl: MMX):
MMX
$exists (ql: MMXS, decl: MMX):
MMX
$assume (hyps: MMXS, decl: MMX):
MMX
$intern (decl: MMX):
MMX
$extern (decl: MMX):
MMX
$method (decl: MMX):
MMX
$constructor (decl: MMX):
MMX
$constant (decl: MMX):
MMX
$mutable (decl: MMX):
MMX
$private (decl: MMX):
MMX
$public (decl: MMX):
MMX
These modifiers alter the semantics of a declaration decl. In the cases of $forall and $exists,
the parameter ql
contains a list of signifiers (see below) over which we quantify. In
the case of $assume,
the parameter hyps
contains a list of hypothesis.
$macro (lhs: MMX, rhs: MMX):
MMX
Declaration of a macro. The left-hand side is either a literal or an
expression var
(a1,…,an), where var,
a1, …, an are all literals. The
right-hand side contains the replacement.
$header (var: MMX, tp: MMX):
MMX
Forward declaration of a variable var
of type tp. To do: check whether var
should be a literal.
$of (var: MMX, tp: MMX):
MMX
Syntactic construction of a signifier (or typed variable)
“var:
tp”, for use in combination with $define, $assign,
$lambda, $in, $Forall,
$Exists. etc. To do: check whether var
should be a literal (exception: typed bodies of lambda
expressions).
$define (lhs: MMX, rhs: MMX):
MMX
$assign (lhs: MMX, rhs: MMX):
MMX
Declaration of a constant resp. a mutable variable. The
left-hand side lhs is
a typed variable of the form “var:
tp” and the right-hand side rhs contains the (initial) value. In
the case of an initialization using $define
inside a constructor, the left-hand side is a literal.
$category (name: MMX, def: MMX):
MMX
$class (name: MMX, def: MMX):
MMX
$module (name: MMX, def: MMX):
MMX
Declaration of a category, class or module. The name can be of the form Comp (a1, …, an) in the case of
compound categories, types or modules, where each of the arguments
ai is a signifier.
The following predicates can be used for the detection and analysis of
declarations:
declaration?: MMX ->
Boolean
Checks whether a program is a variable declaration or a forward
declaration.
forall?: MMX ->
Boolean
exists?: MMX ->
Boolean
assume?: MMX ->
Boolean
intern?: MMX ->
Boolean
extern?: MMX ->
Boolean
method?: MMX ->
Boolean
constructor?: MMX ->
Boolean
constant?: MMX ->
Boolean
mutable?: MMX ->
Boolean
private?: MMX ->
Boolean
public?: MMX ->
Boolean
macro?: MMX ->
Boolean
header?: MMX ->
Boolean
of?: MMX ->
Boolean
define?: MMX ->
Boolean
assign?: MMX ->
Boolean
category?: MMX ->
Boolean
class?: MMX ->
Boolean
module?: MMX ->
Boolean
Predicates corresponding to $forall,
$exists, $assume, $intern,
$extern, $method, $constructor,
$constant, $mutable, $private,
$public, $macro, $header,
$of, $define, $assign,
$category, $class and $module.
The following accessors can be used in order to analyze declarations:
postfix .id: MMX ->
Literal
Find the variable of a variable declaration or signifier.
postfix .var: MMX ->
MMX
postfix
.of: MMX -> MMX
Find the variable and type of a signifier, a forward declaration, or
of the left hand side of a variable declaration.
postfix .lhs: MMX ->
MMX
postfix .rhs: MMX ->
MMX
Find the left-hand side and the right-hand side of a (possibly
forward) declaration. The right hand side of a forward declaration
is the symbolic constant mmx$forward.
Control structures
mmx$begin: Tuple MMX ->
MMX
$BEGIN: Tuple MMX ->
MMX
Sequential block of instructions. In the case of mmx$begin, the block may also contain
local declarations. When using $BEGIN,
it is assumed that the local declarations have already been
processed. To do: is the $BEGIN
primitive really necessary?
$if (cond: MMX, thenp: MMX):
MMX
$if (cond: MMX, thenp: MMX, elsep:
MMX): MMX
Conditional instructions or expressions.
$and (cond1: MMX, cond2: MMX):
MMX
$or (cond1: MMX, cond2: MMX):
MMX
$not (cond: MMX): MMX
Fundamental boolean operations
$loop (mods: MMXS, body: MMX):
MMX
Looping construct, where mods
is a list of looping modulators, as described below.
$for (init: MMX): MMX
$while (cond: MMX):
MMX
$until (cond: MMX):
MMX
$step (inc: MMX): MMX
Elementary looping modulators. The $for
modulator contains a variable declaration, or an instruction to be
executed once before the loop, or an expression of the form
“sf in
gen”, where gen
is a generator. The $while
and $until modulators
allow for the execution as long as resp. until a
certain condition is satisfied. The $step
modulator executes an instruction each time we complete one cycle of
the loop.
$in (sf: MMX, gen: MMX):
MMX
Allows the construction of “sf in
gen” as needed by one of the variants of the
$for looping
modulator. Here sf is
a signifier of the form “var:
tp” and gen
a generator.
mmx$break: () ->
MMX
mmx$continue: () ->
MMX
Construct for breaking or continuing a loop.
$lambda (args: MMXS, tbody: MMX):
MMX
Construction of a lambda expression with arguments args and a typed body tbody. Each of the arguments and the
typed body should be signifiers.
mmx$return: () ->
MMX
$return: MMX ->
MMX
Returning the result of a lambda expression.
$try (body: MMX, handlers: Tuple MMX):
MMX
$raise (err: MMX):
MMX
Constructors for exception handling. The $try
construct attempts to execute body.
If an error is raised using the $raise
construct, then we attempt to handle it using one of the handlers. On failure, the error is
reraised outside the $try
block.
The following predicates can be used for the detection and analysis of
constrol structures:
begin?: MMX ->
Boolean
BEGIN?: MMX ->
Boolean
if?: MMX ->
Boolean
and?: MMX ->
Boolean
or?: MMX ->
Boolean
not?: MMX ->
Boolean
loop?: MMX ->
Boolean
for?: MMX ->
Boolean
while?: MMX ->
Boolean
step?: MMX ->
Boolean
in?: MMX ->
Boolean
break?: MMX ->
Boolean
continue?: MMX ->
Boolean
lambda?: MMX ->
Boolean
return?: MMX ->
Boolean
try?: MMX ->
Boolean
raise?: MMX ->
Boolean
Predicates corresponding to mmx$begin,
$BEGIN, $if, $not,
$or, $not, $loop,
$for, $while, $step,
$in, $break, $continue,
$lambda, $return, $try
and $raise.
The following accessors can be used in order to analyze lambda
expressions:
postfix .lambda_args: MMX ->
MMXS
postfix .lambda_body: MMX ->
MMX
postfix .lambda_of: MMX ->
MMX
Find the arguments, the (non typed) body and the return type of a
lambda expression.
Type expressions
The following constants correspond to the syntactic versions of the
most basic atomic types:
mmx$Void: MMX
mmx$Volatile: MMX
mmx$Generic: MMX
mmx$Class: MMX
mmx$Category: MMX
mmx$Boolean: MMX
mmx$This: MMX
mmx$Structure: MMX
mmx$Any: MMX
The type Structure is
used for the generation of the trivial category. The type Any is used internally as a quantifier
for Forall and Exist types.
The following functions can be used for the syntactic construction of
compound types
$Alias: MMX -> MMX
$Forall: (ql: MMXS, tp: MMX) ->
MMX
$Exists: (ql: MMXS, tp: MMX) ->
MMX
In the case of $Forall
and $Exists, the
first argument ql
constains a list of quantifiers and the second argument tp a quantified type. Each of the
arguments ql should
be a signifier.
Special identifiers
mmx$true: MMX
mmx$false: MMX
mmx$this: MMX
mmx$import: MMX
mmx$cpp: MMX
Special constants and keywords.
mmx$compiled: MMX
mmx$interpreted: MMX
mmx$stable: MMX
mmx$unstable: MMX
mmx$automatic: MMX
mmx$inclusion: MMX
mmx$homomorphism: MMX
mmx$caster: MMX
mmx$fall_back: MMX
mmx$panic: MMX
Standard hypothesis for use with $assume.
mmx$convert: MMX
mmx$upgrade: MMX
mmx$downgrade: MMX
mmx$eq: MMX
mmx$neq: MMX
mmx$apply: MMX
Auxiliary constructs
The following syntactic constructs are invisible to the Mathemagix
programmer, but used internally by the compiler.
$READ (inst: MMX, field: MMX):
MMX
$WRITE (inst: MMX, field: MMX):
MMX
Read and write acces to a field
of the instance inst
of a class.
Assuming that we are inside a class, this construct indicates that
the function lamb is
a method of the class. During the compilation, a special
“method environment” is attached to the class
environment, which allows other methods to be accessed through short
names.
$CONSTRUCT (body: MMX):
MMX
Assuming that we are inside a class, this construct indicates that
body is the body of a
constructor. Such a body is a succession of statements of the form
var == val, where var is an intern class
field and val its
initial value.
$TYPEDEF (tp: MMX, equiv: MMX):
MMX
Declares a new type tp
to be equivalent to equiv.
$PENALTY (pen: Penalty, x: MMX):
MMX
$UNPENALTY (pen: Penalty, x: MMX):
MMX
The first routine $PENALTY
explicitly attaches a penalty pen
to an object x; if
x has type T, then the result has type Penalty (pen, T). The second routine
removes the penalty from an object x
of type Penalty (pen,
T). To do: to be verified.
$EXPLICIT (pen: Penalty, x: MMX):
MMX
$UNEXPLICIT (pen: Penalty, x: MMX):
MMX
The first routine $EXPLICIT
converts an object x
of type T to an
object of type Explicit
T. The second routine $UNEXPLICIT
performs the inverse conversion. To do: to be
verified.
$FORALL (ql: MMXS, tmpl: MMX):
MMX
$UNFORALL (tmpl: MMX, args: Tuple MMX):
MMX
Template construction and template instantiation.
$EXISTS (ql: MMXS, tmpl: MMX):
MMX
$UNEXISTS (tmpl: MMX, args: Tuple MMX):
MMX
Abstraction and concretization.
$as_void (expr: MMX):
MMX
Low level cast of expr
to Void. In
particular, $as_void
(expr) is used as an equivalent for expr :> $Void in the implementation
of :>.
© 2009 Joris van der Hoeven
Permission is granted to copy, distribute and/or modify this document
under the terms of the
GNU General Public License. If you
don't have this file, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.