Haskell 有一个名为的类型NFData
,其形状如下:
class NFData a
where
rnf :: a -> ()
比“功能”更“数据”的类型可以配备NFData
. 每个这样的实例都会彻底分析该类型的给定值及其传递包含的任何内容。这迫使重击并爆炸潜在底部。
注意:有点神秘的是,即使是“函数式”类型也配备了实例,尽管它们实际上并没有将它们的参数简化为正常形式。
案例分析就这么多。但有时采取双重视角并考虑更类似于余数据而不是数据的事物是有用的。我们不想分析其案例的总和,而是希望建立一个记录到其领域。
因此,在对我在说什么没有任何真正想法的情况下,我可能会因此而蒙混过关,将遇到的任何箭头都转过来,并在几个选项中穿插Co
:
class NFCodata a
where
cornf :: () -> a
对于表示非严格字段的有限乘积的类型(粗略地说,“单一构造函数”类型),我期望以下形式的实例:
instance NFCodata ()
where
cornf () = ()
instance NFCodata a => NFCodata (Solo a)
where
cornf () = Solo $ cornf ()
instance (NFCodata a, NFCodata b) => NFCodata (a, b)
where
-- you get the idea
对于所有具有少于/多于一个构造函数的数据类型,我希望有以下形式的实例:
instance NFCodata Void
where
cornf () = undefined
instance NFCodata (Either a b)
where
cornf () = undefined
instance NFCodata Int
where
cornf () = undefined
-- ...
注意:这可能有点可疑,但如果我们不提供这些实例,我们将只使用()
与NFCodata
.
最终结果应该是这样的类型的派生实例:
data Foo = Foo
{ bar :: Bar
, baz :: Baz
}
deriving stock Generic
deriving anyclass NFCodata
data Bar = Bar
{ someInt :: Int
, someString :: String
}
deriving stock Generic
deriving anyclass NFCodata
data Baz = Baz
deriving stock Generic
deriving anyclass NFCodata
行为如下:
-- $> cornf () :: Foo
-- => Foo
-- { bar = Bar
-- { someInt = undefined
-- , someString = undefined
-- }
-- , baz = Baz
-- }
这个想法是可以使用镜头Foo
来Bar
填充这个空脊的内容。
所以......问题:
- 上这样的课有意义吗?是否已经有某种方法可以获取带有底部内容的 codata 类型的“脊椎”?是否
NFCodata
已经以其他更原则的形式存在? - {NF}data 和 {NF}Codata 之间是否存在某种严格的类比?
- “NF”真的是“NFCodata”中使用的正确前缀吗?有没有更贴切的数学术语(更重要的是思维方式)适用于此?