为什么 GHC 会从关联数据的可强制性中推断出统一性,为什么这样做会与它自己的已检查类型签名相矛盾?
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
module Lib
(
) where
import Data.Coerce
class Foo a where
data Bar a
data Baz a = Baz
{ foo :: a
, bar :: Bar a
}
type BarSame a b = (Coercible (Bar a) (Bar b), Coercible (Bar b) (Bar a))
withBaz :: forall a b. BarSame a b => (a -> b) …Run Code Online (Sandbox Code Playgroud) 在Haskell的base文件说:“一个字一个无符号整数类型,同尺寸的诠释。”
如何将 anInt的位表示转换为 a Word,以便获得Word与原始位表示相同的值Int(即使它们表示的数值会不同)?
我不能使用,fromIntegral因为这会改变位表示。
我可能通过与位循环的Bits类,但我怀疑,这将是非常缓慢的-我不需要做任何形式的位操作的。我想要某种将被编译为无操作(或接近它)的函数,因为没有进行转换。
我想IntSet用作快速整数集实现 - 但是,我真正想要存储的是Words。我觉得我可以通过在它们之间快速转换来创建一个WordSet由 支持的IntSet。问题是,我不想按值转换,因为我不想截断Word值的上半部分:我只想保持位表示相同。
我有两个特点:
trait Foo {}
trait Bar {}
struct FooImpl;
impl Foo for FooImpl {}
struct BarImpl;
impl Bar for BarImpl {}
Run Code Online (Sandbox Code Playgroud)
我要转换成第三种类型:
struct Baz;
trait IntoBaz {
fn into(self) -> Baz;
}
Run Code Online (Sandbox Code Playgroud)
由于连贯性,我无法为两个特征定义两个impls IntoBaz,所以我换了一个:
struct FooWrapper<F>(F)
where
F: Sized;
impl<F: Foo + Sized> From<F> for FooWrapper<F> {
fn from(f: F) -> FooWrapper<F> {
FooWrapper(f)
}
}
impl<F: Foo + Sized> IntoBaz for FooWrapper<F> {
fn into(self) -> Baz {
Baz
}
}
Run Code Online (Sandbox Code Playgroud)
我不包装另一个:
impl<B: Bar> IntoBaz for …Run Code Online (Sandbox Code Playgroud)