axiom-developer
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Axiom-developer] 20080218.02.tpd.patch (add function examples)


From: daly
Subject: [Axiom-developer] 20080218.02.tpd.patch (add function examples)
Date: Mon, 18 Feb 2008 19:27:25 -0600

Add additional examples for functions from tree.spad, bags.spad and
array2.spad, namely

DONE )d op arrayStack
DONE )d op balancedBinaryTree
DONE )d op binarySearchTree
DONE )d op binaryTournament
DONE )d op binaryTree
DONE )d op cyclicCopy
DONE )d op cyclicEntries
DONE )d op cyclicEqual?
DONE )d op cyclicParents
DONE )d op heap
DONE )d op insertRoot!
DONE )d op mapDown!
DONE )d op mapUp!
DONE )d op ptree
DONE )d op queue
DONE )d op setColumn!
DONE )d op setleaves!
DONE )d op setRow!
DONE )d op stack
DONE )d op tree


SOME )d op coerce
SOME )d op column
SOME )d op cyclic?
SOME )d op dequeue
SOME )d op elt
SOME )d op fill!
SOME )d op insert!
SOME )d op map
SOME )d op map!
SOME )d op maxColIndex
SOME )d op maxRowIndex
SOME )d op minColIndex
SOME )d op minRowIndex
SOME )d op ncols
SOME )d op nrows
SOME )d op new
SOME )d op parts
SOME )d op qelt
SOME )d op qsetelt!
SOME )d op row
SOME )d op setelt
SOME )d op split

Tim

========================================================================
diff --git a/changelog b/changelog
index 101d2dc..c36de76 100644
--- a/changelog
+++ b/changelog
@@ -1,3 +1,6 @@
+20080218 tpd src/algebra/tree.spad add function examples
+20080218 tpd src/algebra/bags.spad add function examples
+20080218 tpd src/algebra/array2.spad add function examples
 20080218 tpd src/algebra/array1.spad add function examples
 20080218 tpd src/algebra/fr.spad add function examples
 20080217 wxh src/interp/i-intern.boot upload proper file. 
diff --git a/src/algebra/array2.spad.pamphlet b/src/algebra/array2.spad.pamphlet
index 38f6ba0..e6a8b7a 100644
--- a/src/algebra/array2.spad.pamphlet
+++ b/src/algebra/array2.spad.pamphlet
@@ -10,6 +10,15 @@
 \tableofcontents
 \eject
 \section{category ARR2CAT TwoDimensionalArrayCategory}
+TwoDimensionalArrayCategory is a general array category which
+allows different representations and indexing schemes.
+Rows and columns may be extracted with rows returned as objects
+of type Row and columns returned as objects of type Col.
+The index of the 'first' row may be obtained by calling the
+function 'minRowIndex'.  The index of the 'first' column may
+be obtained by calling the function 'minColIndex'.  The index of
+the first element of a 'Row' is the same as the index of the
+first column in an array and vice versa.
 <<category ARR2CAT TwoDimensionalArrayCategory>>=
 )abbrev category ARR2CAT TwoDimensionalArrayCategory
 ++ Two dimensional array categories and domains
@@ -20,105 +29,190 @@
 ++ Examples:
 ++ References:
 TwoDimensionalArrayCategory(R,Row,Col): Category == Definition where
-  ++ TwoDimensionalArrayCategory is a general array category which
-  ++ allows different representations and indexing schemes.
-  ++ Rows and columns may be extracted with rows returned as objects
-  ++ of type Row and columns returned as objects of type Col.
-  ++ The index of the 'first' row may be obtained by calling the
-  ++ function 'minRowIndex'.  The index of the 'first' column may
-  ++ be obtained by calling the function 'minColIndex'.  The index of
-  ++ the first element of a 'Row' is the same as the index of the
-  ++ first column in an array and vice versa.
-  R   : Type
-  Row : FiniteLinearAggregate R
-  Col : FiniteLinearAggregate R
+ R   : Type
+ Row : FiniteLinearAggregate R
+ Col : FiniteLinearAggregate R
 
-  Definition == HomogeneousAggregate(R) with
+ Definition == HomogeneousAggregate(R) with
 
