What it says in the title. If I write a type signature, is it possible to algorithmically generate an expression which has that type signature?
It seems plausible that it might be possible to do this. We already know that if the type is a special-case of a library function’s type signature, Hoogle can find that function algorithmically. On the other hand, many simple problems relating to general expressions are actually unsolvable (e.g., it is impossible to know if two functions do the same thing), so it’s hardly implausible that this is one of them.
It’s probably bad form to ask several questions all at once, but I’d like to know:
-
Can it be done?
-
If so, how?
-
If not, are there any restricted situations where it becomes possible?
-
It’s quite possible for two distinct expressions to have the same type signature. Can you compute all of them? Or even some of them?
-
Does anybody have working code which does this stuff for real?
Djinn does this for a restricted subset of Haskell types, corresponding to a first-order logic. It can’t manage recursive types or types that require recursion to implement, though; so, for instance, it can’t write a term of type
(a -> a) -> a(the type offix), which corresponds to the proposition “if a implies a, then a“, which is clearly false; you can use it to prove anything. Indeed, this is whyfixgives rise to ⊥.If you do allow
fix, then writing a program to give a term of any type is trivial; the program would simply printfix idfor every type.Djinn is mostly a toy, but it can do some fun things, like deriving the correct
Monadinstances forReaderandContgiven the types ofreturnand(>>=). You can try it out by installing the djinn package, or using lambdabot, which integrates it as the@djinncommand.