src/Tools/isac/Interpret/rewtools.sml
changeset 59878 3163e63a5111
parent 59876 eff0b9fc6caa
child 59879 33449c96d99f
equal deleted inserted replaced
59877:e5a83a9fe58d 59878:3163e63a5111
   146             case Rewrite.rewrite_ thy ro erls true tm t of
   146             case Rewrite.rewrite_ thy ro erls true tm t of
   147               NONE => rew_once lim rts t apno rs'
   147               NONE => rew_once lim rts t apno rs'
   148             | SOME (t', a') =>
   148             | SOME (t', a') =>
   149               (if ! Celem.trace_rewrite then tracing ("=== rewrites to: " ^ UnparseC.term t') else ();
   149               (if ! Celem.trace_rewrite then tracing ("=== rewrites to: " ^ UnparseC.term t') else ();
   150               rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs'))
   150               rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs'))
   151         | Rule.Num_Calc (c as (op_, _)) => 
   151         | Rule.Eval (c as (op_, _)) => 
   152           let 
   152           let 
   153             val _ = if not (! Celem.trace_rewrite) then () else tracing ("### trying calc. \"" ^ op_^"\"")
   153             val _ = if not (! Celem.trace_rewrite) then () else tracing ("### trying calc. \"" ^ op_^"\"")
   154             val t = TermC.uminus_to_string t
   154             val t = TermC.uminus_to_string t
   155           in 
   155           in 
   156             case Num_Calc.adhoc_thm thy c t of
   156             case Eval.adhoc_thm thy c t of
   157               NONE => rew_once lim rts t apno rs'
   157               NONE => rew_once lim rts t apno rs'
   158             | SOME (thmid, tm) => 
   158             | SOME (thmid, tm) => 
   159               (let
   159               (let
   160                 val (t', a') = case Rewrite.rewrite_ thy ro erls true tm t of
   160                 val (t', a') = case Rewrite.rewrite_ thy ro erls true tm t of
   161                   SOME ta => ta
   161                   SOME ta => ta
   162                 | NONE => error "adhoc_thm: NONE"
   162                 | NONE => error "adhoc_thm: NONE"
   163                 val _ = if not (! Celem.trace_rewrite) then () else tracing("=== calc. to: " ^ UnparseC.term t')
   163                 val _ = if not (! Celem.trace_rewrite) then () else tracing("=== calc. to: " ^ UnparseC.term t')
   164                 val r' = Rule.Thm (thmid, tm)
   164                 val r' = Rule.Thm (thmid, tm)
   165               in rew_once (lim - 1) (rts @ [(t, r', (t', a'))]) t' Appl rrs' end) 
   165               in rew_once (lim - 1) (rts @ [(t, r', (t', a'))]) t' Appl rrs' end) 
   166                 handle _ => error "derive_norm, Num_Calc: no rewrite"
   166                 handle _ => error "derive_norm, Eval: no rewrite"
   167           end
   167           end
   168       (*| Cal1 (cc as (op_,_)) => ... WN080222 see rewrite__set_: see 7df94616c1bd and earlier*)
   168       (*| Cal1 (cc as (op_,_)) => ... WN080222 see rewrite__set_: see 7df94616c1bd and earlier*)
   169         | Rule.Rls_ rls =>           (* WN060829: CREATES "sym_rlsID", see 7df94616c1bd and earlier*)
   169         | Rule.Rls_ rls =>           (* WN060829: CREATES "sym_rlsID", see 7df94616c1bd and earlier*)
   170           (case Rewrite.rewrite_set_ thy true rls t of
   170           (case Rewrite.rewrite_set_ thy true rls t of
   171             NONE => rew_once lim rts t apno rs'
   171             NONE => rew_once lim rts t apno rs'
   325     in
   325     in
   326 	  (case Applicable.applicable_in pos pt tac of
   326 	  (case Applicable.applicable_in pos pt tac of
   327 	     Applicable.Appl (Tactic.Rewrite_Inst' (thy', ord', erls, _, subst, _, f, (res, asm))) =>
   327 	     Applicable.Appl (Tactic.Rewrite_Inst' (thy', ord', erls, _, subst, _, f, (res, asm))) =>
   328 	       let
   328 	       let
   329            val thm_deriv = Thm.get_name_hint thm
   329            val thm_deriv = Thm.get_name_hint thm
   330            val thminst = TermC.inst_bdv subst ((Num_Calc.norm o #prop o Thm.rep_thm) thm)
   330            val thminst = TermC.inst_bdv subst ((Eval.norm o #prop o Thm.rep_thm) thm)
   331 	       in
   331 	       in
   332 	         ContThmInst
   332 	         ContThmInst
   333 	          {thyID = ThyC.theory'2thyID thy',
   333 	          {thyID = ThyC.theory'2thyID thy',
   334 	           thm = 
   334 	           thm = 
   335 	             Celem.thm2guh (thy_containing_thm thm_deriv) (ThmC.cut_id thm_deriv),
   335 	             Celem.thm2guh (thy_containing_thm thm_deriv) (ThmC.cut_id thm_deriv),
   342            val thm = Global_Theory.get_thm (ThyC.Isac ()(*WN141021 assoc_thy thy' ERROR*)) thmID
   342            val thm = Global_Theory.get_thm (ThyC.Isac ()(*WN141021 assoc_thy thy' ERROR*)) thmID
   343            val thm_deriv = Thm.get_name_hint thm
   343            val thm_deriv = Thm.get_name_hint thm
   344            val pp = Ctree.par_pblobj pt p
   344            val pp = Ctree.par_pblobj pt p
   345            val thy' = Ctree.get_obj Ctree.g_domID pt pp
   345            val thy' = Ctree.get_obj Ctree.g_domID pt pp
   346            val subst = Selem.subs2subst (Celem.assoc_thy thy') subs
   346            val subst = Selem.subs2subst (Celem.assoc_thy thy') subs
   347            val thminst = TermC.inst_bdv subst ((Num_Calc.norm o #prop o Thm.rep_thm) thm)
   347            val thminst = TermC.inst_bdv subst ((Eval.norm o #prop o Thm.rep_thm) thm)
   348            val f = case p_ of
   348            val f = case p_ of
   349                Pos.Frm => Ctree.get_obj Ctree.g_form pt p
   349                Pos.Frm => Ctree.get_obj Ctree.g_form pt p
   350              | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p 
   350              | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p 
   351              | _ => error "context_thy: uncovered case 3"
   351              | _ => error "context_thy: uncovered case 3"
   352          in
   352          in
   377       error ("context_thy: not for tac " ^ Tactic.input_to_string tac ^ " at " ^ Pos.pos'2str p)
   377       error ("context_thy: not for tac " ^ Tactic.input_to_string tac ^ " at " ^ Pos.pos'2str p)
   378 
   378 
   379 (* get all theorems in a rule set (recursivley containing rule sets) *)
   379 (* get all theorems in a rule set (recursivley containing rule sets) *)
   380 fun thm_of_rule Rule.Erule = []
   380 fun thm_of_rule Rule.Erule = []
   381   | thm_of_rule (thm as Rule.Thm _) = [thm]
   381   | thm_of_rule (thm as Rule.Thm _) = [thm]
   382   | thm_of_rule (Rule.Num_Calc _) = []
   382   | thm_of_rule (Rule.Eval _) = []
   383   | thm_of_rule (Rule.Cal1 _) = []
   383   | thm_of_rule (Rule.Cal1 _) = []
   384   | thm_of_rule (Rule.Rls_ rls) = thms_of_rls rls
   384   | thm_of_rule (Rule.Rls_ rls) = thms_of_rls rls
   385 and thms_of_rls Rule_Set.Empty = []
   385 and thms_of_rls Rule_Set.Empty = []
   386   | thms_of_rls (Rule_Def.Repeat {rules,...}) = (flat o (map  thm_of_rule)) rules
   386   | thms_of_rls (Rule_Def.Repeat {rules,...}) = (flat o (map  thm_of_rule)) rules
   387   | thms_of_rls (Rule_Set.Seqence {rules,...}) = (flat o (map  thm_of_rule)) rules
   387   | thms_of_rls (Rule_Set.Sequence {rules,...}) = (flat o (map  thm_of_rule)) rules
   388   | thms_of_rls (Rule_Set.Rrls _) = []
   388   | thms_of_rls (Rule_Set.Rrls _) = []
   389 
   389 
   390 (* check if a rule is contained in a rule-set (recursivley down in Rls_);
   390 (* check if a rule is contained in a rule-set (recursivley down in Rls_);
   391    this rule can even be a rule-set itself                             *)
   391    this rule can even be a rule-set itself                             *)
   392 fun contains_rule r rls = 
   392 fun contains_rule r rls = 
   407 	    SOME _ => [Tactic.rule2tac thy subst thm']
   407 	    SOME _ => [Tactic.rule2tac thy subst thm']
   408 	  | NONE => []
   408 	  | NONE => []
   409     else (case Rewrite.rewrite_ thy ro erls false thm f of
   409     else (case Rewrite.rewrite_ thy ro erls false thm f of
   410 	    SOME _ => [Tactic.rule2tac thy [] thm']
   410 	    SOME _ => [Tactic.rule2tac thy [] thm']
   411 	  | NONE => [])
   411 	  | NONE => [])
   412   | try_rew thy _ _ _ f (cal as Rule.Num_Calc c) = 
   412   | try_rew thy _ _ _ f (cal as Rule.Eval c) = 
   413     (case Num_Calc.adhoc_thm thy c f of
   413     (case Eval.adhoc_thm thy c f of
   414 	    SOME _ => [Tactic.rule2tac thy [] cal]
   414 	    SOME _ => [Tactic.rule2tac thy [] cal]
   415     | NONE => [])
   415     | NONE => [])
   416   | try_rew thy _ _ _ f (cal as Rule.Cal1 c) = 
   416   | try_rew thy _ _ _ f (cal as Rule.Cal1 c) = 
   417     (case Num_Calc.adhoc_thm thy c f of
   417     (case Eval.adhoc_thm thy c f of
   418 	      SOME _ => [Tactic.rule2tac thy [] cal]
   418 	      SOME _ => [Tactic.rule2tac thy [] cal]
   419       | NONE => [])
   419       | NONE => [])
   420   | try_rew thy _ _ subst f (Rule.Rls_ rls) = filter_appl_rews thy subst f rls
   420   | try_rew thy _ _ subst f (Rule.Rls_ rls) = filter_appl_rews thy subst f rls
   421   | try_rew _ _ _ _ _ _ = error "try_rew: uncovered case"
   421   | try_rew _ _ _ _ _ _ = error "try_rew: uncovered case"
   422 and filter_appl_rews thy subst f (Rule_Def.Repeat {rew_ord = ro, erls, rules, ...}) = 
   422 and filter_appl_rews thy subst f (Rule_Def.Repeat {rew_ord = ro, erls, rules, ...}) = 
   423     gen_distinct Tactic.eq_tac (flat (map (try_rew thy ro erls subst f) rules))
   423     gen_distinct Tactic.eq_tac (flat (map (try_rew thy ro erls subst f) rules))
   424   | filter_appl_rews thy subst f (Rule_Set.Seqence {rew_ord = ro, erls, rules,...}) = 
   424   | filter_appl_rews thy subst f (Rule_Set.Sequence {rew_ord = ro, erls, rules,...}) = 
   425     gen_distinct Tactic.eq_tac (flat (map (try_rew thy ro erls subst f) rules))
   425     gen_distinct Tactic.eq_tac (flat (map (try_rew thy ro erls subst f) rules))
   426   | filter_appl_rews _ _ _ (Rule_Set.Rrls _) = []
   426   | filter_appl_rews _ _ _ (Rule_Set.Rrls _) = []
   427   | filter_appl_rews _ _ _ _ = error "filter_appl_rews: uncovered case"
   427   | filter_appl_rews _ _ _ _ = error "filter_appl_rews: uncovered case"
   428 
   428 
   429 (* decide if a tactic is applicable to a given formula; 
   429 (* decide if a tactic is applicable to a given formula; 
   430    in case of Rewrite_Set* go down to _atomic_ rewrite-tactics *)
   430    in case of Rewrite_Set* go down to _atomic_ rewrite-tactics *)
   431 fun atomic_appl_tacs thy _ _ f (Tactic.Calculate scrID) =
   431 fun atomic_appl_tacs thy _ _ f (Tactic.Calculate scrID) =
   432     try_rew thy Rewrite_Ord.e_rew_ordX Rule_Set.empty [] f (Rule.Num_Calc (assoc_calc' thy scrID |> snd))
   432     try_rew thy Rewrite_Ord.e_rew_ordX Rule_Set.empty [] f (Rule.Eval (assoc_calc' thy scrID |> snd))
   433   | atomic_appl_tacs thy ro erls f (Tactic.Rewrite thm'') =
   433   | atomic_appl_tacs thy ro erls f (Tactic.Rewrite thm'') =
   434     try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls [] f (Rule.Thm thm'')
   434     try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls [] f (Rule.Thm thm'')
   435   | atomic_appl_tacs thy ro erls f (Tactic.Rewrite_Inst (subs, thm'')) =
   435   | atomic_appl_tacs thy ro erls f (Tactic.Rewrite_Inst (subs, thm'')) =
   436     try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls (Selem.subs2subst thy subs) f (Rule.Thm thm'')
   436     try_rew thy (ro, Rewrite_Ord.assoc_rew_ord ro) erls (Selem.subs2subst thy subs) f (Rule.Thm thm'')
   437 
   437