src/HOL/Library/code_lazy.ML
changeset 80636 4041e7c8059d
parent 80634 a90ab1ea6458
child 82587 7415414bd9d8
equal deleted inserted replaced
80635:27d5452d20fc 80636:4041e7c8059d
   277       let
   277       let
   278         val (eager_case, caseT) = dest_Const casex
   278         val (eager_case, caseT) = dest_Const casex
   279         val (caseT', lthy') = substitute_ctr (to_destr_eagerT caseT, eagerT) caseT lthy3
   279         val (caseT', lthy') = substitute_ctr (to_destr_eagerT caseT, eagerT) caseT lthy3
   280       in (Const (eager_case, caseT'), lthy') end
   280       in (Const (eager_case, caseT'), lthy') end
   281 
   281 
   282     val ctr_names = map (Long_Name.base_name o fst o dest_Const) ctrs
   282     val ctr_names = map (Long_Name.base_name o dest_Const_name) ctrs
   283     val ((((lazy_ctr_name, lazy_sel_name), lazy_ctrs_name), lazy_case_name), ctxt) = lthy4
   283     val ((((lazy_ctr_name, lazy_sel_name), lazy_ctrs_name), lazy_case_name), ctxt) = lthy4
   284       |> mk_name "Lazy_" "" short_type_name
   284       |> mk_name "Lazy_" "" short_type_name
   285       ||>> mk_name "unlazy_" "" short_type_name
   285       ||>> mk_name "unlazy_" "" short_type_name
   286       ||>> fold_map (mk_name "" "_Lazy") ctr_names
   286       ||>> fold_map (mk_name "" "_Lazy") ctr_names
   287       ||>> mk_name "case_" "_lazy" short_type_name
   287       ||>> mk_name "case_" "_lazy" short_type_name
   318         mk_delay (Abs (Name.uu, \<^typ>\<open>unit\<close>, Abs_lazy $ Bound 1))))
   318         mk_delay (Abs (Name.uu, \<^typ>\<open>unit\<close>, Abs_lazy $ Bound 1))))
   319     val (lazy_sel_def, lthy6) = mk_def (lazy_sel_name, unstr_type, lazy_sel) lthy5
   319     val (lazy_sel_def, lthy6) = mk_def (lazy_sel_name, unstr_type, lazy_sel) lthy5
   320 
   320 
   321     fun mk_lazy_ctr (name, eager_ctr) =
   321     fun mk_lazy_ctr (name, eager_ctr) =
   322       let
   322       let
   323         val (_, ctrT) = dest_Const eager_ctr
   323         val ctrT = dest_Const_type eager_ctr
   324         fun to_lazy_ctrT (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) = T1 --> to_lazy_ctrT T2
   324         fun to_lazy_ctrT (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) = T1 --> to_lazy_ctrT T2
   325           | to_lazy_ctrT T = if T = eagerT then lazy_type else raise Match
   325           | to_lazy_ctrT T = if T = eagerT then lazy_type else raise Match
   326         val lazy_ctrT = to_lazy_ctrT ctrT
   326         val lazy_ctrT = to_lazy_ctrT ctrT
   327         val argsT = binder_types ctrT
   327         val argsT = binder_types ctrT
   328         val lhs = apply_bounds 0 (length argsT) (Free (name, lazy_ctrT))
   328         val lhs = apply_bounds 0 (length argsT) (Free (name, lazy_ctrT))
   332       end
   332       end
   333     val (lazy_ctrs_def, lthy7) = fold_map mk_lazy_ctr (lazy_ctrs_name ~~ ctrs') lthy6
   333     val (lazy_ctrs_def, lthy7) = fold_map mk_lazy_ctr (lazy_ctrs_name ~~ ctrs') lthy6
   334 
   334 
   335     val (lazy_case_def, lthy8) =
   335     val (lazy_case_def, lthy8) =
   336       let
   336       let
   337         val (_, caseT) = dest_Const case'
   337         val caseT = dest_Const_type case'
   338         fun to_lazy_caseT (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) =
   338         fun to_lazy_caseT (Type (\<^type_name>\<open>fun\<close>, [T1, T2])) =
   339             if T1 = eagerT then lazy_type --> T2 else T1 --> to_lazy_caseT T2
   339             if T1 = eagerT then lazy_type --> T2 else T1 --> to_lazy_caseT T2
   340         val lazy_caseT = to_lazy_caseT caseT
   340         val lazy_caseT = to_lazy_caseT caseT
   341         val argsT = binder_types lazy_caseT
   341         val argsT = binder_types lazy_caseT
   342         val n = length argsT
   342         val n = length argsT
   383       ((Lazy_delay_eq_name, mk_Lazy_delay_eq), [#thm lazy_ctr_def, @{thm force_delay}])
   383       ((Lazy_delay_eq_name, mk_Lazy_delay_eq), [#thm lazy_ctr_def, @{thm force_delay}])
   384       lthy8
   384       lthy8
   385 
   385 
   386     fun mk_lazy_ctr_eq (eager_ctr, lazy_ctr) =
   386     fun mk_lazy_ctr_eq (eager_ctr, lazy_ctr) =
   387       let
   387       let
   388         val (_, ctrT) = dest_Const eager_ctr
   388         val ctrT = dest_Const_type eager_ctr
   389         val argsT = binder_types ctrT
   389         val argsT = binder_types ctrT
   390         val args = length argsT
   390         val args = length argsT
   391       in
   391       in
   392         (Rep_lazy $ apply_bounds 0 args lazy_ctr, apply_bounds 0 args eager_ctr)
   392         (Rep_lazy $ apply_bounds 0 args lazy_ctr, apply_bounds 0 args eager_ctr)
   393         |> mk_eq |> all_abs argsT
   393         |> mk_eq |> all_abs argsT
   399         )
   399         )
   400         lthy8a
   400         lthy8a
   401 
   401 
   402     fun mk_ctrs_lazy_eq (eager_ctr, lazy_ctr) =
   402     fun mk_ctrs_lazy_eq (eager_ctr, lazy_ctr) =
   403       let
   403       let
   404         val argsT = dest_Const eager_ctr |> snd |> binder_types
   404         val argsT =  binder_types (dest_Const_type eager_ctr)
   405         val n = length argsT
   405         val n = length argsT
   406         val lhs = apply_bounds 0 n eager_ctr
   406         val lhs = apply_bounds 0 n eager_ctr
   407         val rhs = #const lazy_ctr_def $ 
   407         val rhs = #const lazy_ctr_def $ 
   408           (mk_delay (Abs (Name.uu, \<^typ>\<open>unit\<close>, apply_bounds 1 (n + 1) lazy_ctr)))
   408           (mk_delay (Abs (Name.uu, \<^typ>\<open>unit\<close>, apply_bounds 1 (n + 1) lazy_ctr)))
   409       in
   409       in
   421         ((force_sel_name, force_sel_eq), [#thm lazy_sel_def, @{thm force_delay}])
   421         ((force_sel_name, force_sel_eq), [#thm lazy_sel_def, @{thm force_delay}])
   422         lthy8c
   422         lthy8c
   423 
   423 
   424     val case_lazy_eq = 
   424     val case_lazy_eq = 
   425       let
   425       let
   426         val (_, caseT) = case' |> dest_Const
   426         val caseT = dest_Const_type case'
   427         val argsT = binder_types caseT
   427         val argsT = binder_types caseT
   428         val n = length argsT
   428         val n = length argsT
   429         val lhs = apply_bounds 0 n case'
   429         val lhs = apply_bounds 0 n case'
   430         val rhs = apply_bounds 1 n (#const lazy_case_def) $ (mk_force (#const lazy_sel_def $ Bound 0))
   430         val rhs = apply_bounds 1 n (#const lazy_case_def) $ (mk_force (#const lazy_sel_def $ Bound 0))
   431       in
   431       in
   445       lthy8e
   445       lthy8e
   446 
   446 
   447     fun mk_case_ctrs_eq (i, lazy_ctr) =
   447     fun mk_case_ctrs_eq (i, lazy_ctr) =
   448       let
   448       let
   449         val lazy_case = #const lazy_case_def
   449         val lazy_case = #const lazy_case_def
   450         val (_, ctrT) = dest_Const lazy_ctr
   450         val ctrT = dest_Const_type lazy_ctr
   451         val ctr_argsT = binder_types ctrT
   451         val ctr_argsT = binder_types ctrT
   452         val ctr_args_n = length ctr_argsT
   452         val ctr_args_n = length ctr_argsT
   453         val (_, caseT) = dest_Const lazy_case
   453         val caseT = dest_Const_type lazy_case
   454         val case_argsT = binder_types caseT
   454         val case_argsT = binder_types caseT
   455 
   455 
   456         fun n_bounds_from m n t =
   456         fun n_bounds_from m n t =
   457           if n > 0 then n_bounds_from (m - 1) (n - 1) (t $ Bound (m - 1)) else t
   457           if n > 0 then n_bounds_from (m - 1) (n - 1) (t $ Bound (m - 1)) else t
   458 
   458 
   535         let 
   535         let 
   536           val thy = Proof_Context.theory_of ctxt
   536           val thy = Proof_Context.theory_of ctxt
   537           val table = Laziness_Data.get thy
   537           val table = Laziness_Data.get thy
   538         in fn (s1, s2) => case Symtab.lookup table s1 of
   538         in fn (s1, s2) => case Symtab.lookup table s1 of
   539             NONE => false
   539             NONE => false
   540           | SOME x => #active x andalso s2 <> (#ctr x |> dest_Const |> fst)
   540           | SOME x => #active x andalso s2 <> dest_Const_name (#ctr x)
   541         end
   541         end
   542       val thy = Proof_Context.theory_of ctxt
   542       val thy = Proof_Context.theory_of ctxt
   543       val table = Laziness_Data.get thy
   543       val table = Laziness_Data.get thy
   544       fun num_consts_fun (_, T) =
   544       fun num_consts_fun (_, T) =
   545         let
   545         let
   575 fun print_lazy_type thy (name, info : lazy_info) = 
   575 fun print_lazy_type thy (name, info : lazy_info) = 
   576   let
   576   let
   577     val ctxt = Proof_Context.init_global thy
   577     val ctxt = Proof_Context.init_global thy
   578     fun pretty_ctr ctr = 
   578     fun pretty_ctr ctr = 
   579       let
   579       let
   580         val argsT = dest_Const ctr |> snd |> binder_types
   580         val argsT = binder_types (dest_Const_type ctr)
   581       in
   581       in
   582         Pretty.block [
   582         Pretty.block [
   583           Syntax.pretty_term ctxt ctr,
   583           Syntax.pretty_term ctxt ctr,
   584           Pretty.brk 1,
   584           Pretty.brk 1,
   585           Pretty.block (Pretty.separate "" (map (Pretty.quote o Syntax.pretty_typ ctxt) argsT))
   585           Pretty.block (Pretty.separate "" (map (Pretty.quote o Syntax.pretty_typ ctxt) argsT))