TGuedes
TGuedes

Reputation: 79

SML: Error: match redundant and Warning:match nonexhaustive

I am getting both of these issues for this function:

fun funion([(x:int list,y:bool list)]) =
let
 fun join(nil,final) = final |
     join(x::xs,final) = join(xs,union(x,final)) |
     join(_,final) = final
in
 join([(x,y)],(nil,nil))
end;

Here is the error:

sets.sml:30.6-32.27 Error: match redundant

(nil,final) => ...

(x :: xs,final) => ...

-->   (_,final) => ...

sets.sml:28.5-35.4 Warning: match nonexhaustive

(x,y) :: nil => ...

Does anyone know what might be going on here? I also tried join(_) but that did not work either. Not sure what the problem is here.

uncaught exception Error

Edit:

Here is the definition of union:

fun union((w:int list,x:bool list),(y:int list,z:bool list)) =
let
 fun join((nil,nil),final) = final |
     join((w::ws,x::xs),(intfinal,boolfinal)) = if x andalso elementOf(w,(intfinal,boolfinal))=false then join((ws,xs),(w::intfinal,true::boolfinal)) else join((ws,xs),(intfinal,boolfinal)) |
     join(_,final) = final
in
 join((w,x),join((y,z),(nil:int list,nil:bool list)))
end;

Upvotes: 1

Views: 5658

Answers (2)

ruakh
ruakh

Reputation: 183301

"Error: match redundant" means that a pattern doesn't match anything that previously-tested matches wouldn't also match. In your case, you have the match-pattern (nil, final) (which matches any pair whose first element is the empty list) and the match-pattern (x::xs, final) (which matches any pair whose first element is a non-empty list), which together cover all cases . . . and then you have the match-pattern (_, final), which doesn't match anything new. Technically this doesn't need to be an error — the compiler could just issue a warning and discard this match — but it's such a serious warning that SML/NJ treats it as an error.

"Warning: match nonexhaustive" means that your match-patterns don't cover all cases. In your case, the match-pattern ([(x:int list,y:bool list)]) can only match a single-element list. (You probably just meant to write (x:int list, y:bool list), without the [...] notation to match a list of hardcoded length.)

Upvotes: 2

John Coleman
John Coleman

Reputation: 51998

I gather that funion is supposed to have type

(int list * bool list) list -> (int list * bool list)

But -- you only provide a definition for lists of length 1, which gives a non-exhaustive list warning.

For the inner function join you provide a definition first for patterns of the form (nil, final) and then for patterns of the form (x::xs,final). Since the first component is either empty or matches the pattern x::xs and any list whatsoever matches final, any further pattern is redundant. Perhaps you want the three patterns

1) (nil,ys)

2) (xs,nil)

3) (xs,ys)

A final remark -- if you are happy with union -- why not just use it with foldl or foldl if you have a list of (int list * bool list) and what the union of them all?

Upvotes: 2

Related Questions