gpt4 book ai didi

coq - 将模式传递给战术

转载 作者:行者123 更新时间:2023-12-04 16:04:49 26 4
gpt4 key购买 nike

我正在编写一种策略来查找与绑定(bind)列表中的键关联的值。就像是:

Require Import String List Program.

Ltac assoc needle haystack :=
match haystack with
| @nil (_ * ?T) => constr:(@None T)
| cons (?k, ?v) ?t => let pr := constr:(eq_refl k : k = needle) in constr:(Some v)
| cons _ ?t => let res := assoc needle t in constr:res
end.

不幸的是,我不知道 key 的确切形式;相反,我知道一个与之匹配的模式。更准确地说,我正在寻找的关键是调用类型类方法的结果,但我事先不知道将使用哪个实例。在下面的示例中,我知道关键是调用 show "a" ,但我不知道用什么实例:
Open Scope string_scope.
Open Scope list_scope.

Class Show A := { show: A -> string }.
Instance Show1 : Show string := {| show := fun x => x |}.
Instance Show2 : Show string := {| show := fun x => x ++ x |}.

Goal True.
(* Works (poses Some 1) *)
let v := assoc (show "a") [(show (Show := Show2) "a", 1); ("b", 2)] in pose v.
(* Does not work (poses None) *)
let v := assoc (show "a") [(show (Show := Show1) "a", 1); ("b", 2)] in pose v.

有没有我可以在这里使用的技巧,没有通过 assoc检查匹配的ltac?理想情况下,它看起来像 (show (Show := _) "a") , 或者 (fun inst => show (Show := inst) "a") .

最佳答案

看起来传递一个函数效果很好,实际上:

Ltac assoc needlef haystack :=
match haystack with
| @nil (_ * ?T) => constr:(@None T)
| cons (?k, ?v) ?t => let pr := constr:(eq_refl k : k = needlef _) in constr:(Some v)
| cons _ ?t => let res := assoc needlef t in constr:res
end.

Goal False.
let v := assoc (fun i => show (Show := i) "a") [(show (Show := Show2) "a", 1); ("b", 2)] in pose v.
let v := assoc (fun i => show (Show := i) "a") [(show (Show := Show1) "a", 1); ("b", 2)] in pose v.

关于coq - 将模式传递给战术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33508475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com