intro_patterns.v 7.08 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1 2 3 4 5
From iris.prelude Require Export strings.

Inductive intro_pat :=
  | IName : string  intro_pat
  | IAnom : intro_pat
6
  | IDrop : intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
7
  | IFrame : intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
8
  | IList : list (list intro_pat)  intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
9 10 11 12 13 14 15 16
  | IPureElim : intro_pat
  | IAlwaysElim : intro_pat  intro_pat
  | ILaterElim : intro_pat  intro_pat
  | IVsElim : intro_pat  intro_pat
  | IPureIntro : intro_pat
  | IAlwaysIntro : intro_pat
  | ILaterIntro : intro_pat
  | IVsIntro : intro_pat
Robbert Krebbers's avatar
Robbert Krebbers committed
17
  | ISimpl : intro_pat
18 19
  | IForall : intro_pat
  | IAll : intro_pat
20
  | IClear : list (bool * string)  intro_pat. (* true = frame, false = clear *)
Robbert Krebbers's avatar
Robbert Krebbers committed
21 22 23 24 25

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

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 (cons_name kn k) ""
  | String "?" s => tokenize_go s (TAnom :: cons_name kn k) ""
55
  | String "_" s => tokenize_go s (TDrop :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
56
  | String "$" s => tokenize_go s (TFrame :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
57 58 59 60 61 62
  | 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
63 64 65 66 67 68 69 70 71 72
  | String "%" s => tokenize_go s (TPureElim :: cons_name kn k) ""
  | String "#" s => tokenize_go s (TAlwaysElim :: cons_name kn k) ""
  | String ">" s => tokenize_go s (TLaterElim :: cons_name kn k) ""
  | String "=" (String "=" (String ">" s)) =>
     tokenize_go s (TVsElim :: cons_name kn k) ""
  | String "!" (String "%" s) => tokenize_go s (TPureIntro :: cons_name kn k) ""
  | String "!" (String "#" s) => tokenize_go s (TAlwaysIntro :: cons_name kn k) ""
  | String "!" (String ">" s) => tokenize_go s (TLaterIntro :: cons_name kn k) ""
  | String "!" (String "=" (String "=" (String ">" s))) =>
     tokenize_go s (TVsIntro :: cons_name kn k) ""
73 74
  | 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
75
  | String "/" (String "=" s) => tokenize_go s (TSimpl :: cons_name kn k) ""
76 77
  | String "*" (String "*" s) => tokenize_go s (TAll :: cons_name kn k) ""
  | String "*" s => tokenize_go s (TForall :: cons_name kn k) ""
Robbert Krebbers's avatar
Robbert Krebbers committed
78 79 80 81 82 83 84 85 86
  | String a s => tokenize_go s k (String a kn)
  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
87 88 89 90
  | SAmp : stack_item
  | SAlwaysElim : stack_item
  | SLaterElim : stack_item
  | SVsElim : stack_item.
Robbert Krebbers's avatar
Robbert Krebbers committed
91 92 93 94 95 96 97
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
98 99 100 101 102
  | SAlwaysElim :: k =>
     '(p,ps)  maybe2 (::) ps; close_list k (IAlwaysElim p :: ps) pss
  | SLaterElim :: k =>
     '(p,ps)  maybe2 (::) ps; close_list k (ILaterElim p :: ps) pss
  | SVsElim :: k => '(p,ps)  maybe2 (::) ps; close_list k (IVsElim p :: ps) pss
Robbert Krebbers's avatar
Robbert Krebbers committed
103
  | SBar :: k => close_list k [] (ps :: pss)
104
  | _ => None
Robbert Krebbers's avatar
Robbert Krebbers committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  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
124 125 126
  | SAlwaysElim :: k => p  cur; close_conj_list k (Some (IAlwaysElim p)) ps
  | SLaterElim :: k => p  cur; close_conj_list k (Some (ILaterElim p)) ps
  | SVsElim :: k => p  cur; close_conj_list k (Some (IVsElim p)) ps
Robbert Krebbers's avatar
Robbert Krebbers committed
127
  | SAmp :: k => p  cur; close_conj_list k None (p :: ps)
128 129 130
  | _ => None
  end.

Robbert Krebbers's avatar
Robbert Krebbers committed
131 132 133 134 135
Fixpoint parse_go (ts : list token) (k : stack) : option stack :=
  match ts with
  | [] => Some k
  | TName s :: ts => parse_go ts (SPat (IName s) :: k)
  | TAnom :: ts => parse_go ts (SPat IAnom :: k)
136
  | TDrop :: ts => parse_go ts (SPat IDrop :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
137
  | TFrame :: ts => parse_go ts (SPat IFrame :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
138 139 140 141 142 143
  | 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
144 145 146 147 148 149 150 151
  | TPureElim :: ts => parse_go ts (SPat IPureElim :: k)
  | TAlwaysElim :: ts => parse_go ts (SAlwaysElim :: k)
  | TLaterElim :: ts => parse_go ts (SLaterElim :: k)
  | TVsElim :: ts => parse_go ts (SVsElim :: k)
  | TPureIntro :: ts => parse_go ts (SPat IPureIntro :: k)
  | TAlwaysIntro :: ts => parse_go ts (SPat IAlwaysIntro :: k)
  | TLaterIntro :: ts => parse_go ts (SPat ILaterIntro :: k)
  | TVsIntro :: ts => parse_go ts (SPat IVsIntro :: k)
Robbert Krebbers's avatar
Robbert Krebbers committed
152
  | TSimpl :: ts => parse_go ts (SPat ISimpl :: k)
153 154
  | TAll :: ts => parse_go ts (SPat IAll :: k)
  | TForall :: ts => parse_go ts (SPat IForall :: k)
155 156 157 158 159 160 161 162 163 164
  | TClearL :: ts => parse_clear ts [] k
  | _ => None
  end
with parse_clear (ts : list token)
    (ss : list (bool * string)) (k : stack) : option stack :=
  match ts with
  | TFrame :: TName s :: ts => parse_clear ts ((true,s) :: ss) k
  | TName s :: ts => parse_clear ts ((false,s) :: ss) k
  | TClearR :: ts => parse_go ts (SPat (IClear (reverse ss)) :: k)
  | _ => None
Robbert Krebbers's avatar
Robbert Krebbers committed
165
  end.
166

Robbert Krebbers's avatar
Robbert Krebbers committed
167 168 169 170 171 172 173 174 175
Definition parse (s : string) : option (list intro_pat) :=
  match k  parse_go (tokenize s) [SList]; close_list k [] [] with
  | Some [SPat (IList [ps])] => Some ps
  | _ => None
  end.

Ltac parse s :=
  lazymatch type of s with
  | list intro_pat => s
176 177 178 179 180 181 182 183
  | 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
Robbert Krebbers's avatar
Robbert Krebbers committed
184 185 186 187
  end.
Ltac parse_one s :=
  lazymatch type of s with
  | intro_pat => s
188 189 190 191
  | string =>
     lazymatch eval vm_compute in (parse s) with
     | Some [?pat] => pat | _ => fail "invalid intro_pat" s
     end
Robbert Krebbers's avatar
Robbert Krebbers committed
192 193
  end.
End intro_pat.