intro_patterns.v 7.47 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
From iris.prelude Require Export strings.
2
Set Default Proof Using "Type".
Robbert Krebbers's avatar
Robbert Krebbers committed
3 4 5 6

Inductive intro_pat :=
  | IName : string  intro_pat
  | IAnom : intro_pat
7
  | IDrop : intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
8
  | IFrame : intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
9
  | IList : list (list intro_pat)  intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
10 11
  | IPureElim : intro_pat
  | IAlwaysElim : intro_pat  intro_pat
12
  | IModalElim : intro_pat  intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
13 14
  | IPureIntro : intro_pat
  | IAlwaysIntro : intro_pat
15
  | IModalIntro : intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
16
  | ISimpl : intro_pat
17 18
  | IForall : intro_pat
  | IAll : intro_pat
19 20
  | IClear : string  intro_pat
  | IClearFrame : string  intro_pat.
Robbert Krebbers's avatar
Robbert Krebbers committed
21 22 23 24 25

Module intro_pat.
Inductive token :=
  | TName : string  token
  | TAnom : token
Robbert Krebbers's avatar
Robbert Krebbers committed
26
  | TFrame : token
Robbert Krebbers's avatar
Robbert Krebbers committed
27 28 29 30 31 32
  | TBar : token
  | TBracketL : token
  | TBracketR : token
  | TAmp : token
  | TParenL : token
  | TParenR : token
Robbert Krebbers's avatar
Robbert Krebbers committed
33 34
  | TPureElim : token
  | TAlwaysElim : token
35
  | TModalElim : token
Robbert Krebbers's avatar
Robbert Krebbers committed
36 37
  | TPureIntro : token
  | TAlwaysIntro : token
38
  | TModalIntro : token
Robbert Krebbers's avatar
Robbert Krebbers committed
39
  | TSimpl : token
40
  | TForall : token
Robbert Krebbers's avatar
Robbert Krebbers committed
41 42 43
  | TAll : token
  | TClearL : token
  | TClearR : token.
Robbert Krebbers's avatar
Robbert Krebbers committed
44 45 46 47 48 49 50

Fixpoint cons_name (kn : string) (k : list token) : list token :=
  match kn with "" => k | _ => TName (string_rev kn) :: k end.
