15

Why does Idris require that functions appear in the order of their definitions and mutual recursion declared with mutual?

I would expect Idris to perform a first pass of dependency analysis between functions, and reorder them automatically. I have always believed that Haskell does this. Why is this not possible in Idris?

Lii
  • 11,553
  • 8
  • 64
  • 88
jch
  • 5,382
  • 22
  • 41

1 Answers1

8

In the tutorial it says (emphasis mine):

In general, functions and data types must be defined before use, since dependent types allow functions to appear as part of types, and their reduction behaviour to affect type checking. However, this restriction can be relaxed by using a mutual block, which allows data types and functions to be defined simultaneously.

(Agda has this restriction as well, but has now removed the mutual keyword in favour of giving types then definitions.)

To expand on this: when you have dependant types, the automatic dependency analysis à la Haskell would be difficult or impossible, because the dependancy order at the type level may very well be different than the dependancy order at the value level. Haskell doesn't have this problem because values can not appear in types, so it can just do the dependancy analysis and then typecheck in that order. This is what the Idris tutorial is getting at about the reduction behaviour of values being required for the type checking.

I do not know whether the problem is even solvable in general with dependant types (you lose Hindley-Milner, for one thing), but I bet it wouldn't be efficient even if it were.

Vic Smith
  • 3,477
  • 1
  • 18
  • 29
  • Agda still requires you to be somewhat-explicit about mutually recursive definitions, since you have to give the type signature of each member of a mutually recursive group before giving the definition of any of them. – Cactus Dec 04 '14 at 11:44
  • Yeah, realised I was somewhat off there. Have updated the answer. – Vic Smith Dec 04 '14 at 11:51
  • 3
    This doesn't explain why this choice was made — why not perform automatic dependency analysis, like in Haskell, rather than requiring explicit recursion, like in Caml? – jch Dec 18 '14 at 02:49
  • As I understand it, this is because the dependancy order at the type level may very well be different than the dependancy order at the value level. Haskell doesn't have this problem because values do not appear in types, so it can just do the dependancy analysis and then typecheck in that order. This is what the Idris tutorial is getting at about the reduction behaviour of values being required for the type checking. I do not know whether the problem is even solvable in general with dependant types (you lose Hindley-Milner, for one thing), but I bet it wouldn't be efficient even if it were. – Vic Smith Dec 18 '14 at 14:17
  • Thanks, accepted. What do you mean by "losing Hindley-Milner"? The principal type property? – jch Dec 19 '14 at 21:00
  • 4
    Essentially, yes. With dependant types, the Hindley-Milner algorithm no longer has enough information to decide the type of arbitrary expressions. This is why top level functions require type declarations in Idris and Agda. See [here](http://cs.stackexchange.com/questions/12691/what-makes-type-inference-for-dependent-types-undecidable). – Vic Smith Dec 20 '14 at 21:12