src/HOL/Bali/Term.thy
changeset 37956 ee939247b2fb
parent 35431 8758fe1fc9f8
child 41778 5f79a9e42507
equal deleted inserted replaced
37955:f87d1105e181 37956:ee939247b2fb
   256 
   256 
   257 abbreviation
   257 abbreviation
   258   StatRef :: "ref_ty \<Rightarrow> expr"
   258   StatRef :: "ref_ty \<Rightarrow> expr"
   259   where "StatRef rt == Cast (RefT rt) (Lit Null)"
   259   where "StatRef rt == Cast (RefT rt) (Lit Null)"
   260   
   260   
   261 definition is_stmt :: "term \<Rightarrow> bool" where
   261 definition
   262  "is_stmt t \<equiv> \<exists>c. t=In1r c"
   262   is_stmt :: "term \<Rightarrow> bool"
       
   263   where "is_stmt t = (\<exists>c. t=In1r c)"
   263 
   264 
   264 ML {* bind_thms ("is_stmt_rews", sum3_instantiate @{context} @{thm is_stmt_def}) *}
   265 ML {* bind_thms ("is_stmt_rews", sum3_instantiate @{context} @{thm is_stmt_def}) *}
   265 
   266 
   266 declare is_stmt_rews [simp]
   267 declare is_stmt_rews [simp]
   267 
   268 
   305 
   306 
   306 instantiation stmt :: inj_term
   307 instantiation stmt :: inj_term
   307 begin
   308 begin
   308 
   309 
   309 definition
   310 definition
   310   stmt_inj_term_def: "\<langle>c::stmt\<rangle> \<equiv> In1r c"
   311   stmt_inj_term_def: "\<langle>c::stmt\<rangle> = In1r c"
   311 
   312 
   312 instance ..
   313 instance ..
   313 
   314 
   314 end
   315 end
   315 
   316 
   321 
   322 
   322 instantiation expr :: inj_term
   323 instantiation expr :: inj_term
   323 begin
   324 begin
   324 
   325 
   325 definition
   326 definition
   326   expr_inj_term_def: "\<langle>e::expr\<rangle> \<equiv> In1l e"
   327   expr_inj_term_def: "\<langle>e::expr\<rangle> = In1l e"
   327 
   328 
   328 instance ..
   329 instance ..
   329 
   330 
   330 end
   331 end
   331 
   332 
   337 
   338 
   338 instantiation var :: inj_term
   339 instantiation var :: inj_term
   339 begin
   340 begin
   340 
   341 
   341 definition
   342 definition
   342   var_inj_term_def: "\<langle>v::var\<rangle> \<equiv> In2 v"
   343   var_inj_term_def: "\<langle>v::var\<rangle> = In2 v"
   343 
   344 
   344 instance ..
   345 instance ..
   345 
   346 
   346 end
   347 end
   347 
   348 
   366 
   367 
   367 instantiation list :: (expr_of) inj_term
   368 instantiation list :: (expr_of) inj_term
   368 begin
   369 begin
   369 
   370 
   370 definition
   371 definition
   371   "\<langle>es::'a list\<rangle> \<equiv> In3 (map expr_of es)"
   372   "\<langle>es::'a list\<rangle> = In3 (map expr_of es)"
   372 
   373 
   373 instance ..
   374 instance ..
   374 
   375 
   375 end
   376 end
   376 
   377 
   423   apply (case_tac a)
   424   apply (case_tac a)
   424   apply auto
   425   apply auto
   425   done
   426   done
   426 
   427 
   427 section {* Evaluation of unary operations *}
   428 section {* Evaluation of unary operations *}
   428 consts eval_unop :: "unop \<Rightarrow> val \<Rightarrow> val"
   429 primrec eval_unop :: "unop \<Rightarrow> val \<Rightarrow> val"
   429 primrec
   430 where
   430 "eval_unop UPlus   v = Intg (the_Intg v)"
   431   "eval_unop UPlus v = Intg (the_Intg v)"
   431 "eval_unop UMinus  v = Intg (- (the_Intg v))"
   432 | "eval_unop UMinus v = Intg (- (the_Intg v))"
   432 "eval_unop UBitNot v = Intg 42"                -- "FIXME: Not yet implemented"
   433 | "eval_unop UBitNot v = Intg 42"                -- "FIXME: Not yet implemented"
   433 "eval_unop UNot    v = Bool (\<not> the_Bool v)"
   434 | "eval_unop UNot v = Bool (\<not> the_Bool v)"
   434 
   435 
   435 section {* Evaluation of binary operations *}
   436 section {* Evaluation of binary operations *}
   436 consts eval_binop :: "binop \<Rightarrow> val \<Rightarrow> val \<Rightarrow> val"
   437 primrec eval_binop :: "binop \<Rightarrow> val \<Rightarrow> val \<Rightarrow> val"
   437 primrec
   438 where
   438 "eval_binop Mul     v1 v2 = Intg ((the_Intg v1) * (the_Intg v2))" 
   439   "eval_binop Mul     v1 v2 = Intg ((the_Intg v1) * (the_Intg v2))" 
   439 "eval_binop Div     v1 v2 = Intg ((the_Intg v1) div (the_Intg v2))"
   440 | "eval_binop Div     v1 v2 = Intg ((the_Intg v1) div (the_Intg v2))"
   440 "eval_binop Mod     v1 v2 = Intg ((the_Intg v1) mod (the_Intg v2))"
   441 | "eval_binop Mod     v1 v2 = Intg ((the_Intg v1) mod (the_Intg v2))"
   441 "eval_binop Plus    v1 v2 = Intg ((the_Intg v1) + (the_Intg v2))"
   442 | "eval_binop Plus    v1 v2 = Intg ((the_Intg v1) + (the_Intg v2))"
   442 "eval_binop Minus   v1 v2 = Intg ((the_Intg v1) - (the_Intg v2))"
   443 | "eval_binop Minus   v1 v2 = Intg ((the_Intg v1) - (the_Intg v2))"
   443 
   444 
   444 -- "Be aware of the explicit coercion of the shift distance to nat"
   445 -- "Be aware of the explicit coercion of the shift distance to nat"
   445 "eval_binop LShift  v1 v2 = Intg ((the_Intg v1) *   (2^(nat (the_Intg v2))))"
   446 | "eval_binop LShift  v1 v2 = Intg ((the_Intg v1) *   (2^(nat (the_Intg v2))))"
   446 "eval_binop RShift  v1 v2 = Intg ((the_Intg v1) div (2^(nat (the_Intg v2))))"
   447 | "eval_binop RShift  v1 v2 = Intg ((the_Intg v1) div (2^(nat (the_Intg v2))))"
   447 "eval_binop RShiftU v1 v2 = Intg 42" --"FIXME: Not yet implemented"
   448 | "eval_binop RShiftU v1 v2 = Intg 42" --"FIXME: Not yet implemented"
   448 
   449 
   449 "eval_binop Less    v1 v2 = Bool ((the_Intg v1) < (the_Intg v2))" 
   450 | "eval_binop Less    v1 v2 = Bool ((the_Intg v1) < (the_Intg v2))" 
   450 "eval_binop Le      v1 v2 = Bool ((the_Intg v1) \<le> (the_Intg v2))"
   451 | "eval_binop Le      v1 v2 = Bool ((the_Intg v1) \<le> (the_Intg v2))"
   451 "eval_binop Greater v1 v2 = Bool ((the_Intg v2) < (the_Intg v1))"
   452 | "eval_binop Greater v1 v2 = Bool ((the_Intg v2) < (the_Intg v1))"
   452 "eval_binop Ge      v1 v2 = Bool ((the_Intg v2) \<le> (the_Intg v1))"
   453 | "eval_binop Ge      v1 v2 = Bool ((the_Intg v2) \<le> (the_Intg v1))"
   453 
   454 
   454 "eval_binop Eq      v1 v2 = Bool (v1=v2)"
   455 | "eval_binop Eq      v1 v2 = Bool (v1=v2)"
   455 "eval_binop Neq     v1 v2 = Bool (v1\<noteq>v2)"
   456 | "eval_binop Neq     v1 v2 = Bool (v1\<noteq>v2)"
   456 "eval_binop BitAnd  v1 v2 = Intg 42" -- "FIXME: Not yet implemented"
   457 | "eval_binop BitAnd  v1 v2 = Intg 42" -- "FIXME: Not yet implemented"
   457 "eval_binop And     v1 v2 = Bool ((the_Bool v1) \<and> (the_Bool v2))"
   458 | "eval_binop And     v1 v2 = Bool ((the_Bool v1) \<and> (the_Bool v2))"
   458 "eval_binop BitXor  v1 v2 = Intg 42" -- "FIXME: Not yet implemented"
   459 | "eval_binop BitXor  v1 v2 = Intg 42" -- "FIXME: Not yet implemented"
   459 "eval_binop Xor     v1 v2 = Bool ((the_Bool v1) \<noteq> (the_Bool v2))"
   460 | "eval_binop Xor     v1 v2 = Bool ((the_Bool v1) \<noteq> (the_Bool v2))"
   460 "eval_binop BitOr   v1 v2 = Intg 42" -- "FIXME: Not yet implemented"
   461 | "eval_binop BitOr   v1 v2 = Intg 42" -- "FIXME: Not yet implemented"
   461 "eval_binop Or      v1 v2 = Bool ((the_Bool v1) \<or> (the_Bool v2))"
   462 | "eval_binop Or      v1 v2 = Bool ((the_Bool v1) \<or> (the_Bool v2))"
   462 "eval_binop CondAnd v1 v2 = Bool ((the_Bool v1) \<and> (the_Bool v2))"
   463 | "eval_binop CondAnd v1 v2 = Bool ((the_Bool v1) \<and> (the_Bool v2))"
   463 "eval_binop CondOr  v1 v2 = Bool ((the_Bool v1) \<or> (the_Bool v2))"
   464 | "eval_binop CondOr  v1 v2 = Bool ((the_Bool v1) \<or> (the_Bool v2))"
   464 
   465 
   465 definition need_second_arg :: "binop \<Rightarrow> val \<Rightarrow> bool" where
   466 definition
   466 "need_second_arg binop v1 \<equiv> \<not> ((binop=CondAnd \<and>  \<not> the_Bool v1) \<or>
   467   need_second_arg :: "binop \<Rightarrow> val \<Rightarrow> bool" where
   467                                (binop=CondOr  \<and> the_Bool v1))"
   468   "need_second_arg binop v1 = (\<not> ((binop=CondAnd \<and>  \<not> the_Bool v1) \<or>
       
   469                                  (binop=CondOr  \<and> the_Bool v1)))"
   468 text {* @{term CondAnd} and @{term CondOr} only evalulate the second argument
   470 text {* @{term CondAnd} and @{term CondOr} only evalulate the second argument
   469  if the value isn't already determined by the first argument*}
   471  if the value isn't already determined by the first argument*}
   470 
   472 
   471 lemma need_second_arg_CondAnd [simp]: "need_second_arg CondAnd (Bool b) = b" 
   473 lemma need_second_arg_CondAnd [simp]: "need_second_arg CondAnd (Bool b) = b" 
   472 by (simp add: need_second_arg_def)
   474 by (simp add: need_second_arg_def)