Ok, latest commit seems to work a bit. Always good to mess about debugging the
compiler .. only to find the main problem was actually in the library :)

At present if you get this stuff wrong .. you'll get weird errors at who-know 
what
places -- different stages of Felix compilation or even in the C++. The type 
encoding
U ** V, which requires V to be a tuple (of at least two components) is a second 
representation
of tuples. It is only handled correctly where I found (by experiment) it needed 
to be,
to get the following style of encoding to work. The type ** and pattern match 
,, is only
to be used similarly to below.

Implementation of  "str" for tuples.
==========================

Here's how we do it. We want to handle tuples like lists, this is done by the
class Tuple with polymorphic recursion, which causes compile time
unrolling (similar to C++ template expansion).

Because of the current Felix model, which says that there is a 0 length
tuple () and tuples of length 2 and above, but no tuple of length 1,
the recursion stopper is the "last case" which is a tuple of length 2.
Therefore the pattern ?a ,, ?b of type U ** V only matches tuples
of length THREE or more. So we need three cases to handle stuff:

U ** V, 3 or more
U * V, exactly 2
T, when it isn't a tuple

We need two implementations with this pattern to make "str"
work the INNER one Tuple which lists stuff with comma separators,
and the OUTER one which just puts parens around it.
Note carefully the dispatches with tuple_str and str.

class Tuple[U] {
  virtual fun tuple_str (x:U) => str x;
}

instance[U,V with Str[U], Tuple[V]] Tuple[U ** V] {
  fun tuple_str (x: U ** V) =>
    match x with
    | ?a ,, ?b => str a +", " + tuple_str b
    endmatch
  ;
}

instance[U,V with Str[U], Str[V]] Tuple[U * V] {
  fun tuple_str (x: U * V) =>
    match x with
    | ?a , ?b => str a +", " + str b
    endmatch
  ;
}

instance [U, V with Tuple[U ** V]] Str[U ** V] {
  fun str (x: U ** V) => "(" + tuple_str x +")";
}

// two components
// string
instance[T,U] Str[T*U] {
   fun str (t:T, u:U) => "("+str t + ", " + str u+")";
}
instance[T] Str[T*T] {
   fun str (t1:T, t2:T) => "("+str t1 + ", " + str t2+")";
}


--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to