RE: recursive modules in Haskell
| i am curious however, what is difficult about implementing recursive | modules (that is, if it can be explained without getting into the | technical details of GHC). Nothing deep. GHC is just a fairly big thing and one of its assumptions is that it is compiling one module at a time. There'd be quite a bit of chuffing around to remove this assumption. Nothing fundamental, but real work. Simon
Simon Peyton-Jones <simonpj@microsoft.com> writes:
Nothing deep. GHC is just a fairly big thing and one of its assumptions is that it is compiling one module at a time. There'd be quite a bit of chuffing around to remove this assumption. Nothing fundamental, but real work.
The other big problem is that, in the worst case, you need to typecheck the entire program (however many modules that may be) at once. If your program happens to be GHC, Lolita or one of the other big Haskell programs, this might be prohibitively expensive. A potentially lightweight alternative that Simon PJ and I talked about some time ago (1998/9?) is to break the recursion between two modules by using type signatures on top-level definitions. The idea is to generate the .hi-boot files that GHC currently uses automatically. Suppose we have a pair of mutually recursive modules A and B, we can generate A.hi-boot as follows: - read all of A and B discarding all function bodies as we go. [It might be possible to avoid reading B - depending on how much checking is performed at this stage.] - check all the type, class and instance definitions in A and B - keep the type signatures for exported definitions from A, discard everything else [At this point, GHC would normally start typechecking function bodies and generating code but we can stop now because all we need to do is generate the .hi-boot file.] - write A.hi-boot This approach gives most of what we need from recursive modules. It has the advantage that it probably doesn't take too much effort to add to a compiler that already uses interface files. The big disadvantage is that you have to provide type signatures for any definitions involved in cyclic module dependency. This may not be too bad since Haskell style guides suggest that you should provide type signatures for all exported top-level definitions. -- Alastair Reid alastair@reid-consulting-uk.ltd.uk Reid Consulting (UK) Limited http://www.reid-consulting-uk.ltd.uk/alastair/
hi, Alastair Reid wrote:
Simon Peyton-Jones <simonpj@microsoft.com> writes:
Nothing deep. GHC is just a fairly big thing and one of its assumptions is that it is compiling one module at a time. There'd be quite a bit of chuffing around to remove this assumption. Nothing fundamental, but real work. that's reasonable. incidently i think (but am not 100% on this) that one can still do most of the work one module at a time. it seems that only type checking needs to happen one strongly connected module component at a time.
The other big problem is that, in the worst case, you need to typecheck the entire program (however many modules that may be) at once. If your program happens to be GHC, Lolita or one of the other big Haskell programs, this might be prohibitively expensive. you only need to type check one strongly connected component of modules at a time. of course it could be the case that your whole program is one big strongly connected component, but that is not that easy to achieve. along those lines - it is of course possible to write a program where all *functions* are mutually recursive, but we don't usually do that.
in fact it is very likely that most nontrivial programs have at least two SCCs as it is not usual to import the Main module in other moudles. and of course, most of the time modules don't need to be recusrive and then each module is in a SCC of its own. i very much doubt that GHC has more than a few modules per SCC, but perhaps one of the simons can give us a more definitive answer on that. bye iavor -- ================================================== | Iavor S. Diatchki, Ph.D. student | | Department of Computer Science and Engineering | | School of OGI at OHSU | | http://www.cse.ogi.edu/~diatchki | ==================================================
participants (3)
-
Alastair Reid -
Iavor S. Diatchki -
Simon Peyton-Jones