Index: doc/theses/jiada_liang_MMath/CFAenum.tex
===================================================================
--- doc/theses/jiada_liang_MMath/CFAenum.tex	(revision 3be81a4f4d89f40c05e9d5218b3b49580d239127)
+++ doc/theses/jiada_liang_MMath/CFAenum.tex	(revision dc1c43080c7e19efbb0190198c9459ca44c5774f)
@@ -7,16 +7,18 @@
 The following sections detail all of my new contributions to enumerations in \CFA.
 
-
-\section{Aliasing}
-
-C already provides @const@-style aliasing using the unnamed enumerator \see{\VRef{s:TypeName}}, even if the name @enum@ is misleading (@const@ would be better).
-Given the existence of this form, it is straightforward to extend it with types other than @int@.
-\begin{cfa}
-enum E { Size = 20u, PI = 3.14159L, Jack = L"John" };
-\end{cfa}
-which matches with @const@ aliasing in other programming languages.
-Here, the type of the enumerator is the type of the initialization constant, \eg @typeof(20u)@ for @Size@ implies @unsigned int@.
-Auto-initialization is restricted to the case where all constants are @int@, matching with C.
-As seen in \VRef{s:EnumeratorTyping}, this feature is just a shorthand for multiple typed-enumeration declarations.
+\begin{comment}
+	Not support. 
+\end{comment}
+% \section{Aliasing}
+
+% C already provides @const@-style aliasing using the unnamed enumerator \see{\VRef{s:TypeName}}, even if the name @enum@ is misleading (@const@ would be better).
+% Given the existence of this form, it is straightforward to extend it with types other than @int@.
+% \begin{cfa}
+% enum E { Size = 20u, PI = 3.14159L, Jack = L"John" };
+% \end{cfa}
+% which matches with @const@ aliasing in other programming languages.
+% Here, the type of the enumerator is the type of the initialization constant, \eg @typeof(20u)@ for @Size@ implies @unsigned int@.
+% Auto-initialization is restricted to the case where all constants are @int@, matching with C.
+% As seen in \VRef{s:EnumeratorTyping}, this feature is just a shorthand for multiple typed-enumeration declarations.
 
 
@@ -28,5 +30,6 @@
 
 The \CFA type-system allows extensive overloading, including enumerators.
-Furthermore, \CFA uses the left-hand of assignment in type resolution to pinpoint the best overloaded name.
+Furthermore, \CFA uses the environment, such as the left-had of assignment and function parameter, to pinpoint the best overloaded name. 
+% Furthermore, \CFA uses the left-hand of assignment in type resolution to pinpoint the best overloaded name.
 Finally, qualification and casting are provided to disambiguate any ambiguous situations.
 \begin{cfa}
