如果Reverse :: [k] -> [k]
是一个类型系列,那么Haskell无法分辨(Reverse (Reverse xs)) ~ xs
.有没有办法让类型系统知道这个没有任何运行时成本?
我很想使用unsafeCoerce
,但这似乎很遗憾.
我有一个类型(称之为A),我想创建类型为A的函数的类型类 - > A,A - > A - > A,A - > A - > A - > ...等等工作:
{-# LANGUAGE FlexibleInstances #-}
data A = A
class AsToA a where
takeA :: AsToA b => a -> A -> Either A b
instance AsToA (A -> A) where
takeA f a = Left (f a)
instance AsToA b => AsToA (A -> b) where
takeA f a = Right (f a)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
AsToA.hs:12:22:
Couldn't match expected type ‘b1’ with …
Run Code Online (Sandbox Code Playgroud) 我看了Simon Peyton Jones关于Control.Lens的讨论,他表明这里定义的Lens和LensR是同构的:
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
data LensR s t a b = LensR {
viewR :: s -> a,
setR :: b -> s -> t
}
Run Code Online (Sandbox Code Playgroud)
我正试图对Traversal做同样的事情:
type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t
data TraversalR s t a b = TraversalR {
toListOfR :: s …
Run Code Online (Sandbox Code Playgroud) 据我了解,Haskell的undefined
值 - 特定类型 - 是一个无法确定的值,无论出于何种原因(可能没有合理的定义,或者计算方法不同).例如:undefined :: [Int]
是一个列表,所以它必须用[]或(:)构造,但它不知道哪一个!因此,有意义的是,未定义的案例拆分使得整个表达式未定义:我不知道null (undefined :: [a])
是真还是假,因为我不知道是否undefined :: [a]
为空.
(顺便说一句-如果你用我的建议,不同意undefined
在建有一个构造函数,那么肯定null (undefined :: [a])
应该评估为False毕竟?undefined
不等同于[]!)
但是,Haskell模式匹配并不遵循这种思维方式.
data Foo = Foo Int String -- Only one data constructor
silly :: Foo -> Bool
silly (Foo _ _) = True
ghci> silly (undefined :: Foo)
*** Exception: Prelude.undefined -- But whatever the value is, it must
-- be constructed with Foo.
-- So why fail to …
Run Code Online (Sandbox Code Playgroud)