-    shallowlyMutable
-      ++ one may destructively alter arrays
+   shallowlyMutable
+    ++ one may destructively alter arrays
 
-    finiteAggregate
-      ++ two-dimensional arrays are finite
+   finiteAggregate
+    ++ two-dimensional arrays are finite
 
 --% Array creation
 
-    new: (NonNegativeInteger,NonNegativeInteger,R) -> %
-      ++ new(m,n,r) is an m-by-n array all of whose entries are r
-    fill_!: (%,R) -> %
-      ++ fill!(m,r) fills m with r's
+   new: (NonNegativeInteger,NonNegativeInteger,R) -> %
+    ++ new(m,n,r) is an m-by-n array all of whose entries are r
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,0)
+    
+   fill_!: (%,R) -> %
+    ++ fill!(m,r) fills m with r's
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,0)
+    ++E fill!(arr,10)
 
 --% Size inquiries
 
-    minRowIndex : % -> Integer
-      ++ minRowIndex(m) returns the index of the 'first' row of the array m
-    maxRowIndex : % -> Integer
-      ++ maxRowIndex(m) returns the index of the 'last' row of the array m
-    minColIndex : % -> Integer
-      ++ minColIndex(m) returns the index of the 'first' column of the array m
-    maxColIndex : % -> Integer
-      ++ maxColIndex(m) returns the index of the 'last' column of the array m
-    nrows : % -> NonNegativeInteger
-      ++ nrows(m) returns the number of rows in the array m
-    ncols : % -> NonNegativeInteger
-      ++ ncols(m) returns the number of columns in the array m
+   minRowIndex : % -> Integer
+    ++ minRowIndex(m) returns the index of the 'first' row of the array m
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E minRowIndex(arr)
+
+   maxRowIndex : % -> Integer
+    ++ maxRowIndex(m) returns the index of the 'last' row of the array m
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E maxRowIndex(arr)
+
+   minColIndex : % -> Integer
+    ++ minColIndex(m) returns the index of the 'first' column of the array m
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E minColIndex(arr)
+
+   maxColIndex : % -> Integer
+    ++ maxColIndex(m) returns the index of the 'last' column of the array m
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E maxColIndex(arr)
+
+   nrows : % -> NonNegativeInteger
+    ++ nrows(m) returns the number of rows in the array m
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E nrows(arr)
+
+   ncols : % -> NonNegativeInteger
+    ++ ncols(m) returns the number of columns in the array m
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E ncols(arr)
 
 --% Part extractions
 
-    elt: (%,Integer,Integer) -> R
-      ++ elt(m,i,j) returns the element in the ith row and jth
-      ++ column of the array m
-      ++ error check to determine if indices are in proper ranges
-    qelt: (%,Integer,Integer) -> R
-      ++ qelt(m,i,j) returns the element in the ith row and jth
-      ++ column of the array m
-      ++ NO error check to determine if indices are in proper ranges
-    elt: (%,Integer,Integer,R) -> R
-      ++ elt(m,i,j,r) returns the element in the ith row and jth
-      ++ column of the array m, if m has an ith row and a jth column,
-      ++ and returns r otherwise
-    row: (%,Integer) -> Row
-      ++ row(m,i) returns the ith row of m
-      ++ error check to determine if index is in proper ranges
-    column: (%,Integer) -> Col
-      ++ column(m,j) returns the jth column of m
-      ++ error check to determine if index is in proper ranges
-    parts: % -> List R
-      ++ parts(m) returns a list of the elements of m in row major order
+   elt: (%,Integer,Integer) -> R
+    ++ elt(m,i,j) returns the element in the ith row and jth
+    ++ column of the array m
+    ++ error check to determine if indices are in proper ranges
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E elt(arr,1,1)
+
+   qelt: (%,Integer,Integer) -> R
+    ++ qelt(m,i,j) returns the element in the ith row and jth
+    ++ column of the array m
+    ++ NO error check to determine if indices are in proper ranges
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E qelt(arr,1,1)
+
+   elt: (%,Integer,Integer,R) -> R
+    ++ elt(m,i,j,r) returns the element in the ith row and jth
+    ++ column of the array m, if m has an ith row and a jth column,
+    ++ and returns r otherwise
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E elt(arr,1,1,6)
+    ++E elt(arr,1,10,6)
+
+   row: (%,Integer) -> Row
+    ++ row(m,i) returns the ith row of m
+    ++ error check to determine if index is in proper ranges
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E row(arr,1)
+
+   column: (%,Integer) -> Col
+    ++ column(m,j) returns the jth column of m
+    ++ error check to determine if index is in proper ranges
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E column(arr,1)
+
+   parts: % -> List R
+    ++ parts(m) returns a list of the elements of m in row major order
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E parts(arr)
 
 --% Part assignments
 
