試圖設計一個類型驅動的API,我一直試圖得到像下面的工作(使用更復雜的代碼/嘗試,這是剝離到最低限度所需澄清我'我想找):沒有值的GHC TypeLits
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
module Main where
import Data.Proxy
import GHC.TypeLits
type Printer (s :: Symbol) = IO()
concrete :: Printer "foo"
concrete = generic
generic :: KnownSymbol s => Printer s
generic = putStrLn (symbolVal (Proxy :: Proxy s))
main :: IO()
main = concrete
這個程序將打印 '富',但不會:
Could not deduce (KnownSymbol s0)
arising from the ambiguity check for ‘generic’
from the context (KnownSymbol s)
bound by the type signature for
generic :: KnownSymbol s => Printer s
at test5.hs:14:12-37
The type variable ‘s0’ is ambiguous
In the ambiguity check for:
forall (s :: Symbol). KnownSymbol s => Printer s
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
In the type signature for ‘generic’:
generic :: KnownSymbol s => Printer s
啓用AllowAmbiguousTypes
沒有真正的幫助。任何方式來讓這個工作無論如何?
你應該提供參數在具體的調用網站? – 2015-02-17 22:15:53
@PeterK這是可能的,是的,但我不明白這是怎麼改變的? – 2015-02-17 22:29:36
「Printer」foo「'與'Printer」bar「'是完全相同的類型,因爲它們都是'IO()'的類型同義詞,所以它不能以當前形式工作。 – 2015-02-17 22:30:11