[clean-list] Pattern matching

Diederik van Arkel dvanarkel@mac.com
Sat, 4 Jan 2003 18:05:46 +0100


On Saturday, January 4, 2003, at 09:30 AM, Dmitry Malenko wrote:

> Hi!
>
> I'm new to Clean, so forgive if it is a silly question.
>
> I have the following code:
>
> module test
>
> import StdEnv
>
> s x     = 3*x
> s [x]   = 2*x
> s [x,y] = x*y
>
> Start = s 4
>
> When compile it I get warning: "[test.icl,5,s] alternative will never 
> match". Nevertheless program runs perfectly and gives expected result 
> 12.
>
What's happening here is that apparently the compiler throws away the 
second and
third alternatives for s because the first alternative wil always 
succeed. It does
this before typechecking and therefore does not notice that the types 
of these
alternatives are inconsistent.

> Start = s [4]
> will give type error that Int can not be unified with [v1], so s seems 
> to have type s :: Int -> Int. But what about definitions with list as 
> a parameter? Why there is no complains?
>
Indeed s has type Int -> Int. See above for the explanation of why 
there is no error on the
type of the second and third alternatives. You seem to want s to have 
both type Int -> Int
and [Int] -> [Int], you might want to look up type classes in the 
reference manual for a
possible solution to what you are trying to achieve. If this is not 
what you are looking for
you may want to post a message stating your requirements more precisely.

> Again, warning seems to be incorrect.
>
> How s in the example above is typed?
>
> BTW How can I make "Types" window appear in Clean IDE?
>
You want to modify the setting under the menu Module:Module 
Options:List Types, if you
set this to 'All Types' you should get the 'Types' window.

Regards,

Diederik van Arkel
diederik@cs.kun.nl

P.S. There was no need to send your news message twice, the clean-list 
is a moderated
list and it can occasionally take some time before your mail is 
transmitted to the list.