-    setelt: (%,Integer,Integer,R) -> R
-      -- will become setelt_!
-      ++ setelt(m,i,j,r) sets the element in the ith row and jth
-      ++ column of m to r
-      ++ error check to determine if indices are in proper ranges
-    qsetelt_!: (%,Integer,Integer,R) -> R
-      ++ qsetelt!(m,i,j,r) sets the element in the ith row and jth
-      ++ column of m to r
-      ++ NO error check to determine if indices are in proper ranges
-    setRow_!: (%,Integer,Row) -> %
-      ++ setRow!(m,i,v) sets to ith row of m to v
-    setColumn_!: (%,Integer,Col) -> %
-      ++ setColumn!(m,j,v) sets to jth column of m to v
+   setelt: (%,Integer,Integer,R) -> R
+    -- will become setelt_!
+    ++ setelt(m,i,j,r) sets the element in the ith row and jth
+    ++ column of m to r
+    ++ error check to determine if indices are in proper ranges
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,0)
+    ++E setelt(arr,1,1,17)
+
+   qsetelt_!: (%,Integer,Integer,R) -> R
+    ++ qsetelt!(m,i,j,r) sets the element in the ith row and jth
+    ++ column of m to r
+    ++ NO error check to determine if indices are in proper ranges
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,0)
+    ++E qsetelt!(arr,1,1,17)
+
+   setRow_!: (%,Integer,Row) -> %
+    ++ setRow!(m,i,v) sets to ith row of m to v
+    ++
+    ++E T1:=TwoDimensionalArray Integer
+    ++E arr:T1:= new(5,4,0)
+    ++E T2:=OneDimensionalArray Integer
+    ++E arow:=construct([1,2,3,4]::List(INT))$T2
+    ++E setRow!(arr,1,arow)$T1
+
+   setColumn_!: (%,Integer,Col) -> %
+    ++ setColumn!(m,j,v) sets to jth column of m to v
+    ++
+    ++E T1:=TwoDimensionalArray Integer
+    ++E arr:T1:= new(5,4,0)
+    ++E T2:=OneDimensionalArray Integer
+    ++E acol:=construct([1,2,3,4,5]::List(INT))$T2
+    ++E setColumn!(arr,1,acol)$T1
 
 --% Map and Zip
 
