PS7: Set Yourself Free
 Dueish: Wed. Apr. 12.
 Notes:
 This pset has 100 total points.
 This pset contains one solo problems worth 15 points.
 It is strongly recommended that you do the nonsolo SML problems (Problems 4 and 5) first, to get more practice with SML and modules/abstract datatypes that will be valuable going forward.
 This second problem involves reading parts of a long paper. It is best to spread this problem out over multiple sittings.
 Submission:
 In your yourFullName CS251 Spring 2017 Folder, create a Google Doc named yourFullName CS251 PS7.
 At the top of your yourFullName CS251 PS7 doc, include your name, problem set number, date of submission, and an approximation of how long each problem part took.
 For all parts of all problems, include all answers (including SML and Racket code) in your PS7 google doc. Format code using a fixedwidth font, like Consolas or Courier New. You can use a small font size if that helps.
 In Problems 1, 4, and 5, you will create the following code files from starter files populating your
~wx/cs251/sml/ps7
directory on your wx Virtual Machine: Problem 1:
yourAccountNamemarbles.sml
 Problem 4:
yourAccountNameFunSet.sml
 Problem 5:
yourAccountNameOperationTreeSet.sml
For these problems:
 include all functions from the four files named
yourAccountName...
in your Google Doc (so that I can comment on them).  For Problem 4b, don’t forget to include your English answers in your Google Doc.

Drop a copy of your
~wx/cs251/sml/ps7
folder in your~/cs251/drop/ps07
drop folder oncs.wellesley.edu
by executing the following (replacinggdome
by your cs server username):scp r ~wx/cs251/sml/ps7 gdome@cs.wellesley.edu:/students/gdome/cs251/drop/ps07
 Problem 1:
 For Problem 2 (Backus paper): Include English answers to all parts in your Google Doc.
 For Problem 3:
 Be sure that your
deepreverse
definition inyourAccountNameps6deepreverse.rkt
also appears in your Google Doc.  Drop a copy of your
yourAccountNameps6deepreverse.rkt
in your~/cs251/drop/ps06
drop folder oncs.wellesley.edu
.
 Be sure that your
Starting this Problem Set
Problems 1, 4, and 5 involve starter files in the ~wx/cs251/sml/ps7
directory in your wx Virtual Machine.
To create this directory, execute the following two commands in a wx VM shell:
cd ~/cs251/sml
git pull origin master
1. Solo Problem: Losing Your Marbles (15 points)
This is a solo problem. This means you must complete it entirely on your own without help from any other person and without consulting resources other than course materials or online documentation. You may ask Lyn for clarification, but not for help.
Put all your definitions for this problem in the starter file ~/cs251/sml/ps7/marbles.sml
. When you finish this problem, rename the file to yourAccountNameps7marbles.sml
before you submit it.
In this problem, you will define an SML function satisfying the following specification:
val marbles: int > int > int list list
Assume that
m
is a nonnegative integer and thatc
is a positive integer. Givenm
marbles and a row ofc
cups,marbles m c
returns a sorted list of all configurations whereby allm
marbles are distributed among thec
cups. Each configuration should be a list of lengthc
whose elements are integers between 0 andm
and the sum of whose elements ism
. The returned list should be ordered lexicographically (i.e., in dictionary order).
At the end of this problem description are numerous sample invocations of the marbles
function.
Your task is to define the marbles
function in SML so that it satisfies the above specification and has the same behavior as the sample invocations below.
Notes:

As usual, you should use divide/conquer/glue as your problemsolving strategy. Strive to make your solution as simple as possible. For example, do not use more base cases than are strictly necessary.

Don’t forget the very powerful notion of “wishful” thinking, in which you blindly apply a recursive function to smaller versions of the same problem and combine their results. Study the examples carefully to see how the result of a call to
marbles
is stitched together from the results of calls to “smaller” versions ofmarbles
. 
Your
marbles
function should generate the elements in sorted order without calling any kind of sorting function. 
You are expected to use higherorder list functions where they can simplify your definition.

The stater file begins with the following helper functions, which you might find useful:
fun cons x ys = x :: ys (* curried list consing operator *) fun range lo hi = (* list all ints from lo up to (but not including) hi *) List.tabulate(hi  lo, fn i => i + lo)
Reminder: these and other helper functions must be defined above your definition of
marbles
, because definition order matters in SML. 
Feel free to define any additional auxiliary functions you find helpful.

