我正在尝试为类型类编写通用函数:
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Foo1 where
import Data.Foldable
class Foo t where
type FooPType t :: * -- Base type.
type FooFType t :: * -> * -- Container type.
defPs :: FooFType t (FooPType t) -- Initialized container.
-- An attempt at a universal testing function valid for all types, t,
-- of class Foo for which `FooFType t` is a foldable functor.
tst :: forall t.
( Foo t
, Functor (FooFType t)
, Foldable (FooFType t)
) => FooPType t
tst = (head . toList) defPs
但是,我从GHC(8.0.2)收到此错误:
Foo1.hs:30:23: error:
• Couldn't match type ‘FooPType t0’ with ‘FooPType t’
Expected type: FooFType t0 (FooPType t)
Actual type: FooFType t0 (FooPType t0)
NB: ‘FooPType’ is a type function, and may not be injective
The type variable ‘t0’ is ambiguous
• In the first argument of ‘head . toList’, namely ‘defPs’
In the expression: (head . toList) defPs
In an equation for ‘tst’: tst = (head . toList) defPs
• Relevant bindings include
tst :: FooPType t (bound at Foo1.hs:30:1)
稍微探讨一下,我发现有些人通过将“类型”更改为“数据”来解决这个问题,但这对我不起作用。 (我为 FooFType 更改了它。我应该为 FooPType 更改它吗?对于两者?)
D'哦!在发布之前,我应该尝试自己回答最后一个问题。 果然,改变这行代码:
type FooPType t :: * -- Base type.
阅读:
data FooPType t :: * -- Base type.
摆脱了我的编译错误。
有人可以解释为什么这种改变有效吗?
根据@HTNW的提示,这是解决方案:在“defPs”之后添加“@t”:
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Foo1 where
import Data.Foldable
class Foo t where
type FooPType t :: * -- Base type.
type FooFType t :: * -> * -- Container type.
defPs :: FooFType t (FooPType t) -- Initialized container.
-- An attempt at a universal testing function valid for all types of class Foo.
tst :: forall t.
( Foo t
, Functor (FooFType t)
, Foldable (FooFType t)
) => FooPType t
tst = (head . toList) $ defPs @t
上面的代码在GHC 8.0.2下编译没有错误:
Davids-Air-2:so_noninjective_type_funcs dbanas$ stack ghc -- -c Foo1.hs
Davids-Air-2:so_noninjective_type_funcs dbanas$
答案 0 :(得分:1)
tst :: forall t. _ => FooPType t
tst = head $ toList _ -- want f (FooPType t)
defPs :: FooFType u (FooPType u)
tst = head $ toList defPs
-- try to unify (f (FooPType t)) with
-- (FooFType u (FooPType u))
-- Assume that f is injective: f x ~ g y iff f ~ g and x ~ y
-- GHC assumes this because it doesn't allow you to abstract over non-injective
-- type constructors anyway.
-- try to unify f with FooFType u; OK
-- try to unify FooPType t with FooPType u; oops
如果FooPType
是data family
,则FooPType x ~ FooPType y
表示x ~ y
,因为data family
s是单射的。在这里,它只是type family
,这意味着编译器无法推断您要为类型defPs
调用t
。例如,您可以将FooPType u ~ FooPType t
添加到test
的上下文,现在u
和t
都是defPs
的有效类型参数。
test :: forall t u.
( Foo t, Foo u
, Foldable (FooFType t), Foldable (FooFType u)
, FooPType t ~ FooPType u
) => FooPType u
test = head $ toList defPs -- uh oh; which one?
instance Foo Bool where
type FooPType Bool = Int
type FooFType Bool = []
defPs = [1]
instance Foo Int where
type FooPType Int = Int
type FooFType Int = []
defPs = [3]
test @Bool @Int -- 1 or 3?
有趣的是,即使是类型签名也不能在这里拯救你。似乎有必要使用类型应用程序:
{-# LANGUAGE ExplicitForAll, ScopedTypeVariables, TypeApplications, ... #-}
test :: forall t. (Foo t, Foldable (FooFType t)) => FooPType t
test = head $ toList $ defPs @t