-    map: (R -> R,%) -> %
-      ++ map(f,a) returns \spad{b}, where \spad{b(i,j) = f(a(i,j))} for all 
\spad{i, j}
-    map_!: (R -> R,%) -> %
-      ++ map!(f,a)  assign \spad{a(i,j)} to \spad{f(a(i,j))} for all \spad{i, 
j}
-    map:((R,R) -> R,%,%) -> %
-      ++ map(f,a,b) returns \spad{c}, where \spad{c(i,j) = f(a(i,j),b(i,j))}
-      ++ for all \spad{i, j}
-    map:((R,R) -> R,%,%,R) -> %
-      ++ map(f,a,b,r) returns \spad{c}, where \spad{c(i,j) = f(a(i,j),b(i,j))} 
when both
-      ++ \spad{a(i,j)} and \spad{b(i,j)} exist;
-      ++ else \spad{c(i,j) = f(r, b(i,j))} when \spad{a(i,j)} does not exist;
-      ++ else \spad{c(i,j) = f(a(i,j),r)} when \spad{b(i,j)} does not exist;
-      ++ otherwise \spad{c(i,j) = f(r,r)}.
-
-   add
+   map: (R -> R,%) -> %
+    ++ map(f,a) returns \spad{b}, where \spad{b(i,j) = f(a(i,j))} 
+    ++ for all \spad{i, j}
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E map(-,arr)
+    ++E map((x +-> x + x),arr)
+
+   map_!: (R -> R,%) -> %
+    ++ map!(f,a)  assign \spad{a(i,j)} to \spad{f(a(i,j))} for all \spad{i, j}
+    ++
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E map!(-,arr)
+
+   map:((R,R) -> R,%,%) -> %
+    ++ map(f,a,b) returns \spad{c}, where \spad{c(i,j) = f(a(i,j),b(i,j))}
+    ++ for all \spad{i, j}
+    ++
+    ++E adder(a:Integer,b:Integer):Integer == a+b
+    ++E arr : ARRAY2 INT := new(5,4,10)
+    ++E map(adder,arr,arr)
+
+   map:((R,R) -> R,%,%,R) -> %
+    ++ map(f,a,b,r) returns \spad{c}, where \spad{c(i,j) = f(a(i,j),b(i,j))}
+    ++ when both \spad{a(i,j)} and \spad{b(i,j)} exist;
+    ++ else \spad{c(i,j) = f(r, b(i,j))} when \spad{a(i,j)} does not exist;
+    ++ else \spad{c(i,j) = f(a(i,j),r)} when \spad{b(i,j)} does not exist;
+    ++ otherwise \spad{c(i,j) = f(r,r)}.
+    ++
+    ++E adder(a:Integer,b:Integer):Integer == a+b
+    ++E arr1 : ARRAY2 INT := new(5,4,10)
+    ++E arr2 : ARRAY2 INT := new(3,3,10)
+    ++E map(adder,arr1,arr2,17)
+
+  add
 
 --% Predicates
 
@@ -276,12 +370,12 @@ TwoDimensionalArrayCategory(R,Row,Col): Category == 
Definition where
 
 @
 \section{domain IIARRAY2 InnerIndexedTwoDimensionalArray}
+This is an internal type which provides an implementation of
+2-dimensional arrays as PrimitiveArray's of PrimitiveArray's.
 <<domain IIARRAY2 InnerIndexedTwoDimensionalArray>>=
 )abbrev domain IIARRAY2 InnerIndexedTwoDimensionalArray
 InnerIndexedTwoDimensionalArray(R,mnRow,mnCol,Row,Col):_
        Exports == Implementation where
-  ++ This is an internal type which provides an implementation of
-  ++ 2-dimensional arrays as PrimitiveArray's of PrimitiveArray's.
   R : Type
   mnRow, mnCol : Integer
   Row : FiniteLinearAggregate R
@@ -363,18 +457,18 @@ InnerIndexedTwoDimensionalArray(R,mnRow,mnCol,Row,Col):_
 
 @
 \section{domain IARRAY2 IndexedTwoDimensionalArray}
+An IndexedTwoDimensionalArray is a 2-dimensional array where
+the minimal row and column indices are parameters of the type.
+Rows and columns are returned as IndexedOneDimensionalArray's with
+minimal indices matching those of the IndexedTwoDimensionalArray.
+The index of the 'first' row may be obtained by calling the
+function 'minRowIndex'.  The index of the 'first' column may
+be obtained by calling the function 'minColIndex'.  The index of
+the first element of a 'Row' is the same as the index of the
+first column in an array and vice versa.
 <<domain IARRAY2 IndexedTwoDimensionalArray>>=
 )abbrev domain IARRAY2 IndexedTwoDimensionalArray
 IndexedTwoDimensionalArray(R,mnRow,mnCol):Exports == Implementation where
-  ++ An IndexedTwoDimensionalArray is a 2-dimensional array where
-  ++ the minimal row and column indices are parameters of the type.
-  ++ Rows and columns are returned as IndexedOneDimensionalArray's with
-  ++ minimal indices matching those of the IndexedTwoDimensionalArray.
-  ++ The index of the 'first' row may be obtained by calling the
-  ++ function 'minRowIndex'.  The index of the 'first' column may
-  ++ be obtained by calling the function 'minColIndex'.  The index of
-  ++ the first element of a 'Row' is the same as the index of the
-  ++ first column in an array and vice versa.
   R : Type
   mnRow, mnCol : Integer
   Row ==> IndexedOneDimensionalArray(R,mnCol)
