src/HOL/ex/NBE.thy
changeset 23778 18f426a137a9
parent 23503 234b83011a9b
child 23854 688a8a7bcd4e
equal deleted inserted replaced
23777:60b7800338d5 23778:18f426a137a9
    42  and us vs ws :: "ml list"
    42  and us vs ws :: "ml list"
    43  and nm :: const_name
    43  and nm :: const_name
    44  and x :: lam_var_name
    44  and x :: lam_var_name
    45  and X :: ml_var_name
    45  and X :: ml_var_name
    46 
    46 
    47 consts Pure_tms :: "tm set"
    47 inductive_set Pure_tms :: "tm set"
    48 inductive Pure_tms
    48 where
    49 intros
    49   "Ct s : Pure_tms"
    50 "Ct s : Pure_tms"
    50 | "Vt x : Pure_tms"
    51 "Vt x : Pure_tms"
    51 | "t : Pure_tms ==> Lam t : Pure_tms"
    52 "t : Pure_tms ==> Lam t : Pure_tms"
    52 | "s : Pure_tms ==> t : Pure_tms ==> At s t : Pure_tms"
    53 "s : Pure_tms ==> t : Pure_tms ==> At s t : Pure_tms"
       
    54 
    53 
    55 consts
    54 consts
    56   R :: "(const_name * tm list * tm)set" (* reduction rules *)
    55   R :: "(const_name * tm list * tm)set" (* reduction rules *)
    57   compR :: "(const_name * ml list * ml)set" (* compiled reduction rules *)
    56   compR :: "(const_name * ml list * ml)set" (* compiled reduction rules *)
    58   tRed :: "(tm * tm)set" (* beta + R reduction on pure terms *)
       
    59   tRed_list :: "(tm list * tm list) set"
       
    60 
    57 
    61 fun
    58 fun
    62   lift_tm :: "nat \<Rightarrow> tm \<Rightarrow> tm" ("lift") and
    59   lift_tm :: "nat \<Rightarrow> tm \<Rightarrow> tm" ("lift") and
    63   lift_ml :: "nat \<Rightarrow> ml \<Rightarrow> ml" ("lift")
    60   lift_ml :: "nat \<Rightarrow> ml \<Rightarrow> ml" ("lift")
    64 where
    61 where
   224 apply(induct k t and k u arbitrary: v and v rule: lift_tm_ML_lift_ml_ML.induct)
   221 apply(induct k t and k u arbitrary: v and v rule: lift_tm_ML_lift_ml_ML.induct)
   225 apply (simp_all add:map_idI map_compose[symmetric])
   222 apply (simp_all add:map_idI map_compose[symmetric])
   226 apply (simp cong:if_cong)
   223 apply (simp cong:if_cong)
   227 done
   224 done
   228 
   225 
   229 abbreviation
   226 inductive_set
   230   tred :: "[tm, tm] => bool"  (infixl "\<rightarrow>" 50) where
   227   tRed :: "(tm * tm)set" (* beta + R reduction on pure terms *)
       
   228   and tred :: "[tm, tm] => bool"  (infixl "\<rightarrow>" 50)
       
   229 where
   231   "s \<rightarrow> t == (s, t) \<in> tRed"
   230   "s \<rightarrow> t == (s, t) \<in> tRed"
       
   231 | "At (Lam t) s \<rightarrow> t[s/0]"
       
   232 | "(nm,ts,t) : R ==> foldl At (Ct nm) (map (subst rs) ts) \<rightarrow> subst rs t"
       
   233 | "t \<rightarrow> t' ==> Lam t \<rightarrow> Lam t'"
       
   234 | "s \<rightarrow> s' ==> At s t \<rightarrow> At s' t"
       
   235 | "t \<rightarrow> t' ==> At s t \<rightarrow> At s t'"
       
   236 
   232 abbreviation
   237 abbreviation
   233   treds :: "[tm, tm] => bool"  (infixl "\<rightarrow>*" 50) where
   238   treds :: "[tm, tm] => bool"  (infixl "\<rightarrow>*" 50) where
   234   "s \<rightarrow>* t == (s, t) \<in> tRed^*"
   239   "s \<rightarrow>* t == (s, t) \<in> tRed^*"
   235 abbreviation
   240 
   236   treds_list :: "[tm list, tm list] \<Rightarrow> bool" (infixl "\<rightarrow>*" 50) where
   241 inductive_set
       
   242   tRed_list :: "(tm list * tm list) set"
       
   243   and treds_list :: "[tm list, tm list] \<Rightarrow> bool" (infixl "\<rightarrow>*" 50)
       
   244 where
   237   "ss \<rightarrow>* ts == (ss, ts) \<in> tRed_list"
   245   "ss \<rightarrow>* ts == (ss, ts) \<in> tRed_list"
   238 
   246 | "[] \<rightarrow>* []"
   239 inductive tRed
   247 | "ts \<rightarrow>* ts' ==> t \<rightarrow>* t' ==> t#ts \<rightarrow>* t'#ts'"
   240 intros
       
   241 "At (Lam t) s \<rightarrow> t[s/0]"
       
   242 "(nm,ts,t) : R ==> foldl At (Ct nm) (map (subst rs) ts) \<rightarrow> subst rs t"
       
   243 "t \<rightarrow> t' ==> Lam t \<rightarrow> Lam t'"
       
   244 "s \<rightarrow> s' ==> At s t \<rightarrow> At s' t"
       
   245 "t \<rightarrow> t' ==> At s t \<rightarrow> At s t'"
       
   246 
       
   247 inductive tRed_list
       
   248 intros
       
   249 "[] \<rightarrow>* []"
       
   250 "ts \<rightarrow>* ts' ==> t \<rightarrow>* t' ==> t#ts \<rightarrow>* t'#ts'"
       
   251 
   248 
   252 declare tRed_list.intros[simp]
   249 declare tRed_list.intros[simp]
   253 
   250 
   254 lemma tRed_list_refl[simp]: includes Vars shows "ts \<rightarrow>* ts"
   251 lemma tRed_list_refl[simp]: includes Vars shows "ts \<rightarrow>* ts"
   255 by(induct ts) auto
   252 by(induct ts) auto
   256 
       
   257 consts
       
   258   Red :: "(ml * ml)set"
       
   259   Redl :: "(ml list * ml list)set"
       
   260   Redt :: "(tm * tm)set"
       
   261 
       
   262 abbreviation
       
   263   red :: "[ml, ml] => bool"  (infixl "\<Rightarrow>" 50) where
       
   264   "s \<Rightarrow> t == (s, t) \<in> Red"
       
   265 abbreviation
       
   266   redl :: "[ml list, ml list] => bool"  (infixl "\<Rightarrow>" 50) where
       
   267   "s \<Rightarrow> t == (s, t) \<in> Redl"
       
   268 abbreviation
       
   269   redt :: "[tm, tm] => bool"  (infixl "\<Rightarrow>" 50) where
       
   270   "s \<Rightarrow> t == (s, t) \<in> Redt"
       
   271 abbreviation
       
   272   reds :: "[ml, ml] => bool"  (infixl "\<Rightarrow>*" 50) where
       
   273   "s \<Rightarrow>* t == (s, t) \<in> Red^*"
       
   274 abbreviation
       
   275   redts :: "[tm, tm] => bool"  (infixl "\<Rightarrow>*" 50) where
       
   276   "s \<Rightarrow>* t == (s, t) \<in> Redt^*"
       
   277 
   253 
   278 
   254 
   279 fun ML_closed :: "nat \<Rightarrow> ml \<Rightarrow> bool"
   255 fun ML_closed :: "nat \<Rightarrow> ml \<Rightarrow> bool"
   280 and ML_closed_t :: "nat \<Rightarrow> tm \<Rightarrow> bool" where
   256 and ML_closed_t :: "nat \<Rightarrow> tm \<Rightarrow> bool" where
   281 "ML_closed i (C nm vs) = (ALL v:set vs. ML_closed i v)" |
   257 "ML_closed i (C nm vs) = (ALL v:set vs. ML_closed i v)" |
   290 "ML_closed_t i (At r s) = (ML_closed_t i r & ML_closed_t i s)" |
   266 "ML_closed_t i (At r s) = (ML_closed_t i r & ML_closed_t i s)" |
   291 "ML_closed_t i (Lam t) = (ML_closed_t i t)" |
   267 "ML_closed_t i (Lam t) = (ML_closed_t i t)" |
   292 "ML_closed_t i v = True"
   268 "ML_closed_t i v = True"
   293 thm ML_closed.simps ML_closed_t.simps
   269 thm ML_closed.simps ML_closed_t.simps
   294 
   270 
   295 inductive Red Redt Redl
   271 inductive_set
   296 intros
   272   Red :: "(ml * ml)set"
       
   273   and Redt :: "(tm * tm)set"
       
   274   and Redl :: "(ml list * ml list)set"
       
   275   and red :: "[ml, ml] => bool"  (infixl "\<Rightarrow>" 50)
       
   276   and redl :: "[ml list, ml list] => bool"  (infixl "\<Rightarrow>" 50)
       
   277   and redt :: "[tm, tm] => bool"  (infixl "\<Rightarrow>" 50)
       
   278   and reds :: "[ml, ml] => bool"  (infixl "\<Rightarrow>*" 50)
       
   279   and redts :: "[tm, tm] => bool"  (infixl "\<Rightarrow>*" 50)
       
   280 where
       
   281   "s \<Rightarrow> t == (s, t) \<in> Red"
       
   282 | "s \<Rightarrow> t == (s, t) \<in> Redl"
       
   283 | "s \<Rightarrow> t == (s, t) \<in> Redt"
       
   284 | "s \<Rightarrow>* t == (s, t) \<in> Red^*"
       
   285 | "s \<Rightarrow>* t == (s, t) \<in> Redt^*"
   297 (* ML *)
   286 (* ML *)
   298 "A_ML (Lam_ML u) [v] \<Rightarrow> u[v/0]"
   287 | "A_ML (Lam_ML u) [v] \<Rightarrow> u[v/0]"
   299 (* compiled rules *)
   288 (* compiled rules *)
   300 "(nm,vs,v) : compR ==> ALL i. ML_closed 0 (f i) \<Longrightarrow> A_ML (CC nm) (map (subst\<^bsub>ML\<^esub> f) vs) \<Rightarrow> subst\<^bsub>ML\<^esub> f v"
   289 | "(nm,vs,v) : compR ==> ALL i. ML_closed 0 (f i) \<Longrightarrow> A_ML (CC nm) (map (subst\<^bsub>ML\<^esub> f) vs) \<Rightarrow> subst\<^bsub>ML\<^esub> f v"
   301 (* apply *)
   290 (* apply *)
   302 apply_Fun1: "apply (Fun f vs (Suc 0)) v \<Rightarrow> A_ML f (vs @ [v])"
   291 | apply_Fun1: "apply (Fun f vs (Suc 0)) v \<Rightarrow> A_ML f (vs @ [v])"
   303 apply_Fun2: "n > 0 ==>
   292 | apply_Fun2: "n > 0 ==>
   304  apply (Fun f vs (Suc n)) v \<Rightarrow> Fun f (vs @ [v]) n"
   293  apply (Fun f vs (Suc n)) v \<Rightarrow> Fun f (vs @ [v]) n"
   305 apply_C: "apply (C nm vs) v \<Rightarrow> C nm (vs @ [v])"
   294 | apply_C: "apply (C nm vs) v \<Rightarrow> C nm (vs @ [v])"
   306 apply_V: "apply (V x vs) v \<Rightarrow> V x (vs @ [v])"
   295 | apply_V: "apply (V x vs) v \<Rightarrow> V x (vs @ [v])"
   307 (* term_of *)
   296 (* term_of *)
   308 term_of_C: "term_of (C nm vs) \<Rightarrow> foldl At (Ct nm) (map term_of vs)"
   297 | term_of_C: "term_of (C nm vs) \<Rightarrow> foldl At (Ct nm) (map term_of vs)"
   309 term_of_V: "term_of (V x vs) \<Rightarrow> foldl At (Vt x) (map term_of vs)"
   298 | term_of_V: "term_of (V x vs) \<Rightarrow> foldl At (Vt x) (map term_of vs)"
   310 term_of_Fun: "term_of(Fun vf vs n) \<Rightarrow>
   299 | term_of_Fun: "term_of(Fun vf vs n) \<Rightarrow>
   311  Lam (term_of ((apply (lift 0 (Fun vf vs n)) (V_ML 0))[V 0 []/0]))"
   300  Lam (term_of ((apply (lift 0 (Fun vf vs n)) (V_ML 0))[V 0 []/0]))"
   312 (* Context *)
   301 (* Context *)
   313 ctxt_Lam: "t \<Rightarrow> t' ==> Lam t \<Rightarrow> Lam t'"
   302 | ctxt_Lam: "t \<Rightarrow> t' ==> Lam t \<Rightarrow> Lam t'"
   314 ctxt_At1: "s \<Rightarrow> s' ==> At s t \<Rightarrow> At s' t"
   303 | ctxt_At1: "s \<Rightarrow> s' ==> At s t \<Rightarrow> At s' t"
   315 ctxt_At2: "t \<Rightarrow> t' ==> At s t \<Rightarrow> At s t'"
   304 | ctxt_At2: "t \<Rightarrow> t' ==> At s t \<Rightarrow> At s t'"
   316 ctxt_term_of: "v \<Rightarrow> v' ==> term_of v \<Rightarrow> term_of v'"
   305 | ctxt_term_of: "v \<Rightarrow> v' ==> term_of v \<Rightarrow> term_of v'"
   317 ctxt_C: "vs \<Rightarrow> vs' ==> C nm vs \<Rightarrow> C nm vs'"
   306 | ctxt_C: "vs \<Rightarrow> vs' ==> C nm vs \<Rightarrow> C nm vs'"
   318 ctxt_V: "vs \<Rightarrow> vs' ==> V x vs \<Rightarrow> V x vs'"
   307 | ctxt_V: "vs \<Rightarrow> vs' ==> V x vs \<Rightarrow> V x vs'"
   319 ctxt_Fun1: "f \<Rightarrow> f'   ==> Fun f vs n \<Rightarrow> Fun f' vs n"
   308 | ctxt_Fun1: "f \<Rightarrow> f'   ==> Fun f vs n \<Rightarrow> Fun f' vs n"
   320 ctxt_Fun3: "vs \<Rightarrow> vs' ==> Fun f vs n \<Rightarrow> Fun f vs' n"
   309 | ctxt_Fun3: "vs \<Rightarrow> vs' ==> Fun f vs n \<Rightarrow> Fun f vs' n"
   321 ctxt_apply1: "s \<Rightarrow> s'   ==> apply s t \<Rightarrow> apply s' t"
   310 | ctxt_apply1: "s \<Rightarrow> s'   ==> apply s t \<Rightarrow> apply s' t"
   322 ctxt_apply2: "t \<Rightarrow> t'   ==> apply s t \<Rightarrow> apply s t'"
   311 | ctxt_apply2: "t \<Rightarrow> t'   ==> apply s t \<Rightarrow> apply s t'"
   323 ctxt_A_ML1: "f \<Rightarrow> f'   ==> A_ML f vs \<Rightarrow> A_ML f' vs"
   312 | ctxt_A_ML1: "f \<Rightarrow> f'   ==> A_ML f vs \<Rightarrow> A_ML f' vs"
   324 ctxt_A_ML2: "vs \<Rightarrow> vs' ==> A_ML f vs \<Rightarrow> A_ML f vs'"
   313 | ctxt_A_ML2: "vs \<Rightarrow> vs' ==> A_ML f vs \<Rightarrow> A_ML f vs'"
   325 ctxt_list1: "v \<Rightarrow> v'   ==> v#vs \<Rightarrow> v'#vs"
   314 | ctxt_list1: "v \<Rightarrow> v'   ==> v#vs \<Rightarrow> v'#vs"
   326 ctxt_list2: "vs \<Rightarrow> vs' ==> v#vs \<Rightarrow> v#vs'"
   315 | ctxt_list2: "vs \<Rightarrow> vs' ==> v#vs \<Rightarrow> v#vs'"
   327 
   316 
   328 
   317 
   329 consts
   318 consts
   330  ar :: "const_name \<Rightarrow> nat"
   319  ar :: "const_name \<Rightarrow> nat"
   331 
   320 
   789   moreover have "ML_closed 0 (subst\<^bsub>ML\<^esub> f v)" (is "?C") using prems sorry
   778   moreover have "ML_closed 0 (subst\<^bsub>ML\<^esub> f v)" (is "?C") using prems sorry
   790   ultimately show "?A & ?C" ..
   779   ultimately show "?A & ?C" ..
   791 next
   780 next
   792   case term_of_V thus ?case apply (auto simp:map_compose[symmetric]) sorry
   781   case term_of_V thus ?case apply (auto simp:map_compose[symmetric]) sorry
   793 next
   782 next
   794   case (term_of_Fun n vf vs)
   783   case (term_of_Fun vf vs n)
   795   hence "term_of (Fun vf vs n)! \<rightarrow>*
   784   hence "term_of (Fun vf vs n)! \<rightarrow>*
   796        Lam (term_of (apply (lift 0 (Fun vf vs n)) (V_ML 0)[V 0 []/0]))!" sorry
   785        Lam (term_of (apply (lift 0 (Fun vf vs n)) (V_ML 0)[V 0 []/0]))!" sorry
   797   moreover
   786   moreover
   798   have "ML_closed_t 0
   787   have "ML_closed_t 0
   799         (Lam (term_of (apply (lift 0 (Fun vf vs n)) (V_ML 0)[V 0 []/0])))" sorry
   788         (Lam (term_of (apply (lift 0 (Fun vf vs n)) (V_ML 0)[V 0 []/0])))" sorry