Woj*_*ilo 8 constructor haskell partial-application algebraic-data-types
是否可以在Haskell中为部分应用类型创建数据构造函数?
ghci会话:
Prelude> data Vector a b = Vector {x::a, y::b}
Prelude> :t Vector
Vector :: a -> b -> Vector a b
Prelude> type T1 = Vector Int
Prelude> :t T1
<interactive>:1:1: Not in scope: data constructor `T1'
Prelude> let x = Vector
Prelude> let y = T1
<interactive>:46:9: Not in scope: data constructor `T1'
Run Code Online (Sandbox Code Playgroud)
我想为T1类型创建数据构造函数 - 它甚至可能吗?或者我是否必须使用newtypes,因为无法手动定义此类函数?
我对你的目标有点困惑,但让我们一点一点地经历这一点,也许我会指出正确的观点:
:t告诉你变量的类型; 应用于类型时没有任何意义,因为它只会返回您传递的内容.请注意,此处的错误告诉您:t需要某种数据值作为参数:
Prelude> :t Maybe
<interactive>:1:1: Not in scope: data constructor `Maybe'
Prelude> :t (Maybe Integer)
<interactive>:1:2: Not in scope: data constructor `Maybe'
<interactive>:1:8: Not in scope: data constructor `Integer'
Run Code Online (Sandbox Code Playgroud)
您可以创建部分类型:
Prelude> type T = Maybe
Prelude> Just 5 :: T Integer
Just 5
type T a = Maybe a -- alternately, with explicit type parameters
Prelude> Just 'a' :: T Char
Just 'a'
Run Code Online (Sandbox Code Playgroud)
您无法为部分类型创建数据构造函数,因为它们不表示数据.如果没有在类型上进行参数化,可以拥有Maybe或Vector拥有什么值?您可能倾向于认为Maybe可能具有该值Nothing,但Nothing键入为:
Prelude> :t Nothing
Nothing :: Maybe a
Run Code Online (Sandbox Code Playgroud)
关键是Nothing可以是任何 Maybe a,但它仍然需要a知道它Nothing.(有点像我告诉你"给我拿一杯"而不是"给我一杯任何东西" - 你至少在完成我的想法之后才能有效地遵守).
您当然可以创建部分应用的函数,这些函数在应用后将返回完整类型:
Prelude> let f = Just :: a -> T a
Prelude> f 5
Just 5
Prelude> :t f 'a'
f 'a' :: T CharRun Code Online (Sandbox Code Playgroud)
GADT 可以做到这一点。GHCi 会议:
\n\n\xce\xbb :set -XGADTs\n\xce\xbb :{\n| data Vector a b where\n| Vector :: a -> b -> Vector a b\n| T1 :: Int -> b -> T1 b\n| type T1 = Vector Int\n| :}\n\xce\xbb :t T1\nT1 :: Int -> b -> T1 b\nRun Code Online (Sandbox Code Playgroud)\n