No subject

James William Coffey jcoffey@cs.utexas.edu
Fri, 4 Oct 1996 17:01:17 -0500


 	I have a question concerning classes and ADT`s in Clean. I have 
 noticed that the same question could be asked of Haskell and Gofer. 
 I will use the syntax of Clean to ask the question because this is the 
 language that I am most familiar with. The question can be summarize as
 "Why does Clean (and other FPL`s) not allow the use of type synonyms when
 defining class instances?" 
	The code that I show here has not been tested (or tried for that 
 matter). Most of it is copied from the Clean Language Report 1.1.

	The following is an example of an ADT or type synonym in Clean.

definition module stack

::Stack type
Empty		::	(Stack type) 
isEmpty		::	(Stack type)		-> Bool
Top		::	(Stack type)		-> type
Push		:: 	type (Stack type)	-> (Stack type)
Pop		:: 	(Stack type)		-> (Stack type)

implementation module stack

::Stack type :== [type]

Empty :: (Stack type)
Empty = []

isEmpty :: (Stack type)	-> Bool
isEmpty [] = True
isEmpty someStack = False

Top :: (Stack type) -> type
Top [anInstanceOfType : someStack] = anInstanceOfType

Push :: type (Stack type) -> (Stack type)
Push anInstanceOfType someStack = [anInstanceOfType : someStack]

Pop :: (Stack type) -> (Stack type)
Pop [] = []
Pop [anInstanceOfType : someStack] = someStack

	The following is an example of an algebraic type in Clean.

::Stack2 type = Empty | Stk type (Stack2 type)

isEmpty :: (Stack2 type)	-> Bool
isEmpty Empty = True
isEmpty _ = False

Top :: (Stack2 type) -> type
Top (Stk anInstanceOfType someStack) = anInstanceOfType

Push :: type (Stack2 type) -> (Stack2 type)
Push anInstanceOfType someStack = (Stk anInstanceOfType  someStack)

Pop :: (Stack2 type) -> (Stack2 type)
Pop Empty = Empty
Pop (Stk anInstanceOfType  someStack) = someStack

	The following is an example of a Class in Clean.

class Eq type
where
	(==) infix 2 :: type type -> Bool

instance Eq [type] | Eq type
where
	(==) infix 2 :: [type] [type] -> Bool | Eq type
	(==) [x:xs] [y:ys] = x == y && xs == ys
	(==) []	    []     = True
	(==) _	    _      = False

	In Clean one is "...not allowed to use a type synonym as instance."
 A similar statement is made for Haskell. So you couldn't for instance do the
 following.

instance Eq (Stack type) | Eq type
where
	(==) infix 2 :: (Stack type) (Stack type) -> Bool | Eq type
	(==) [x:xs] [y:ys] = x == y && xs == ys
	(==) []	    []     = True
	(==) _	    _      = False	

But you could do the this.

instance Eq (Stack2 type) | Eq type
where
	(==) infix 2 :: (Stack2 type) (Stack2 type) -> Bool | Eq type
	(==) Empty Empty     = True
	(==) (Stk x xs) (Stk y ys) = x == y && xs == ys
	(==) _	    _      = False	

	Why? What is the motivation for not allowing the programmer to
 to use a type synonym as a class instance? 
	I appreciate your time in answering this question. If you know of any 
 papers or books which would answer this question please direct me to them.

Thank you,

Jan William Coffey
jcoffey@cs.utexas.edu
jcoffey@arlut.utexas.edu