Fixpoint tokenize_go (s : string) (k : list token) (kn : string) : list token :=
  match s with
  | "" => rev (cons_name kn k)
  | String "?" s => tokenize_go s (TAnom :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
51
  | String "$" s => tokenize_go s (TFrame :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
52 53 54 55 56 57
  | String "[" s => tokenize_go s (TBracketL :: cons_name kn k) ""
  | String "]" s => tokenize_go s (TBracketR :: cons_name kn k) ""
  | String "|" s => tokenize_go s (TBar :: cons_name kn k) ""
  | String "(" s => tokenize_go s (TParenL :: cons_name kn k) ""
  | String ")" s => tokenize_go s (TParenR :: cons_name kn k) ""
  | String "&" s => tokenize_go s (TAmp :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
58 59
  | String "%" s => tokenize_go s (TPureElim :: cons_name kn k) ""
  | String "#" s => tokenize_go s (TAlwaysElim :: cons_name kn k) ""
60
  | String ">" s => tokenize_go s (TModalElim :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
61 62
  | String "!" (String "%" s) => tokenize_go s (TPureIntro :: cons_name kn k) ""
  | String "!" (String "#" s) => tokenize_go s (TAlwaysIntro :: cons_name kn k) ""
63
  | String "!" (String ">" s) => tokenize_go s (TModalIntro :: cons_name kn k) ""
64 65
  | String "{" s => tokenize_go s (TClearL :: cons_name kn k) ""
  | String "}" s => tokenize_go s (TClearR :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
66
  | String "/" (String "=" s) => tokenize_go s (TSimpl :: cons_name kn k) ""
67 68
  | String "*" (String "*" s) => tokenize_go s (TAll :: cons_name kn k) ""
  | String "*" s => tokenize_go s (TForall :: cons_name kn k) ""
69 70 71
  | String a s =>
     if is_space a then tokenize_go s (cons_name kn k) ""
     else tokenize_go s k (String a kn)
72
  (* TODO: Complain about invalid characters, to future-proof this against making more characters special. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
73 74 75 76 77 78 79 80
  end.
Definition tokenize (s : string) : list token := tokenize_go s [] "".

Inductive stack_item :=
  | SPat : intro_pat  stack_item
  | SList : stack_item
  | SConjList : stack_item
  | SBar : stack_item
Robbert Krebbers's avatar
Robbert Krebbers committed
81 82
  | SAmp : stack_item
  | SAlwaysElim : stack_item
83
  | SModalElim : stack_item.
Robbert Krebbers's avatar
Robbert Krebbers committed
84 85 86 87 88 89 90
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
Robbert Krebbers's avatar
Robbert Krebbers committed
91 92
  | SAlwaysElim :: k =>
     '(p,ps)  maybe2 (::) ps; close_list k (IAlwaysElim p :: ps) pss
93 94
  | SModalElim :: k =>
     '(p,ps)  maybe2 (::) ps; close_list k (IModalElim p :: ps) pss
Robbert Krebbers's avatar
Robbert Krebbers committed
95
  | SBar :: k => close_list k [] (ps :: pss)
96
  | _ => None
Robbert Krebbers's avatar
Robbert Krebbers committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  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
Robbert Krebbers's avatar
Robbert Krebbers committed
116
  | SAlwaysElim :: k => p  cur; close_conj_list k (Some (IAlwaysElim p)) ps
117
  | SModalElim :: k => p  cur; close_conj_list k (Some (IModalElim p)) ps
Robbert Krebbers's avatar
Robbert Krebbers committed
118
  | SAmp :: k => p  cur; close_conj_list k None (p :: ps)
119 120 121
  | _ => None
  end.

Robbert Krebbers's avatar
Robbert Krebbers committed
122 123 124
Fixpoint parse_go (ts : list token) (k : stack) : option stack :=
  match ts with
  | [] => Some k
125
  | TName "_" :: ts => parse_go ts (SPat IDrop :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
126 127
  | TName s :: ts => parse_go ts (SPat (IName s) :: k)
  | TAnom :: ts => parse_go ts (SPat IAnom :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
128
  | TFrame :: ts => parse_go ts (SPat IFrame :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
129 130 131 132 133 134
  | 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
Robbert Krebbers's avatar
Robbert Krebbers committed
135 136
  | TPureElim :: ts => parse_go ts (SPat IPureElim :: k)
  | TAlwaysElim :: ts => parse_go ts (SAlwaysElim :: k)
137
  | TModalElim :: ts => parse_go ts (SModalElim :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
138 139
  | TPureIntro :: ts => parse_go ts (SPat IPureIntro :: k)
  | TAlwaysIntro :: ts => parse_go ts (SPat IAlwaysIntro :: k)
140
  | TModalIntro :: ts => parse_go ts (SPat IModalIntro :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
141
  | TSimpl :: ts => parse_go ts (SPat ISimpl :: k)
142 143
  | TAll :: ts => parse_go ts (SPat IAll :: k)
  | TForall :: ts => parse_go ts (SPat IForall :: k)
144
  | TClearL :: ts => parse_clear ts k
145 146
  | _ => None
  end
147
with parse_clear (ts : list token) (k : stack) : option stack :=
148
  match ts with
149 150 151
  | TFrame :: TName s :: ts => parse_clear ts (SPat (IClearFrame s) :: k)
  | TName s :: ts => parse_clear ts (SPat (IClear s) :: k)
  | TClearR :: ts => parse_go ts k
152
  | _ => None
Robbert Krebbers's avatar
Robbert Krebbers committed
153
  end.
154

155 156 157 158 159 160
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)
Robbert Krebbers's avatar
Robbert Krebbers committed
161 162 163
  | _ => None
  end.

164 165 166 167

Definition parse (s : string) : option (list intro_pat) :=
  k  parse_go (tokenize s) []; close k [].

Robbert Krebbers's avatar
Robbert Krebbers committed
168 169 170
Ltac parse s :=
  lazymatch type of s with
  | list intro_pat => s
171
  | intro_pat => constr:([s])
172 173 174 175 176 177 178 179
  | list string =>
     lazymatch eval vm_compute in (mjoin <$> mapM parse s) with
     | Some ?pats => pats | _ => fail "invalid list intro_pat" s
     end
  | string =>
     lazymatch eval vm_compute in (parse s) with
     | Some ?pats => pats | _ => fail "invalid list intro_pat" s
     end
180
  | ?X => fail "intro_pat.parse:" s "has unexpected type" X
Robbert Krebbers's avatar
Robbert Krebbers committed
181 182 183 184
  end.
Ltac parse_one s :=
  lazymatch type of s with
  | intro_pat => s
185 186 187 188
  | string =>
     lazymatch eval vm_compute in (parse s) with
     | Some [?pat] => pat | _ => fail "invalid intro_pat" s
     end
189
  | ?X => fail "intro_pat.parse_one:" s "has unexpected type" X
Robbert Krebbers's avatar
Robbert Krebbers committed
190 191
  end.
End intro_pat.
Robbert Krebbers's avatar
Robbert Krebbers committed
192 193 194 195 196 197 198 199 200 201 202 203

Fixpoint intro_pat_persistent (p : intro_pat) :=
  match p with
  | IPureElim => true
  | IAlwaysElim _ => true
  | IList pps => forallb (forallb intro_pat_persistent) pps
  | _ => false
  end.

Ltac intro_pat_persistent p :=
  lazymatch type of p with
  | intro_pat => eval cbv in (intro_pat_persistent p)
204
  | list intro_pat => eval cbv in (forallb intro_pat_persistent p)
Robbert Krebbers's avatar
Robbert Krebbers committed
205
  | string =>
206 207 208 209
     let pat := intro_pat.parse p in
     eval cbv in (forallb intro_pat_persistent pat)
  | bool => p
  | ?X => fail "intro_pat_persistent:" p "has unexpected type" X
Robbert Krebbers's avatar
Robbert Krebbers committed
210
  end.