我一直在尝试使用指定 eval 函数的 Component 类型类在 Purescript 中开发一个组件系统。for 的 eval 函数可以由组件为组件的每个子组件递归调用,实质上是获取输入的值。
由于组件可能希望使用运行时值,因此也会将一条记录传递给 eval。我的目标是要求顶级 eval 的 Record 参数中的行包含每个子组件的所有行。这对于本身不使用任何行的组件来说并不太难,但是它们的单个子组件会这样做,因为我们可以简单地将子组件行传递给组件的行。这显示在 中evalIncrement
。
import Prelude ((+), one)
import Data.Symbol (class IsSymbol, SProxy(..))
import Record (get)
import Prim.Row (class Cons, class Union)
class Component a b c | a -> b where
eval :: a -> Record c -> b
data Const a = Const a
instance evalConst :: Component (Const a) a r where
eval (Const v) r = v
data Var (a::Symbol) (b::Type) = Var
instance evalVar ::
( IsSymbol a
, Cons a b r' r) => Component (Var a b) b r where
eval _ r = get (SProxy :: SProxy a) r
data Inc a = Inc a
instance evalInc ::
( Component a Int r
) => Component (Inc a) Int r where
eval (Inc a) r = (eval a r) + one
以上所有代码都可以正常工作。但是,一旦我尝试引入一个包含多个输入组件并合并它们的行的组件,我似乎无法让它工作。例如,当尝试使用class Union
from时Prim.Row
:
data Add a b = Add a b
instance evalAdd ::
( Component a Int r1
, Component b Int r2
, Union r1 r2 r3
) => Component (Add a b) Int r3 where
eval (Add a b) r = (eval a r) + (eval b r)
产生以下错误:
No type class instance was found for
Processor.Component a3
Int
r35
while applying a function eval
of type Component t0 t1 t2 => t0 -> { | t2 } -> t1
to argument a
while inferring the type of eval a
in value declaration evalAdd
where a3 is a rigid type variable
r35 is a rigid type variable
t0 is an unknown type
t1 is an unknown type
t2 is an unknown type
事实上,即使修改evalInc
实例以使用具有空行的虚拟 Union 也会产生类似的错误,如下所示:
instance evalInc :: (Component a Int r, Union r () r1)
=> Component (Increment a) Int r1 where
我是否错误地使用了联合?还是我的班级需要更多的功能依赖——我不太了解它们。
我正在使用 purs 版本 0.12.0