在 Idris 中将变量类型化为具体类型的方法(改进?是吗?)?对于依赖类型的 HOAS 拉链
Ways to (refine? is it?) type variables to concrete types in Idris? For a dependently typed HOAS Zipper
我有以下内容:
data Expr : Type -> Type where
Lift : a -> Expr a
Add : Num a => Expr a -> Expr a -> Expr a
And : Expr Bool -> Expr Bool -> Expr Bool
Cnst : Expr a -> Expr b -> Expr a
data Context : Type -> Type where
Root : Context ()
L : Expr w -> Context x -> Expr y -> Expr z -> Context w
R : Expr w -> Context x -> Expr y -> Expr z -> Context w
M : Expr w -> Context x -> Expr y -> Expr z -> Context w
data Zipper : Type -> Type -> Type where
Z : Expr f -> Context g -> Zipper f g
E : String -> Context g -> Zipper String ()
total
ZipUp : Zipper focus parent -> Type
ZipUp (Z e (R (And e1 e2) {x} c t u)) = Zipper Bool x
ZipUp (Z e (L (And e1 e2) {x} c t u)) = Zipper Bool x
ZipUp (Z e (R (Cnst {a} e1 e2) {x} c t u)) = Zipper a x
ZipUp (Z {f} e (L (Cnst l r) {x} c t u)) = Zipper f x
ZipUp (Z e (R (Add {a} e1 e2) {x} c t u)) = Zipper a x
ZipUp (Z {f} e (L (Add e1 e2) {x} c t u)) = Zipper f x
ZipUp _ = Zipper String ()
up : (x : Zipper focus parent) -> ZipUp x
up (Z e (R (And l r) c x y)) = Z (And l e) c
up (Z e (R (And l r) c x y)) = Z (And e r) c
up (Z {f = b} e (R (Cnst l {b} r) c x y)) = Z (Cnst l e) c
up (Z {f = a} e (L (Cnst l {a} r) c x y)) = Z (Cnst e r) c
up (Z {f = a} e (R (Add {a} l r) c x y)) = Z (Add l e) c
up (Z {f = a} e (L (Add {a} l r) c x y)) = Z (Add e r) c
up (Z e (R (Lift x) c l r)) = E "Some error" c
up (Z e (L (Lift x) c l r)) = E "Some error" c
up (E s c) = E s c
这不会对 up
的 And
情况进行类型检查,因为 Idris 无法将 Zipper f g
与 Zipper Bool g
匹配 我的问题是,我怎样才能让它工作以便当我的表达式具有 children 个具体类型时,我可以在树中重建一层?我想我需要以某种方式改进类型,但我真的看不出制作一个新的 dependent-type(或 GADT)对此有何帮助(而且我不知道任何其他改进类型的方法! )
我已经研究了观点和证明,但是在尽我最大努力几周之后,我似乎无法找到一种方法来写出下面的问题 The focus of the zipper has the same type as the right child of the context's parent expression, if the context was constructed with R. The same is true for the focus and the context's left child, if the context was constructed with L.
我尝试增加类型参数,以便每个表达式都带有其 children Expr a l m r
的类型,但我最终还是遇到了同样的错误,即使我可以编写一个新版本R : Expr r t u v -> Expr a l m r -> Expr m h i j -> Expr r x y z -> Context -> Context
来捕捉这种关系。最后,我需要一种方法来告诉 Idris,某些参数 a
在某种模式匹配下确实是 Bool
!
我非常困惑,所以非常感谢任何建议!
亲切的问候,
多诺万
当然这些构造函数的类型不够精确:w
、x
、y
和z
之间没有link。
L : Expr w -> Context x -> Expr y -> Expr z -> Context w
R : Expr w -> Context x -> Expr y -> Expr z -> Context w
M : Expr w -> Context x -> Expr y -> Expr z -> Context w
我有以下内容:
data Expr : Type -> Type where
Lift : a -> Expr a
Add : Num a => Expr a -> Expr a -> Expr a
And : Expr Bool -> Expr Bool -> Expr Bool
Cnst : Expr a -> Expr b -> Expr a
data Context : Type -> Type where
Root : Context ()
L : Expr w -> Context x -> Expr y -> Expr z -> Context w
R : Expr w -> Context x -> Expr y -> Expr z -> Context w
M : Expr w -> Context x -> Expr y -> Expr z -> Context w
data Zipper : Type -> Type -> Type where
Z : Expr f -> Context g -> Zipper f g
E : String -> Context g -> Zipper String ()
total
ZipUp : Zipper focus parent -> Type
ZipUp (Z e (R (And e1 e2) {x} c t u)) = Zipper Bool x
ZipUp (Z e (L (And e1 e2) {x} c t u)) = Zipper Bool x
ZipUp (Z e (R (Cnst {a} e1 e2) {x} c t u)) = Zipper a x
ZipUp (Z {f} e (L (Cnst l r) {x} c t u)) = Zipper f x
ZipUp (Z e (R (Add {a} e1 e2) {x} c t u)) = Zipper a x
ZipUp (Z {f} e (L (Add e1 e2) {x} c t u)) = Zipper f x
ZipUp _ = Zipper String ()
up : (x : Zipper focus parent) -> ZipUp x
up (Z e (R (And l r) c x y)) = Z (And l e) c
up (Z e (R (And l r) c x y)) = Z (And e r) c
up (Z {f = b} e (R (Cnst l {b} r) c x y)) = Z (Cnst l e) c
up (Z {f = a} e (L (Cnst l {a} r) c x y)) = Z (Cnst e r) c
up (Z {f = a} e (R (Add {a} l r) c x y)) = Z (Add l e) c
up (Z {f = a} e (L (Add {a} l r) c x y)) = Z (Add e r) c
up (Z e (R (Lift x) c l r)) = E "Some error" c
up (Z e (L (Lift x) c l r)) = E "Some error" c
up (E s c) = E s c
这不会对 up
的 And
情况进行类型检查,因为 Idris 无法将 Zipper f g
与 Zipper Bool g
匹配 我的问题是,我怎样才能让它工作以便当我的表达式具有 children 个具体类型时,我可以在树中重建一层?我想我需要以某种方式改进类型,但我真的看不出制作一个新的 dependent-type(或 GADT)对此有何帮助(而且我不知道任何其他改进类型的方法! )
我已经研究了观点和证明,但是在尽我最大努力几周之后,我似乎无法找到一种方法来写出下面的问题 The focus of the zipper has the same type as the right child of the context's parent expression, if the context was constructed with R. The same is true for the focus and the context's left child, if the context was constructed with L.
我尝试增加类型参数,以便每个表达式都带有其 children Expr a l m r
的类型,但我最终还是遇到了同样的错误,即使我可以编写一个新版本R : Expr r t u v -> Expr a l m r -> Expr m h i j -> Expr r x y z -> Context -> Context
来捕捉这种关系。最后,我需要一种方法来告诉 Idris,某些参数 a
在某种模式匹配下确实是 Bool
!
我非常困惑,所以非常感谢任何建议!
亲切的问候, 多诺万
当然这些构造函数的类型不够精确:w
、x
、y
和z
之间没有link。
L : Expr w -> Context x -> Expr y -> Expr z -> Context w
R : Expr w -> Context x -> Expr y -> Expr z -> Context w
M : Expr w -> Context x -> Expr y -> Expr z -> Context w