小编yon*_*ong的帖子

这些数据是什么......试图完成的是什么?(Haskell的)

在random-fu包中,有这样的data声明:

data Multinomial p a where
    Multinomial :: [p] -> a -> Multinomial p [a]
Run Code Online (Sandbox Code Playgroud)

我知道这是一个GADT,但它想要完成什么?难道是放置在限制pa等?

haskell

3
推荐指数
2
解决办法
143
查看次数

使用Unicode"显示"记录(Haskell)

如果我们运行以下Haskell代码:

data R = R {? :: Double} deriving Show

main = print $ show $ R 3
Run Code Online (Sandbox Code Playgroud)

我们得到:

"R {\956 = 3.0}"

处理Unicode名称的好方法是什么Show

haskell

3
推荐指数
1
解决办法
129
查看次数

如何包装不安全的FFI?(Haskell的)

这是一个后续问题,是否有充分的理由使用unsafePerformIO?

所以我们知道

p_sin(double *p) { return sin(*p); }
Run Code Online (Sandbox Code Playgroud)

不安全,不能用unsafePerformIO.

但该p_sin函数仍然是一个数学函数,它以不安全的方式实现的事实是一个实现细节.我们并不完全希望矩阵乘法在IO中,因为它涉及分配临时内存.

我们如何以安全的方式包装这个功能?我们需要锁定,自己分配内存等吗?是否有处理此问题的指南/教程?

haskell ffi unsafe-perform-io

3
推荐指数
1
解决办法
143
查看次数

GHC.Generics示例不起作用

我试图获得GHC.Generics中描述的通用二进制编码类的示例实现,但是当我尝试编译它时,我得到一个错误.

import GHC.Generics

class Encode' f where
  encode' :: f p -> [Bool]

instance Encode' V1 where
  encode' x = undefined

instance Encode' U1 where
  encode' U1 = []

instance (Encode' f, Encode' g) => Encode' (f :+: g) where
  encode' (L1 x) = False : encode' x
  encode' (R1 x) = True  : encode' x

instance (Encode' f, Encode' g) => Encode' (f :*: g) where
  encode' (x :*: y) = encode' x ++ encode' y

instance …
Run Code Online (Sandbox Code Playgroud)

generics haskell

3
推荐指数
1
解决办法
146
查看次数

使用约束实现From/Into for struct

既然.max()不适用于f64s,我正在编写一个ForceOrd断言该参数不是a的结构NaN.预期用途如下:

let m = xs.iter().map(|&x| ForceOrd(x)).max().unwrap().into();
Run Code Online (Sandbox Code Playgroud)

但是,我无法Into使用错误编译特征实现:

conflicting implementations of trait `std::convert::Into<_>` for type `ForceOrd<_>`
Run Code Online (Sandbox Code Playgroud)

代码(游乐场):

#[derive(PartialEq, PartialOrd)]
pub struct ForceOrd<X: PartialEq + PartialOrd>(pub X);
impl<X: PartialEq + PartialOrd> Eq for ForceOrd<X> { }
impl<X: PartialEq + PartialOrd> Ord for ForceOrd<X> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}
/// doesn't work
impl<X: PartialEq + PartialOrd> Into<X> for ForceOrd<X> {
    fn into(x: Self) -> X …
Run Code Online (Sandbox Code Playgroud)

traits rust

3
推荐指数
1
解决办法
126
查看次数

为什么以下代码不解析?

以下代码不解析:

main :: IO ()
main = do
    print $ result
        where result = foldl' (+) 0 [1..1000000]
    print $ result
        where result = last [1..1000000]
Run Code Online (Sandbox Code Playgroud)

编译器在第二次打印时抱怨:src/Main.hs:10:5:输入`print'时解析错误

为什么是这样?

haskell

2
推荐指数
1
解决办法
86
查看次数

管理约束爆炸(Haskell)

我正在编写一个函数,我想在Vectors上使用它.换句话说,我有类似的东西:

import qualified Data.Vector.Generic as G
foo :: (G.Vector v a, G.Vector v b, G.Vector v c, G.Vector v d) 
    => v a -> v b -> v c -> v d 
Run Code Online (Sandbox Code Playgroud)

这样用户可以选择使用Unboxedvs StorableVectors等.

但是如果我需要将中间值放入其中v,我会得到Vector约束的组合爆炸,例如:

foo :: (G.Vector v a, G.Vector v b, G.Vector v c, G.Vector v d,
        G.Vector v (a, b), G.Vector v (a, c), G.Vector v (c, b))
    => v a -> v b -> v c -> v d 
Run Code Online (Sandbox Code Playgroud)

我如何管理这种冗长?有没有办法要么

1)GHC隐式生成约束 …

haskell constraints typeclass

2
推荐指数
1
解决办法
132
查看次数

GHC抱怨重叠实例,实际上它们不是

我有这样toAVector定义的函数:

class Elt a => ToAVector v a where
  toAVector :: v a -> A.Array DIM1 a

instance Elt a => ToAVector [] a where
  toAVector l =
    A.fromList (Z :. P.length l) l

instance (Elt a, G.Vector v a) => ToAVector v a where
  toAVector v =
    A.fromFunction (Z :. G.length v) (\(Z :. i) -> v G.! i)
  {-# INLINE toAVector #-}
Run Code Online (Sandbox Code Playgroud)

当试图toAVector在另一个库中使用时,我收到错误:

Overlapping instances for ToAVector [] Double
  arising from a use of …
Run Code Online (Sandbox Code Playgroud)

haskell ghc instances

2
推荐指数
1
解决办法
96
查看次数