[cduce-users] default match policy not working in sequences

Giuseppe Castagna Giuseppe.Castagna at ens.fr
Tue Nov 2 18:47:27 CET 2004


Matthias Radestock wrote:
> # let ((x & 1) | (x := 2)) = 3;;
> val x : 2 = 2
> # let [((x & 1) | (x := 2))] = [3];;
> Error at chars 30-31:
> This expression should have type:
> 1
> but its inferred type is:
> 3
> which is not a subtype, as shown by the sample:
> 3
> 
> 
> Am I doing something wrong, or is this a bug?
> 
> 

I am afraid this is not a bug but a consequence of implementing list as 
pairs and of a very loooong discussion I and Alain had about whether 
default patterns should "eat" a sequence element or not (and IIRC 
neither of us were fully convinced that the solution we chose was the 
better one). We chose that the default pattern does not "eat" a sequence 
element. If you prefer, default patterns match only end of lists (nil).

You should read this pattern

[((x & 1) | (x := 2))]

as "if the list contains one element and it is of type 1 capture it 
otherwise (and here "otherwise" means if the list is empty) assign 2 to x".

So the type accepted by this pattern is [1?]

You see it by writing this expression

# let [((x & 1) | (x := 2))] = 5;;
Error at chars 29-30:
This expression should have type:
[ 1? ]
but its inferred type is:
5
which is not a subtype, as shown by the sample:
5


What you probably wanted to write is

# let [(x & 1)] | (x := 2) = [3];;
val x : 2 = 2

Which says that if you have a the list [1] then caputuer 1 in x in all 
other cases x evals to 2.

Now the type accepted by this pattern is Any. Indeed

# let [(x & 1)] | (x := 2) = 5;;
val x : 2 = 2


So if you want a pattern whose accepted type is just [_] (i.e. any 
sequence of length 1), then you must modify your pattern so that the 
alternative with the default pattern "eats" the single element. This can 
be done as follows.

#  let [((x & 1) | (_& (x := 2)) )] = [3];;
val x : 2 = 2

The _ pattern eats the element and the default pattern assign 2 to x.

The more I think about it and the more I believe we haven't done the 
right choice.

I guess that we have to add a section on default patterns in the tutorial.

---Beppe---



More information about the Cduce-users mailing list