我无法理解UNPACKHaskell的工作原理.例如,考虑以下数据声明:
data P a b = P !a !b
data T = T {-# UNPACK #-} !(P Int Int)
Run Code Online (Sandbox Code Playgroud)
如何T解压缩数据类型?它会等同于
data T' = T' !Int !Int
Run Code Online (Sandbox Code Playgroud)
或将Int进一步解压缩:
data T'' = T'' Int# Int#
Run Code Online (Sandbox Code Playgroud)
?关于什么
data U = U {-# UNPACK #-} !(P Int (P Int Int))
Run Code Online (Sandbox Code Playgroud)
?
我想知道为什么Haskell中的未装箱类型有这些限制:
您无法为未装箱类型定义新类型:
newtype Vec = Vec (# Float#, Float# #)
Run Code Online (Sandbox Code Playgroud)
但您可以定义类型synonim:
type Vec = (# Float#, Float# #)
Run Code Online (Sandbox Code Playgroud)类型系列无法返回未装箱类型:
type family Unbox (a :: *) :: # where
Unbox Int = Int#
Unbox Word = Word#
Unbox Float = Float#
Unbox Double = Double#
Unbox Char = Char#
Run Code Online (Sandbox Code Playgroud)这背后有一些根本原因,还是因为没有人要求这个功能?
任何人都可以告诉为什么这段代码不能编译
data A = A {
_b :: B
}
makeLenses ''A
type B = String
Run Code Online (Sandbox Code Playgroud)
与消息
Not in scope: type constructor or class B
Run Code Online (Sandbox Code Playgroud)
这样做:
type B = String
data A = A {
_b :: B
}
makeLenses ''A
Run Code Online (Sandbox Code Playgroud)
没有makeLenses一切编译好.
为什么我不能在makeLenses之后有类型的synonim声明?
考虑一下这段代码(需要单例-2.2):
{-# LANGUAGE TypeFamilies, DataKinds, PolyKinds, TypeOperators, KindSignatures, TypeInType, TypeFamilyDependencies, UndecidableInstances #-}
module Bug where
import Data.Kind (Type)
import Data.Singletons.Prelude (Map, SndSym0)
import GHC.TypeLits (Nat)
data Payload = A | B
newtype NewType a = NewType Int
type List = [Payload]
type NatList = [(Nat, Payload)]
type StripNatList (natList :: NatList) = Map SndSym0 natList
type family Family (natList :: NatList) = (r :: Type) | r -> natList where
Family '[] = ()
Family xs = NewType (StripNatList xs) …Run Code Online (Sandbox Code Playgroud) 我有一个按类型级别列表索引的数据系列,其中列表中的类型对应于数据实例的参数.我想编写具有不同arity和参数的函数,具体取决于数据实例,因此我可以像家族中每个数据实例的同义词一样使用它.
{-# LANGUAGE KindSignatures, DataKinds, TypeOperators,
TypeFamilies, FlexibleInstances, PolyKinds #-}
module Issue where
type family (->>) (l :: [*]) (y :: *) :: * where
'[] ->> y = y
(x ': xs) ->> y = x -> (xs ->> y)
class CVal (f :: [*]) where
data Val f :: *
construct :: f ->> Val f
instance CVal '[Int, Float, Bool] where
data Val '[Int, Float, Bool] = Val2 Int Float Bool
construct = Val2
Run Code Online (Sandbox Code Playgroud)
编译好了.但是当我尝试应用construct函数时: …
为什么这段代码不起作用?
class Foo a where
foo :: Proxy a -> Int
bar :: Foo a => a -> Int
bar _ = foo (Proxy :: Proxy a)
Run Code Online (Sandbox Code Playgroud)
它无法使用以下消息进行编译:
Could not deduce (Foo a0) arising from a use of `foo'
from the context (Foo a)
bound by the type signature for bar :: Foo a => a -> Int
The type variable `a0' is ambiguous
In the expression: foo (Proxy :: Proxy a)
In an equation for `bar': bar _ = …Run Code Online (Sandbox Code Playgroud) 我正在尝试写一个固定大小的矢量,如下所示:
{-# LANGUAGE GADTs, DataKinds, KindSignatures, TypeOperators #-}
import GHC.TypeLits
data NVector (n :: Nat) a where
Nil :: NVector 0 a
Cons :: a -> NVector n a -> NVector (n + 1) a
instance Eq a => Eq (NVector n a) where
Nil == Nil = True
(Cons x xs) == (Cons y ys) = x == y && xs == ys
Run Code Online (Sandbox Code Playgroud)
但它无法使用此消息进行编译:
Could not deduce (n2 ~ n1)
from the context (Eq a)
bound by the instance …Run Code Online (Sandbox Code Playgroud) 我有一个有限向量的数据族:
data family Vec (n :: Nat) e
data instance Vec 2 Float = Vec2f !Float !Float
data instance Vec 3 Float = Vec3f !Float !Float !Float
-- and so on
Run Code Online (Sandbox Code Playgroud)
我也有一系列的getter函数:
class Get (i :: Nat) (n :: Nat) e where
get :: Vec n e -> e
instance Get 0 2 Float where
get (Vec2f x _) = x
instance Get 1 2 Float where
get (Vec2f _ x) = x
instance Get 0 3 Float where
get …Run Code Online (Sandbox Code Playgroud) 我有N-ary函数的类型族,从n类型的参数到类型t的值o:
type family NAry (n :: Nat) (t :: Type) (o :: Type) = (r :: Type) | r -> n t o where
NAry 1 t o = t -> o
NAry n t o = t -> (NAry (n - 1) t o)
Run Code Online (Sandbox Code Playgroud)
我认为这个家庭应该是无形的,我无法向GHC证明:
error:
* Type family equations violate injectivity annotation:
NAry 1 t o = t -> o
NAry n t o = t -> NAry (n - 1) t …Run Code Online (Sandbox Code Playgroud)