我正在使用sop-core库,我想编写以下类型的函数:
patch :: NS I xs -> NP Maybe xs -> NP Maybe xs
xs
是类似'[Int,Char,Bool]
的类型的类型级别列表。 I
是一个身份函子。 I
是n元和类型。 NS I xs
是每个元素可能存在或可能不存在的n元产品。
NS I xs
应该从n元总和中获取值,并填充n元乘积的相应位置,如果该积已经存在,则将其覆盖。例如:
NP Maybe xs
我怀疑解决方案可能涉及NP Maybe xs
函数,但我很困惑。
解决方案根本不需要patch
,但是patched :: NP Maybe '[Int,Char,Bool]
patched = patch (Z (I 3)) (Nothing :* Nothing :* Nothing :* Nil)
-- patched == Just 3 :* Nothing :* Nothing :* Nil
函数通过使用默认值填充乘积的不匹配成分来将和转换为乘积。
技巧是构建setters的乘积,我们可以使用injections
将其应用于原始记录。当我们扩展总和时,不匹配的成分将用恒等函数填充。但是我们用总和中的值覆盖了匹配的组件。
首先我们需要这种辅助类型
injections
injections
的代码为:
injections