@@ -883,7 +977,6 @@ TwoDimensionalArray(R):Exports == Implementation where
 --SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 @
 <<*>>=
-<<license>>
 
 <<category ARR2CAT TwoDimensionalArrayCategory>>
 <<domain IIARRAY2 InnerIndexedTwoDimensionalArray>>
diff --git a/src/algebra/bags.spad.pamphlet b/src/algebra/bags.spad.pamphlet
index 32f07cf..65efc9e 100644
--- a/src/algebra/bags.spad.pamphlet
+++ b/src/algebra/bags.spad.pamphlet
@@ -31,6 +31,9 @@ Stack(S:SetCategory): StackAggregate S with
     stack: List S -> %
       ++ stack([x,y,...,z]) creates a stack with first (top)
       ++ element x, second element y,...,and last element z.
+      ++
+      ++E a:Stack INT:= stack [1,2,3,4,5]
+
   == add
     Rep := Reference List S
     s = t == deref s = deref t
@@ -77,6 +80,9 @@ ArrayStack(S:SetCategory): StackAggregate(S) with
     arrayStack: List S -> %
       ++ arrayStack([x,y,...,z]) creates an array stack with first (top)
       ++ element x, second element y,...,and last element z.
+      ++
+      ++E c:ArrayStack INT:= arrayStack [1,2,3,4,5]
+
   == add
     Rep := IndexedFlexibleArray(S,0)
  
@@ -127,6 +133,9 @@ Queue(S:SetCategory): QueueAggregate S with
     queue: List S -> %
       ++ queue([x,y,...,z]) creates a queue with first (top)
       ++ element x, second element y,...,and last (bottom) element z.
+      ++
+      ++E e:Queue INT:= queue [1,2,3,4,5]
+
   == Stack S add
     Rep := Reference List S
     lastTail==> LAST$Lisp
@@ -171,6 +180,9 @@ Dequeue(S:SetCategory): DequeueAggregate S with
      dequeue: List S -> %
        ++ dequeue([x,y,...,z]) creates a dequeue with first (top or front)
        ++ element x, second element y,...,and last (bottom or back) element z.
+       ++
+       ++E g:Dequeue INT:= dequeue [1,2,3,4,5]
+
   == Queue S add
     Rep := Reference List S
     bottom_! d ==
@@ -363,6 +375,9 @@ Heap(S:OrderedSet): Exports == Implementation where
     heap : List S -> %
       ++ heap(ls) creates a heap of elements consisting of the 
       ++ elements of ls.
+      ++
+      ++E i:Heap INT := heap [1,6,3,7,5,2,4]
+
   Implementation == IndexedFlexibleArray(S,0) add
     Rep := IndexedFlexibleArray( S,0)
     empty() == empty()$Rep
@@ -450,7 +465,6 @@ Heap(S:OrderedSet): Exports == Implementation where
 --SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 @
 <<*>>=
-<<license>>
  
 <<domain STACK Stack>>
 <<domain ASTACK ArrayStack>>
diff --git a/src/algebra/tree.spad.pamphlet b/src/algebra/tree.spad.pamphlet
index 573f7f8..0a208e0 100644
--- a/src/algebra/tree.spad.pamphlet
+++ b/src/algebra/tree.spad.pamphlet
@@ -30,23 +30,53 @@ Tree(S: SetCategory): T==C where
      finiteAggregate
      shallowlyMutable
      tree: (S,List %) -> %
-       ++ tree(nd,ls) creates a tree with value nd, and children
-       ++ ls.
+       ++ tree(nd,ls) creates a tree with value nd, and children ls.
+       ++
+       ++E t1:=tree [1,2,3,4]
+       ++E tree(5,[t1])
+
      tree: List S -> %
        ++ tree(ls) creates a tree from a list of elements of s. 
+       ++
+       ++E tree [1,2,3,4]
+
      tree: S -> %
        ++ tree(nd) creates a tree with value nd, and no children
+       ++
+       ++E tree 6
+
      cyclic?: % -> Boolean
        ++ cyclic?(t) tests if t is a cyclic tree.
+       ++
+       ++E t1:=tree [1,2,3,4]
+       ++E cyclic? t1
+
      cyclicCopy: % -> %
-      ++ cyclicCopy(l) makes a copy of a (possibly) cyclic tree l.
+       ++ cyclicCopy(l) makes a copy of a (possibly) cyclic tree l.
+       ++
+       ++E t1:=tree [1,2,3,4]
+       ++E cyclicCopy t1
+
      cyclicEntries:    % -> List %
        ++ cyclicEntries(t) returns a list of top-level cycles in tree t.
+       ++
+       ++E t1:=tree [1,2,3,4]
+       ++E cyclicEntries t1
+
      cyclicEqual?: (%, %) -> Boolean
        ++ cyclicEqual?(t1, t2) tests of two cyclic trees have 
        ++ the same structure.
+       ++
+       ++E t1:=tree [1,2,3,4]
+       ++E t2:=tree [1,2,3,4]
+       ++E cyclicEqual?(t1,t2)
+
      cyclicParents: % -> List %
        ++ cyclicParents(t) returns a list of cycles that are parents of t.
+       ++
+       ++E t1:=tree [1,2,3,4]
+       ++E cyclicParents t1
+
  C== add
     cycleTreeMax ==> 5
 
@@ -333,12 +363,13 @@ Tree(S: SetCategory): T==C where
 ++ of a value and a \spadfun{left} and \spadfun{right}, both binary trees. 
 BinaryTreeCategory(S: SetCategory): Category == BinaryRecursiveAggregate(S) 
with
    shallowlyMutable
-     ++ Binary trees have updateable components
+    ++ Binary trees have updateable components
    finiteAggregate
-     ++ Binary trees have a finite number of components
+    ++ Binary trees have a finite number of components
    node: (%,S,%) -> %
-     ++ node(left,v,right) creates a binary tree with value \spad{v}, a binary
-     ++ tree \spad{left}, and a binary tree \spad{right}.
+    ++ node(left,v,right) creates a binary tree with value \spad{v}, a binary
+    ++ tree \spad{left}, and a binary tree \spad{right}.
+    ++
  add
      cycleTreeMax ==> 5
 
@@ -372,12 +403,20 @@ BinaryTreeCategory(S: SetCategory): Category == 
BinaryRecursiveAggregate(S) with
 ++ and \spadfun{left} which are both binary trees.
 BinaryTree(S: SetCategory): Exports == Implementation where
   Exports == BinaryTreeCategory(S) with
-     binaryTree: S -> %
-       ++ binaryTree(v) is an non-empty binary tree
-       ++ with value v, and left and right empty.
-     binaryTree: (%,S,%) -> %    
-       ++ binaryTree(l,v,r) creates a binary tree with
-       ++ value v with left subtree l and right subtree r.
+   binaryTree: S -> %
+    ++ binaryTree(v) is an non-empty binary tree
+    ++ with value v, and left and right empty.
+    ++
+    ++E t1:=binaryTree([1,2,3])
+    
+   binaryTree: (%,S,%) -> %    
+    ++ binaryTree(l,v,r) creates a binary tree with
+    ++ value v with left subtree l and right subtree r.
+    ++
+    ++E t1:=binaryTree([1,2,3])
+    ++E t2:=binaryTree([4,5,6])
+    ++E binaryTree(t1,[7,8,9],t2)
+    
   Implementation == add
      Rep := List Tree S
      t1 = t2 == (t1::Rep) =$Rep (t2::Rep)
@@ -628,14 +667,29 @@ BinarySearchTree(S: OrderedSet): Exports == 
Implementation where
     shallowlyMutable
     finiteAggregate
     binarySearchTree: List S -> %
-       ++ binarySearchTree(l) \undocumented
+     ++ binarySearchTree(l) \undocumented
+     ++
+     ++E binarySearchTree [1,2,3,4]
+
     insert_!: (S,%) -> %
-      ++ insert!(x,b) inserts element x as leaves into binary search tree b.
+     ++ insert!(x,b) inserts element x as leaves into binary search tree b.
+     ++
+     ++E t1:=binarySearchTree [1,2,3,4]
+     ++E insert!(5,t1)
+
     insertRoot_!: (S,%) -> %
