Debian Codegraph @

src/Util/TagList.v

raw
1 From Coq Require Import List.
2 Require Import Crypto.Util.Notations.
3 Import ListNotations.
4
5 Module Tag.
6 Record tagged :=
7 { keyT : Type;
8 key : keyT;
9 valueT : Type;
10 value : valueT;
11 local : bool }.
12
13 Definition Context := list tagged.
14
15 Definition add_gen {K V} (key' : K) (value' : V) (local' : bool) (ctx : Context) : Context
16 := cons {| key := key' ; value := value' ; local := local' |} ctx.
17
18 Definition add {K V} (key' : K) (value' : V) (ctx : Context) : Context
19 := add_gen key' value' false ctx.
20
21 Definition add_local {K V} (key' : K) (value' : V) (ctx : Context) : Context
22 := add_gen key' value' true ctx.
23
24 Definition strip_local (ctx : Context) : Context
25 := List.filter (fun '{| key := k ; value := v ; local := loc |}
26 => negb loc)
27 ctx.
28
29 Definition get_local (ctx : Context) : Context
30 := List.filter (fun '{| key := k ; value := v ; local := loc |}
31 => loc)
32 ctx.
33
34 Ltac subst_from_list ls :=
35 lazymatch eval hnf in ls with
36 | {| value := ?id |} :: ?ls
37 => try subst id; subst_from_list ls
38 | nil => idtac
39 end.
40
41 Ltac subst_local ctx :=
42 let loc := (eval cbv [get_local List.filter add add_local add_gen]
43 in (get_local ctx)) in
44 subst_from_list loc.
45
46 Ltac strip_local ctx :=
47 let upd := (eval cbv [strip_local negb List.filter] in strip_local) in
48 eval cbv [add add_local add_gen] in (upd ctx).
49
50 Ltac strip_subst_local ctx :=
51 let ret := strip_local ctx in
52 let dummy := match goal with
53 | _ => subst_local ctx
54 end in
55 ret.
56
57 Ltac update ctx key value :=
58 constr:(add key value ctx).
59
60 Ltac local_update ctx key value :=
61 constr:(add_local key value ctx).
62
63 Ltac get_gen_cont ctx key' tac_found tac_not_found allow_unfound :=
64 lazymatch (eval hnf in ctx) with
65 | context[{| key := key' ; value := ?value' |}]
66 => tac_found value'
67 | context[add_gen key' ?value' _ _] => tac_found value'
68 | context[add_local key' ?value' _] => tac_found value'
69 | context[add key' ?value' _] => tac_found value'
70 | _ => lazymatch allow_unfound with
71 | true => tac_not_found ()
72 end
73 end.
74
75 Ltac update_by_tac_if_not_exists ctx key value_tac :=
76 get_gen_cont
77 ctx key
78 ltac:(fun value' => ctx)
79 ltac:(fun _ => let value := value_tac () in
80 update ctx key value)
81 true.
82
83 Ltac update_if_not_exists ctx key value :=
84 update_by_tac_if_not_exists ctx key ltac:(fun _ => value).
85
86 Ltac get_gen ctx key' default :=
87 get_gen_cont ctx key' ltac:(fun v => v) default true.
88
89 Ltac get_error ctx key' :=
90 get_gen_cont ctx key' ltac:(fun v => v) ltac:(fun _ => constr:(I)) false.
91 Ltac get_default ctx key' := get_gen ctx key' ltac:(fun _ => constr:(I)).
92
93 Ltac get ctx key' := get_error ctx key'.
94
95 Notation get ctx key' := ltac:(let v := get ctx key' in exact v) (only parsing).
96
97 Notation empty := (@nil tagged).
98
99 Module Export TagNotations.
100 Declare Scope tagged_scope.
101 Delimit Scope tagged_scope with tagged.
102 Bind Scope tagged_scope with tagged.
103 Notation "t[ k ~> v ]" := {| key := k ; value := v ; local := _ |} : tagged_scope.
104 Notation "l[ k ~> v ]" := {| key := k ; value := v ; local := true |} : tagged_scope.
105 End TagNotations.
106 End Tag.
107
108 Export Tag.TagNotations.