[英]Understanding `~` with 2 Functions
Background: I don't understand ~ and am requesting a use case. 背景:我不了解〜,并且正在请求一个用例。
Given: 鉴于:
{-# LANGUAGE GADTs #-}
f :: a ~ b => a -> b -> b
f a b = a
g :: a -> a -> a
g a b = 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
Under what circumstances would it be useful to use f
over g
? 在什么情况下将
f
应用于g
会有用?
{-# 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
You may now object that this should better have been done with a Foldable
constraint, instead of the ugly conversion to a list. 您现在可能会反对,最好用
Foldable
约束来完成此操作,而不是将其转换为丑陋的列表。 Actually this couldn't be done, because unboxed vectors do not have a foldable instance due to the Unbox
constraint! 其实这是办不到的,因为未装箱的载体不具有可折叠的情况下,由于
Unbox
约束!
It could, however, just as well have been done with a non-equational constraint, namely 但是,它也可以通过非等式约束来完成,即
fizzbuzz :: (IsList l, Num (Item l), Eq (Item l), Show (Item l))
=> l -> IO ()
That is more general, but arguably also more awkward. 这比较笼统,但可以说也比较尴尬。 When you need, in practice, only one contained-type anyway, an equational constraint may be a good choice.
无论如何,实际上只要需要一种包含类型,就可以选择方程式约束。
Indeed, I sometimes find it convenient to toss in an equational constraint just to make a type signature more concise, if it's a bit repetitive: the 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 with 可以替换为
complicatedFunction :: r ~ Long (Awkward (Type a) (Maybe String))
=> r -> [r] -> Either String r
which may be better than the other DRY-possibility of 这可能比其他DRY可能性更好
type LAwkTS a = Long (Awkward (Type a) (Maybe String))
complicatedFunction :: LAwkTS a -> [LAwkTS a] -> Either String (LAwkTS a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.