How to request unification type for ghci?

Can I request ghci for a union type?

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

What I'm trying to solve is exercise 13.23 from the third edition of Haskell: Craft functional programming .

How can you use the Haskell system to check for two types of expressions that are unified, and if so, what is their union? Hint: you can make a mannequin. Definitions in Haskell, in which a specific value, zircon, is said to be equated to itself:

zircon = zircon

Values ​​defined in this way can be declared with any desired type.

Thanks,
Sebastian.

+4
source share
1 answer

One way is to use . As a function, it’s not very interesting, but its type is good: it combines two of its arguments and its return type. So:asTypeOf :: a -> a -> aasTypeOf

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

So you can see that these two types are unified before Int -> Bool. For a slightly more interesting example, let me unify Maybe aand 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 exercises, I urge you to try to do the merging manually. It is not difficult as soon as you get it, and it is a skill that you will use again and again.

+5
source

Source: https://habr.com/ru/post/1537282/


All Articles