src/Tools/isac/Interpret/derive.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 04 May 2020 11:13:16 +0200
changeset 59934 955d6fa8bb9b
parent 59932 87336f3b021f
child 59970 ab1c25c0339a
permissions -rw-r--r--
cleanup struct.Derive
     1 (* Title:  Interpret/derive.sml
     2    Author: Walther Neuper
     3    (c) due to copyright terms
     4 
     5 Try to make (term * rule * result) steps (= derivation) by use of a Rule_Set.T.
     6 Two purposes:
     7 (1) derive steps from a given term towards another give term
     8 (2) term transformations, which cannot be done by rewriting, e.g cancellation of polynomials.
     9 *)
    10 
    11 signature DERIVE =
    12 sig
    13   type rule_result
    14   type step
    15   type derivation
    16 
    17   val do_one : theory -> Rule_Set.T -> Rule.rule list -> Rule_Def.rew_ord_ ->
    18     term option -> term -> derivation
    19   val steps_reverse : theory -> Rule_Set.T -> Rule.rule list -> Rule_Def.rew_ord_ ->
    20     term option -> term -> rule_result list
    21   val steps : Rule_Def.rew_ord -> Rule_Set.T -> Rule.rule list -> term -> term ->
    22     bool * derivation
    23   val embed: State_Steps.T -> Calc.T -> Pos.pos' list * Calc.T
    24 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    25   (*  NONE *)
    26 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    27   val trtas2str : (term * Rule.rule * (term * term list)) list -> string
    28   val deriv2str : (term * Rule.rule * (term * term list)) list -> string
    29   val rev_deriv' : 'a * Rule.rule * ('b * 'c) -> 'b * Rule.rule * ('a * 'c)
    30 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    31 end
    32 
    33 (**)
    34 structure Derive(**): DERIVE(**) =
    35 struct
    36 (**)
    37 
    38 (** the triple for a step **)
    39 
    40 type rule_result = Rule.rule * Calc.result;
    41 type step = term * Rule.rule * Calc.result;
    42 type derivation = step list;
    43 
    44 fun trta2str (t, r, (t', a)) =
    45   "\n(" ^ UnparseC.term t ^ ", " ^ Rule.to_string_short r ^ ", (" ^ UnparseC.term t' ^ ", " ^ UnparseC.terms a ^ "))"
    46 fun trtas2str trtas = (strs2str o (map trta2str)) trtas
    47 val deriv2str = trtas2str
    48 
    49 
    50 (** make one triple towards the goal term **)
    51 
    52 fun msg_1 rts =
    53   (tracing ("do_one exceeds " ^ int2str (! Rewrite.lim_deriv) ^ "with derivation =\n");
    54    tracing (deriv2str rts));
    55 fun msg_2 thmid =
    56   if not (! Rewrite.trace_on) then () else tracing ("### trying thm \"" ^ thmid ^ "\"");
    57 fun msg_3 t' =
    58   if ! Rewrite.trace_on then tracing ("=== rewrites to: " ^ UnparseC.term t') else ();
    59 fun msg_4 op_ =
    60   if not (! Rewrite.trace_on) then () else tracing ("### trying calc. \"" ^ op_^"\"");
    61 fun msg_5 t' =
    62   if not (! Rewrite.trace_on) then () else tracing("=== calc. to: " ^ UnparseC.term t')
    63 
    64 
    65 fun do_one thy erls rs ro goal tt = 
    66   let 
    67     datatype switch = Appl | Noap (* TODO: unify with version in Rewrite *)
    68     fun rew_once _ rts t Noap [] = 
    69         (case goal of NONE => rts | SOME _ =>
    70           raise ERROR ("Derive.do_one: no derivation for " ^ UnparseC.term t))
    71       | rew_once lim rts t Appl [] = rew_once lim rts t Noap rs
    72       | rew_once lim rts t apno rs' =
    73         (case goal of 
    74           NONE => rew_or_calc lim rts t apno rs'
    75         | SOME g => if g = t then rts else rew_or_calc lim rts t apno rs')
    76     and rew_or_calc lim rts t apno (rrs' as (r :: rs')) =
    77       if lim < 0 
    78       then (msg_1 rts; rts)
    79       else
    80         (case r of
    81           Rule.Thm (thmid, tm) => 
    82             (msg_2 thmid;
    83             case Rewrite.rewrite_ thy ro erls true tm t of
    84               NONE => rew_once lim rts t apno rs'
    85             | SOME (t', a') =>
    86               (msg_3 t'; rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs'))
    87         | Rule.Eval (c as (op_, _)) => 
    88             (msg_4 op_;
    89             case Eval.adhoc_thm thy c (TermC.uminus_to_string t) of
    90               NONE => rew_once lim rts t apno rs'
    91             | SOME (thmid, tm) => 
    92               (let
    93                 val (t', a') = case Rewrite.rewrite_ thy ro erls true tm t of
    94                   SOME ta => ta
    95                 | NONE => raise ERROR "adhoc_thm: NONE"
    96                 val _ = msg_5 t'
    97                 val r' = Rule.Thm (thmid, tm)
    98               in rew_once (lim - 1) (rts @ [(t, r', (t', a'))]) t' Appl rrs' end) 
    99                 handle _ => raise ERROR "derive_norm, Eval: no rewrite")
   100         | Rule.Rls_ rls =>
   101           (case Rewrite.rewrite_set_ thy true rls t of
   102             NONE => rew_once lim rts t apno rs'
   103           | SOME (t', a') => rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs')
   104         | rule => raise ERROR ("rew_once: uncovered case " ^ Rule.to_string rule))
   105     | rew_or_calc _ _ _ _ [] = raise ERROR "rew_or_calc: called with []"
   106   in rew_once (! Rewrite.lim_deriv) [] tt Noap rs end
   107 
   108 
   109 (** concatenate several steps in revers order **)
   110 
   111 fun rev_deriv (t, r, (_, a)) = (ThmC.make_sym_rule r, (t, a));
   112 fun steps_reverse thy erls rs ro goal t =
   113     (rev o (map rev_deriv)) (do_one thy erls rs ro goal t)
   114 
   115 
   116 (** concatenate several steps **)
   117 
   118 fun rev_deriv' (t, r, (t', a)) = (t', ThmC.make_sym_rule r, (t, a));
   119 
   120 (* fo = ifo excluded already in inform *)
   121 fun steps rew_ord erls rules fo ifo =
   122   let 
   123     fun derivat ([]:(term * Rule.rule * (term * term list)) list) = TermC.empty
   124       | derivat dt = (#1 o #3 o last_elem) dt
   125     fun equal (_, _, (t1, _)) (_, _, (t2, _)) = t1 = t2
   126     val  fod = do_one (ThyC.Isac()) erls rules (snd rew_ord) NONE  fo
   127     val ifod = do_one (ThyC.Isac()) erls rules (snd rew_ord) NONE ifo
   128   in 
   129     case (fod, ifod) of
   130       ([], []) => if fo = ifo then (true, []) else (false, [])
   131     | (fod, []) => if derivat fod = ifo then (true, fod) (*ifo is normal form*) else (false, [])
   132     | ([], ifod) => if fo = derivat ifod then (true, ((map rev_deriv') o rev) ifod) else (false, [])
   133     | (fod, ifod) =>
   134       if derivat fod = derivat ifod (*common normal form found*) then
   135         let 
   136           val (fod', rifod') = dropwhile' equal (rev fod) (rev ifod)
   137         in (true, fod' @ (map rev_deriv' rifod')) end
   138       else (false, [])
   139   end
   140 
   141 (** embed a derivation into the Ctree **)
   142 
   143 fun embed tacis (pt, pos as (p, Pos.Frm)) =
   144   (*inform at Frm: replace the whole PrfObj by a Transitive-ProfObj FIXME?0402
   145     and transfer the istate (from _after_ compare_deriv) from Frm to Res*)
   146     let
   147       val (res, asm) = (State_Steps.result o last_elem) tacis
   148     	val (ist, ctxt) = case Ctree.get_obj Ctree.g_loc pt p of
   149     	  (SOME (ist, ctxt), _) => (ist, ctxt)
   150       | (NONE, _) => raise ERROR "Derive.embed Frm: uncovered case get_obj"
   151     	val form =  Ctree.get_obj  Ctree.g_form pt p
   152       (*val p = lev_on p; ---------------only difference to (..,Res) below*)
   153     	val tacis = (Tactic.Begin_Trans, Tactic.Begin_Trans' form, (pos, (Istate_Def.Uistate, ctxt))) ::
   154     		(State_Steps.insert_pos ((Pos.lev_on o Pos.lev_dn) p) tacis) @ [(Tactic.End_Trans, Tactic.End_Trans' (res, asm),
   155     			(Pos.pos_plus (length tacis) (Pos.lev_dn p, Pos.Res), (Ctree.new_val res ist, ctxt)))]
   156     	val {nrls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt (Ctree.par_pblobj pt p))
   157     	val (pt, c, pos as (p, _)) = Solve_Step.s_add_general (rev tacis) (pt, [], (p, Pos.Res))
   158     	val pt = Ctree.update_tac pt p (Tactic.Derive (Rule_Set.id nrls))
   159     	val pt = Ctree.update_branch pt p Ctree.TransitiveB
   160     in (c, (pt, pos)) end
   161   | embed tacis (pt, (p, Pos.Res)) =
   162     (*inform at Res: append a Transitive-PrfObj FIXME?0402 other branch-types ?
   163       and transfer the istate (from _after_ compare_deriv) from Res to new Res*)
   164     let
   165       val (res, asm) = (State_Steps.result o last_elem) tacis
   166     	val (ist, ctxt) = case Ctree.get_obj Ctree.g_loc pt p of
   167     	  (_, SOME (ist, ctxt)) => (ist, ctxt)
   168       | (_, NONE) => raise ERROR "Derive.embed Frm: uncovered case get_obj"
   169     	val (f, _) = Ctree.get_obj Ctree.g_result pt p
   170     	val p = Pos.lev_on p(*---------------only difference to (..,Frm) above*);
   171     	val tacis = (Tactic.Begin_Trans, Tactic.Begin_Trans' f, ((p, Pos.Frm), (Istate_Def.Uistate, ctxt))) ::
   172     		(State_Steps.insert_pos ((Pos.lev_on o Pos.lev_dn) p) tacis) @ [(Tactic.End_Trans, Tactic.End_Trans' (res, asm), 
   173     			(Pos.pos_plus (length tacis) (Pos.lev_dn p, Pos.Res), (Ctree.new_val res ist, ctxt)))];
   174     	val {nrls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt (Ctree.par_pblobj pt p))
   175     	val (pt, c, pos as (p, _)) = Solve_Step.s_add_general (rev tacis) (pt, [], (p, Pos.Res))
   176     	val pt = Ctree.update_tac pt p (Tactic.Derive (Rule_Set.id nrls))
   177     	val pt = Ctree.update_branch pt p Ctree.TransitiveB
   178     in (c, (pt, pos)) end
   179   | embed _ _ = raise ERROR "Derive.embed: uncovered definition"
   180 
   181 (**)end(**)