On Wed, 2007-10-03 at 03:35 +1000, skaller wrote:
[]
Well here we go, with diagnostics:

///////////////////////////////////////
instance[T,U] Str[T * U] {
  fun str (x:T, y:U)=> str x + ", " + str y;
}

instance[T] Str[T*unit] {
  fun str( x:T,y:unit) => str x;
}

println$ str (1,(2,("3",())));
///////////////////////////////////////
Unimplemented: Multiple matching instances for typeclass virtual
instance
str<1898>[string<2133> * unit]
Function 4818[string<2133>,unit]
 instance parent 4814[<T4815> * <T4816>]
 instance vs= T<4815>,U<4816>
Function 4829[string<2133>]
 instance parent 4826[<T4827> * unit]
 instance vs= T<4827>
Considering candidate sig (<T4815> * <T4816>)
Considering candidate sig (<T4827> * unit)
 .. comparing with (<T4815> * <T4816>)
Found most specialised instance 4829[string<2133>]
 instance [<T4827> * unit]
Unimplemented: Multiple matching instances for typeclass virtual
instance
str<1898>[string<2133> * unit]
Function 4818[string<2133>,unit]
 instance parent 4814[<T4815> * <T4816>]
 instance vs= T<4815>,U<4816>
Function 4829[string<2133>]
 instance parent 4826[<T4827> * unit]
 instance vs= T<4827>
Considering candidate sig (<T4815> * <T4816>)
Considering candidate sig (<T4827> * unit)
 .. comparing with (<T4815> * <T4816>)
Found most specialised instance 4829[string<2133>]
 instance [<T4827> * unit]
1, 2, 3
///////////////////////////////////////

Note, this is a polymorphic recursion with the induction
terminated by a specialisation. You can do this with
C++ templates too.

Now the problem: the instantiator cannot create functions at
the moment. What we REALLY need is:

        pile_right_1 (x,y,z,a) ) --> x, (y,z,a)

i.e., convert a tuple into a pair of head and tail.

A combinator to do that is easy to provide.. but it only
works on values of tuple type, that is, it won't work
on a value whose type is just a type variable: to work,
it has to know how many components the tuple has.

Now, this is not known UNTIL the instantiation of the
type variable occurs. The problem is roughly cloning again:
the instance created contains another call to a virtual 
function, which spawns another instance.. is that so bad ..
well it is happening NOW in the above example, so maybe not:
it happens during monomorphisation if not before.

So actually, we cannot do this at instantiation time BUT
typeclass virtuals are dispatched BEFORE instantiation
these days, during an instantiation pass, during inlining,
during monomorphisation, and the optimisation pass that
follows it.

In theory, if monomorphisation is complete, there is no
instantiation to do (all the type variables are eliminated).
The pass is kept around though because some things like
regexps and GLRs aren't monomorphised yet, and also,
it provides a hook for dynamic typing!

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to