To display elements beyond the default cutoff length for lists, use
Control.Print.printLength := 100;
 Control.Print.printLength := 100; val it = () : unit  marbles 0 1; val it = [[0]] : int list list  marbles 0 2; val it = [[0,0]] : int list list  marbles 0 3; val it = [[0,0,0]] : int list list  marbles 0 4; val it = [[0,0,0,0]] : int list list  marbles 1 1; val it = [[1]] : int list list  marbles 1 2; val it = [[0,1],[1,0]] : int list list  marbles 1 3; val it = [[0,0,1],[0,1,0],[1,0,0]] : int list list  marbles 1 4; val it = [[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]] : int list list  marbles 2 1; val it = [[2]] : int list list  marbles 2 2; val it = [[0,2],[1,1],[2,0]] : int list list  marbles 2 3; val it = [[0,0,2],[0,1,1],[0,2,0],[1,0,1],[1,1,0],[2,0,0]] : int list list  marbles 2 4; val it = [[0,0,0,2],[0,0,1,1],[0,0,2,0],[0,1,0,1],[0,1,1,0],[0,2,0,0],[1,0,0,1], [1,0,1,0],[1,1,0,0],[2,0,0,0]] : int list list  marbles 3 1; val it = [[3]] : int list list  marbles 3 2; val it = [[0,3],[1,2],[2,1],[3,0]] : int list list  marbles 3 3; val it = [[0,0,3],[0,1,2],[0,2,1],[0,3,0],[1,0,2],[1,1,1],[1,2,0],[2,0,1],[2,1,0], [3,0,0]] : int list list  marbles 3 4; val it = [[0,0,0,3],[0,0,1,2],[0,0,2,1],[0,0,3,0],[0,1,0,2],[0,1,1,1],[0,1,2,0], [0,2,0,1],[0,2,1,0],[0,3,0,0],[1,0,0,2],[1,0,1,1],[1,0,2,0],[1,1,0,1], [1,1,1,0],[1,2,0,0],[2,0,0,1],[2,0,1,0],[2,1,0,0],[3,0,0,0]] : int list list  marbles 4 1; val it = [[4]] : int list list  marbles 4 2; val it = [[0,4],[1,3],[2,2],[3,1],[4,0]] : int list list  marbles 4 3; val it = [[0,0,4],[0,1,3],[0,2,2],[0,3,1],[0,4,0],[1,0,3],[1,1,2],[1,2,1],[1,3,0], [2,0,2],[2,1,1],[2,2,0],[3,0,1],[3,1,0],[4,0,0]] : int list list  marbles 4 4; val it = [[0,0,0,4],[0,0,1,3],[0,0,2,2],[0,0,3,1],[0,0,4,0],[0,1,0,3],[0,1,1,2], [0,1,2,1],[0,1,3,0],[0,2,0,2],[0,2,1,1],[0,2,2,0],[0,3,0,1],[0,3,1,0], [0,4,0,0],[1,0,0,3],[1,0,1,2],[1,0,2,1],[1,0,3,0],[1,1,0,2],[1,1,1,1], [1,1,2,0],[1,2,0,1],[1,2,1,0],[1,3,0,0],[2,0,0,2],[2,0,1,1],[2,0,2,0], [2,1,0,1],[2,1,1,0],[2,2,0,0],[3,0,0,1],[3,0,1,0],[3,1,0,0],[4,0,0,0]] : int list list  marbles 5 1; val it = [[5]] : int list list  marbles 5 2; val it = [[0,5],[1,4],[2,3],[3,2],[4,1],[5,0]] : int list list  marbles 5 3; val it = [[0,0,5],[0,1,4],[0,2,3],[0,3,2],[0,4,1],[0,5,0],[1,0,4],[1,1,3],[1,2,2], [1,3,1],[1,4,0],[2,0,3],[2,1,2],[2,2,1],[2,3,0],[3,0,2],[3,1,1],[3,2,0], [4,0,1],[4,1,0],[5,0,0]] : int list list  marbles 5 4; val it = [[0,0,0,5],[0,0,1,4],[0,0,2,3],[0,0,3,2],[0,0,4,1],[0,0,5,0],[0,1,0,4], [0,1,1,3],[0,1,2,2],[0,1,3,1],[0,1,4,0],[0,2,0,3],[0,2,1,2],[0,2,2,1], [0,2,3,0],[0,3,0,2],[0,3,1,1],[0,3,2,0],[0,4,0,1],[0,4,1,0],[0,5,0,0], [1,0,0,4],[1,0,1,3],[1,0,2,2],[1,0,3,1],[1,0,4,0],[1,1,0,3],[1,1,1,2], [1,1,2,1],[1,1,3,0],[1,2,0,2],[1,2,1,1],[1,2,2,0],[1,3,0,1],[1,3,1,0], [1,4,0,0],[2,0,0,3],[2,0,1,2],[2,0,2,1],[2,0,3,0],[2,1,0,2],[2,1,1,1], [2,1,2,0],[2,2,0,1],[2,2,1,0],[2,3,0,0],[3,0,0,2],[3,0,1,1],[3,0,2,0], [3,1,0,1],[3,1,1,0],[3,2,0,0],[4,0,0,1],[4,0,1,0],[4,1,0,0],[5,0,0,0]] : int list list
