The core
module provides the language primitives and the standard
library.
Interprets x
as a literal symbol rather than a node
declaration. See Literal Symbols.
The same symbol object is always returned for a given symbol name.
|
An atom node expression. |
Matches the literal symbol x
. See Pattern Matching.
Returns x
interpreted as a literal character.
x
is a symbol of one character, the character is returned.
x
is a string, the first character in the string is returned.
x
is an integer in the range 0 — 9, the character
corresponding to the digit is returned.
|
The value to convert to a character. |
Matches the character literal produced by the given argument. See Pattern Matching.
Returns the raw node object corresponding to the node with identifier
node
. See Node References.
|
The node identifier, which can be any valid node expression. The expression is processed in the module in which the macro node instance occurs. |
Establishes a binding between node source
and node target
.
|
The source node. |
|
The target node. |
Registered as an infix operator with precedence 10 and right associativity. |
Establishes a binding between node source
and node target
.
Same as ->
however the argument order is reversed with the first
argument being the target node and the second argument being the
source node.
|
The target node. |
|
The source node. |
Registered as an infix operator with precedence 10 and left associativity. |
Indicates an explicit context to which bindings, involving node
as
the target, should be established.
When the @
expression appears as the target of a binding, the
binding to node
is established in the context with identifier
context
, see Section 2.5, “Contexts”. The context identifier may be omitted
in which case the identifier default
is assumed.
context
may also be a functor of the form when(context-id, type)
,
where context-id
is the context identifier and type
is a node, of
which the value is interpreted as a failure type. In this case the
binding will only be activated if the failure type, of the previous
binding in the context, is equal to type
by =
. See Conditionally Active Bindings based on Failure Type. If context-id
is
omitted, that is context
is of the form when(type)
, the identifier
default
is assumed.
|
The node. |
|
The context identifier, or |
Registered as an infix operator with precedence 800 and left
associativity. The |
When appearing as the target of a binding, a binding is established to
node
which is only active when node
is in the state with
identifier state
.
state
may also be a functor of the form from => state
, in which
case the binding is only active when the state of node
changes from
the state with identifier from
to the state with identifier to
.
See Section 2.10, “Node States” for more information.
|
The node. |
|
The state identifier or |
Registered as an infix operator with precedence 700 and left
associativity. The |
Defines a meta-node with identifier id
, argument list args
and
definition body
. See Section 3, “Meta-Nodes”.
|
Meta-node identifier. |
|
Comma-separated list of node identifiers, to which the meta-node arguments are bound. |
|
Node expression making up the definition of the meta-node. If the definition consists of more than a single expression, it should be enclosed in braces, see Node Lists. |
Registered as an infix operator with precedence 5 and right associativity. |
Explicitly references a node defined in the enclosing scope of the meta-node. See Section 3.3, “Outer Node References”.
|
The node identifier, which can be any valid node expression. |
Returns a failure with a given failure type.
type
(Optional)
Returns the failure type of x
.
Returns a failure if x
does not evaluate to a failure or evaluates
to a failure with no type.
|
The value of which to return the failure type. |
Returns true if x
fails to evaluate to a value.
|
The value to test for failure. |
Matches if the source node evaluates to a failure. If the argument x
is provided matches only failures of type x
otherwise matches any
failure. See Pattern Matching.
Returns true if x
evaluates to a value, false if x
fails to
evaluate to a value.
|
The value to test for failure. |
Tests for failure with a given type.
Returns true if x
fails with failure type equal to type
, by
=
. Returns false if the failure type of x
is not
equal to type
or x
does not fail.
|
The value to check. |
|
The failure type. |
Evaluates to true if x
evaluates to a value. If x
evaluates to a
failure, evaluates to the failure value.
|
The value to test for failure. |
Returns value
if test
does not fail. If test
fails, the failure
is returned.
|
The value which is checked for failure. |
|
The value which should be returned if |
Tests that each argument of a functor expression does not fail, before evaluating the expression.
If at least one argument fails, then the entire functor node fails.
|
The functor expression. |
Returns the value of try
if it does not evaluate to a failure. If
try
evaluates to a failure returns the value of catch
.
try
catch
try
evaluates to a
failure.
test
try
. If the function returns true, the value of catch
is
returned otherwise the value of try
is returned.
Failure type representing the absense of a value.
Optional meta-nodes arguments, for which no value is provided, are bound to a failure of this type.
Node No-Value!
is bound to a failure of this type.
A failure of this type is returned when an argument to a meta-node is not of the expected type.
Node Type-Error!
is bound to a failure of this type.
A failure of this type is returned when attempting to access an element at an index that is outside the bounds of the list or string.
Node Index-Out-Bounds!
is bound to a failure of this type.
A failure of this type is returned by int
when a
string, from which an integer cannot be parsed, is provided as an
argument.
Node Invalid-Integer!
is bound to a failure of this type.
A failure of this type is returned by real
when a
string, from which a real number cannot be parsed, is provided as an
argument.
Node Invalid-Real!
is bound to a failure of this type.
A failure of this type is returned when a meta-node is invoked indirectly, by a meta-node reference see Section 3.5, “Higher-Order Meta-Nodes”, with an incorrect number of arguments.
Node Arity-Error!
is bound to a failure of this type.
Computes the sum of x
and y
.
|
A number. |
|
A number. |
Registered as an infix operator with precedence 100 and left associativity. |
Computes the difference of x
and y
.
If y
is not provided, returns the negation of x
, i.e. x
multiplied by -1
.
x
y
(Optional)
Registered as an infix operator with precedence 100 and left associativity. |
Computes the product of x
and y
.
|
A number. |
|
A number. |
Registered as an infix operator with precedence 200 and left associativity. |
Computes the quotient of x
and y
.
|
A number. |
|
A number. |
Registered as an infix operator with precedence 200 and left associativity. |
Returns true if x
is less than y
.
|
A number. |
|
A number. |
Registered as an infix operator with precedence 50 and left associativity. |
Returns true if x
is less than or equal to y
.
|
A number. |
|
A number. |
Registered as an infix operator with precedence 50 and left associativity. |
Returns true if x
is greater than y
.
|
A number. |
|
A number. |
Registered as an infix operator with precedence 50 and left associativity. |
Returns true if x
is greater than or equal to y
.
|
A number. |
|
A number. |
Registered as an infix operator with precedence 50 and left associativity. |
Returns true if a
is equal to b
.
a
and b
are equal if they evaluate to the same object.
|
A value. |
|
A value. |
Registered as an infix operator with precedence 50 and left associativity. |
Returns true if a
is not equal to b
.
See =
for the rules of equality.
|
A value. |
|
A value. |
Registered as an infix operator with precedence 50 and left associativity. |
Logical AND.
Returns True
if x
and y
evaluate to
True
. Returns False
otherwise.
|
A Boolean value |
|
A Boolean value |
Registered as an infix operator with precedence 25 and left associativity. |
Matches if both the nested patterns in x
and y
match the source
node. See Pattern Matching.
Logical OR.
Returns True
if either x
or y
evaluate to
True
. Returns False
if both x
and y
evaluate
to False
.
|
A Boolean value |
|
A Boolean value |
Registered as an infix operator with precedence 20 and left associativity. |
Matches if at least one of the nested patterns in x
and y
match
the source node. Both the bindings generated by the patterns x
and
y
are established if the corresponding pattern condition
matches. See Pattern Matching.
This pattern matches even if not all its nested patterns have matched. |
Logical NOT.
Returns True
if x
evaluates to False
.
|
A Boolean value |
Matches if the nested pattern x
does not match. The bindings
generated by x
are not established by this pattern. See
Pattern Matching.
Since this binding does not establish any bindings, it is treated as a constant pattern and may only appear nested inside other patterns. |
Returns true-value
if condition
is True
otherwise
returns false-value
.
If false-value
is not provided, a failure is returned if condition
evaluates to False
.
condition
true-value
condition
is true.
false-value
(Optional)
condition
is false. If
not provided defaults to a failure of type No-Value
.
<clause> = <condition> : <value>
Expands to nested if
expressions.
Each argument is a clause
is of the form condition : value
. The
case
expression evaluates to the value
corresponding to the first
clause of which the condition
node evaluates to
True
. The final clause may also be of the form
value
, in which case it becomes the default value, to which the
case
expression evaluates if the conditions of all the other clauses
evaluate to False
.
|
The clauses. |
Example.
case( a < b : a, b >= a : b ) # Is equivalent to: if(a < b, a, if(b >= a, b))
Example with default value.
case( a < b : -1, b > a : 1, 0 ) # Is equivalent to: if(a < b, -1, if(b > a, 1, 0))
Converts x
to an integer value.
x
is an integer returns x
.
x
is a real returns x
with the fractional part
truncated.
x
is a string, attempts to parse an integer from
x
. Returns the parsed value if successful otherwise
returns a failure of type Invalid-Integer
.
If x
is neither of the above returns a failure of type
Type-Error
.
|
The value to convert to an integer. |
Matches if the source node is an integer, in which case x
is
matched to the integer value. See Pattern Matching.
Converts x
to a real number value.
x
is an integer or real returns x
.
x
is a string, attempts to parse a real number from
x
. Returns the parsed value if successful otherwise
returns a failure of type Invalid-Real
.
If x
is neither of the above returns a failure of type
Type-Error
.
|
The value to convert to a real. |
Matches if the source node is a real, in which case x
is matched
to the real value. See Pattern Matching.
Converts x
to a string.
|
The value to convert to a string. |
Matches if the source node is a string, in which case x
is matched
to the string value. See Pattern Matching.
Converts x
to an integer value.
Same as int
however with the target-node
attribute set to int
. As such, in the following:
a -> to-int(b)
Node b
is set to the value of a
converted to an integer.
|
The value to convert. |
Converts x
to an real number value.
Same as real
however with the target-node
attribute set to real
. As such, in the following:
a -> to-real(b)
Node b
is set to the value of a
converted to a real number.
|
The value to convert. |
Lists are represented by a special cons
type, in which the head
stores the first element of the list and the tail stores the list of
remaining elements. Neither the head nor the tail are evaluated
until they are actually referenced and used.
The empty list is represented by the value of the node Empty
.
Creates a list with the head
as the first element and tail
as
the list of remaining elements.
|
The first element of the list. |
|
The list containing the remaining elements after the first. |
Matches if the source node is a non-empty list, in which case head
is matched to the head of the list and tail is matched to the tail
of the list. See Pattern Matching.
Returns the head (first element) of a list.
If list
is the empty list, returns a failure of type
Empty
.
If list
is not a list returns a failure value of type
Type-Error
.
|
The list. |
Returns the tail, the list containing the elements after the first element, of a list.
If list
is the empty list, returns a failure of type
Empty
.
If list
is not a list returns a failure value of type
Type-Error
.
|
The list. |
Returns true if thing
is a list of at least one element, false
otherwise.
Does not return true if |
|
The list. |
The value of this node is a failure of type Empty
. This failure is returned when attempting to access the
head or tail of an empty list.
Creates a list with elements xs
.
|
The list elements. |
Matches if the source node is a list of the same size as xs
, in
which case each argument in xs
is matched to the corresponding list
element. See Pattern Matching.
Creates a list containing, as elements, all the arguments in xs
excluding the last. The last argument in xs
is treated as a list
containing the remaining elements.
|
The list elements, with the last argument being the list containing the remaining elements. |
Matches if the source node is a list of at least one less elements
that the number of elements in xs
. The arguments, excluding the
last, are matched to the corresponding elements in the list with the
last argument being matched to the remaining list elements. See
Pattern Matching.
Creates a list containing, as elements, all the arguments in xs
.
Unlike list
, if at least one of xs
fails to evaluate to a value,
the failure is returned.
|
The list elements. |
Retrieves the element of a list at a particular index.
Returns a failure of type Index-Out-Bounds
if n
is greater than the number of elements in
list
.
|
The list. |
|
The index of the element to retrieved. |
Returns a list containing the elements of list2
appended to
list1
.
|
The initial list. |
|
The list which is appended onto |
Folds a list to a single value, starting from the first element.
The function f
is first applied on x
and the head of
list
. Subsequently, f
is applied on the result of the previous
application and the next element of list
, until the end of list
is
reached.
|
Initial first argument to |
|
Function of two arguments. |
|
List to fold. |
Folds a list to a single value, starting from the first element.
Same as foldl'
except the head of list
is used as the initial first argument to the fold
function f
.
|
Function of two arguments. |
|
List to fold. |
Folds a list to a single value, starting from the last element.
f
is first applied on the last element of list
and the value of
x
. If the x
argument is not provided or x
evaluates to a failure
of type No-Value
, f
is first applied on the
last two elements of list
. Subsequently f
is applied on the
previous element of list
and the result of the previous application,
until the head of list list
is reached.
If list
only has a single element and x
is not provided, the
element is returned as is. If l
is empty and x
is provided, x
is
returned as is.
f
list
x
(Optional)
f
on the
last element of list
.
Applies a function on each element of a list.
Returns a list containing the result of applying f
on each element
of list
in turn.
|
Function of one argument. |
|
The list. |
Filters elements from a list.
Returns a list containing only the elements of list
for which the
function f
returns true.
|
Function of one argument, which should return true if the argument should be retained in the list or false if it should be removed. |
|
The list to filter. |
Returns true if f
returns true for every element of list
.
|
Function of one argument. |
|
The list. |
Returns true if f
returns true for at least one element of list
.
|
Function of one argument. |
|
The list. |
Returns true if f
returns false for every element of list
.
|
Function of one argument. |
|
The list. |
Returns the character at a given index in the string.
If the index is greater than the number of characters in this string,
returns a failure of type Index-Out-Bounds
.
|
The string. |
|
The index of the character. |
Concatenates str2
to the end of str1
.
|
The first string. |
|
The string which is concatenated to |
Returns a list containing the characters in a string.
|
The string. |
Returns a string containing the concatenation of the elements in a list.
Each element of list
is converted to a string and concatenated to
the result string.
|
List of elements to concatenate. |
Creates a formatted string, in which placeholders are replaced by the
arguments in args
.
The sequence %s
designates a placeholder which is to be replaced by
an argument. The first placeholder is replaced by the first argument,
the second with the second argument and so on. Each argument is
converted to a string prior to being substituted into the result
string.
The sequence %%
designates a literal %
character and is thus
replaced with a %
.
|
The format string. |
|
The arguments to substitute into the string. |
Retrieves the value of an entry in a dictionary.
If dict
does not have an entry for key
a failure, of type
No-Value
is returned.
|
The dictionary. |
|
The entry key. |
Applies a function on an argument list.
The argument list, on which f
is applied consist of each argument of
xs
, excluding the last, followed by each element of the last
argument of xs
.
|
The function to apply. |
|
The arguments to apply |
If |
The core/introspection
module provides utility meta-nodes for
introspecting the nodes comprising a program. These meta-nodes may
only be used within macro nodes, during macro expansion, as runtime
definitions are not available.
Returns true if thing
is a node object.
|
The thing to check whether it is a node. |
Looks-up a node in a module.
Returns the node object or a failure if no node is found.
node
module
(Optional)
/module
declaration that is processed.
Currently there is no way to retrieve a module object, thus the
|
Pattern matching is provided by the core module in the form of bindings involving the meta-node instance, which is to be matched, as the target. The binding succeeds if the pattern matches, otherwise it fails.
A meta-node which supports pattern matching, has a target-node
or
target-transform
, see Section 3.7, “Instances as Targets”, such that when an
instance of the meta-node appears as the target of a binding, the
argument nodes are bound to the values, required in order for the
meta-node to return a value that is equivalent to the value of the
source node. When there are such values, the pattern is said to have
matched. If there is no possible value for at least one argument
node, all argument nodes should evaluate to failures of type
Match-Fail
. In this case the pattern has not
matched
Example.
x -> int(y)
In the example, above, y
is bound to the value of x
if it is an
integer, otherwise y
evaluates to a failure. There is no argument
which will result in int
returning a non-integer value thus if the
source node, x
, is not an integer the argument node, y
, is bound
to a failure. Since int
returns the value of its argument directly,
when it is an integer, the argument node is simply bound to the source
node.
Example.
x -> list(y, z)
In the example, above, y
is bound to the first element of x
and
z
is bound to the second element of x
if x
is a list of two
elements. These bindings will result in a list, equivalent to x
,
being produced when y
and z
are passed as arguments to the list
meta-node.
Patterns may be nested, that is an argument to a meta-node instance is itself a meta-node instance of which the operator meta-node supports pattern matching. When the arguments contain one or more nested patterns, the bindings to the argument nodes should only succeed if all nested patterns match.
Example.
x -> list(int(y), z)
The example, above, is similar to the previous example except with the
additional condition that the first element of x
should also be an
integer. That is y
is bound to the first element of x
and z
to
the second element of x
if x
is a list of two elements of which
the first element is an integer.
When _
appears nested inside a pattern it matches anything and does
not establish any bindings. This is used to indicate that the value
for a particular argument is unimportant.
Example.
x -> list(_, y)
In the example, above, y
is bound to the second element of x
if it
is a list of two elements. The value of the first element of x
is
ignored completely.
Constant patterns comprise a constant value as opposed to a
node. These patterns match when the source node is equal, by
=
, to the constant value. Constant patterns do not
result in any bindings being established however they do affect the
condition of the pattern in which they are nested.
Constant patterns may only be used when nested inside a non-constant pattern. |
Constant values include any literal constants, such as numbers,
strings as well as character literals, produced by the c
macro, and literal symbols, produced by the
'
macro.
Example.
x -> list(1, y, z)
In the example, above, y
is bound to the second element of x
and
z
to the third element of x
if x
is a list of three elements of
which the first element is equal to 1
.
The following are examples of invalid uses of constant patterns:
Examples: Invalid use of Constant Patterns.
# Invalid as the pattern is not nested x -> 1 # Invalid as at least one argument should not be a constant. x -> list(1, 2)
Functor nodes, of which the arguments are all constants, such
as |
The matcher
node attribute stores a meta-node which is called to
construct the pattern for a given list of arguments. The matcher
meta-node is called with two arguments: the place to be matched, which
should become the source node of any bindings established by the
pattern, and the pattern functor expression itself (including the
operator). The meta-node should return a Pattern
object, which is a
dictionary containing the following entries:
condition
bindings
List of bindings established by the pattern. If there are
no bindings established by the pattern, then this entry should be set
to the empty list, see Empty
.
The bindings should not be conditioned on |
Pattern objects may be created with the |
All bindings, established by a pattern, should be established in an
explicit context with identifier match
, which is activated only on
failures with type Match-Fail
. This allows
multiple patterns to be specified on a single node, with the node
being set to the value corresponding to the binding of the first
pattern that matches.
Example: Multiple Patterns.
x -> int(y) x -> list(int(y)) x -> list("x", int(y))
The example above contains multiple patterns involving a single node
y
.
y
is bound to:
x
if it is an integer, or
x
if it is a list of one element, which is an
integer, or
x
if it is a list of two elements, with the
first element being the string value "x"
and the second element
being an integer.
The following meta-nodes in the core
module all have a matcher and
may thus appear within patterns.
This module contains utilities for creating and processing patterns.
Creates a Pattern
object. See Matchers
condition
binding (Optional)
Returns the matcher function, stored in the matcher
attribute of a
node.
Returns a failure if the node’s matcher
attribute is not set.
|
The node object of which to retrieve the matcher. |
Creates the Pattern
object for a pattern expression.
Can be used for any pattern, including constant patterns. |
place
pattern
If |
Returns an expression which is the conjunction of two expressions, by
and
.
c1
and
.
c2
and
.
If c1
evaluates to a failure, returns c2
. If c2
evaluates to a
failure, returns c1
.
This is useful for creating a condition which combines the conditions of multiple argument nodes. |
Returns a list where each binding in bindings
is conditioned on
condition
. See Conditional Bindings.
If condition
evaluates to a failure, a condition of True
is
assumed, thus the bindings are not conditioned.
condition
bindings
Failure type indicating that a pattern failed to match.
Node Match-Fail!
is bound to a failure of this type.
If condition
evaluates to false or to a failure, returns a failure
of type Match-Fail
, otherwise returns true.
condition
Generates a binding src -> target
, with target
in the match
context which is activated on failures of type
Match-Fail
.
src
target
Creates the node declarations implementing a pattern.
Returns a single node declaration.
pattern
Pattern
object for which to create the declarations.
The declaration returned by this meta-node is a suitable return
value for a |