src/Tools/isac/Interpret/derive.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 23 Apr 2020 12:34:54 +0200
changeset 59907 4c62e16e842e
parent 59906 cc8df204dcb6
child 59932 87336f3b021f
permissions -rw-r--r--
use "Derive" for renaming identifiers
walther@59906
     1
(* Title:  Interpret/derive.sml
walther@59906
     2
   Author: Walther Neuper 2019
walther@59906
     3
   (c) due to copyright terms
walther@59906
     4
walther@59907
     5
Derive makes (term * rule * result) steps (= derivation) for term transformations,
walther@59907
     6
which cannot be done by rewriting, e.g cancellation of polynomials.
walther@59906
     7
*)
walther@59906
     8
walther@59907
     9
signature DERIVE =
walther@59906
    10
sig
walther@59907
    11
  (*TODO cleanup signature*)
walther@59907
    12
  type der
walther@59907
    13
  type deri
walther@59907
    14
  type derivation
walther@59907
    15
(*val make_deriv *)
walther@59907
    16
  val do_one : theory -> Rule_Set.T -> Rule.rule list -> Rule_Def.rew_ord_ ->
walther@59907
    17
    term option -> term -> derivation
walther@59907
    18
(*val reverse_deriv *)
walther@59907
    19
  val steps_reverse : theory -> Rule_Set.T -> Rule.rule list -> Rule_Def.rew_ord_ ->
walther@59907
    20
    term option -> term -> deri list
walther@59907
    21
(*val concat_deriv *)
walther@59907
    22
  val steps : Rule_Def.rew_ord -> Rule_Set.T -> Rule.rule list -> term -> term ->
walther@59907
    23
    bool * der list
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@59907
    40
type der =        (* derivation for inserting one level of nodes into the Ctree *) 
walther@59906
    41
  ( term *        (* where the rule is applied to                               *)
walther@59906
    42
    Rule.rule *   (* rule to be applied                                         *)
walther@59906
    43
    ( term *      (* resulting from rule application                            *)
walther@59907
    44
      term list));(* assumptions resulting from rule application                *)
walther@59907
    45
type deri = Rule.rule * (term * term list)
walther@59907
    46
type derivation = der list
walther@59906
    47
walther@59906
    48
