[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Instance restriction
Original-Via: uk.ac.nsf; Thu, 13 Sep 90 00:14:08 BST
To: haskell@cs.yale.edu
Reply-To: mleone@cs.cmu.edu
Subject: Instance restriction
Date: Wed, 12 Sep 90 14:42:46 EDT
From: Mark.Leone@f.gp.cs.cmu.edu
Original-Sender: Mark.Leone%edu.cmu.cs.gp.f@yale.edu
Sender: haskell-request@cs.glasgow.ac.uk
I'm curious about a certain syntactic restriction in Haskell instance
statements. It seems to me that this restriction is easy to circumvent
and therefore might be unnecessary.
The syntax of Haskell type classes, from Section 4.3 of the Haskell report,
is as follows (keywords in all caps):
topdecl ::= CLASS [context =>] class [WHERE { cdecls }]
| INSTANCE [context =>] tycls inst [WHERE { decls }]
context ::= (class_1, ..., class_k) (k >= 1)
class ::= tycls tyvar
inst ::= tcon (arity tcon = 0)
| (tcon tyvar_1 ... tyvar_k) (arity tcon = k > 0)
| (tyvar_1, ..., tyvar_k) (k >= 2)
| ()
| [ tyvar ]
| tyvar_1 -> tyvar_2
In particular, note that the form of an "inst" is severely restricted.
However, consider a more general definition:
inst' ::= tcon
| (tcon type_1 ... type_k)
| (type_1, ..., type_k)
| ()
| [ type ]
| type -> type
It seems to me that the programmer can achieve this effect by simply
adding a type synonym to his program! That is to say,
INSTANCE [context =>] tycls inst' [WHERE {decls}]
can be accomplished by:
TYPE tcon a1 ... ak = inst'
INSTANCE [context =>] tycls (tcon a1 ... ak) [WHERE {decls}]
where a1 ... ak are the free type variables in inst' and tcon is a "new"
type constructor name.
Is this correct, or am I misunderstanding the Haskell report? Why is
this restriction present anyway?
--
Mark R. Leone <mleone@cs.cmu.edu>
Computer Science, Carnegie Mellon University
Pittsburgh, PA 15213