试图设计一个类型驱动的API,我一直试图得到类似下面的工作(使用更复杂的代码/尝试,这被剥离到澄清我正在寻找的最低要求):
{-# 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
Run Code Online (Sandbox Code Playgroud)
该程序将打印'foo',但不会:
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 …Run Code Online (Sandbox Code Playgroud)