floverParserScript.sml 17 KB
Newer Older
Heiko Becker's avatar
Heiko Becker committed
1
open AbbrevsTheory MachineTypeTheory ExpressionsTheory CommandsTheory FloverMapTheory
2
open preamble
3

Heiko Becker's avatar
Heiko Becker committed
4
val _ = new_theory "floverParser";
5 6 7 8 9 10 11 12

val _ = Datatype `
  Token =
    | DLET
    | DRET
    | DPRE
    | DABS
    | DCOND
13
    | DGAMMA
Heiko Becker's avatar
Heiko Becker committed
14
    | DTYPE mType
Heiko Becker's avatar
Heiko Becker committed
15
    | DFIXED
16 17 18 19 20 21 22
    | DVAR
    | DCONST num
    | DNEG
    | DPLUS
    | DSUB
    | DMUL
    | DDIV
Heiko Becker's avatar
Heiko Becker committed
23 24
    | DFRAC
    | DCAST `;
25

Heiko Becker's avatar
Heiko Becker committed
26 27 28 29
val getConst_def = Define `
  getConst (c:char) = ORD c - 48`;

val lexConst_def = Define`
30
  lexConst (input:string) (akk:num) =
Heiko Becker's avatar
Heiko Becker committed
31 32 33 34 35 36 37 38
    case input of
      | STRING char input' =>
        if (isDigit char)
        then lexConst input' (akk * 10 + (getConst char))
        else (akk, input)
      |"" => (akk, input)`;

val lexName_def = Define `
39
  lexName (input:string) =
Heiko Becker's avatar
Heiko Becker committed
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
    case input of
     | STRING char input' =>
       if (isAlphaNum char)
       then
         let (name, input') = lexName input' in
         (STRING char name, input')
       else ("", input)
         | "" => ("",input)`;

val strSize_def = Define `
  strSize str :num=
    case str of
      | STRING _ str' => 1 + strSize str'
      | "" => 0`;

val lexName_imp = prove(
  ``!s s1 s2. lexName s = (s1,s2) ==> (s = s1 ++ s2)``,
  Induct \\ simp [Once lexName_def]
  \\ rw [] \\ pairarg_tac \\ fs []);

val lexConst_imp = prove(
  ``!s n s1 s2. lexConst s n = (s1,s2) ==> LENGTH s2 <= LENGTH s``,
  Induct \\ simp [Once lexConst_def]
  \\ rw [] \\ res_tac \\ fs []);

val lex_def = tDefine "lex" `
  lex input =
    case input of
    | STRING char input' =>
      if isDigit char
      then
        let (num, input'') = lexConst input 0 in
          DCONST num :: lex input''
      else
        if isAlpha char
        then
          let (name, input'') = lexName input in
            case name of
              | "Ret" => DRET :: lex input''
              | "Let" => DLET :: lex input''
              | "PRE" => DPRE :: lex input''
              | "ABS" => DABS :: lex input''
Heiko Becker's avatar
Heiko Becker committed
82
              | "GAMMA" => DGAMMA :: lex input''
Heiko Becker's avatar
Heiko Becker committed
83
              | "Var" => DVAR :: lex input''
Heiko Becker's avatar
Heiko Becker committed
84
              | "Cast" => DCAST :: lex input''
Heiko Becker's avatar
Heiko Becker committed
85
              | "F" => DFIXED :: lex input''
Heiko Becker's avatar
Heiko Becker committed
86 87
              | "MTYPE" => let ts = lex input'' in
                           (case ts of
Heiko Becker's avatar
Heiko Becker committed
88
                            | DCONST 16 :: ts' => DTYPE M16 :: ts'
Heiko Becker's avatar
Heiko Becker committed
89 90
                            | DCONST 32 :: ts' => DTYPE M32 :: ts'
                            | DCONST 64 :: ts' => DTYPE M64 :: ts'
Heiko Becker's avatar
Heiko Becker committed
91 92
                            | DFIXED :: DCONST w :: DCONST f :: ts' =>
                                DTYPE (F w f) :: ts'
93 94
                            (* | DCONST 128 :: ts' => DTYPE M128 :: ts' *)
                            (* | DCONST 256 :: ts' => DTYPE M256 :: ts' *)
Heiko Becker's avatar
Heiko Becker committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                            | _ => NIL)
              | _ => NIL
        else
          (case char of
            | #"+" => DPLUS :: lex input'
            | #"-" => DSUB  :: lex input'
            | #"*" => DMUL :: lex input'
            | #"/" => DDIV :: lex input'
            | #"#" => DFRAC :: lex input'
            | #"?" => DCOND :: lex input'
            | #"~" => DNEG :: lex input'
            | #" " => lex input'
            | #"\n" => lex input'
            | _ => NIL)
    |  _  => NIL`
 (WF_REL_TAC `measure LENGTH` \\ rw [] \\ fs []
  \\ imp_res_tac (GSYM lexName_imp) \\ fs [] \\ rveq \\ fs []
  \\ fs [Once lexConst_def] \\ rfs []
  \\ imp_res_tac (GSYM lexConst_imp) \\ fs [] \\ rveq \\ fs []);

