[q-lang-users] Views and Virtual Constructors
Brought to you by:
agraef
From: Rob H. <hub...@gm...> - 2007-06-23 00:19:54
|
Hello Albert, [This is not urgent -- it's just a question out of curiosity.] Suppose I wish to have two separate virtual constructor sets for my own type. As an artificial example, here's a type MyList that behaves just like a 'Q' list: public type MyList = virtual mylist_cons Head Tail, mylist_nil | virtual mylist_join Left Right, mylist_singleton Elt | private const mylist List; mylist_nil = mylist []; mylist_cons Head (mylist Tail) = mylist [Head | Tail]; mylist_join (mylist Left) (mylist Right) = mylist (Left ++ Right); mylist_singleton Elt = mylist [Elt]; view (mylist []) = 'mylist_nil; view (mylist [Head | Tail]) = '(mylist_cons Head Tail) where Tail = mylist Tail; view (mylist [Elt]) = '(mylist_singleton Elt); view (mylist Elts:List) = '(mylist_join Left Right) where Left = (mylist (take N Elts)), Right = (mylist (drop N Elts)) where N = (#Elts) div 2; Suppose I have a list ==> def L = mylist [1,2,3,4] I'd like to perform pattern matching like this: ==> def (mylist_cons H T) = L; H; T 1 mylist_cons 2 (mylist_cons 3 (mylist_cons 4 mylist_nil)) or like this: ==> def (mylist_join X Y) = L; X; Y mylist_cons 1 (mylist_cons 2 mylist_nil) mylist_cons 3 (mylist_cons 4 mylist_nil) The trouble is: the latter of these does not work because the second view definition 'hides' the third and fourth, in that the match will always succeed if it reaches the second. Do you know of a way around this please? That is, is there a way for 'overlapping' views to be defined for use by the new pattern matching feature on virtual constructors? (I imagine not.) Thanks, Rob. |