Reputation: 1041
I was wondering if there is any better way for finding FUNCTIONS given only TYPE of it in Haskell. Ex.-
Find function for given type- (a->b->c) ?
Always I have to think hard in a brute force manner. please help.
I have gone through this - Haskell: Deducing function from type, but could not find a better way.
Upvotes: 0
Views: 193
Reputation: 709
Besides DJINN (which is an external tool), you can also use the following code
http://okmij.org/ftp/Haskell/types.html#de-typechecker
to, informally, convert a type into a value, within Haskell. You can immediately invoke this function.
Upvotes: 1
Reputation: 59
I guess .. (\x y -> (x,y) )
can fit into the type (a->b->c)
Upvotes: 0
Reputation: 74384
It's sometimes possible. For instance, we all know that there are no "proper" values with type forall a. a
and only one with type forall a . a -> a
, but there are an infinite number of ones with type forall a. (a -> a) -> a -> a
.
In particular, you can think of some Haskell types as expressing theorems in type theory and the process of instantiating them as the process of proving that theorem. There's no general method for doing this, but there are a number of tautological types like forall a . a -> a
which have easily discoverable proofs.
To learn much more about this process, consider reading Software Foundations by Benjamin Pierce or Certified Programming with Dependent Types by Adam Chlipala. These two both explore how to prove theorems (instantiate types) like this using the language Coq which is similar to Haskell being based on OCaml.
Upvotes: 2