Imp
[using it]
Interface Manipulation Package v4.0 (2008-06-25) a-projector ©copyright Author


imp.core
Class Operator

java.lang.Object
  extended by imp.core.Function
      extended by imp.core.Operator

public class Operator
extends Function

Defines and implements a function with algebraic properties. Algebraic expressions, including numerical expressions, are taken into account using generic algebraic properties, in order to derive some canonical representation.

Here algebraic properties allows to obtain a canonical form for such operators.
Specification of the conditional operator.
Standard evaluation. The
if condition then true-value else false-value
construct is the standard conditional operator which evaluation is simply defined though:
# if condition then true-value else false-value)
if true then $v-t else $v-f -> v-t
if false then $v-t else $v-f -> v-f
Extended syntax. The extended syntax of the operator is a construct of the form:
if c_1 then v_1 ( elif c_i then v_i )* ( else v_0 )?
yielding if c_1 then v_1 ( else if c_i then v_i )* ( else v_0 )? where the else statement may be omitted (the related value being null).
Canonical form. In order to generate a canonical form for conditional expressions we define:
if $c then true else false -> c
if $c then $v else $v -> v
and the ``expand'' condition:
(if (if $c then $c-t else $c-f) then $v-t else $v-f) -> if c then (if c-t then v-t else v-f) else (if c-f then v-t else v-f)
This last equation yields expressions which evaluation time is identical but with a minimal conditional expression. It is used to derive a canonical form during contextual evaluation, as defined now.
Contextual evaluation. Given a conditional construct if(c, v-t, v-f) it is clear that v-t is evaluated if and only if c is true, while v-f is evaluated if and only if c is false. It is thus possible to assume c to be true in v-t and false in v-f. Such information is stored in a ``context'' defined by a set of equation of the form
{variable-name = value, ..}
and evaluated over expressions as follows:
# Simplification in the context
eval($v, $context ; v @ context) -> context.get(v)
# Recursion over the conditional operator
eval(if $c then $v-t else $v-f, $context) -> if eval(c, context) then eval(v-t, context.set(c, true)) else eval(v-f, context.set(c, false))
# Recursion over the structure
eval($v, $context) ; is(v, string) -> v
eval($v, $context) -> map(eval, v, context)
Logical expressions: and / or / not
Logical expressions are combinations of conjunctions (and which symbol is &), disjunctions (or which symbol is |) and negation (not which symbol is !).
As for other functions, they are evaluated from left to right, evaluating the second argument only if the first one is not sufficient to provide the answer (lazy evaluation). As such, boolean operators are not commutative, but verify all other natural algebraic properties.
E.g.: x != 0 , y == 1/x is valid since y == 1/x is never evaluated if x == 0
This is equivalent to evaluate such expressions as conditional expressions:
# Logical expressions reduction
$x & $y -> if x then y else false
$x | $y -> if x then true else y
! $x -> if x then false else true
yielding a canonical form with respect to logical algebraic properties (associativity of ``and'' and ``'or'', zero and unit elements, idempotent of ``not'', distributivity, etc..).
Relational expressions: equal / greater / lower / ...
Relation expressions are combinations of set inclusion (e @ s means ``the element e is in the set s''), range operator (v @ min .. max means ``the value v is in the range min .. max) and comparisons (equal ==, not-equal !=, less-than <, less-or-equal <=, greater-than >, greater-or-equal >=).
Relational operators are partially reduced by the following rules:
# Range operator reduction
$x @ $a .. $b -> (x >= a) & (b >= x)
# Set inclusion reduction
$x @ $s -> !is(get(s, x), null)
# Inequalities partial reduction
$x < $y -> y > x
$x <= $y -> y >= x
$x >= $y -> ! y > x
$x != $y -> ! x == y
# Canceling trivial relations
$x == $x -> true
$x > $x -> false
so that finally only x == y and x > y constructs remain.
Relational operators with algebraic expressions may also be reduced using (not-implemented here) so called ``semi-algebraic'' rules, e.g.:
eval($x * $y == 0) -> (x == 0) | (y == 0)
eval($x * $y > 0) -> ((x > 0) & (y > 0)) | ((0 > x) & (0 > y))
these two examples not being exhaustive.
Reduction to homogeneous algebraic construct:
The following equations allows to obtain homogeneous algebraic construct, in order to simplify their internal representation.
$x - $y -> x + (-1 * y)
(-($x)) -> -1 * x
$x / $y -> x * y^(-1)
Otherwise the +, * and ^ operators are put in canonical forms considering associativity, commutativity, zero and unit elements.


Method Summary
 Operator setAssociative(boolean associative)
          Sets the fact this operator is associative.
 Operator setCommutative(boolean commutative)
          Sets the fact this operator is commutative.
 Operator setIdentity(String identity)
          Sets the identity or ``neutral'' operand for this operator and return this.
 Operator setPower(String identity)
          Sets the fact this operator is a power operator.
 Operator setPowerOperator(String power)
          Sets the power operand for this operator and return this.
 Operator setZero(String zero)
          Sets the zero operand for this operator and return this.
 
Methods inherited from class imp.core.Function
eval, popContext, pushContext, setArity, setFixedPoint, setName, setQuoted, setRemember, setTrace
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

setAssociative

public Operator setAssociative(boolean associative)
Sets the fact this operator is associative.
The associative property of an operator o yields the combination of operators to be ``flatten'' to obtain a canonical form.

Parameters:
associative - True if this operator is associative, default is false.

setCommutative

public Operator setCommutative(boolean commutative)
Sets the fact this operator is commutative.
The commutative property of an operator o yields arguments to be sorted since they can be treated in any order.

Parameters:
commutative - True if this operator is commutative, default is false.

setIdentity

public Operator setIdentity(String identity)
Sets the identity or ``neutral'' operand for this operator and return this.
A identity u of an operator o yields u to be canceled if the term contains it.

Parameters:
identity - The identity operand of this operator or the null value to set that there is no identity operator, default is null.

setPower

public Operator setPower(String identity)
Sets the fact this operator is a power operator.
The power property of an operator o yields reduction rules of the form: o($v, 0) -> u, ..

Parameters:
identity - The identity operand u of this operator or the null value to set that it is not a power operator, default is null.

setPowerOperator

public Operator setPowerOperator(String power)
Sets the power operand for this operator and return this.
The power element is used to reduce inverse (represented by power -1) and multiple instantiation of the same operand.
A power p of an operator o yields usual reductions of a power.

Parameters:
power - The power operand of this operator or the null value to set that there is no power operator, default is null.

setZero

public Operator setZero(String zero)
Sets the zero operand for this operator and return this.
A zero z of an operator o yields z if the term contains it.

Parameters:
zero - The zero operand of this operator or the null value to set that there is no zero operator, default is null.