On Tue, 2007-10-02 at 20:37 +1000, skaller wrote: > On Mon, 2007-10-01 at 22:34 -0700, Erick Tryzelaar wrote:
> ///////////////////////////////////////////// > instance[T,U] Str[T * U] { > fun str (x:T, y:U)=> str x + ", " + str y; > } > instance Str[unit] { > fun str() => ""; > } > > println$ str (1,(2,("3",()))); > //////////////////////////////////////////////// > $ f s2 > 1, 2, 3, > ////////////////////////////////////////////////// > > Unfortunately this: > > instance[T] Str[T*unit] { > fun str( x:T,y:unit) => str x; > } > > gives: > > Unimplemented: Multiple matching instances for typeclass virtual > instance > str<1898>[string<2133> * unit] > Function 4818 instance parent 4814 > SYSTEM FAILURE > Woops can't find instance 4814 (The 'instance has been lost, it isn't needed). The rule as usual is: given some typeclass C with N variables, and two instances: instance[ v1,v2... vm] C[ t1, t2, .. tN] instance[ v1',v2'... vm'] C[ t1', t2', .. tN'] where each ti is expressed in terms of vi, then the second instance specialises the first if there exists a substitution vi -> ui where ui is expressed in terms of vj', such that ti (vi->ui) = ti' that is, each ti with vi replaced by ui equals ti'. Note m and m' don't have to be equal, nor equal to N, but N=N'. For example: instance[U,V] C[U,V*U] instance[S,T] C[S*T,(T*T)*(S*T)] gives the second instance as a specialisation of the first by U -> S*T V -> T*T so that C[U,V*U] becomes C[S*T,(T*T)*(S*T)] as required. This means to do the calculation we need to know: a) N = how many typeclass variables there are b) the type variables for both instances (U,V) and (S,T) (instance vs) c) the bindings of the typeclass variables to types expressed in these terms (given positionally, instance ts) Unfortunately .. the existing data structure tells details of an individual function, not the instance. It may have additional type variables. These are ignored, and cannot be used in the first N ts values. -- 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