[MLton] warnUnused and functors

Matthew Fluet fluet at tti-c.org
Tue Feb 6 16:07:29 PST 2007


Consider the following.

z.sml:
type unit = {}
functor F(structure X : sig type t end) =
    struct
       type t = X.t
       fun f1 (_ : X.t) = ()
       fun f2 (_ : X.t) = ()
       val z = ()
    end
functor G(structure Y : sig
                            type t
                            val f1 : t -> unit
                            val f2 : t -> unit
                            val z : unit
                         end) =
    struct
       fun g (x : Y.t) = Y.f1 x
    end
functor H(structure Y : sig
                            type t
                            val f1 : t -> unit
                            val f2 : t -> unit
                            val z : unit
                         end) =
    struct
       fun h (x : Y.t) = Y.f1 x
    end
functor Z() =
    struct
       structure S = F(structure X = struct type t = unit end)
       structure SG = G(structure Y = S)
       structure SH = H(structure Y = S)
    end
structure U = Z()
val _ = U.SG.g {}
val _ = U.SH.h {}


z.mlb:
local
   ann "warnUnused true" in z.sml end
in
end


fenrir:~/tmp fluet$ mlton -show-def-use z.du -stop tc z.mlb
fenrir:~/tmp fluet$ cat z.du
type unit z.sml 1.6
     z.sml 11.42
     z.sml 12.42
     z.sml 13.36
     z.sml 20.42
     z.sml 21.42
     z.sml 22.36
     z.sml 29.53
functor F z.sml 2.9
     z.sml 29.21
type t z.sml 4.12
     z.sml 16.20
     z.sml 25.20
variable f1 z.sml 5.11
     z.sml 16.27
     z.sml 25.27
variable f2 z.sml 6.11
variable z z.sml 7.11
functor G z.sml 9.9
     z.sml 30.22
variable g z.sml 16.11
     z.sml 34.14
variable x z.sml 16.14
     z.sml 16.30
functor H z.sml 18.9
     z.sml 31.22
variable h z.sml 25.11
     z.sml 35.14
variable x z.sml 25.14
     z.sml 25.30
functor Z z.sml 27.9
     z.sml 33.15
structure S z.sml 29.17
     z.sml 30.38
     z.sml 31.38
structure X z.sml 29.33
     z.sml 4.16
     z.sml 5.19
     z.sml 6.19
type t z.sml 29.49
     z.sml 4.18
     z.sml 5.21
     z.sml 6.21
structure SG z.sml 30.17
     z.sml 34.11
structure Y z.sml 30.34
     z.sml 16.18
     z.sml 16.25
structure SH z.sml 31.17
     z.sml 35.11
structure Y z.sml 31.34
     z.sml 25.18
     z.sml 25.25
structure U z.sml 33.11
     z.sml 34.9
     z.sml 35.9

Note that there were no unused warnings.  Yet, the def-use information 
clearly indicates that
   variable f2 z.sml 6.11
and
   variable z z.sml 7.11
have no uses.

Looking at elaborate-env.fun, I see that we forceUse both the result of 
a functor *and* the actual argument to a functor.  Is there a reason for 
doing this?

I came across this using Vesa's def-use-mode (which is superbly 
helpful!).  I was very surprised when browsing some portions of the 
mlton codebase to find definitions with no uses, since we always compile 
with 'warnUnused true'.

I tried commenting out the uses of forceUse in the functor elaboration, 
and I got warnings on f2 and z for the code above.  Unfortunately, I 
also got a *lot* of warnings on the compiler itself -- mlton's highly 
functorized style means that there are a lot of values bound in a 
functor body that are unused (and aren't warned about otherwise).




More information about the MLton mailing list