簡體   English   中英

在GHC 7.6下工作的代碼的模糊檢查和自由覆蓋條件失敗

[英]ambiguity check and liberal coverage condition failures for code working under GHC 7.6

我有一個聰明的類型級機械,適用於GHC 7.6但不是更高版本。 回想起來,我不完全確定它為什么會工作,但無論如何我想以某種方式取回這個功能:

{-# LANGUAGE 
    PolyKinds 
  , FunctionalDependencies , FlexibleInstances , FlexibleContexts
  , OverlappingInstances
  , ScopedTypeVariables
  , TypeFamilies
  , UndecidableInstances
 #-}
module M where

import Data.Proxy

-- | A relation between a (maybe-partially-applied) type and that type fully
-- applied.
class Applied t (tab :: *) | t -> tab where
    -- | Fully apply a type @t@ with polymorphic arguments, yielding @tab@.
    applied :: Proxy t -> Proxy tab

instance Applied (t a) tab=> Applied t tab where
    applied _ = applied (Proxy :: Proxy (t a))

instance t ~ tab=> Applied t tab where -- always matches when `t` is kind `*`
    applied _ = Proxy :: Proxy tab

這取決於GHC 7.6上的tagged庫。 我們可以使用它:

$ ghci-7.6.3
Prelude> :l M.hs
[1 of 1] Compiling M                ( M.hs, interpreted )
Ok, modules loaded: M.
*M> 
*M> :t applied (Proxy :: Proxy Either)
applied (Proxy :: Proxy Either) :: Proxy (Either a a1)
*M> (return $ Right 'a') == applied (Proxy :: Proxy Either)
True

但是,這至少不能在GHC 7.8.3或更高版本上編譯:

$ ghci-7.8.3
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l M.hs
[1 of 1] Compiling M                ( M.hs, interpreted )

M.hs:19:10:
    Could not deduce (Applied (t a0) tab)
      arising from the ambiguity check for an instance declaration
    from the context (Applied (t a) tab)
      bound by an instance declaration:
                 Applied (t a) tab => Applied t tab
      at M.hs:19:10-42
    The type variable ‘a0’ is ambiguous
    In the ambiguity check for:
      forall (k :: BOX) (k1 :: BOX) (t :: k1 -> k) tab (a :: k1).
      Applied (t a) tab =>
      Applied t tab
    To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
    In the instance declaration for ‘Applied t tab’

M.hs:19:10:
    Illegal instance declaration for ‘Applied t tab’
      The liberal coverage condition fails in class ‘Applied’
        for functional dependency: ‘t -> tab’
      Reason: lhs type ‘t’ does not determine rhs type ‘tab’
    In the instance declaration for ‘Applied t tab’
Failed, modules load

我認為這里答案是相關的,但我還不明白這個建議。

我可能能夠解決這個問題。 我使用這個類的唯一地方是形式的簽名:

instance (Foo tab, Applied t tab)=> Bar (Proxy t) where

這可能表明我想讓Foo變為多態,但這是一個大型復雜的庫,我不知道這種改變是否可行。

如果我擺脫FD,你的ghci示例在ghc-7.8.3中適用於我,並啟用-XAllowAmbiguousTypes 該擴展將要求您在instance (Foo tab, Applied t tab)=> Bar (Proxy t)使用該applied函數的類型時使用ScopedTypeVariables進行注釋( instance (Foo tab, Applied t tab)=> Bar (Proxy t)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM