Understanding `~` with 2 functions

Reference Information. I do not understand ~ and request use.

Given:

{-# LANGUAGE GADTs #-} f :: a ~ b => a -> b -> b fab = a g :: a -> a -> a gab = a 

It seems to me that both functions are equal:

 Prelude> :r [1 of 1] Compiling Main ( TypeEq.hs, interpreted ) Ok, modules loaded: Main. *Main> f 10 20 10 *Main> g 10 20 10 

In what circumstances would it be useful to use f over g ?

+5
source share
1 answer
 {-# LANGUAGE TypeFamilies #-} import GHC.Exts (IsList(..)) fizzbuzz :: (IsList l, Item l ~ Int) => l -> IO () fizzbuzz = go . toList where go [] = return () go (n:m) | n`mod`3==0 = putStrLn "fizz" >> go m | n`mod`5==0 = putStrLn "buzz" >> go m | otherwise = print n >> go m 

Then

 Prelude> fizzbuzz [1..7] 1 2 fizz 4 buzz fizz 7 Prelude> import Data.Vector.Unboxed as UA Prelude UA> fizzbuzz (UA.fromList[1..7] :: UA.Vector Int) 1 2 fizz 4 buzz fizz 7 

Now you can argue that this would be better done using the Foldable constraint instead of the ugly conversion to list. In fact, this could not be done, because unboxed vectors do not have a folding instance due to the Unbox restriction!

However, this could also be done with a nonequilibrium limitation, namely

 fizzbuzz :: (IsList l, Num (Item l), Eq (Item l), Show (Item l)) => l -> IO () 

This is more general, but perhaps also more inconvenient. When you need, in practice, only one low-key type anyway, an equivalent constraint may be a good choice.

In fact, sometimes it’s convenient for me to move around in an equational constraint to make a type signature shorter if it repeats a little: signature

 complicatedFunction :: Long (Awkward (Type a) (Maybe String)) -> [Long (Awkward (Type a) (Maybe String))] -> Either String (Long (Awkward (Type a) (Maybe String))) 

can be replaced by

 complicatedFunction :: r ~ Long (Awkward (Type a) (Maybe String)) => r -> [r] -> Either String r 

which may be better than another DRY opportunity

 type LAwkTS a = Long (Awkward (Type a) (Maybe String)) complicatedFunction :: LAwkTS a -> [LAwkTS a] -> Either String (LAwkTS a) 
+9
source

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


All Articles