fun trta2str (t, r, (t', a)) =
walther@59906
    49
  "\n(" ^ UnparseC.term t ^ ", " ^ Rule.to_string_short r ^ ", (" ^ UnparseC.term t' ^ ", " ^ UnparseC.terms a ^ "))"
walther@59906
    50
fun trtas2str trtas = (strs2str o (map trta2str)) trtas
walther@59906
    51
val deriv2str = trtas2str
walther@59906
    52
walther@59907
    53
(** make one triple towards the goal term **)
walther@59906
    54
walther@59907
    55
fun msg_1 rts =
walther@59907
    56
  (tracing ("do_one exceeds " ^ int2str (! Rewrite.lim_deriv) ^ "with derivation =\n");
walther@59907
    57
   tracing (deriv2str rts));
walther@59907
    58
fun msg_2 thmid =
walther@59907
    59
  if not (! Rewrite.trace_on) then () else tracing ("### trying thm \"" ^ thmid ^ "\"");
walther@59907
    60
fun msg_3 t' =
walther@59907
    61
  if ! Rewrite.trace_on then tracing ("=== rewrites to: " ^ UnparseC.term t') else ();
walther@59907
    62
fun msg_4 op_ =
walther@59907
    63
  if not (! Rewrite.trace_on) then () else tracing ("### trying calc. \"" ^ op_^"\"");
walther@59907
    64
fun msg_5 t' =
walther@59907
    65
  if not (! Rewrite.trace_on) then () else tracing("=== calc. to: " ^ UnparseC.term t')
walther@59906
    66
walther@59907
    67
fun do_one thy erls rs ro goal tt = 
walther@59906
    68
  let 
walther@59907
    69
    datatype switch = Appl | Noap (* TODO: unify with version in Rewrite *)
walther@59906
    70
    fun rew_once _ rts t Noap [] = 
walther@59907
    71
        (case goal of NONE => rts | SOME _ =>
walther@59907
    72
          raise ERROR ("do_one: no derivation for " ^ UnparseC.term t))
walther@59906
    73
      | rew_once lim rts t Appl [] = rew_once lim rts t Noap rs
walther@59906
    74
        (*| Seq _ => rts) FIXXXXXME 14.3.03*)
walther@59906
    75
      | rew_once lim rts t apno rs' =
walther@59906
    76
        (case goal of 
walther@59906
    77
          NONE => rew_or_calc lim rts t apno rs'
walther@59906
    78
        | SOME g => if g = t then rts else rew_or_calc lim rts t apno rs')
walther@59906
    79
    and rew_or_calc lim rts t apno (rrs' as (r :: rs')) =
walther@59906
    80
      if lim < 0 
walther@59907
    81
      then (msg_1 rts; rts)
walther@59906
    82
      else
walther@59906
    83
        (case r of
walther@59906
    84
          Rule.Thm (thmid, tm) => 
walther@59907
    85
            (msg_2 thmid;
walther@59906
    86
            case Rewrite.rewrite_ thy ro erls true tm t of
walther@59906
    87
              NONE => rew_once lim rts t apno rs'
walther@59906
    88
            | SOME (t', a') =>
walther@59907
    89
              (msg_3 t'; rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs'))
walther@59906
    90
        | Rule.Eval (c as (op_, _)) => 
walther@59907
    91
            (msg_4 op_;
walther@59907
    92
            case Eval.adhoc_thm thy c (TermC.uminus_to_string t) of
walther@59906
    93
              NONE => rew_once lim rts t apno rs'
walther@59906
    94
            | SOME (thmid, tm) => 
walther@59906
    95
              (let
walther@59906
    96
                val (t', a') = case Rewrite.rewrite_ thy ro erls true tm t of
walther@59906
    97
                  SOME ta => ta
walther@59907
    98
                | NONE => raise ERROR "adhoc_thm: NONE"
walther@59907
    99
                val _ = msg_5 t'
walther@59906
   100
                val r' = Rule.Thm (thmid, tm)
walther@59906
   101
              in rew_once (lim - 1) (rts @ [(t, r', (t', a'))]) t' Appl rrs' end) 
walther@59907
   102
                handle _ => raise ERROR "derive_norm, Eval: no rewrite")
walther@59907
   103
        | Rule.Rls_ rls =>
walther@59906
   104
          (case Rewrite.rewrite_set_ thy true rls t of
walther@59906
   105
            NONE => rew_once lim rts t apno rs'
walther@59906
   106
          | SOME (t', a') => rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs')
walther@59907
   107
        | rule => raise ERROR ("rew_once: uncovered case " ^ Rule.to_string rule))
walther@59907
   108
    | rew_or_calc _ _ _ _ [] = raise ERROR "rew_or_calc: called with []"
walther@59906
   109
  in rew_once (! Rewrite.lim_deriv) [] tt Noap rs end
walther@59906
   110
walther@59907
   111
walther@59907
   112
(** concatenate several steps in revers order **)
walther@59907
   113
walther@59906
   114
fun rev_deriv (t, r, (_, a)) = (ThmC.make_sym_rule r, (t, a));
walther@59907
   115
fun steps_reverse thy erls rs ro goal t =
walther@59907
   116
    (rev o (map rev_deriv)) (do_one thy erls rs ro goal t)
walther@59906
   117
walther@59907
   118
walther@59907
   119
(** concatenate several steps **)
walther@59907
   120
walther@59906
   121
fun rev_deriv' (t, r, (t', a)) = (t', ThmC.make_sym_rule r, (t, a));
walther@59906
   122
walther@59906
   123
(* fo = ifo excluded already in inform *)
walther@59907
   124
fun steps rew_ord erls rules fo ifo =
walther@59906
   125
  let 
walther@59906
   126
    fun derivat ([]:(term * Rule.rule * (term * term list)) list) = TermC.empty
walther@59906
   127
      | derivat dt = (#1 o #3 o last_elem) dt
walther@59907
   128
    fun equal (_, _, (t1, _)) (_, _, (t2, _)) = t1 = t2
walther@59907
   129
    val  fod = do_one (ThyC.Isac()) erls rules (snd rew_ord) NONE  fo
walther@59907
   130
    val ifod = do_one (ThyC.Isac()) erls rules (snd rew_ord) NONE ifo
walther@59906
   131
  in 
walther@59906
   132
    case (fod, ifod) of
walther@59906
   133
      ([], []) => if fo = ifo then (true, []) else (false, [])
walther@59906
   134
    | (fod, []) => if derivat fod = ifo then (true, fod) (*ifo is normal form*) else (false, [])
walther@59906
   135
    | ([], ifod) => if fo = derivat ifod then (true, ((map rev_deriv') o rev) ifod) else (false, [])
walther@59906
   136
    | (fod, ifod) =>
walther@59906
   137
      if derivat fod = derivat ifod (*common normal form found*) then
walther@59906
   138
        let 
walther@59906
   139
          val (fod', rifod') = dropwhile' equal (rev fod) (rev ifod)
walther@59906
   140
        in (true, fod' @ (map rev_deriv' rifod')) end
walther@59906
   141
      else (false, [])
walther@59906
   142
  end
walther@59906
   143
walther@59906
   144
(**)end(**)