閱讀“程式員的范疇論”我正在嘗試為 Op 重新創建 Functor 型別類實體。
{-# LANGUAGE TypeSynonymInstances #-}
module Type.Op where
import Data.Functor.Contravariant ( Contravariant, contramap )
type Op r a = a -> r
-- data Op r a = (->) a r
instance Contravariant (Op r) where
contramap f g = g . f
編譯產生以下錯誤:
? The type synonym ‘Op’ should have 2 arguments, but has been given 1
? In the instance declaration for ‘Contravariant (Op r)’
|
10 | instance Contravariant (Op r) where
| ^^^^^^^^^^^^^^^^^^^^
我該怎么辦 ?對于正常情況,Functor我會以相同的方式使用部分型別......
uj5u.com熱心網友回復:
讓我們考慮一個更簡單的類
class Unftor f where
unfmap :: (a -> a) -> f a -> f a
instance Unftor ((->) x) where
unfmap f g = f . g
現在,如果你試圖做的事情是可能的,我也可以寫
instance Unftor (Op r) where
unfmap f g = g . f
...但是等等,讓我們看看每種情況下的簽名
instance Unftor ((->) x) where
unfmap :: (a -> a) -> (x -> a) -> (x -> a)
instance Unftor (Op r) where
unfmap :: (a -> a) -> Op r a -> Op r a
由于Op只是型別同義詞,因此與
instance Unftor (\a -> (a -> r)) where
unfmap :: (a -> a) -> (a -> r) -> (a -> r)
現在,選擇所有型別都是 的特殊情況Int,
unfmap :: (Int -> Int) -> (Int -> Int) -> (Int -> Int)
unfmap f g = f . g -- from `instance Unftor ((->) x)`
unfmap :: (Int -> Int) -> (Int -> Int) -> (Int -> Int)
unfmap f g = g . f -- from `instance Unftor (Op r)`
……同樣的簽名!但不知何故,編譯器應該將它們分開?
那行不通。
你需要做的,而不是什么是做Op一個區分型別的只是同構到a -> r。這正是 newtypes 的用途(data也可以):
newtype Op r a = Op {getOp :: a -> r}
instance Contravariant (Op r) where
contramap (Op f) g = Op $ g . f
uj5u.com熱心網友回復:
請記住,型別同義詞本身并不是型別。它們只是讓你給現有的事物貼上新的標簽,然后用標簽來談論它們,而不是直接參考它們。
型別同義詞對大多數型別系統特性是完全透明的,只能通過替換同義詞的定義來作業。型別同義詞不是為了擁有類實體而“真正存在”的東西。這就是為什么 vanilla Haskell 根本不允許您使用型別同義詞宣告實體的原因。
該TypeSynonymInstances擴展實際上并沒有改變這個基本的圖片; 它所做的只是讓您為同義詞定義中使用的底層型別宣告實體,但使用同義詞來參考它。那句話是滿口的;示例時間:
{-# LANGUAGE TypeSynonymInstances, FlexibleContexts, FlexibleInstances #-}
data List a = a `Cons` List a | Nil
type Str = List Char
instance Eq Str
where x `Cons` xs == y `Cons` ys
= x == y && xs == ys
Nil == Nil = True
_ == _ = False
在這里,我定義了一個新的串列型別和一個新的字串同義詞(很像內置的type String = [Char],但我不希望現有實體妨礙)。然后我宣布instance Eq Str。但這并沒有真正為Str; 它為List Char. 所有TypeSynonymInstances所做的就是讓我用我的標簽Str作為寫替身List Char的實體宣告; 而已。結果是完全一樣,如果我一樣已經寫instance Eq (List Char)。當我做這樣的事情時,你可以清楚地看到區別:
isEmpty :: Eq (List a) => List a -> Bool
isEmpty xs = xs == Nil
的定義isEmpty需要一個Eq (List a)實體,但它本身并不知道Stror Char。如果instance Str與instance (List Char). 但是沒有宣告其他實體,我仍然可以呼叫isEmpty ('x' `Cons` Nil); GHC 甚至不需要我說我是否認為 aList Char或 aStr因為它們是同一件事,并且Eq Str與Eq (List Char).
如果你考慮一下,定義一個型別同義詞然后想要給它自己的實體(與底層型別分開)有點像試圖說“讓 x = 5,然后我們將定義否定意味著什么x 說 -x = -50"。x 不是一個新事物,它只是 5 的一個名稱。 x 既不需要也不能有自己的否定定義,因為 5 已經有了。
現在,讓我們回到 OP 的Op型別同義詞。同義詞可以比現有型別的標簽更復雜;它們可以是引數化標簽,例如type Op r a = a -> r. 然而,從根本上說Op“不存在”仍然是正確的。任何時候我使用Op Foo Bar我真的只是在談論Bar -> Foo. 并且沒有任何意義Op可以實際存在哪些實體;只有->在其定義中使用的基礎型別建構式的實體。
例如,這會因重復實體錯誤而失敗:
{-# LANGUAGE TypeSynonymInstances, FlexibleContexts, InstanceSigs #-}
type Op r a = a -> r
class Silly t
where silly :: t -> Bool
instance Silly (a -> r)
where silly :: (a -> r) -> Bool
silly _ = True
instance Silly (Op r a)
where silly = False
Op r a已經有一個 的實體Silly,因為a -> r有一個。我無法為Op我不希望經常->擁有的實體創建任何實體,反之亦然。
Now lets consider instance Contravariant (Op r). This can't "really" be declaring an instance for Op r, only for the thing that's a synonym for. So we need something to substitute for Op r to know what instance we're declaring, but what? Op r a is well-defined; it means a -> r. But to expand just Op r we need to be able to partially apply -> to its second argument. That's not something you can even really express directly in Haskell1. So it's certainly not something you can make an instance of a type class.
To avoid problems like this, type synonyms must be supplied with all of their parameters. This means that the compiler can "look through" the synonym to see what types you're really talking about. Partially applied type synonyms aren't guaranteed to be meaningful at all, so GHC normally forbids them.2 In the cases where a partially applied synonym could expand to something sensible, you just have to write the expansion yourself to declare an instance.
In cases like this where the partially applied synonym can't expand to something sensible, you just can't declare the instance. You can always use a newtype rather than a type synonym; since these create real new types, not just labels for existing types, there's no problem with them being partially applied:
newtype Op r a = Op { getOp :: a -> r }
instance Contravariant (Op r) where
contramap :: (a' -> a) -> (Op r a -> Op r a')
contramap f g = Op (getOp g . f)
A new type is what you need anyway if you don't want the underlying type constructor to also get the instance (i.e. you want an instance for Op without also making an instance for ->).
Indeed, the Data.Functor.Contravariant module already defines Op as a newtype (and this Contravariant instance), precisely because the class system can only work with partial application of type constructors "in the right order". Op only exists to be a version of -> that takes its parameters in the opposite order so that instances can be declared. If partial application of -> to its second argument was possible, there would be no need for Op, we'd just write something like:
instance Contravariant (-> r)
where contramap f g = g . f
Then we'd be able to write fmap ( 1) func or contramap ( 1) func depending on whether we wanted to post-process func's result with ( 1) or pre-process func's argument with ( 1). But Haskell's type system does not support this.
1 At the value level we have operator sections, and so could write (-> r). That's really just shorthand for a function defined as \a -> a -> r though, whereas we have direct syntactic support for partial application to first arguments. Regardless, we don't have operator sections at the type level, nor type-level functions for them to be shorthand for. (Type synonym families are the closest thing to type level functions we have, but you can't make partially applied type families instances of type classes either, for much the same reasons as regular type synonyms)
2 In some contexts GHC is clever and can work with partially applied synonyms, if you turn on LiberalTypeSynonyms. It does this by basically waiting to see if the type expression the partially-applied synonym appears in eventually provides the missing arguments, so that everything becomes well defined. For example this works:
{-# LANGUAGE LiberalTypeSynonyms #-}
type Op r a = a -> r
type OnInt f = f Int
foo :: OnInt (Op String)
-- foo :: (Op String) Int
-- foo :: Op String Int
-- foo :: Int -> String
foo x = show x
Op String isn't a well defined type expression on its own at all, but GHC temporarily ignores that and passes this "unevaluated" to OnInt anyway, which results in Op String Int, which is well defined so it's okay. That still doesn't mean that Op String is a first class type expression that we could try to make an instance for, however.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/351373.html
標籤:哈斯克尔
上一篇:您能否在Haskell函式中訪問fromList,而不在函式宣告中包含型別,以及如何正確使用它?
下一篇:避免違反開閉原則的設計模式