2. Backus’s Paper (25 points)
This problem is about John Backus’s 1977 Turing Award Lecture: Can Programming be Liberated from the von Neumann Style? A Functional Style and its Algebra of Programs. His paper can be found here.
You should begin this problem by reading Sections 1–11 and 15–16 of this paper. (Although Sections 12–14 are very interesting, they require more time than I want you to spend on this problem.)
Section 11.2 introduces the details of the FP language. Backus uses many notations that may be unfamiliar to you. For example:

p_{1} → e_{1}; … ; p_{n} → e_{n}; e_{n+1} is similar to the Racket expression
(if
p_{1}e_{1}
…
(if
p_{n}e_{n}
e_{n+1}
)
…)
. 
⟨e_{1}, …, e_{n}⟩ denotes the sequence of the n values of the expressions e_{1}, … e_{n}. φ denotes the empty sequence. Because FP is dynamically typed, such sequences can represent both tuples and lists from Python and OCaml.

The symbol ⊥ (pronounced “bottom”) denotes the value of an expression that doesn’t terminate (i.e., it loops infinitely) or terminates with an error.

If f is a function and x is an object (atom or sequence of objects), then f : x denotes the result of applying f to x.

[f_{1}, …, f_{n}] is a functional form denoting a sequence of n functions, f_{1} through f_{n}. The application rule for this functional form is [f_{1}, …, f_{n}] : x = ⟨f_{1} : x, … , f_{n} : x⟩ — i.e., the result of applying a sequence of n functions to an object x is an nelement sequence consisting of the results of applying each of the functions in the function sequence to x.
Consult Lyn if you have trouble understanding Backus’s notation.
Answer the following questions about Backus’s paper. Your answers should be concise but informative.

(2 points) One of the reasons this paper is wellknown is that in it Backus coined the term “von Neumann bottleneck”. Describe what this is and its relevance to the paper.

(2 points) Many programming languages have at least two syntactic categories: expressions and statements. Backus claims that expressions are good but statements are bad. Explain his claim.

(3 points) In Sections 6, 7, and 9 of the paper, Backus discusses three problems/defects with von Neumann languages. Summarize them.

(3 points) What are applicative languages and how do they address the three problems/defects mentioned by Backus for von Neumann languages?

(2 points) The FP language Backus introduces in Section 11 does not support abstraction expressions like Racket’s
lambda
. Why did Backus make this decision in FP? 
(3 points) Backus wrote this paper long before the development of Java and Python. Based on his paper, how do you think he would evaluate these two languages?