-      ++ insertRoot!(x,b) inserts element x as a root of binary search tree b.
+     ++ insertRoot!(x,b) inserts element x as a root of binary search tree b.
+     ++
+     ++E t1:=binarySearchTree [1,2,3,4]
+     ++E insertRoot!(5,t1)
+
     split:      (S,%) -> Record(less: %, greater: %)
-      ++ split(x,b) splits binary tree b into two trees, one with elements 
greater
-      ++ than x, the other with elements less than x.
+     ++ split(x,b) splits binary tree b into two trees, one with elements 
+     ++ greater than x, the other with elements less than x.
+     ++
+     ++E t1:=binarySearchTree [1,2,3,4]
+     ++E split(3,t1)
+
   Implementation == BinaryTree(S) add
     Rep := BinaryTree(S)
     binarySearchTree(u:List S) ==
@@ -663,21 +717,28 @@ BinarySearchTree(S: OrderedSet): Exports == 
Implementation where
 
 @
 \section{domain BTOURN BinaryTournament}
+A BinaryTournament(S) is the domain of binary trees where elements are
+ordered down the tree.  A binary search tree is either empty or is a
+node containing a value of type S, and a right and a left which are
+both BinaryTree(S)
 <<domain BTOURN BinaryTournament>>=
 )abbrev domain BTOURN BinaryTournament
-++ Description: \spadtype{BinaryTournament(S)} is the domain of
-++ binary trees where elements are ordered down the tree.
-++ A binary search tree is either empty or is a node containing a
-++ \spadfun{value} of type \spad{S}, and a \spadfun{right}
-++ and a \spadfun{left} which are both \spadtype{BinaryTree(S)}
 BinaryTournament(S: OrderedSet): Exports == Implementation where
   Exports == BinaryTreeCategory(S) with
     shallowlyMutable
     binaryTournament: List S -> %
       ++ binaryTournament(ls) creates a binary tournament with the
       ++ elements of ls as values at the nodes.
+      ++
+      ++E binaryTournament [1,2,3,4]
+
     insert_!: (S,%) -> %
       ++ insert!(x,b) inserts element x as leaves into binary tournament b.
+      ++
+      ++E t1:=binaryTournament [1,2,3,4]
+      ++E insert!(5,t1)
+      ++E t1
+
   Implementation == BinaryTree(S) add
     Rep := BinaryTree(S)
     binaryTournament(u:List S) ==
@@ -908,23 +969,46 @@ BalancedBinaryTree(S: SetCategory): Exports == 
Implementation where
     balancedBinaryTree: (NonNegativeInteger, S) -> %
       ++ balancedBinaryTree(n, s) creates a balanced binary tree with
       ++ n nodes each with value s.
+      ++
+      ++E balancedBinaryTree(4, 0)
+
     setleaves_!: (%, List S) -> %
       ++ setleaves!(t, ls) sets the leaves of t in left-to-right order
       ++ to the elements of ls.
+      ++
+      ++E t1:=balancedBinaryTree(4, 0)
+      ++E setleaves!(t1,[1,2,3,4])
+
     mapUp_!: (%, (S,S) -> S) -> S
       ++ mapUp!(t,f) traverses balanced binary tree t in an "endorder"
       ++ (left then right then node) fashion returning t with the value
       ++ at each successive interior node of t replaced by
       ++ f(l,r) where l and r are the values at the immediate
       ++ left and right nodes.
+      ++
+      ++E T1:=BalancedBinaryTree Integer
+      ++E t2:=balancedBinaryTree(4, 0)$T1
+      ++E setleaves!(t2,[1,2,3,4]::List(Integer))
+      ++E adder(a:Integer,b:Integer):Integer == a+b
+      ++E mapUp!(t2,adder)
+      ++E t2
+
     mapUp_!: (%, %, (S,S,S,S) -> S) -> %
-      ++ mapUp!(t,t1,f) traverses t in an "endorder" (left then right then 
node)
-      ++ fashion  returning t with the value at each successive interior
-      ++ node of t replaced by
+      ++ mapUp!(t,t1,f) traverses balanced binary tree t in an "endorder"
+      ++ (left then right then node) fashion returning t with the value
+      ++ at each successive interior node of t replaced by
       ++ f(l,r,l1,r1) where l and r are the values at the immediate
       ++ left and right nodes. Values l1 and r1 are values at the
       ++ corresponding nodes of a balanced binary tree t1, of identical
       ++ shape at t.
