Frage

It is possible to query ghci for an unification type?

For example, if I want to know the type of the unification between (Int -> Bool) and (a -> Bool) how can I query this to ghci?

What I'm trying to solve is the exercise 13.23 from third edition of Haskell: The Craft of Functional Programming.

How can you use the Haskell system to check whether two type expressions are unifiable, and if so what is their unification? Hint: you can make dummy definitions in Haskell in which the defined value, zircon say, is equated with itself:

zircon = zircon

Values defined like this can be declared to have any type you wish.

Thanks,
Sebastián.

War es hilfreich?

Lösung

One way is to use asTypeOf:: a -> a -> a. As a function, asTypeOf isn't very interesting, but its type is nice: it forces its two arguments and its return type to unify. So:

> :t asTypeOf (undefined :: Int -> Bool) (undefined :: a -> Bool)
asTypeOf (undefined :: Int -> Bool) (undefined :: a -> Bool)
  :: Int -> Bool

So you can see that those two types unified to Int -> Bool. For a slightly more interesting example, let's unify Maybe a and f (Bool, c):

> :t asTypeOf (undefined :: Maybe a) (undefined :: f (Bool, c))
asTypeOf (undefined :: Maybe a) (undefined :: f (Bool, c))
  :: Maybe (Bool, c)

On the other hand, for the purpose of exercises, I encourage you to attempt to do the unification by hand. It's not hard, once you get the hang of it, and is a skill that you will use over and over.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top