3
我創建了一個玩具程序試圖做一個提升的類型分支:我可以使用升級類型進行分支控制嗎?
{-# LANGUAGE KindSignatures, DataKinds, TypeFamilies, ScopedTypeVariables, GADTs #-}
module Foo where
import Data.Proxy
data Out = ToInt | ToBool
type family F (a :: Out) where
F ToInt = Int
F ToBool = Bool
foo :: forall k. Proxy (k :: Out) -> Int -> F k
foo p = case p of
(Proxy :: Proxy 'ToInt) -> id
(Proxy :: Proxy 'ToBool) -> (== 0)
在這裏,我想對分支和Proxy
對他們的使用顯式類型的簽名,但這不起作用,GHC抱怨:
[1 of 1] Compiling Foo (Foo.hs, Foo.o)
Foo.hs:15:6: error:
• Couldn't match type ‘k’ with ‘'ToBool’
‘k’ is a rigid type variable bound by
the type signature for:
foo :: forall (k :: Out). Proxy k -> Int -> F k
at Foo.hs:12:15
Expected type: Proxy 'ToBool
Actual type: Proxy k
• When checking that the pattern signature: Proxy 'ToBool
fits the type of its context: Proxy k
In the pattern: Proxy :: Proxy ToBool
In a case alternative: (Proxy :: Proxy ToBool) -> (== 0)
• Relevant bindings include
p :: Proxy k (bound at Foo.hs:13:5)
foo :: Proxy k -> Int -> F k (bound at Foo.hs:13:1)
我認爲這基本上說GHC有困難搞清楚第二個分支k ~ 'ToBool
。 (實際上第一個分支不工作或者爲非常類似的錯誤信息)
這是真的可能或我做錯了什麼?