+      ++
+      ++E T1:=BalancedBinaryTree Integer
+      ++E t2:=balancedBinaryTree(4, 0)$T1
+      ++E setleaves!(t2,[1,2,3,4]::List(Integer))
+      ++E adder4(i:INT,j:INT,k:INT,l:INT):INT == i+j+k+l
+      ++E mapUp!(t2,t2,adder4)
+      ++E t2
+
     mapDown_!: (%,S,(S,S) -> S) -> %
       ++ mapDown!(t,p,f) returns t after traversing t in "preorder"
       ++ (node then left then right) fashion replacing the successive
@@ -932,6 +1016,14 @@ BalancedBinaryTree(S: SetCategory): Exports == 
Implementation where
       ++ replaced by q := f(p,x). The mapDown!(l,q,f) and
       ++ mapDown!(r,q,f) are evaluated for the left and right subtrees
       ++ l and r of t.
+      ++
+      ++E T1:=BalancedBinaryTree Integer
+      ++E t2:=balancedBinaryTree(4, 0)$T1
+      ++E setleaves!(t2,[1,2,3,4]::List(Integer))
+      ++E adder(i:Integer,j:Integer):Integer == i+j
+      ++E mapDown!(t2,4::INT,adder)
+      ++E t2
+
     mapDown_!: (%,S, (S,S,S) -> List S) -> %
       ++ mapDown!(t,p,f) returns t after traversing t in "preorder"
       ++ (node then left then right) fashion replacing the successive
@@ -941,6 +1033,14 @@ BalancedBinaryTree(S: SetCategory): Exports == 
Implementation where
       ++ and right subtrees of t, is evaluated producing two values
       ++ pl and pr. Then \spad{mapDown!(l,pl,f)} and \spad{mapDown!(l,pr,f)}
       ++ are evaluated.
+      ++
+      ++E T1:=BalancedBinaryTree Integer
+      ++E t2:=balancedBinaryTree(4, 0)$T1
+      ++E setleaves!(t2,[1,2,3,4]::List(Integer))
+      ++E adder3(i:Integer,j:Integer,k:Integer):List Integer == [i+j,j+k]
+      ++E mapDown!(t2,4::INT,adder3)
+      ++E t2
+
   Implementation == BinaryTree(S) add
     Rep := BinaryTree(S)
     leaf? x ==
@@ -1003,20 +1103,30 @@ BalancedBinaryTree(S: SetCategory): Exports == 
Implementation where
 
 @
 \section{domain PENDTREE PendantTree}
+A PendantTree(S)is either a leaf? and is an S or has
+a left and a right both PendantTree(S)'s
 <<domain PENDTREE PendantTree>>=
 )abbrev domain PENDTREE PendantTree
-++ A PendantTree(S)is either a leaf? and is an S or has
-++ a left and a right both PendantTree(S)'s
 PendantTree(S: SetCategory): T == C where
  T == BinaryRecursiveAggregate(S) with
-     ptree : S->%
-       ++ ptree(s) is a leaf? pendant tree
-     ptree:(%, %)->%
-       ++ ptree(x,y) \undocumented
-     coerce:%->Tree S
-       ++ coerce(x) \undocumented
+   ptree : S->%
+    ++ ptree(s) is a leaf? pendant tree
+    ++
+    ++E t1:=ptree([1,2,3])
+       
+   ptree:(%, %)->%
+    ++ ptree(x,y) \undocumented
+    ++
+    ++E t1:=ptree([1,2,3])
+    ++E ptree(t1,ptree([1,2,3]))
+
+   coerce:%->Tree S
+    ++ coerce(x) \undocumented
+    ++
+    ++E t1:=ptree([1,2,3])
+    ++E t2:=ptree(t1,ptree([1,2,3]))
+    ++E t2::Tree List PositiveInteger
 
- 
  C == add
      Rep := Tree S
      import Tree S
@@ -1073,7 +1183,6 @@ PendantTree(S: SetCategory): T == C where
 --SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 @
 <<*>>=
-<<license>>
  
 <<domain TREE Tree>>
 <<category BTCAT BinaryTreeCategory>>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]