2

I am trying to understand the monomorphism restriction with the help of What is the monomorphism restriction? but do not understand, what Section 4.5.1 of the report is trying to explain.

A declaration group is a minimal set of mutually dependent bindings.

what exactly is a declaration group? Could someone provide an example?

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

3

Imagine a program

foo x =
   let f n = g (n-1)
       g n = f (n-2)
       h n = 42*n
   in
      f (h x)

Here f and g form a minimal set of mutually depending bindings.

They do not depend on h, nor does h depend on any of them. So we could re-write it as

foo x =
  let  h n = 42*n    in
    let f n = g (n-1)
        g n = f (n-2)
    in
       f (h x)

but we couldn't break up the group of f and g -- they must go together, as each one of them refers to the other.

Here, both f and g binding are function bindings, so they are unrestricted, but if we had g = \n -> f (n-2) there, it would mean that g's binding is a simple pattern binding, and both f an g would become subject to the monomorphism restriction.

We could say h, f, g are a set of bindings, but it is not minimal, because we can take h out of that group. Only when we can't take out any names anymore, we've got the minimal (i.e. the smallest in size) group. So if we re-write g = \n -> f (n-2), g becomes simple pattern binding, becomes subject to the restriction, and f becomes subject to the restriction together with it, even though f's bindind is a function binding. But h remains unaffected.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • so `f` and `g` have to be on the same scope? *Here f and g form a minimal set of mutually depending bindings* what is the meaning of minimal? – softshipper Feb 21 '19 at 10:33
  • yes, to be able to refer to each other they must be in the same scope! – Will Ness Feb 21 '19 at 10:39
  • the canonical answer (the duplicate) says : "The monomorphism restriction affects restricted declaration groups." so the names in a restricted group are subject to the restriction. – Will Ness Feb 21 '19 at 10:42
  • What does *subject to the restriction* mean? It is relevant for the restriction? – softshipper Feb 21 '19 at 10:45
  • that is defined further down in that answer. – Will Ness Feb 21 '19 at 10:46
  • It means *f an g would become subject to the monomorphism restriction.*, that `f` and `g` are getting restricted right? Both become a particular type and the same type. – softshipper Feb 21 '19 at 10:52
  • not necessarily the same, but monomorphic. some type variable shared between the two, which gets monomorphic type, of course refers to the same type every time it is referenced, whether in `f`'s or `g`'s type. – Will Ness Feb 21 '19 at 13:23