src/sml/Scripts/reverse-rew.sml
author agriesma
Thu, 17 Apr 2003 18:01:03 +0200
branchgriesmayer
changeset 328 c2c709366301
child 839 7c694e61470d
permissions -rw-r--r--
neues cvs-verzeichnis
agriesma@328
     1
(*. reverse rewriting
agriesma@328
     2
    WN.12.8.02
agriesma@328
     3
    use"Isa02/reverse-rew.sml";
agriesma@328
     4
.*)
agriesma@328
     5
agriesma@328
     6
fun trta2str (t,r,(t',a)) = "\n("^(term2str t)^", "^(rule2str r)^", ("^
agriesma@328
     7
			    (term2str t')^", "^(terms2str a)^"))";
agriesma@328
     8
fun trtas2str trtas = (strs2str o (map trta2str)) trtas;
agriesma@328
     9
fun rta2str (r,(t,a)) = "\n("^(rule2str r)^", ("^
agriesma@328
    10
			    (term2str t)^", "^(terms2str a)^"))";
agriesma@328
    11
fun rtas2str rtas = (strs2str o (map rta2str)) rtas;
agriesma@328
    12
agriesma@328
    13
agriesma@328
    14
(*.A1==>...==>An==>(Lhs = Rhs) goes to A1==>...==>An==>(Rhs = Lhs).*)
agriesma@328
    15
fun sym_thm thm =
agriesma@328
    16
  let 
agriesma@328
    17
    val {sign_ref = sign_ref, der = der, maxidx = maxidx,
agriesma@328
    18
	    shyps = shyps, hyps = hyps, prop = prop} = rep_thm_G thm;
agriesma@328
    19
    val (lhs,rhs) = (dest_equals' o strip_trueprop 
agriesma@328
    20
		     o Logic.strip_imp_concl) prop;
agriesma@328
    21
    val prop' = case strip_imp_prems' prop of
agriesma@328
    22
		   None => Trueprop $ (mk_equality (rhs, lhs))
agriesma@328
    23
		 | Some cs => 
agriesma@328
    24
		   ins_concl cs (Trueprop $ (mk_equality (rhs, lhs)));
agriesma@328
    25
  in assbl_thm sign_ref der maxidx shyps hyps prop' end;
agriesma@328
    26
(*
agriesma@328
    27
  (sym RS real_mult_div_cancel1) handle e => print_exn e;
agriesma@328
    28
Exception THM 1 raised:
agriesma@328
    29
RSN: no unifiers
agriesma@328
    30
"?s = ?t ==> ?t = ?s"
agriesma@328
    31
"?k ~= 0 ==> ?k * ?m / (?k * ?n) = ?m / ?n"
agriesma@328
    32
agriesma@328
    33
  val thm = real_mult_div_cancel1;
agriesma@328
    34
  val prop = (#prop o rep_thm) thm;
agriesma@328
    35
  atomt prop;
agriesma@328
    36
  val ppp = Logic.strip_imp_concl prop;
agriesma@328
    37
  atomt ppp;
agriesma@328
    38
  ((#prop o rep_thm o sym_thm o sym_thm) thm) = (#prop o rep_thm) thm;
agriesma@328
    39
val it = true : bool
agriesma@328
    40
  ((sym_thm o sym_thm) thm) = thm;
agriesma@328
    41
val it = true : bool
agriesma@328
    42
agriesma@328
    43
  val thm = real_le_anti_sym;
agriesma@328
    44
  ((sym_thm o sym_thm) thm) = thm;
agriesma@328
    45
val it = true : bool                                                           
agriesma@328
    46
agriesma@328
    47
  val thm = real_minus_zero;
agriesma@328
    48
  ((sym_thm o sym_thm) thm) = thm;
agriesma@328
    49
val it = true : bool                                                           
agriesma@328
    50
*)
agriesma@328
    51
agriesma@328
    52
agriesma@328
    53
agriesma@328
    54
(*.derive normalform of a rls, or derive until Some goal,
agriesma@328
    55
   and record rules applied and rewrites.
agriesma@328
    56
val it = fn
agriesma@328
    57
  : theory
agriesma@328
    58
    -> rls
agriesma@328
    59
    -> rule list
agriesma@328
    60
    -> rew_ord       : the order of this rls, which 1 theorem of is used 
agriesma@328
    61
                       for rewriting 1 single step (?14.4.03)
agriesma@328
    62
    -> term option 
agriesma@328
    63
    -> term 
agriesma@328
    64
    -> (term *       : to this term ...
agriesma@328
    65
        rule * 	     : ... this rule is applied yielding ...
agriesma@328
    66
        (term *      : ... this term ...
agriesma@328
    67
         term list)) : ... under these assumptions.
agriesma@328
    68
       list          :
agriesma@328
    69
*)
agriesma@328
    70
fun make_deriv thy erls (rs:rule list) ro(*rew_ord*) goal tt = 
agriesma@328
    71
    let
agriesma@328
    72
	datatype switch = Appl | Noap;
agriesma@328
    73
	fun rew_once rts t Noap [] = 
agriesma@328
    74
	    (case goal of 
agriesma@328
    75
		 None => rts
agriesma@328
    76
	       | Some g => 
agriesma@328
    77
		 raise error ("make_deriv: no derivation for "^(term2str t)))
agriesma@328
    78
	  | rew_once rts t Appl [] = 
agriesma@328
    79
	    (*(case rs of Rls _ =>*) rew_once rts t Noap rs
agriesma@328
    80
	  (*| Seq _ => rts) FIXXXXXME 14.3.03*)
agriesma@328
    81
	  | rew_once rts t apno rs' =
agriesma@328
    82
	    (case goal of 
agriesma@328
    83
		 None => rew_or_calc rts t apno rs'
agriesma@328
    84
	       | Some g => 
agriesma@328
    85
		 if g = t then rts
agriesma@328
    86
		 else rew_or_calc rts t apno rs')
agriesma@328
    87
	and rew_or_calc rts t apno (rrs' as (r::rs')) =
agriesma@328
    88
	    case r of
agriesma@328
    89
		Thm (thmid, tm) =>
agriesma@328
    90
		(if not (!trace_rewrite) then () else
agriesma@328
    91
		 writeln ("### trying thm '" ^ thmid ^ "'");
agriesma@328
    92
		 case rewrite_ thy ro erls true tm t of
agriesma@328
    93
		     None => rew_once rts t apno rs'
agriesma@328
    94
		   | Some (t',a') =>
agriesma@328
    95
		     (if ! trace_rewrite 
agriesma@328
    96
		      then writeln ("### rewrites to: "^(term2str t')) else ();
agriesma@328
    97
		      rew_once (rts@[(t,r,(t',a'))]) t' Appl rrs'))
agriesma@328
    98
	      | Calc (c as (op_,_)) => 
agriesma@328
    99
		let val _ = if not (!trace_rewrite) then () else
agriesma@328
   100
			    writeln ("### trying calc. '" ^ op_ ^ "'")
agriesma@328
   101
		    val t = app_num_tr'2 t
agriesma@328
   102
		in case get_calculation_ thy c t of
agriesma@328
   103
		       None => rew_once rts t apno rs'
agriesma@328
   104
		     | Some (thmid, tm) => 
agriesma@328
   105
		       (let val Some (t',a') = rewrite_ thy ro erls true tm t
agriesma@328
   106
			    val _ = if not (!trace_rewrite) then () else
agriesma@328
   107
				    writeln("### calc. to: " ^ (term2str t'))
agriesma@328
   108
			    val r' = Thm (thmid, tm)
agriesma@328
   109
			in rew_once (rts@[(t,r',(t',a'))]) t' Appl rrs'  end) 
agriesma@328
   110
		       handle _ => raise error "derive_norm, Calc: no rewrite"
agriesma@328
   111
		end
agriesma@328
   112
	      (*| Rls_ <> rule list !!! FIXXXXXME 14.3.03*) 
agriesma@328
   113
    in rew_once [] tt Noap rs end;
agriesma@328
   114
(*
agriesma@328
   115
  val t9 = (term_of o the o (parse thy)) "(8*(-1 + x))/(9*(-1 + x))";
agriesma@328
   116
  val rst = make_deriv thy eval_rls make_polynomial None t9;
agriesma@328
   117
  writeln(trtas2str rst);
agriesma@328
   118
*)
agriesma@328
   119
fun sym_thmID thmID =
agriesma@328
   120
    case explode thmID of
agriesma@328
   121
	"s"::"y"::"m"::"_"::id => implode id
agriesma@328
   122
      | id => "sym_"^thmID;
agriesma@328
   123
(* 
agriesma@328
   124
  val thmID = "sym_real_mult_2";
agriesma@328
   125
  sym_thmID thmID;
agriesma@328
   126
val it = "real_mult_2" : string
agriesma@328
   127
  val thmID = "real_num_collect";
agriesma@328
   128
  sym_thmID thmID;
agriesma@328
   129
val it = "sym_real_num_collect" : string*)
agriesma@328
   130
agriesma@328
   131
fun sym_Thm (Thm (thmID, thm)) = Thm (sym_thmID thmID, sym_thm thm)
agriesma@328
   132
  | sym_Thm r = raise error ("sym_Thm: not for "^(rule2str r));
agriesma@328
   133
(*
agriesma@328
   134
  val th =  Thm ("real_one_collect",num_str real_one_collect);
agriesma@328
   135
  sym_Thm th;
agriesma@328
   136
val th =
agriesma@328
   137
  Thm ("real_one_collect","?m is_const ==> ?n + ?m * ?n = (1 + ?m) * ?n")
agriesma@328
   138
  : rule
agriesma@328
   139
ML> val it =
agriesma@328
   140
  Thm ("sym_real_one_collect","?m is_const ==> (1 + ?m) * ?n = ?n + ?m * ?n")*)
agriesma@328
   141
agriesma@328
   142
fun rev_deriv (t, r, (t', a)) = (sym_Thm r, (t, a));
agriesma@328
   143
fun reverse_deriv trta = (rev o (map rev_deriv)) trta;
agriesma@328
   144
fun reverse_deriv thy erls (rs:rule list) ro(*rew_ord*) goal t =
agriesma@328
   145
    (rev o (map rev_deriv)) (make_deriv thy erls (rs:rule list) ro goal t);
agriesma@328
   146
(*
agriesma@328
   147
  val rev_rew = reverse_deriv thy e_rls ; 
agriesma@328
   148
  writeln(rtas2str rev_rew);
agriesma@328
   149
*)
agriesma@328
   150
agriesma@328
   151
agriesma@328
   152
fun eq_Thm (Thm (id1,_), Thm (id2,_)) = id1 = id2
agriesma@328
   153
  | eq_Thm (r1, r2) = raise error ("eq_Thm: called with '"^
agriesma@328
   154
				(rule2str r1)^"' '"^(rule2str r2)^"'");
agriesma@328
   155
fun distinct_Thm r = gen_distinct eq_Thm r;
agriesma@328
   156
agriesma@328
   157
fun eq_Thms thmIDs thm = (id_of_thm thm) mem thmIDs
agriesma@328
   158
  | eq_Thms _ thm = raise error ("eq_Thms: called with '"^(rule2str thm)^"'");
agriesma@328
   159
agriesma@328
   160
agriesma@328
   161
agriesma@328
   162
agriesma@328
   163
(* use"Isa02/reverse-rew.sml";
agriesma@328
   164
   *)
agriesma@328
   165