Heiko Becker's avatar
Heiko Becker committed
115 116 117 118
val str_join_def = Define `
  (str_join (STRING c s1) s2 = STRING c (str_join s1 s2)) /\
  (str_join "" s2 = s2)`;

Heiko Becker's avatar
Heiko Becker committed
119 120
val str_of_num_def = Define `
  str_of_num (n:num) =
Heiko Becker's avatar
Heiko Becker committed
121 122
    if n < 10 then STRING (CHR (n + 48)) ""
    else str_join (str_of_num (n DIV 10)) (STRING (CHR ( (n MOD 10) + 48)) "")`
Heiko Becker's avatar
Heiko Becker committed
123 124

val type_to_string = Define `
125
  (type_to_string (M16) = "MTYPE 16") /\
Heiko Becker's avatar
Heiko Becker committed
126
  (type_to_string (M32) = "MTYPE 32") /\
Heiko Becker's avatar
Heiko Becker committed
127 128 129 130 131 132 133
  (type_to_string (M64) = "MTYPE 64") /\
  (type_to_string (F w f) =
    str_join "MTYPE "
      (str_join ("F ")
      (str_join (str_of_num w)
      (str_join " "
      (str_of_num f)))))`; (* FIXME *)
134 135 136
  (* (type_to_string (M128) = "MTYPE 128") /\ *)
  (* (type_to_string (M256) = "MTYPE 256")`; *)

137 138 139 140 141 142 143 144 145 146
val pp_token_def = Define `
  pp_token (token:Token) =
    case token of
    | DLET => "Let"
    | DRET => "Ret"
    | DPRE => "PRE"
    | DABS => "ABS"
    | DCOND => "?"
    | DVAR => "Var"
    | DCONST num => str_of_num num
147
    | DGAMMA => "GAMMA"
Heiko Becker's avatar
Heiko Becker committed
148
    | DTYPE m => type_to_string m
Heiko Becker's avatar
Heiko Becker committed
149
    | DFIXED => ""
150 151 152 153 154
    | DNEG => "~"
    | DPLUS => "+"
    | DSUB => "-"
    | DMUL => "*"
    | DDIV => "/"
Heiko Becker's avatar
Heiko Becker committed
155 156
    | DFRAC => "#"
    | DCAST => "Cast"`;
Heiko Becker's avatar
Heiko Becker committed
157

158 159 160 161 162
(* val pp_token_correct = store_thm ("pp_token_correct", *)
(*   ``!t. lex (pp_token t) = [t]``, *)
(*  Induct_on `t`  \\ EVAL_TAC fs[pp_token_def, lex_def] *)
(* ); *)

163 164 165
(* val str_join_empty = store_thm ("str_join_empty", *)
(*   ``!s. str_join s "" = s``, *)
(*   Induct \\ fs[str_join_def]); *)
Heiko Becker's avatar
Heiko Becker committed
166 167 168

(* Pretty Printer for Tokens *)
val pp_def = Define `
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  (pp (token :: tokList) = str_join (pp_token token) (pp tokList)) /\
  (pp NIL = "")`;

(* val lex_thm = prove ( *)
(*   ``!s. *)
(*     lex (pp (lex s)) = lex s``, *)
(*   measureInduct_on `STRLEN s` \\ Cases_on `s` *)
(*   >- (EVAL_TAC) *)
(*   >- (qspec_then `STRING h t` (fn thm => rw [Once thm]) lex_def *)
(*       >- (simp[Once lexConst_def] *)
(*           \\ qspec_then `STRING h t` (fn thm => rw [Once thm]) lex_def *)
(*           \\ qspec_then `STRING h t` (fn thm => simp[Once thm]) lexConst_def *)
(*           \\ Cases_on `lexConst t (getConst h)` \\ fs[] *)
(*           \\ `STRLEN r < SUC (STRLEN t)` by cheat *)
(*           \\ first_x_assum drule *)
(*           \\ disch_then assume_tac *)
(*           \\ fs[Once pp_def] *)
(*           \\ fs [Once pp_token_def] *)
(*           \\ cheat) *)
(*       >- (qspec_then `STRING h t` (fn thm => rw [Once thm]) lex_def *)
(*           \\ Cases_on `lexName (STRING h t)` \\ fs[] *)
(*           \\ Cases_on `q` \\ fs[] \\ TRY (EVAL_TAC) *)
(*           \\ fs[] *)
(*           \\ cheat) *)
(*       >- (rw [Once pp_def, Once str_join_def, pp_token_def, Once str_join_def, Once lex_def] *)
(*           \\ qspec_then `STRING #"+" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (rw [Once pp_def, Once str_join_def, pp_token_def, Once str_join_def, Once lex_def] *)
(*           \\ qspec_then `STRING #"-" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (rw [Once pp_def, Once str_join_def, pp_token_def, Once str_join_def, Once lex_def] *)
(*           \\ qspec_then `STRING #"*" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (rw [Once pp_def, Once str_join_def, pp_token_def, Once str_join_def, Once lex_def] *)
(*           \\ qspec_then `STRING #"/" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (rw [Once pp_def, Once str_join_def, pp_token_def, Once str_join_def, Once lex_def] *)
(*           \\ qspec_then `STRING #"#" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (rw [Once pp_def, Once str_join_def, pp_token_def, Once str_join_def, Once lex_def] *)
(*           \\ qspec_then `STRING #"?" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (rw [Once pp_def, Once str_join_def, pp_token_def, Once str_join_def, Once lex_def] *)
(*           \\ qspec_then `STRING #"~" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (qspec_then `STRING #" " s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (qspec_then `STRING #"\n" s` (fn thm => rw [Once thm]) lex_def) *)
(*       >- (qspec_then `STRING h s` (fn thm => rw [Once thm]) lex_def *)
(*           \\ rw [Once pp_def, Once lex_def]))); *)
Heiko Becker's avatar
Heiko Becker committed
211 212 213 214 215 216 217 218 219 220 221 222

val fix_res_def = Define `
  fix_res xs NONE = NONE /\
  fix_res xs (SOME (x,y)) =
    if LENGTH xs < LENGTH y then SOME (x,xs) else SOME (x,y)`

val fix_res_imp = prove(
  ``fix_res xs a = SOME (x,y) ==> LENGTH y <= LENGTH xs``,
  Cases_on `a` \\ rw [fix_res_def]
  \\ Cases_on `x'` \\ fs [fix_res_def]
  \\ every_case_tac \\ fs [] \\ rveq \\ fs []);

223
(** Prefix form parser for exprressions **)
Heiko Becker's avatar
Heiko Becker committed
224
val parseExp_def = tDefine "parseExp" `
225
  parseExp (tokList:Token list) :(real expr # Token list) option =
Heiko Becker's avatar
Heiko Becker committed
226
    case tokList of
Heiko Becker's avatar
Heiko Becker committed
227
    | DCONST n :: DFRAC :: DCONST m :: DTYPE t :: tokRest =>
228
      if (m = 0) then NONE else SOME (Const t ((&n):real / &m), tokRest)
Heiko Becker's avatar
Heiko Becker committed
229 230 231
    | DVAR :: DCONST n :: tokRest => SOME (Var n, tokRest)
    | DNEG :: tokRest =>
      (case (parseExp tokRest) of
232
         | SOME (Const t c, tokRest) => SOME (Const t (- c), tokRest)
Heiko Becker's avatar
Heiko Becker committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
         | SOME (e1,tokRest') => SOME (Unop Neg e1, tokRest')
         | NONE => NONE)
    | DPLUS :: tokRest =>
      (case fix_res tokRest (parseExp tokRest) of
         | SOME (e1,tokRest') =>
           (case (parseExp tokRest') of
              | SOME (e2, tokRest'') => SOME (Binop Plus e1 e2, tokRest'')
              | NONE => NONE)
         | NONE => NONE)
    | DSUB :: tokRest =>
      (case fix_res tokRest (parseExp tokRest) of
         | SOME (e1,tokRest') =>
           (case (parseExp tokRest') of
              | SOME (e2, tokRest'') => SOME (Binop Sub e1 e2, tokRest'')
              | NONE => NONE)
         | NONE => NONE)
    | DMUL :: tokRest =>
      (case fix_res tokRest (parseExp tokRest) of
         | SOME (e1,tokRest') =>
           (case (parseExp tokRest') of
              | SOME (e2, tokRest'') => SOME (Binop Mult e1 e2, tokRest'')
              | NONE => NONE)
         | NONE => NONE)
    | DDIV :: tokRest =>
      (case fix_res tokRest (parseExp tokRest) of
         | SOME (e1,tokRest') =>
           (case (parseExp tokRest') of
              | SOME (e2, tokRest'') => SOME (Binop Div e1 e2, tokRest'')
              | NONE => NONE)
         | NONE => NONE)
Heiko Becker's avatar
Heiko Becker committed
263 264 265 266
    | DCAST :: DTYPE m :: tokRest =>
      (case fix_res tokRest (parseExp tokRest) of
         | SOME (e1, tokRest') => SOME (Downcast m e1,tokRest')
         | _ => NONE)
Heiko Becker's avatar
Heiko Becker committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    | _ => NONE`
 (WF_REL_TAC `measure LENGTH`
  \\ rw [] \\ fs []
  \\ imp_res_tac fix_res_imp \\ fs []);

val parseExp_ind = fetch "-" "parseExp_ind";

val parseExp_LESS_EQ = prove(
  ``!xs x y. parseExp xs = SOME (x,y) ==> LENGTH y <= LENGTH xs``,
  recInduct parseExp_ind
  \\ rw [] \\ fs [] \\ pop_assum mp_tac
  \\ once_rewrite_tac [parseExp_def]
  \\ ntac 8 (TRY (TOP_CASE_TAC \\ fs [])) \\ rw []
  \\ fs [] \\ imp_res_tac fix_res_imp \\ fs [])

val fix_res_parseExp = prove(
  ``fix_res xs (parseExp xs) = parseExp xs``,
  Cases_on `parseExp xs` \\ fs [fix_res_def]
  \\ Cases_on `x` \\ fs [fix_res_def]
  \\ imp_res_tac parseExp_LESS_EQ \\ fs []);

val parseExp_def = save_thm("parseExp_def",
  parseExp_def |> REWRITE_RULE [fix_res_parseExp]);

val parseExp_ind = save_thm("parseExp_ind",
  parseExp_ind |> REWRITE_RULE [fix_res_parseExp]);

val parseRet_def = Define `
  parseRet input :(real cmd # Token list) option =
    case parseExp input of
     | SOME (e, tokRest) => SOME (Ret e, tokRest)
     | NONE => NONE`;

val parseLet_def = tDefine "parseLet" `
  parseLet input :(real cmd # Token list) option =
    case input of
     (* We have a valid let binding *)
304 305 306
     | DVAR :: DCONST n :: DTYPE m :: exprLetRest =>
       (* so we parse an exprression *)
       (case parseExp exprLetRest of
Heiko Becker's avatar
Heiko Becker committed
307 308 309 310 311 312 313
          | SOME (e, letRest) =>
            (case letRest of
               (* If we continue with a let *)
               | DLET :: letBodyRest =>
                 (* Parse it *)
                 (case (parseLet letBodyRest) of
                    (* And construct a result from it *)
314
                    | SOME (letCmd, arbRest) => SOME (Let m n e letCmd, arbRest)
Heiko Becker's avatar
Heiko Becker committed
315 316 317 318 319 320
                    | _ => NONE)
               (* But if we have a return *)
               | DRET :: retBodyRest =>
                 (* parse only this *)
                 (case (parseRet retBodyRest) of
                    (* and construct the result *)
321
                    | SOME (retCmd, arbRest) => SOME (Let m n e retCmd, arbRest)
Heiko Becker's avatar
Heiko Becker committed
322 323
                    | _ => NONE)
               | _ => NONE) (* fail if there is no continuation for the let *)
324
          | NONE => NONE) (* fail if we do not have an exprression to bind *)
Heiko Becker's avatar
Heiko Becker committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
     | _ => NONE` (* fail if we cannot find a variable *)
  (WF_REL_TAC `measure LENGTH`
   \\ rw [] \\ imp_res_tac fix_res_imp \\ fs []
   \\ imp_res_tac parseExp_LESS_EQ \\ fs [])

val parseLet_ind = fetch "-" "parseLet_ind";

val parseFrac_def = Define `
  parseFrac (input:Token list) :(real # Token list) option =
     case input of
      | DNEG :: DCONST n :: DFRAC :: DCONST m :: tokRest =>
        if (m = 0) then NONE else SOME ((- &n):real / (&m),tokRest)
      | DCONST n :: DFRAC :: DCONST m :: tokRest =>
        if (m = 0) then NONE else SOME ((&n):real / (&m),tokRest)
      | _ => NONE `;

val parseIV_def = Define `
  parseIV (input:Token list) :(interval # Token list) option =
    case (parseFrac input) of
      | SOME (iv_lo, tokRest) =>
        (case (parseFrac tokRest) of
           | SOME (iv_hi, tokList) => SOME ((iv_lo, iv_hi), tokList)
           | _ => NONE )
      | _ => NONE`;

val defaultPre_def = Define
  `defaultPre:precond = \x. (0,0)`;

val updPre_def = Define
  `updPre (n:num) (iv:interval) (P:precond) =
     \m. if (n = m) then iv else P m`;

val parseFrac_LESS_EQ = prove(
  ``parseFrac xs = SOME (x,y) ==> LENGTH y <= LENGTH xs``,
  fs [parseFrac_def] \\ every_case_tac \\ fs []);

val parseIV_LESS_EQ = prove(
  ``parseIV xs = SOME (x,y) ==> LENGTH y <= LENGTH xs``,
  fs [parseIV_def] \\ every_case_tac \\ fs []
  \\ imp_res_tac parseFrac_LESS_EQ \\ fs [] \\ rw [] \\ fs []);

(** Precondition parser: *)
367 368 369
(*   The precondition should be encoded in the following format: *)
(*   PRECOND ::= DCOND DVAR DCONST FRACTION FRACTION PRECOND | EPSILON *)
(*   The beginning of the precondition is marked by the DPRE token *)
Heiko Becker's avatar
Heiko Becker committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
(* **)
val parsePrecondRec_def = tDefine "parsePrecondRec" `
  parsePrecondRec (input:Token list) :(precond # Token list) option =
    (case input of
       | DCOND :: DVAR :: DCONST n :: fracRest =>
         (case parseIV fracRest of
            | SOME (iv, precondRest) =>
              (case parsePrecondRec precondRest of
                 | SOME (P, tokRest) => SOME (updPre n iv P, tokRest)
                 | NONE => SOME (updPre n iv defaultPre, precondRest))
            | _ => NONE)
       | _ => NONE) `
  (WF_REL_TAC `measure LENGTH` \\ rw []
   \\ imp_res_tac parseIV_LESS_EQ \\ fs []);

val parsePrecond_def = Define `
  parsePrecond (input :Token list) =
    case input of
    | DPRE :: tokRest => parsePrecondRec tokRest
    | _ => NONE`;

val defaultAbsenv_def = Define
Heiko Becker's avatar
Heiko Becker committed
392
  `defaultAbsenv:analysisResult = FloverMapTree_empty`;
Heiko Becker's avatar
Heiko Becker committed
393 394

val updAbsenv_def = Define
395
  `updAbsenv (e:real expr) (iv:interval) (err:real) (A:analysisResult) =
Heiko Becker's avatar
Heiko Becker committed
396
     FloverMapTree_insert e (iv,err) A`;
Heiko Becker's avatar
Heiko Becker committed
397 398

(** Abstract environment parser: *)
399 400 401
(*   The abstract environment should be encoded in the following format: *)
(*   ABSENV ::= ? EXPRESSION FRACTION FRACTION FRACTION ABSENV | EPSILON *)
(*   The beginning of the abstract environment is marked by the DABS token *)
Heiko Becker's avatar
Heiko Becker committed
402 403 404 405
(* **)
val parseAbsEnvRec_def = tDefine "parseAbsEnvRec" `
  parseAbsEnvRec (input:Token list) :(analysisResult # Token list) option =
    (case input of
406 407
       | DCOND :: exprRest =>
         (case parseExp exprRest of
Heiko Becker's avatar
Heiko Becker committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
            | SOME (e,fracRest) =>
              (case parseIV fracRest of
                 | SOME (iv, errRest) =>
                   (case parseFrac errRest of
                      | SOME (err, absenvRest) =>
                        (case parseAbsEnvRec absenvRest of
                           | SOME (A, tokRest) => SOME (updAbsenv e iv err A, tokRest)
                           | NONE => SOME (updAbsenv e iv err defaultAbsenv, absenvRest))
                      | NONE => NONE)
                 | _ => NONE)
              | NONE => NONE)
       | _ => SOME (defaultAbsenv, input))`
  (WF_REL_TAC `measure LENGTH` \\ rw []
   \\ imp_res_tac parseFrac_LESS_EQ
   \\ imp_res_tac parseIV_LESS_EQ
   \\ imp_res_tac parseExp_LESS_EQ
   \\ fs []);

val parseAbsEnv_def = Define `
  parseAbsEnv (input:Token list) =
    case input of
    | DABS :: tokRest => parseAbsEnvRec tokRest
    | _ => NONE`;

432
val defaultGamma_def = Define `
433 434 435 436 437 438 439 440
  defaultGamma:mType fMap = FloverMapTree_empty`;

val parseGammaRec_def = tDefine "parseGammaRec"
  `parseGammaRec (input: Token list) : (typeMap # Token list) option =
    (case parseExp input of
      |NONE => SOME (defaultGamma, input)
      |SOME (e,residual) =>
        (case residual of
441
         | DTYPE m :: inputRest =>
442
          (case parseGammaRec inputRest of
443 444
            | SOME (Gamma, rest) =>
              SOME (FloverMapTree_insert e m Gamma, rest)
445
            | NONE => NONE)
446 447 448 449
         | _ => SOME (defaultGamma, input)))`
  (WF_REL_TAC `measure LENGTH` \\ rw[]
  \\ IMP_RES_TAC parseExp_LESS_EQ
  \\ fs[]);
450 451 452 453 454 455 456 457

val parseGamma_def = Define `
  parseGamma (input:Token list) =
      case input of
       | DGAMMA :: tokenRest => parseGammaRec tokenRest
       | _ => NONE`;

(* Global parsing function*)
Heiko Becker's avatar
Heiko Becker committed
458 459
val dParse_def = Define `
  dParse (input:Token list) =
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    let cmdParse =
      (case input of
        | DLET :: tokRest => parseLet tokRest
        | DRET :: tokRest => parseRet tokRest
        | _ => NONE) in
    case cmdParse of
      |NONE => NONE
      | SOME (dCmd, tokRest) =>
      (case parseGamma tokRest of
      |NONE => NONE
      |SOME (Gamma, residual) =>
        (case parsePrecond residual of
        |NONE => NONE
        |SOME (P, residual) =>
          (case parseAbsEnv residual of
          |NONE => NONE
          |SOME (A, residual) =>
477
              SOME ((dCmd, P, A, Gamma), residual))))`;
478 479

val _ = export_theory();