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