(10 points) Consider the following FP definition:
Def F ≡ α/+ ◦ αα× ◦ αdistl ◦ distr ◦ [id, id]
What is the value of F⟨2, 3, 5⟩? Show the evaluation of this expression in a sequence of smallstep algebralike steps.
3. Deep Reverse (10 points)
We saw in lecture that tree recursion on trees represented as sexpressions could be expressed rather elegantly. For example:
(define (atom? x)
(or (number? x) (boolean? x) (string? x) (symbol? x)))
(define (numatoms sexp)
(if (atom? sexp)
1
(foldr + 0 (map numatoms sexp))))
> (numatoms '((a (b c) d) e (((f) g h) i j k)))
11
> (numatoms '(a b c d))
4
> (numatoms 'a)
1
> (numatoms '())
0
(define (listatoms sexp)
(if (atom? sexp)
(list sexp)
(foldr append null (map listatoms sexp))))
> (listatoms '((a (b c) d) e (((f) g h) i j k)))
'(a b c d e f g h i j k)
> (listatoms '(a b c d))
'(a b c d)
> (listatoms 'a)
'(a)
> (listatoms '())
'()
In this problem, you will define a function (deepreverse sexp)
that returns a new sexpression in which the order of the children at every node of the sexpression tree sexp
is reversed.
> (deepreverse '((a (b c) d) e (((f) g h) i j k)))
'((k j i (h g (f))) e (d (c b) a))
> (deepreverse '(a b c d))
'(d c b a)
> (deepreverse 'a)
'a
> (deepreverse '())
'()
Notes:

Begin with this starter file ps7deepreversestarter.rkt, which you should rename to
yourAccountNameps7deepreverse.rkt
. Add your definition ofdeepreverse
to this file. 
Your definition should have form similar to the defintions for
numatoms
andlistatoms
, but you’ll want to use something other thanfoldr
.
4. Fun Sets (22 points)
In SML, we can implement abstract data types in terms of familiar structures like lists and trees. But we can also use functions to implement data types. In this problem, you will investigate a compelling example of using functions to implement sets.
Your ~wx/cs251/sml/ps7
folder contains the starter file FunSet.sml. This includes the same SET
signature we studied in class:
signature SET =
sig
(* The type of sets *)
type ''a t
(* An empty set *)
val empty : ''a t
(* Construct a singleelement set from that element. *)
val singleton : ''a > ''a t
(* Check if a set is empty. *)
val isEmpty : ''a t > bool
(* Return the number of elements in the set. *)
val size : ''a t > int
(* Check if a given element is a member of the given set. *)
val member : ''a > ''a t > bool
(* Construct a set containing the given element and all elements
of the given set. *)
val insert : ''a > ''a t > ''a t
(* Construct a set containing all elements of the given set
except for the given element. *)
val delete : ''a > ''a t > ''a t
(* Construct the union of two sets. *)
val union : ''a t > ''a t > ''a t
(* Construct the intersection of two sets. *)
val intersection : ''a t > ''a t > ''a t
(* Construct the difference of two sets
(all elements in the first set but not in the second.) *)
val difference : ''a t > ''a t > ''a t
(* Construct a set from a list of elements.
Do not assume the list elements are unique. *)
val fromList : ''a list > ''a t
(* Convert a set to a list without duplicates.
The elements in the resulting list may be in any order. *)
val toList : ''a t > ''a list
(* Construct a set from a predicate function:
the resulting set should contain all elements for which
this predicate function returns true.
This acts like math notation for sets. For example:
{ x  x mod 3 = 0 }
would be written:
fromPred (fn x => x mod 3 = 0)
*)
val fromPred : (''a > bool) > ''a t
(* Convert a set to a predicate function. *)
val toPred : ''a t > ''a > bool
(* Convert a set to a string representation, given a function
that converts a set element into a string representation. *)
val toString : (''a > string) > ''a t > string
(* Convert a set to a list without duplicates.
The elements in the resulting list may be in any order. *)
val toList : ''a t > ''a list
(* Construct a set from a predicate function:
the resulting set should contain all elements for which
this predicate function returns true.
This acts like math notation for sets. For example:
{ x  x mod 3 = 0 }
would be written:
fromPred (fn x => x mod 3 = 0)
*)
val fromPred : (''a > bool) > ''a t
(* Convert a set to a predicate function. *)
val toPred : ''a t > ''a > bool
(* Convert a set to a string representation, given a function
that converts a set element into a string representation. *)
val toString : (''a > string) > ''a t > string
end
In this problem, you will flesh out the skeleton of the FunSet
structure below. When you finish this probelm, you should rename this file to yourAccountNameFunSet.sml
before you submit it.
exception Unimplemented (* Placeholder during development. *)
exception Unimplementable (* Impossible to implement *)
(* Implement a SET ADT using predicates to represent sets. *)
structure FunSet :> SET = struct
(* Sets are represented by predicates. *)
type ''a t = ''a > bool
(* The empty set is a predicate that always returns false. *)
val empty = fn _ => false
(* The singleton set is a predicate that returns true for exactly one element *)
fun singleton x = fn y => x=y
(* Determining membership is trivial with a predicate *)
fun member x pred = pred x
(* complete this structure by replacing "raise Unimplemented"
with implementations of each function. Many of the functions
*cannot* be implemented; for those, use raise Unimplementable
as there implementation *)
fun isEmpty _ = raise Unimplemented
fun size _ = raise Unimplemented
fun member _ = raise Unimplemented
fun insert _ = raise Unimplemented
fun delete _ = raise Unimplemented
fun union _ = raise Unimplemented
fun intersection _ = raise Unimplemented
fun difference _ = raise Unimplemented
fun fromList _ = raise Unimplemented
fun toList _ = raise Unimplemented
fun fromPred _ = raise Unimplemented
fun toPred _ = raise Unimplemented
fun toString _ = raise Unimplemented
end
The fromPred
and toPred
operations are based on the observation that a membership predicate describes exactly which elements are in the set and which are not. Consider the following example:
 val s235 = fromPred (fn x => (x = 2) orelse (x = 3) orelse (x = 5));
val s235 =  : int t
 member 2 s235;
val it = true : bool
 member 3 s235;
val it = true : bool
 member 5 s235;
val it = true : bool
 member 4 s235;
val it = false : bool
 member 100 s235;
val it = false : bool
The set s235
consists of exactly those elements satisfying the predicate passed to fromPred
– in this case, the integers 2, 3, and 5.
Defining sets in terms of predicates has many benefits. Most important, it is easy to specify sets that have infinite numbers of elements! For example, the set of all even integers can be expressed as:
fromPred (fn x => (x mod 2) = 0)
This predicate is true of even integers, but is false for all other integers. The set of all values of a given type is expressed as fromPred (fn x => true). Many large finite sets are also easy to specify. For example, the set of all integers between 251 and 6821 (inclusive) can be expressed as
fromPred (fn x => (x >= 251) && (x <= 6821))
Although defining sets in terms of membership predicates is elegant and has many benefits, it has some big downsides. The biggest one is that several functions in the SET
signature simply cannot be implemented. You will explore this downside in this problem.

(18 points) Flesh out the code skeleton for the
FunSet
structure inFunSet.sml
. Some value bindings cannot be implement; for these, useraise Unimplementable
as the implementation.At the end of the starter file are the following commented out test cases. Uncomment these test cases to test your implementation. Feel free to add additional tests.
open FunSet (* range helper function *) fun range lo hi = if lo >= hi then [] else lo::(range (lo + 1) hi) (* Test an int pred set on numbers from 0 to 100, inclusive *) fun intPredSetToList predSet = List.filter (toPred predSet) (range 0 101) val mod2Set = fromPred (fn x => x mod 2 = 0) val mod3Set = fromPred (fn x => x mod 3 = 0) val lowSet = fromList (range 0 61) val highSet = fromList (range 40 101) val smallSet = insert 17 (insert 19 (insert 23 (singleton 42))) val smallerSet = delete 23 (delete 19 (delete 57 smallSet)) (* Be sure to print all details *) val _ = Control.Print.printLength := 101; val smallSetTest = intPredSetToList(smallSet) val smallerSetTest = intPredSetToList(smallerSet) val mod3SetTest = intPredSetToList(mod3Set) val mod2SetUnionMod3SetTest = intPredSetToList(union mod2Set mod3Set) val mod2SetIntersectionMod3SetTest = intPredSetToList(intersection mod2Set mod3Set) val mod2SetDifferenceMod3SetTest = intPredSetToList(difference mod2Set mod3Set) val mod3SetDifferenceMod2SetTest = intPredSetToList(difference mod3Set mod2Set) val bigIntersection = intPredSetToList(intersection (intersection lowSet highSet) (intersection mod2Set mod3Set)) val bigDifference = intPredSetToList(difference (difference lowSet highSet) (difference mod2Set mod3Set))
For the test expressions generating lists, the results are as follows:
val smallSetTest = [17,19,23,42] : int list val smallerSetTest = [17,42] : int list val mod3SetTest = [0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75, 78,81,84,87,90,93,96,99] : int list val mod2SetUnionMod3SetTest = [0,2,3,4,6,8,9,10,12,14,15,16,18,20,21,22,24,26,27,28,30,32,33,34,36,38,39, 40,42,44,45,46,48,50,51,52,54,56,57,58,60,62,63,64,66,68,69,70,72,74,75,76, 78,80,81,82,84,86,87,88,90,92,93,94,96,98,99,100] : int list val mod2SetIntersectionMod3SetTest = [0,6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96] : int list val mod2SetDifferenceMod3SetTest = [2,4,8,10,14,16,20,22,26,28,32,34,38,40,44,46,50,52,56,58,62,64,68,70,74,76, 80,82,86,88,92,94,98,100] : int list val mod3SetDifferenceMod2SetTest = [3,9,15,21,27,33,39,45,51,57,63,69,75,81,87,93,99] : int list val bigIntersection = [42,48,54,60] : int list val bigDifference = [0,1,3,5,6,7,9,11,12,13,15,17,18,19,21,23,24,25,27,29,30,31,33,35,36,37,39] : int list

(4 points) For each function that you declared being unimplementable, explain in English why it is unimplementable. Give concrete examples where appropriate.
5. Operation Tree Sets (28 points)
A very different way of representing a set as a tree is to remember the structure of the set operations empty, insert, delete, union, intersection, and difference used to create the set. For example, consider the set t created as follows:
val s = (delete 4 (difference (union (union (insert 1 empty)
(insert 4 empty))
(union (insert 7 empty)
(insert 4 empty)))
(intersection (insert 1 empty)
(union (insert 1 empty)
(insert 6 empty)))))
Abstractly, s
is the singleton set {7}
. But one concrete representation of s
is the following operation tree:
One advantage of using such operation trees to represent sets is that the empty, insert, delete, union, difference, and intersection operations are extremely cheap — they just create a new tree node with the operands as subtrees, and thus take constant time and space! But other operations, such as member and toList, can be more expensive than in other implementations.
In this problem, you are asked to flesh out the missing operations in the skeleton of the OperationTreeSet structure shown below. Your ~wx/cs251/sml/ps7
folder contains the starter file OperationTreeSet.sml. When you finish this problem, rename the file to yourAccountNameOperationTreeSet.sml
before you submit it.
structure OperationTreeSet :> SET = struct
datatype ''a t = Empty
 Insert of ''a * ''a t
 Delete of ''a * ''a t
 Union of ''a t * ''a t
 Intersection of ''a t * ''a t
 Difference of ''a t * ''a t
val empty = Empty
fun insert x s = Insert(x,s)
fun singleton x = Insert(x, Empty)
fun delete x s = Delete(x, s)
fun union s1 s2 = Union(s1,s2)
fun intersection s1 s2 = Intersection(s1,s2)
fun difference s1 s2 = Difference(s1,s2)
fun member _ _ = raise Unimplemented
fun toList _ = raise Unimplemented
fun isEmpty _ = raise Unimplemented
fun size _ = raise Unimplemented
fun toPred _ = raise Unimplemented
fun toString eltToString _ = raise Unimplemented
fun fromList _ = raise Unimplemented
fun fromPred _ = raise Unimplementable
end
In the OperationTreeSet
structure, the set datatype t
is create by constructors Empty
, Insert
, Delete
, Union
, Intersection
, and Difference
. The empty
, singleton
, insert
, delete
, union
, intersection
, difference
operations are easy and have been implemented for you. You are responsible for fleshing out the definitions of the member
, toList
, size
, isEmpty
, toPred
, toString
, and fromList
operations.
Notes:

Your implementation of
member
should not use thetoList
function. Instead, it should be defined by case analysis on the structure of the operation tree. 
Your
toList
function should also be defined by case analysis on the structure of the operation tree. Themember
function should not be used in the implementation oftoList
(because it can be very inefficient). Keep in mind that sets are unordered, and yourtoList
function may return lists of elements in any order, but it should not have any duplicates. 
In your
toList
definition, be very careful with includingtoList
inside functional arguments, because this can often cause the same list to be calculated a tremendous number of times, leading to tests that take a very long time for large sets. If some of the test cases for large sets don’t return in a reasonable time, this is probably the cause. 
Your implementation of
size
andisEmpty
should use thetoList
function. Indeed, it is difficult to implement these functions by a direct case analysis on the operation tree. Note thatsize
will only work correcty if the output oftoList
does not contain duplicates. 
In the implementation of
toString
, the functionString.concatWith
is particularly useful. 
In the implementation of
fromList
, for lists with >= 2 elements, you should first split the list into two (nearly) equallength sublists usingList.take
andList.drop
and union the results of turning the sublists into sets. This yields a heightbalanced operation tree. 
OperationTreeSet.sml
ends with two collections of test cases. The first collection involves “small” sets that are defined without usingfromList
. The second collection involves “big” sets that are defined usingfromList
. The expected outputs to these tests can be found in this transcript file. Because of the way the testing functions are written, the integer elements in the outputs formember
andtoPred
will be in sorted order from low to high, but the integers in the outputs fortoList
andtoString
may be in any order. 
The testing code for most functions (all except for
member
and assumes thattoList
works correctly. So you must implementtoList
for for most of the tests to work.