Search code examples
smlsmlnj

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;

Solution

  • 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?