de.cm.frw.core.fun.impl
Class FP

java.lang.Object
  extended by de.cm.frw.core.fun.impl.FP

public class FP
extends java.lang.Object

Implementation of the FP system described in section 11 of "Can Programming be liberated from the Von Neumann Style" http://www.stanford.edu/class/cs242/readings/backus.pdf Definitions for functions and functional forms are quoted from there using the papers notation as far as possible ... Conventions:

Author:
cm

Field Summary
static Function add
           
static Function alpha
          Apply to all elements of a sequence.
static Function and
           
static Function apndl
          Append left
static Function apndr
          Append left
static Function atom
          atom tests if the given argument is an atom (i.e.
static Function bu
          Binary to unary
static Function compose
          Returns the composition of functions f and g:
static Function cond
          conditional function application
static Function constant
          Returns a constant function with given value:
static Function construct
          Combines functions in given collection into a function producing a sequence of results f_i(x).
static Function distl
          Distribute left
static Function distr
          Distribute right
static Function divide
           
static Function eq
          Equals:
static Function id
          Identity:
static Function insert
          Insert a binary function into a sequence.
static Function length
          Length
static Function multiply
           
static Function not
           
static Function nul
          Null: function to test if a given object is the empty sequence
static Function or
           
static Function reverse
          Reverse
static Function rotl
           
static Function rotr
           
static Function rsel
          rsel creates a numeric selector function (counting from one)
static Function rtail
          rtail produces the tail of a sequence x, i.e.
static Function sel
          sel creates a numeric selector function (counting from one)
static Function substract
           
static Function tail
          tail produces the tail of a sequence x, i.e.
static Function transpose
           
static Function whyle
          Build a while loop from a given predicate p and a function f:
 
Constructor Summary
FP()
           
 
Method Summary
static boolean atom(java.lang.Object x)
          Method to simplify the use of atom
static Function compose(Function f, Function g)
           
static boolean equals(java.lang.Object y, java.lang.Object z)
          Method to simplify the use of eq
static boolean nul(java.lang.Object x)
          Method to simplify the use of nul
static Function rsel(int s)
          Method to simplify using rsel
static java.util.Collection rtail(java.lang.Object x)
          Method to simplify using rtail
static Function sel(int s)
          Method to simplify using sel
static java.util.Collection tail(java.lang.Object x)
          Method to simplify using tail
static Function whyle(Predicate p, Function f)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sel

public static final Function sel
sel creates a numeric selector function (counting from one)

For any positive integer s the function created is:

   (sel:s):x = x == <x1, ..., xn> && n >= s --> xs ; 
               null
 


rsel

public static final Function rsel
rsel creates a numeric selector function (counting from one)

For any positive integer s the function created is:

   (sel:s):x = x == <x1, ..., xn> && n >= s --> xn-s ; 
               null
 


tail

public static final Function tail
tail produces the tail of a sequence x, i.e. the sequence x without its first element:
 tail:x = x == <x1> --> <>;
          x == <x1 ... xn> & n >= 2 --> <x2 ... xn>;
          null
 


rtail

public static final Function rtail
rtail produces the tail of a sequence x, i.e. the sequence x without its last element:
 rtail:x = x == <x1> --> <>;
           x == <x1 ... xn> & n >= 2 --> <x1 ... xn-1>;
           null
 


id

public static final Function id
Identity:
 id x = x
 


atom

public static final Function atom
atom tests if the given argument is an atom (i.e. not a sequence)
 atom:x = x == <x1 ... xn> --> F; 
          x == <> --> F; 
          x == null --> null;    // bottom preserving 
          T
 


eq

public static final Function eq
Equals:
 eq:x = x == <y,z> && y == z --> T;
        x == <y,z> && y != z --> F;
        null
 


nul

public static final Function nul
Null: function to test if a given object is the empty sequence
 Def nul : x = x == <> --> T;
               x != null --> F;
               null
 


reverse

public static final Function reverse
Reverse
 reverse:x = x == <> --> <>
             x == <x1 .. xn> --> <xn ... x1>
             null
 


distl

public static final Function distl
Distribute left
 distl:x = x == <y, <>> --> <>
           x == <y, <x1 .. xn>> --> <<y,x1> ..  <y,xn>>
           null
 


distr

public static final Function distr
Distribute right
 distl:x = x == <<>, z> --> <>
           x == <<x1 .. xn>, z> --> <<x1,z> ..  <xn,z>>
           null
 


length

public static final Function length
Length
 length:x = x == <x1 ... xn> -->n
            x == <> --> 0
            null
 


add

public static final Function add

substract

public static final Function substract

multiply

public static final Function multiply

divide

public static final Function divide

and

public static final Function and

or

public static final Function or

not

public static final Function not

rotl

public static final Function rotl

rotr

public static final Function rotr

constant

public static final Function constant
Returns a constant function with given value:
 const(x):y = y == null --> null; 
              otherwise --> x
 


compose

public static final Function compose
Returns the composition of functions f and g:
 (f.g):x = f : ( g : x )
 


construct

public static final Function construct
Combines functions in given collection into a function producing a sequence of results f_i(x).
 [f1 ... fn]:x = <f1 :x ... fn:x>
 


cond

public static final Function cond
conditional function application
 (p --> f, g):x = (p:x) --> f:x; 
                  !(p:x) --> g:x; 
                  null
 


insert

public static final Function insert
Insert a binary function into a sequence.
 /f:x = x == <x1> --> x1;
        x == <x1 .. xn> & n>=2 --> f:< x1, /f<x2 .. xn> >;
        null 
 


alpha

public static final Function alpha
Apply to all elements of a sequence.
 alpha f:x = x == <> --> <>;
             x == <x1 .. xn> --> x == <f:x1 .. f:xn>
             null
 


transpose

public static final Function transpose

apndl

public static final Function apndl
Append left
 appendl:x = x == <y, <>> --> <y>
             x == <y, <x1 .. xn>> --> <y, x1 ... xn>
             null
 


apndr

public static final Function apndr
Append left
 appendl:x = x == <<>, z> --> <z>
             x == <<x1 .. xn>, z> --> <x1 ... xn, z>
             null
 


bu

public static final Function bu
Binary to unary
 (bu f x):x = f:<x, y>
 


whyle

public static final Function whyle
Build a while loop from a given predicate p and a function f:
 (while p f):x = p:x == T --> (while p f):(f:x);
                 p:x == F --> x;
                 null
 
Note:Cannot have the name while in JAVA ; while the definition is recursive the implementation uses a while loop to avoid stack build up.

Constructor Detail

FP

public FP()
Method Detail

sel

public static Function sel(int s)
                    throws Function.InvalidArguments
Method to simplify using sel

Parameters:
s - a positive integer (> 0)
Returns:
the selector function for s
Throws:
Function.InvalidArguments

rsel

public static Function rsel(int s)
                     throws Function.InvalidArguments
Method to simplify using rsel

Parameters:
s - a positive integer (> 0)
Returns:
the right selector function for s
Throws:
Function.InvalidArguments

tail

public static java.util.Collection tail(java.lang.Object x)
                                 throws Function.InvalidArguments
Method to simplify using tail

Parameters:
x - an object
Returns:
a collection containing all but the 1st element of x, if x is a non-empty collection, otherwise null
Throws:
Function.InvalidArguments

rtail

public static java.util.Collection rtail(java.lang.Object x)
                                  throws Function.InvalidArguments
Method to simplify using rtail

Parameters:
x - an object
Returns:
a collection containing all but the last element of x, if x is a non-empty collection, otherwise null
Throws:
Function.InvalidArguments

atom

public static boolean atom(java.lang.Object x)
                    throws Function.InvalidArguments
Method to simplify the use of atom

Parameters:
x - object to test
Returns:
true if x is an atom and not null; false otherwise
Throws:
Function.InvalidArguments

equals

public static boolean equals(java.lang.Object y,
                             java.lang.Object z)
                      throws Function.InvalidArguments
Method to simplify the use of eq

Parameters:
y - object to test for equality with z
z - object to test for equality with y
Returns:
true, if y == z, false otherwise
Throws:
Function.InvalidArguments

nul

public static boolean nul(java.lang.Object x)
                   throws Function.InvalidArguments
Method to simplify the use of nul

Parameters:
x - object to test
Returns:
true, if x is an empty sequence, false otherwise
Throws:
Function.InvalidArguments

compose

public static Function compose(Function f,
                               Function g)
                        throws Function.InvalidArguments
Throws:
Function.InvalidArguments

whyle

public static Function whyle(Predicate p,
                             Function f)
                      throws Function.InvalidArguments
Throws:
Function.InvalidArguments