On Fri, 2006-09-08 at 20:46 -0400, Peter Tanski wrote:
> On Sep 8, 2006, at 7:32 PM, skaller wrote:
>
> > Why do we have to type check them?
> same[ Eq[T] => T ] ...
Ok .. so now 'typeclass .. ' is implemented, and carries through
to (unbound) symbol table. Overloads could be a problem (more on
this soon). This code parses now:
-------------------------------
#import <flx.flxh>
typeclass X[t,u] {
fun add: t * u -> t;
fun sub: t * t -> t;
}
typeclass Y[t:fast_ints,u] {
fun add: t * u -> t;
fun sub: t * t -> t;
}
-------------------------------
Probably requires 'val/const' entries and defaults but I'll leave
them out for now.
The syntax t:fast_ints is a special constraint that says t
must be member of the set 'fast_ints'. This is not general
enough. First, such constraints can only be applied to individual
type variables. Secondly, we need to understand how the
constraints are built:
....
| `BTYP_intersect type_constraints
| `BTYP_var of int * 't
| `BTYP_apply of 't * 't
| `BTYP_typefun of (int * 't) list * 't * 't
| `BTYP_type
| `BTYP_type_tuple of 't list
| `BTYP_type_match of 't * ('t btpattern_t' * 't) list
(* type sets *)
| `BTYP_typeset of 't list (** open union *)
| `BTYP_typesetunion of 't list (** open union *)
| `BTYP_typesetintersection of 't list (** open union *)
(* Barry Jay pattern calculus *)
| `BTYP_case of 't * IntSet.t * 't
The original constraint is usually a typeset, which is just a list
of types, eg 'fast_ints' is just a list int,long, etc.
There's also a union and intersection operator for typesets.
The typeset is converted into a typematch: for example,
int,long,float
is converted into
typematch t with
| int => 1
| long => 1
| float => 1
| _ => 0
endmatch
which is applied to the argument x and then reduced. If the result is
the type 1 (unit), the constraint is met, if it is 0, it is not met,
and otherwise the terms can't be reduced and the program
chucks an exception.
Actually, there is a constraint for each variable and they're
joined by an type intersection (not to be confused with a
typeset intersection).
So: the notation
t:S
is converted to
t in S
if S is a typeset (and that is represented by exhaustive checks
in a typematch as above). If S is NOT a typeset, then
typematch t with
| S => 1
| _ => 0
is used instead. This is your 'traditional' pattern match,
that is, match by unification.
The point of explaining this is that the 'constraint' is
simply a type: the constraint is satisfied iff it reduces to 1.
SO .. to handle typeclasses we need a new term in the type
system, such as:
`BTYP_typeclass i
The 'i' is a symbol table index, typeclasses are nominally typed.
This is then handled by converting the 'noun' typeclass into
a predicate (similarly to typeset).
Secondly, I need to extend the syntax and data structures --
quite independently of typesets -- to allow constraints
on sets of variables, like:
[t,u,v where P[t,v] & Eq[v]]
This is simple enough, the additional term is just cojoined
to the others.
So ... with these two changes, it remains only to implement the
check
P[t,v]
where P is a typeclass of two variables.
This is the EASY part. The HARD part is: type constraints
don't propagate. That is, given
fun f[t:Q](x:t)=> g x;
the fact that Q(t) is not propagated to the call g.
Instead, x is just plain type t, an unconstrained type
variable ranging over all types, so overload resolution
of g x will .. and does .. fail, if g has constraints,
since an unconstrained type variable doesn't meet any
proper constraints.
This is critical, because it is what actually allows
you to apply the typeclass methods to x.
Now for the really hard part! I have NO idea how to
introduce the typeclass methods here.
What we have is to do a lookup of g of (typeof x).
I have a routine for that. But it is only luck
that x is of type t. What if x was of type t * t?
This really IS the case for:
typeclass X[t] {
fun add: t * t -> t;
}
The argument needed here is NOT t, but t * t.
Why should I lookup in X?
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language