« Pattern matching | Main | Expression problem »

November 15, 2004

Conditional pattern matching

Et voilà, pattern matching is available everywhere it makes sense. Comprehensions support 'conditional pattern matching' where the pattern contains non-binding variables, constants or variants that are used to filter the values. This allows nice code like this (using a constant as the filter in this case):

? def ^the_list = [lst {#x=1,#y=true}, {#x=2,#y=true}, {#x=3,#y=false}, {#x=4,#y=true}];
  Defined the_list : [lst {#y:pre(bool), #x:pre(int)}]
? [lst x | {#x=^x,#y=true} <lst the_list];
  [lst 1, 2, 4] : [lst int]

I must admit that getting this conditional pattern matching to work, even though it was only syntactic sugar, was a bit more difficult than I had thought. Anyway, it is working now. Letrec does not support pattern matching because I didn't find any way of obtaining it as syntactic sugar from the basic let operator (actually, I believe it is impossible). I am not the only one: O'Caml doesn't support it either (It says 'Only variables are allowed as left-hand side of `let rec'').

From last week's list, two big points are remaining: profiling the software to understand why it is slow for lists (I got an answer from an O'Caml guy that told me he would be working on the bug that prevents me from doing that) and looking up the expression problem. A few smaller points also need to be done (dot notation, unlabelled tuples) but I should be able to finish them quickly tomorrow morning.