src/Tools/isac/Interpret/derive.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 23 Apr 2020 09:29:56 +0200
changeset 59906 cc8df204dcb6
child 59907 4c62e16e842e
permissions -rw-r--r--
separate struct. Derive
     1 (* Title:  Interpret/derive.sml
     2    Author: Walther Neuper 2019
     3    (c) due to copyright terms
     4 
     5 
     6 *)
     7 
     8 signature DERIVATION =
     9 sig
    10   type deriv
    11   val make_deriv : theory -> Rule_Set.T -> Rule.rule list -> ((term * term) list -> term * term -> bool) ->
    12     term option -> term -> deriv
    13   val concat_deriv : 'a * ((term * term) list -> term * term -> bool) ->
    14     Rule_Set.T -> Rule.rule list -> term -> term -> bool * (term * Rule.rule * (term * term list)) list
    15   val reverse_deriv : theory -> Rule_Set.T -> Rule.rule list -> ((term * term) list -> term * term -> bool) ->
    16     term option -> term -> (Rule.rule * (term * term list)) list
    17 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    18   (*  NONE *)
    19 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    20   val trtas2str : (term * Rule.rule * (term * term list)) list -> string
    21   val deriv2str : (term * Rule.rule * (term * term list)) list -> string
    22   val rev_deriv' : 'a * Rule.rule * ('b * 'c) -> 'b * Rule.rule * ('a * 'c)
    23 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    24 end
    25 
    26 (**)
    27 structure Derive(**): DERIVATION(**) =
    28 struct
    29 (**)
    30 (*/------- to Derive from Rtools -------\*)
    31 (*** reverse rewriting ***)
    32 (*. a '_deriv'ation is constructed during 'reverse rewring' by an Rrls       *
    33  *  of for connecting a user-input formula with the current calc-state.	     *
    34  *# It is somewhat incompatible with the rest of the math-engine:	     *
    35  *  (1) it is not created by a script					     *
    36  *  (2) thus there cannot be another user-input within a derivation	     *
    37  *# It suffers particularily from the not-well-foundedness of the math-engine*
    38  *  (1) FIXME other branchtyptes than Transitive will change 'embed_deriv'   *
    39  *  (2) FIXME and eventually 'compare_step' (ie. the script interpreter)     *
    40  *  (3) FIXME and eventually 'lev_back'                                      *
    41  *# SOME improvements are evident FIXME.040215 '_deriv'ation:	             *
    42  *  (1) FIXME nest Rls_ in 'make_deriv'					     *
    43  *  (2) FIXME do the not-reversed part in 'make_deriv' by scripts -- thus    *
    44  *	user-input will become possible in this part of a derivation	     *
    45  *  (3) FIXME do (2) only if a derivation has been found -- for efficiency,  *
    46  *	while a non-derivable inform requires to step until End_Proof'	     *
    47  *  (4) FIXME find criteria on when _not_ to step until End_Proof'           *
    48  *  (5) FIXME 
    49 .*)
    50 type deriv =      (* derivation for inserting one level of nodes into the Ctree *) 
    51   ( term *        (* where the rule is applied to                               *)
    52     Rule.rule *   (* rule to be applied                                         *)
    53     ( term *      (* resulting from rule application                            *)
    54       term list)) (* assumptions resulting from rule application                *)
    55   list
    56 
    57 fun trta2str (t, r, (t', a)) =
    58   "\n(" ^ UnparseC.term t ^ ", " ^ Rule.to_string_short r ^ ", (" ^ UnparseC.term t' ^ ", " ^ UnparseC.terms a ^ "))"
    59 fun trtas2str trtas = (strs2str o (map trta2str)) trtas
    60 val deriv2str = trtas2str
    61 (*\------- to Derive from Rtools -------/*)
    62 (*/------- to Derive from Rtools -------\*)
    63 (* derive normalform of a rls, or derive until SOME goal,
    64    and record rules applied and rewrites.
    65 val it = fn
    66   : theory
    67     -> rls
    68     -> rule list
    69     -> rew_ord       : the order of this rls, which 1 theorem of is used 
    70                        for rewriting 1 single step (?14.4.03)
    71     -> term option   : 040214 ??? use for "derive until SOME goal" ??? 
    72     -> term 
    73     -> (term *       : to this term ...
    74         rule * 	     : ... this rule is applied yielding ...
    75         (term *      : ... this term ...
    76          term list)) : ... under these assumptions.
    77        list          :
    78 returns empty list for a normal form
    79 FIXME.WN040214: treats rules as in Rls, _not_ as in Seq
    80 
    81 WN060825 too complicated for the intended use by cancel_, common_nominator_
    82 and unreflectedly adapted to extension of rules by Rls_: returns Rls_("sym_simpl..
    83  -- replaced below *)
    84 
    85 
    86 fun make_deriv thy erls rs ro goal tt = 
    87   let 
    88     datatype switch = Appl | Noap (* unify with version in rewrite.sml *)
    89     fun rew_once _ rts t Noap [] = 
    90         (case goal of NONE => rts | SOME _ => error ("make_deriv: no derivation for " ^ UnparseC.term t))
    91       | rew_once lim rts t Appl [] = rew_once lim rts t Noap rs
    92         (*| Seq _ => rts) FIXXXXXME 14.3.03*)
    93       | rew_once lim rts t apno rs' =
    94         (case goal of 
    95           NONE => rew_or_calc lim rts t apno rs'
    96         | SOME g => if g = t then rts else rew_or_calc lim rts t apno rs')
    97     and rew_or_calc lim rts t apno (rrs' as (r :: rs')) =
    98       if lim < 0 
    99       then (tracing ("make_deriv exceeds " ^ int2str (! Rewrite.lim_deriv) ^ "with deriv =\n");
   100         tracing (deriv2str rts); rts)
   101       else
   102         (case r of
   103           Rule.Thm (thmid, tm) => 
   104             (if not (! Rewrite.trace_on) then () else tracing ("### trying thm \"" ^ thmid ^ "\"");
   105             case Rewrite.rewrite_ thy ro erls true tm t of
   106               NONE => rew_once lim rts t apno rs'
   107             | SOME (t', a') =>
   108               (if ! Rewrite.trace_on then tracing ("=== rewrites to: " ^ UnparseC.term t') else ();
   109               rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs'))
   110         | Rule.Eval (c as (op_, _)) => 
   111           let 
   112             val _ = if not (! Rewrite.trace_on) then () else tracing ("### trying calc. \"" ^ op_^"\"")
   113             val t = TermC.uminus_to_string t
   114           in 
   115             case Eval.adhoc_thm thy c t of
   116               NONE => rew_once lim rts t apno rs'
   117             | SOME (thmid, tm) => 
   118               (let
   119                 val (t', a') = case Rewrite.rewrite_ thy ro erls true tm t of
   120                   SOME ta => ta
   121                 | NONE => error "adhoc_thm: NONE"
   122                 val _ = if not (! Rewrite.trace_on) then () else tracing("=== calc. to: " ^ UnparseC.term t')
   123                 val r' = Rule.Thm (thmid, tm)
   124               in rew_once (lim - 1) (rts @ [(t, r', (t', a'))]) t' Appl rrs' end) 
   125                 handle _ => raise ERROR "derive_norm, Eval: no rewrite"
   126           end
   127         | Rule.Rls_ rls => (* WN060829: CREATES "sym_rlsID", see 7df94616c1bd and earlier*)
   128           (case Rewrite.rewrite_set_ thy true rls t of
   129             NONE => rew_once lim rts t apno rs'
   130           | SOME (t', a') => rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs')
   131         | rule => error ("rew_once: uncovered case " ^ Rule.to_string rule))
   132     | rew_or_calc _ _ _ _ [] = error "rew_or_calc: called with []"
   133   in rew_once (! Rewrite.lim_deriv) [] tt Noap rs end
   134 
   135 (*version for reverse rewrite used before 040214*)
   136 fun rev_deriv (t, r, (_, a)) = (ThmC.make_sym_rule r, (t, a));
   137 fun reverse_deriv thy erls rs ro goal t =
   138     (rev o (map rev_deriv)) (make_deriv thy erls rs ro goal t)
   139 (*\------- to Derive from Rtools -------/*)
   140 
   141 (*/------- to NEW Derive from Error_Fill_Pattern -------\*)
   142 (* 040214: version for concat_deriv *)
   143 fun rev_deriv' (t, r, (t', a)) = (t', ThmC.make_sym_rule r, (t, a));
   144 
   145 (* fo = ifo excluded already in inform *)
   146 fun concat_deriv rew_ord erls rules fo ifo =
   147   let 
   148     fun derivat ([]:(term * Rule.rule * (term * term list)) list) = TermC.empty
   149       | derivat dt = (#1 o #3 o last_elem) dt
   150     fun equal (_,_,(t1, _)) (_,_,(t2, _)) = t1=t2
   151     val  fod = make_deriv (ThyC.Isac()) erls rules (snd rew_ord) NONE  fo
   152     val ifod = make_deriv (ThyC.Isac()) erls rules (snd rew_ord) NONE ifo
   153   in 
   154     case (fod, ifod) of
   155       ([], []) => if fo = ifo then (true, []) else (false, [])
   156     | (fod, []) => if derivat fod = ifo then (true, fod) (*ifo is normal form*) else (false, [])
   157     | ([], ifod) => if fo = derivat ifod then (true, ((map rev_deriv') o rev) ifod) else (false, [])
   158     | (fod, ifod) =>
   159       if derivat fod = derivat ifod (*common normal form found*) then
   160         let 
   161           val (fod', rifod') = dropwhile' equal (rev fod) (rev ifod)
   162         in (true, fod' @ (map rev_deriv' rifod')) end
   163       else (false, [])
   164   end
   165 (*\------- to NEW Derive from Error_Fill_Pattern-------/*)
   166 
   167 (**)end(**)