@@ -35,8 +38,11 @@
 E1 f() { return Third; }				$\C{// overloaded functions, different return types}$
 E2 f() { return Fourth; }
+void g(E1 e);
+void h(E2 e);
 void foo() {
 	E1 e1 = First;   E2 e2 = First;		$\C{// initialization}$
 	e1 = Second;   e2 = Second;			$\C{// assignment}$
-	e1 = f();   e2 = f();				$\C{// function call}$
+	e1 = f();   e2 = f();				$\C{// function return}$
+	g(First); h(First);					$\C{// function parameter}$
 	int i = @E1.@First + @E2.@First;	$\C{// disambiguate with qualification}$
 	int j = @(E1)@First + @(E2)@First;	$\C{// disambiguate with cast}$
@@ -72,11 +78,84 @@
 
 
-\section{Enumeration Trait}
-
-The header file \lstinline[deletekeywords=enum]{<enum.hfa>} defines the set of traits containing operators and helper functions for @enum@.
+\section{Enumeration Traits}
+
+\CFA defines the set of traits containing operators and helper functions for @enum@.
 A \CFA enumeration satisfies all of these traits allowing it to interact with runtime features in \CFA.
 Each trait is discussed in detail.
 
-The trait @Bounded@:
+The trait @CfaEnum@:
+\begin{cfa}
+forall( E ) trait CfaEnum {
+	char * label( E e );
+	unsigned int posn( E e );
+};
+\end{cfa}
+
+describes an enumeration as a named constant with position. And @TypeEnum@
+\begin{cfa}
+forall( E, V ) trait TypeEnum {
+	V value( E e );
+};	
+\end{cfa}
+asserts two types @E@ and @T@, with @T@ being the base type for the enumeration @E@. 
+
+The declarative syntax
+\begin{cfa}
+enum(T) E { A = ..., B = ..., C = ... };
+\end{cfa}
+creates an enumerated type E with @label@, @posn@ and @value@ implemented automatically.
+
+\begin{cfa}
+void foo( T t ) { ... }
+void bar(E e) {
+	choose (e) {
+		case A: printf("\%d", posn(e));
+		case B: printf("\%s", label(e));
+		case C: foo(value(e));
+	} 
+}
+\end{cfa}
+
+Implementing general functions across all enumeration types is possible by asserting @CfaEnum( E, T )@, \eg:
+\begin{cfa}
+#include <string.hfa>
+forall( E, T | CfaEnum( E, T ) | {unsigned int toUnsigned(T)} )
+string formatEnum( E e ) {
+	unsigned int v = toUnsigned(value(e));
+	string out = label(e) + '(' + v +')';
+	return out;
+}
+printEunm( Week.Mon );
+printEnum( RGB.Green );
+\end{cfa}
+
+\CFA does not define attributes functions for C style enumeration. But it is possilbe for users to explicitly implement
+enumeration traits for C enum and any other types.
+
+\begin{cfa}
+enum Fruit { Apple, Bear, Cherry };			$\C{// C enum}$
+char * label(Fruit f) {
+	switch(f) {
+		case Apple: "A"; break;
+		case Bear: "B"; break;
+		case Cherry: "C"; break;
+	}
+}
+unsigned posn(Fruit f) { return f; }
+char* value(Fruit f) { return ""; } 		$\C{// value can return any non void type}$
+formatEnum( Apple );							$\C{// Fruit is now a Cfa enum}$
+\end{cfa}
+
+A type that implement trait @CfaEnum@, \ie, a type has no @value@, is called an opaque enum.
+
+% \section{Enumerator Opaque Type}
+
+% \CFA provides a special opaque enumeration type, where the internal representation is chosen by the compiler and only equality operations are available.
+\begin{cfa}
+enum@()@ Planets { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE };
+\end{cfa}
+
+
+In addition, \CFA implements @Bound@ and @Serial@ for \CFA Enums.
 \begin{cfa}
 forall( E ) trait Bounded {
@@ -85,5 +164,5 @@
 };
 \end{cfa}
-defines the bounds of the enumeration, where @first()@ returns the first enumerator and @last()@ returns the last, \eg:
+The function @first()@ and @last()@ of enumertated type E return the first and the last enumerator declared in E, respectively. \eg:
 \begin{cfa}
 Workday day = first();					$\C{// Mon}$
@@ -93,5 +172,5 @@
 Calling either functions without a context results in a type ambiguity, except in the rare case where the type environment has only one enumeration.
 \begin{cfa}
-@first();@								$\C{// ambiguous Workday and Planet implement Bounded}$
+@first();@								$\C{// ambiguous because both Workday and Planet implement Bounded}$
 sout | @last()@;
 Workday day = first();					$\C{// day provides type Workday}$
@@ -116,30 +195,11 @@
 Specifically, for enumerator @E@ declaring $N$ enumerators, @fromInt( i )@ returns the $i-1_{th}$ enumerator, if $0 \leq i < N$, or raises the exception @enumBound@.
 
-The @Serial@ trait also requires interface functions @succ( E e )@ and @pred( E e )@ be implemented for a serial type, which imply the enumeration positions are consecutive and ordinal. 
+The @succ( E e )@ and @pred( E e )@ imply the enumeration positions are consecutive and ordinal. 
 Specifically, if @e@ is the $i_{th}$ enumerator, @succ( e )@ returns the $i+1_{th}$ enumerator when $e \ne last()$, and @pred( e )@ returns the $i-1_{th}$ enumerator when $e \ne first()$. 
 The exception @enumRange@ is raised if the result of either operation is outside the range of type @E@.
 
-The trait @TypedEnum@:
-\begin{cfa}
-forall( E, T ) trait TypedEnum {
-	T valueE( E e );
-	char * labelE( E e );
-	unsigned int posE( E e );
-};
-\end{cfa}
-captures three basic attributes of an enumeration type: value, label, and position. 
-@TypedEnum@ asserts two types @E@ and @T@, with @T@ being the base type of the enumeration @E@, \eg @enum( T ) E { ... };@.
-Implementing general functions across all enumeration types is possible by asserting @TypeEnum( E, T )@, \eg:
-\begin{cfa}
-forall( E, T | TypeEnum( E, T ) )
-void printEnum( E e ) {
-	sout | "Enum "| labelE( e );
-}
-printEunm( MARS );
-\end{cfa}
-
 Finally, there is an associated trait defining comparison operators among enumerators. 
 \begin{cfa}
-forall( E, T | TypedEnum( E, T ) ) {
+forall( E, T | CfaEnum( E, T ) ) {
 	// comparison
 	int ?==?( E l, E r ); 		$\C{// true if l and r are same enumerators}$
@@ -152,41 +212,6 @@
 }
 \end{cfa}
-Note, the overloaded operators are defined only when the header @<enum.hfa>@ is included.
-If not, the compiler converts an enumerator to its value, and applies the operators defined for the value type @E@, \eg:
-\begin{cfa}
-// if not include <enum.hfa>
-enum( int ) Fruits { APPLE = 2, BANANA = 10, CHERRY = 2 };
-APPLE == CHERRY; // true because valueE( APPLE ) == valueE( CHERRY )
-
-#include <enum.hfa>
-APPLE == CHERRY; // false because posE( APPLE ) != posE( CHERRY )
-\end{cfa}
-An enumerator returns its @position@ by default.
-In particular, @printf( ... )@ from @<stdio.h>@ functions provides no context to its parameter type, so it prints @position@. 
-On the other hand, the pipeline operator @?|?( ostream os, E enumType )@ provides type context for type @E@, and \CFA has overwritten this operator to print the enumeration @value@ over @position@.
-\begin{cfa}
-printf( "Position of BANANA is \%d", BANANA ); // Position of BANANA is 1
-sout | "Value of BANANA is " | BANANA; // Value of BANANA is 10
-\end{cfa}
-Programmers can overwrite this behaviour by overloading the pipeline operator themselves.
-\PAB{This needs discussing because including \lstinline{<enum.hfa>} can change the entire meaning of a program.}
-
-
-% \section{Enumeration Pseudo-functions}
-
-% Pseudo-functions are function-like operators that do not result in any run-time computations, \ie like @sizeof@, @alignof@, @typeof@.
-% A pseudo-function call is often substituted with information extracted from the compilation symbol-table, like storage size or alignment associated with the underlying architecture.
-
-% The attributes of an enumerator are accessed by pseudo-functions @posE@, @valueE@, and @labelE@.
-% \begin{cfa}
-% int jane_pos = @posE@( Names.Jane );   $\C{// 2}$
-% char * jane_value = @valueE@( Names.Jane ); $\C{// "JANE"}$
-% char * jane_label = @labelE@( Names.Jane ); $\C{// "Jane"}$
-% sout | posE( Names.Jane) | labelE( Names.Jane ) | valueE( Names.Jane );
-% \end{cfa}
-% Note the ability to print all of an enumerator's properties.
-
-
-\section{Enumerator Typing}
+
+\section{Typed Enum}
 \label{s:EnumeratorTyping}
 
@@ -256,30 +281,29 @@
 calling constructors happens at runtime (dynamic).
 
-
-\section{Enumerator Opaque Type}
-
-\CFA provides a special opaque enumeration type, where the internal representation is chosen by the compiler and only equality operations are available.
-\begin{cfa}
-enum@()@ Planets { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE };
-\end{cfa}
-
-
 \section{Enumeration Inheritance}
 
 \CFA Plan-9 inheritance may be used with enumerations, where Plan-9 inheritance is containment inheritance with implicit unscoping (like a nested unnamed @struct@/@union@ in C).
+
 \begin{cfa}
 enum( char * ) Names { /* as above */ };
 enum( char * ) Names2 { @inline Names@, Jack = "JACK", Jill = "JILL" };
-@***@enum /* inferred */ Names3 { @inline Names2@, Sue = "SUE", Tom = "TOM" };
-\end{cfa}
+enum( char * ) Names3 { @inline Names2@, Sue = "SUE", Tom = "TOM" };
+\end{cfa}
+
 Enumeration @Name2@ inherits all the enumerators and their values from enumeration @Names@ by containment, and a @Names@ enumeration is a subtype of enumeration @Name2@.
 Note, enumerators must be unique in inheritance but enumerator values may be repeated.
 
-The enumeration type for the inheriting type must be the same as the inherited type;
-hence the enumeration type may be omitted for the inheriting enumeration and it is inferred from the inherited enumeration, as for @Name3@.
+% The enumeration type for the inheriting type must be the same as the inherited type;
+% hence the enumeration type may be omitted for the inheriting enumeration and it is inferred from the inherited enumeration, as for @Name3@.
 % When inheriting from integral types, automatic numbering may be used, so the inheritance placement left to right is important.
 Specifically, the inheritance relationship for @Names@ is:
 \begin{cfa}
-Names $\(\subset\)$ Names2 $\(\subset\)$ Names3 $\(\subset\)$ const char * $\C{// enum type of Names}$
+Names $\(\subset\)$ Names2 $\(\subset\)$ Names3 $\C{// enum type of Names}$
+\end{cfa}
+A subtype can be casted to its super type, assigned to a super type variable, or be used as a function argument that expects the super type.
+\begin{cfa}
+Names fred = Name.Fred;
+(Names2) fred; (Names3) fred; (Name3) Names.Jack;  $\C{// cast to super type}$
+Names2 fred2 = fred; Names3 fred3 = fred2; $\C{// assign to super type}$
 \end{cfa}
 For the given function prototypes, the following calls are valid.
@@ -297,10 +321,9 @@
 g( Fred );   g( Jill );
 h( Fred );   h( Jill );   h( Sue );
- j( Fred );    j( Jill );    j( Sue );    j( "WILL" );
+j( Fred );    j( Jill );    j( Sue );    j( "WILL" );
 \end{cfa}
 \end{tabular}
 \end{cquote}
 Note, the validity of calls is the same for call-by-reference as for call-by-value, and @const@ restrictions are the same as for other types.
-
 
 \section{Enumerator Control Structures}
@@ -364,11 +387,57 @@
 \end{cfa}
 
-
-@if@ statement
-
-@switch@ statement
-
-looping statements
-
+\begin{cfa}
+for (d; Workday) { sout | d; }
+for (p; +~=Planet) { sout | p; }
+for (c: -~=Alphabet ) { sout | c; }
+\end{cfa}
+The @range loop@ for enumeration is a syntax sugar that looping over all enumeerators and assign each enumeration to a variable in every iteration.
+The loop control of range loop consists of two parts: a variable declaration and a @range expression@, with type of the variable
+can be inferred from the range expression.
+
+The range expression is an enumeration type, optionally prefixed by @+~=@ or @-~=@. Without a prefix, or prefixed with @+~=@, the control
+loop over all enumerator from the first to the last. With a @-~=@ prefix, the control loops backwards.
+
+On a side note, the loop syntax
+\begin{cfa}
+for ( typeof(Workday) d; d <= last(); d = succ(d) );
+\end{cfa}
+does not work. When d == last(), the loop control will still attempt to assign succ(d) to d, which causes an @enumBound@ exception.
+
+\CFA reduces conditionals to its "if case" if the predicate not equal to ( @!=@ ) zero, and the "else case" otherwises.
+Overloading the @!=@ operator with an enumeration type against the zero defines a conceptual conversion from
+enum to boolean, which can be used as predicates.
+
+\begin{cfa}
+enum(int) ErrorCode { Normal = 0, Slow = 1, Overheat = 1000, OutOfResource = 1001 };
+bool ?!=?(ErrorCode lhs, zero_t) { return value(lhs) >= 1000; }
+ErrorCode code = /.../
+if (code) { scream(); }
+\end{cfa}
+
+Indicentally, \CFA does not define boolean conversion for enumeration. If no 
+@?!=?(ErrorCode, zero_t)@ 
+overloading defined,
+\CFA looks for the boolean conversion in terms of its value, and gives an compiler error if no such conversion is available.
+
+\begin{cfa}
+enum(int) Weekday { Mon, Tues, Wed, Thurs, Fri, Sat, Sun, };
+enum() Colour { Red, Green, Blue };
+enum(S) Fruit { Apple, Banana, Cherry }
+Weekday w = ...; Colour c = ...; Fruit f = ...;
+if (w) { ... } // w is true if and only if w != Mon, because value(Mon) == 0 (auto initialized)
+if (c) { ... } // error
+if (s) { ... } // depends on ?!=?(S lhs, zero_t ), and error if no such overloading available
+\end{cfa}
+
+As an alternatively, users can define the boolean conversion for CfaEnum:
+
+\begin{cfa}
+forall(E | CfaEnum(E))
+bool ?!=?(E lhs, zero_t) {
+	return posn(lhs) != 0;
+}
+\end{cfa}
+which effectively turns the first enumeration as a logical zero and non-zero for others.
 
 \section{Enumerated Arrays}
