-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathintro_patterns.v
172 lines (161 loc) · 6.02 KB
/
intro_patterns.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
From stdpp Require Export strings.
From iris.proofmode Require Import base tokens sel_patterns.
Set Default Proof Using "Type".
Inductive intro_pat :=
| IIdent : ident → intro_pat
| IAnom : intro_pat
| IDrop : intro_pat
| IFrame : intro_pat
| IList : list (list intro_pat) → intro_pat
| IPureElim : intro_pat
| IAlwaysElim : intro_pat → intro_pat
| IModalElim : intro_pat → intro_pat
| IRewrite : direction → intro_pat
| IPureIntro : intro_pat
| IAlwaysIntro : intro_pat
| IModalIntro : intro_pat
| ISimpl : intro_pat
| IDone : intro_pat
| IForall : intro_pat
| IAll : intro_pat
| IClear : sel_pat → intro_pat
| IClearFrame : sel_pat → intro_pat.
Module intro_pat.
Inductive stack_item :=
| SPat : intro_pat → stack_item
| SList : stack_item
| SConjList : stack_item
| SBar : stack_item
| SAmp : stack_item
| SAlwaysElim : stack_item
| SModalElim : stack_item.
Notation stack := (list stack_item).
Fixpoint close_list (k : stack)
(ps : list intro_pat) (pss : list (list intro_pat)) : option stack :=
match k with
| SList :: k => Some (SPat (IList (ps :: pss)) :: k)
| SPat pat :: k => close_list k (pat :: ps) pss
| SAlwaysElim :: k =>
''(p,ps) ← maybe2 (::) ps; close_list k (IAlwaysElim p :: ps) pss
| SModalElim :: k =>
''(p,ps) ← maybe2 (::) ps; close_list k (IModalElim p :: ps) pss
| SBar :: k => close_list k [] (ps :: pss)
| _ => None
end.
Fixpoint big_conj (ps : list intro_pat) : intro_pat :=
match ps with
| [] => IList [[]]
| [p] => IList [[ p ]]
| [p1;p2] => IList [[ p1 ; p2 ]]
| p :: ps => IList [[ p ; big_conj ps ]]
end.
Fixpoint close_conj_list (k : stack)
(cur : option intro_pat) (ps : list intro_pat) : option stack :=
match k with
| SConjList :: k =>
ps ← match cur with
| None => guard (ps = []); Some [] | Some p => Some (p :: ps)
end;
Some (SPat (big_conj ps) :: k)
| SPat pat :: k => guard (cur = None); close_conj_list k (Some pat) ps
| SAlwaysElim :: k => p ← cur; close_conj_list k (Some (IAlwaysElim p)) ps
| SModalElim :: k => p ← cur; close_conj_list k (Some (IModalElim p)) ps
| SAmp :: k => p ← cur; close_conj_list k None (p :: ps)
| _ => None
end.
Fixpoint parse_go (ts : list token) (k : stack) : option stack :=
match ts with
| [] => Some k
| TName "_" :: ts => parse_go ts (SPat IDrop :: k)
| TName s :: ts => parse_go ts (SPat (IIdent s) :: k)
| TAnom :: ts => parse_go ts (SPat IAnom :: k)
| TFrame :: ts => parse_go ts (SPat IFrame :: k)
| TBracketL :: ts => parse_go ts (SList :: k)
| TBar :: ts => parse_go ts (SBar :: k)
| TBracketR :: ts => close_list k [] [] ≫= parse_go ts
| TParenL :: ts => parse_go ts (SConjList :: k)
| TAmp :: ts => parse_go ts (SAmp :: k)
| TParenR :: ts => close_conj_list k None [] ≫= parse_go ts
| TPure :: ts => parse_go ts (SPat IPureElim :: k)
| TAlways :: ts => parse_go ts (SAlwaysElim :: k)
| TModal :: ts => parse_go ts (SModalElim :: k)
| TArrow d :: ts => parse_go ts (SPat (IRewrite d) :: k)
| TPureIntro :: ts => parse_go ts (SPat IPureIntro :: k)
| TAlwaysIntro :: ts => parse_go ts (SPat IAlwaysIntro :: k)
| TModalIntro :: ts => parse_go ts (SPat IModalIntro :: k)
| TSimpl :: ts => parse_go ts (SPat ISimpl :: k)
| TDone :: ts => parse_go ts (SPat IDone :: k)
| TAll :: ts => parse_go ts (SPat IAll :: k)
| TForall :: ts => parse_go ts (SPat IForall :: k)
| TBraceL :: ts => parse_clear ts k
| _ => None
end
with parse_clear (ts : list token) (k : stack) : option stack :=
match ts with
| TFrame :: TName s :: ts => parse_clear ts (SPat (IClearFrame (SelIdent s)) :: k)
| TFrame :: TPure :: ts => parse_clear ts (SPat (IClearFrame SelPure) :: k)
| TFrame :: TAlways :: ts => parse_clear ts (SPat (IClearFrame SelPersistent) :: k)
| TFrame :: TSep :: ts => parse_clear ts (SPat (IClearFrame SelSpatial) :: k)
| TName s :: ts => parse_clear ts (SPat (IClear (SelIdent s)) :: k)
| TPure :: ts => parse_clear ts (SPat (IClear SelPure) :: k)
| TAlways :: ts => parse_clear ts (SPat (IClear SelPersistent) :: k)
| TSep :: ts => parse_clear ts (SPat (IClear SelSpatial) :: k)
| TBraceR :: ts => parse_go ts k
| _ => None
end.
Fixpoint close (k : stack) (ps : list intro_pat) : option (list intro_pat) :=
match k with
| [] => Some ps
| SPat pat :: k => close k (pat :: ps)
| SAlwaysElim :: k => ''(p,ps) ← maybe2 (::) ps; close k (IAlwaysElim p :: ps)
| SModalElim :: k => ''(p,ps) ← maybe2 (::) ps; close k (IModalElim p :: ps)
| _ => None
end.
Definition parse (s : string) : option (list intro_pat) :=
k ← parse_go (tokenize s) []; close k [].
Ltac parse s :=
lazymatch type of s with
| list intro_pat => s
| intro_pat => constr:([s])
| list string =>
lazymatch eval vm_compute in (mjoin <$> mapM parse s) with
| Some ?pats => pats | _ => fail "intro_pat.parse: cannot parse" s
end
| string =>
lazymatch eval vm_compute in (parse s) with
| Some ?pats => pats | _ => fail "intro_pat.parse: cannot parse" s
end
| ident => constr:([IIdent s])
| ?X => fail "intro_pat.parse:" s "has unexpected type" X
end.
Ltac parse_one s :=
lazymatch type of s with
| intro_pat => s
| string =>
lazymatch eval vm_compute in (parse s) with
| Some [?pat] => pat | _ => fail "intro_pat.parse_one: cannot parse" s
end
| ?X => fail "intro_pat.parse_one:" s "has unexpected type" X
end.
End intro_pat.
Fixpoint intro_pat_persistent (p : intro_pat) :=
match p with
| IPureElim => true
| IAlwaysElim _ => true
| IList pps => forallb (forallb intro_pat_persistent) pps
| ISimpl => true
| IClear _ => true
| IClearFrame _ => true
| _ => false
end.
Ltac intro_pat_persistent p :=
lazymatch type of p with
| intro_pat => eval cbv in (intro_pat_persistent p)
| list intro_pat => eval cbv in (forallb intro_pat_persistent p)
| string =>
let pat := intro_pat.parse p in
eval cbv in (forallb intro_pat_persistent pat)
| ident => false
| bool => p
| ?X => fail "intro_pat_persistent:" p "has unexpected type" X
end.