src/Tools/isac/ProgLang/rewrite.sml
changeset 52101 c3f399ce32af
parent 52085 39f0a7b9b054
child 52105 2786cc9704c8
equal deleted inserted replaced
52100:0831a4a6ec8a 52101:c3f399ce32af
     6 *)
     6 *)
     7 
     7 
     8 
     8 
     9 exception NO_REWRITE;
     9 exception NO_REWRITE;
    10 exception STOP_REW_SUB; (*WN050820 quick and dirty*)
    10 exception STOP_REW_SUB; (*WN050820 quick and dirty*)
       
    11 
       
    12 fun trace i str = 
       
    13   if ! trace_rewrite andalso i < ! depth then tracing (idt "#" i ^ str) else ()
       
    14 fun trace1 i str = 
       
    15   if ! trace_rewrite andalso i < ! depth then tracing (idt "#" (i + 1) ^ str) else ()
    11 
    16 
    12 (* 
    17 (* 
    13 Synopsis rewriting (prep for reference manual):
    18 Synopsis rewriting (prep for reference manual):
    14 ----------------------------------------------
    19 ----------------------------------------------
    15 Rewriting uses theorems for transforming terms. However, not all kinds
    20 Rewriting uses theorems for transforming terms. However, not all kinds
   112 	  end)
   117 	  end)
   113 (* simplify asumptions until one evaluates to false, store intermined's (x~=0)*)
   118 (* simplify asumptions until one evaluates to false, store intermined's (x~=0)*)
   114 and eval__true thy i asms bdv rls =
   119 and eval__true thy i asms bdv rls =
   115   if asms = [@{term True}] orelse asms = [] then ([], true)
   120   if asms = [@{term True}] orelse asms = [] then ([], true)
   116   (* this allows to check Rrls with prepat = ([@{term True}], pat) *)
   121   (* this allows to check Rrls with prepat = ([@{term True}], pat) *)
   117   else if asms = [@{term False}] then ([], false)
   122   else if asms = [@{term False}] then ([], false) else
   118   else
       
   119     let                            
   123     let                            
   120       fun chk indets [] = (indets, true)(*return asms<>True until false*)
   124       fun chk indets [] = (indets, true)(*return asms<>True until false*)
   121         | chk indets (a::asms) =
   125         | chk indets (a::asms) =
   122           (case rewrite__set_ thy (i+1) false bdv rls a of
   126           (case rewrite__set_ thy (i + 1) false bdv rls a of
   123             NONE => (chk (indets @ [a]) asms)
   127             NONE => (chk (indets @ [a]) asms)
   124           | SOME (t, a') =>
   128           | SOME (t, a') =>
   125             if t = @{term True} then (chk (indets @ a') asms) 
   129             if t = @{term True} then (chk (indets @ a') asms) 
   126             else if t = @{term False} then ([], false)
   130             else if t = @{term False} then ([], false)
   127           (*asm false .. thm not applied ^^^; continue until False vvv*)
   131           (*asm false .. thm not applied ^^^; continue until False vvv*)
   128           else chk (indets @ [t] @ a') asms);
   132           else chk (indets @ [t] @ a') asms);
   129     in chk [] asms end
   133     in chk [] asms end
   130 (* rewrite with a rule set, which must not be the empty Erls *)
   134 (* rewrite with a rule set, which must not be the empty Erls *)
   131 and rewrite__set_ _ _ __ Erls t = 
   135 and rewrite__set_ _ _ __ Erls t = 
   132     error("rewrite__set_ called with 'Erls' for '"^term2str t^"'")
   136     error ("rewrite__set_ called with 'Erls' for '" ^ term2str t ^ "'")
   133   (* rewrite with a 'reverse rule set' implemented by ML code *)
   137   (* rewrite with a 'reverse rule set' implemented by ML code *)
   134   | rewrite__set_ thy i _ _ (rrls as Rrls _) t =
   138   | rewrite__set_ thy i _ _ (rrls as Rrls _) t =
   135     let val _= if ! trace_rewrite andalso i < ! depth 
   139     let
   136 	       then tracing ((idt"#"i)^" rls: "^(id_rls rrls)^" on: "^
   140       val _= trace i (" rls: " ^ id_rls rrls ^ " on: " ^ term2str t)
   137 			     (term2str t)) else ()
   141 	    val (t', asm, rew) = app_rev thy (i + 1) rrls t
   138 	val (t', asm, rew) = app_rev thy (i+1) rrls t
   142     in if rew then SOME (t', distinct asm) else NONE end
   139     in if rew then SOME (t', distinct asm)
       
   140        else NONE end
       
   141   (* rewrite with a rule set containing Thms or Calculations *)
   143   (* rewrite with a rule set containing Thms or Calculations *)
   142   | rewrite__set_ thy i put_asm bdv rls ct =
   144   | rewrite__set_ thy i put_asm bdv rls ct =
   143 (* val (thy, i, put_asm, bdv, rls, ct) = (thy, 1, bool, [], rls, term);
   145     let
   144    *)
   146       datatype switch = Appl | Noap;
   145   let
   147       fun rew_once ruls asm ct Noap [] = (ct, asm)
   146     datatype switch = Appl | Noap;
   148         | rew_once ruls asm ct Appl [] = 
   147     fun rew_once ruls asm ct Noap [] = (ct,asm)
   149           (case rls of Rls _ => rew_once ruls asm ct Noap ruls
   148       | rew_once ruls asm ct Appl [] = 
   150           | Seq _ => (ct, asm))
   149 	(case rls of Rls _ => rew_once ruls asm ct Noap ruls
   151         | rew_once ruls asm ct apno (rul::thms) =
   150 		   | Seq _ => (ct,asm))
   152           case rul of
   151       | rew_once ruls asm ct apno (rul::thms) =
   153             Thm (thmid, thm) =>
   152 (* val (ruls, asm, ct, apno, (rul::thms)) = (ruls, [], ct, Noap, ruls);
   154               (trace1 i (" try thm: " ^ thmid);
   153    val Thm (thmid, thm) = rul;
   155               case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o rep_rls) rls)
   154    *)
   156                   ((#erls o rep_rls) rls) put_asm thm ct of
   155       case rul of
   157                 NONE => rew_once ruls asm ct apno thms
   156 	Thm (thmid, thm) =>
   158               | SOME (ct',asm') => 
   157 	  (if !trace_rewrite andalso i < ! depth 
   159                 (trace1 i (" rewrites to: " ^ term2str ct');
   158 	   then tracing((idt"#"(i+1))^" try thm: "^thmid) else ();
   160                 rew_once ruls (union (op =) asm asm') ct' Appl (rul::thms)))
   159 	   case rewrite__ thy (i+1) bdv ((snd o #rew_ord o rep_rls) rls)
   161           | Calc (cc as (op_, _)) => 
   160 	     ((#erls o rep_rls) rls) put_asm thm ct of
   162             let val _= trace1 i (" try calc: " ^ op_ ^ "'")
   161 	     NONE => rew_once ruls asm ct apno thms
   163               val ct = uminus_to_string ct
   162 	   | SOME (ct',asm') => (if ! trace_rewrite andalso i < ! depth 
   164             in case get_calculation_ thy cc ct of
   163 	     then tracing((idt"="(i+1))^" rewrites to: "^
   165                 NONE => rew_once ruls asm ct apno thms
   164 			  (term2str ct')) else ();
   166               | SOME (thmid, thm') => 
   165 	       rew_once ruls (union (op =) asm asm') ct' Appl (rul::thms)))
   167                 let 
   166       | Calc (cc as (op_,_)) => 
   168                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o rep_rls) rls)
   167 	  (let val _= if !trace_rewrite andalso i < ! depth then
   169                     ((#erls o rep_rls) rls) put_asm thm' ct;
   168 		      tracing((idt"#"(i+1))^" try calc: "^op_^"'") else ();
   170                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
   169 	     val ct = uminus_to_string ct
   171                     string_of_thmI thm' ^ "\" " ^ term2str ct ^ " = NONE")
   170 	   in case get_calculation_ thy cc ct of
   172                   val _ = trace1 i (" calc. to: " ^ term2str ((fst o the) pairopt))
   171 	     NONE => rew_once ruls asm ct apno thms
   173                 in rew_once ruls asm ((fst o the) pairopt) Appl (rul :: thms) end
   172 	   | SOME (thmid, thm') => 
   174             end
   173 	       let 
   175           | Cal1 (cc as (op_, _)) => 
   174 		 val pairopt = 
   176             let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
   175 		   rewrite__ thy (i+1) bdv ((snd o #rew_ord o rep_rls) rls)
   177               val ct = uminus_to_string ct
   176 		   ((#erls o rep_rls) rls) put_asm thm' ct;
   178             in case get_calculation1_ thy cc ct of
   177 		 val _ = if pairopt <> NONE then () 
   179                 NONE => (ct, asm)
   178 			 else error("rewrite_set_, rewrite_ \""^
   180               | SOME (thmid, thm') =>
   179 			 (string_of_thmI thm')^"\" "^(term2str ct)^" = NONE")
   181                 let 
   180 		 val _ = if ! trace_rewrite andalso i < ! depth 
   182                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o rep_rls) rls)
   181 			   then tracing((idt"="(i+1))^" calc. to: "^
   183                     ((#erls o rep_rls) rls) put_asm thm' ct;
   182 					(term2str ((fst o the) pairopt)))
   184                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
   183 			 else()
   185                      string_of_thmI thm' ^ "\" " ^ term2str ct ^ " = NONE")
   184 	       in rew_once ruls asm ((fst o the) pairopt) Appl (rul::thms) end
   186                   val _ = trace1 i (" cal1. to: " ^ term2str ((fst o the) pairopt))
   185 	   end)
   187                 in the pairopt end
   186       | Cal1 (cc as (op_,_)) => 
   188             end
   187 	  (let val _= if !trace_rewrite andalso i < ! depth then
   189           | Rls_ rls' => 
   188 		      tracing((idt"#"(i+1))^" try cal1: "^op_^"'") else ();
   190             (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
   189 	     val ct = uminus_to_string ct
   191               SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
   190 	   in case get_calculation1_ thy cc ct of
   192             | NONE => rew_once ruls asm ct apno thms);
   191 	     NONE => (ct, asm)
   193       val ruls = (#rules o rep_rls) rls;
   192 	   | SOME (thmid, thm') =>
   194       val _= trace i (" rls: " ^ id_rls rls ^ " on: " ^ term2str ct)
   193 	       let 
   195       val (ct', asm') = rew_once ruls [] ct Noap ruls;
   194 		 val pairopt = 
   196 	  in if ct = ct' then NONE else SOME (ct', distinct asm') end
   195 		   rewrite__ thy (i+1) bdv ((snd o #rew_ord o rep_rls) rls)
       
   196 		   ((#erls o rep_rls) rls) put_asm thm' ct;
       
   197 		 val _ = if pairopt <> NONE then () 
       
   198 			 else error("rewrite_set_, rewrite_ \""^
       
   199 			 (string_of_thmI thm')^"\" "^(term2str ct)^" = NONE")
       
   200 		 val _ = if ! trace_rewrite andalso i < ! depth 
       
   201 			   then tracing((idt"="(i+1))^" cal1. to: "^
       
   202 					(term2str ((fst o the) pairopt)))
       
   203 			 else()
       
   204 	       in the pairopt end
       
   205 	   end)
       
   206       | Rls_ rls' => 
       
   207 	(case rewrite__set_ thy (i+1) put_asm bdv rls' ct of
       
   208 	     SOME (t',asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
       
   209 	   | NONE => rew_once ruls asm ct apno thms);
       
   210 
       
   211     val ruls = (#rules o rep_rls) rls;
       
   212     val _= if ! trace_rewrite andalso i < ! depth 
       
   213 	   then tracing ((idt"#"i)^" rls: "^(id_rls rls)^" on: "^
       
   214 			 (term2str ct)) else ()
       
   215     val (ct',asm') = rew_once ruls [] ct Noap ruls;
       
   216   in if ct = ct' then NONE else SOME (ct', distinct asm') end
       
   217 
   197 
   218 (* apply an Rrls; if not applicable proceed with subterms *)
   198 (* apply an Rrls; if not applicable proceed with subterms *)
   219 and app_rev thy i rrls t = 
   199 and app_rev thy i rrls t = 
   220   let (* check a (precond, pattern) of a rev-set; stops with 1st true *)
   200   let (* check a (precond, pattern) of a rev-set; stops with 1st true *)
   221     fun chk_prepat thy erls [] t = true
   201     fun chk_prepat thy erls [] t = true
   231            fun scan_ f [] = false (*scan_ NEVER called by []*)
   211            fun scan_ f [] = false (*scan_ NEVER called by []*)
   232              | scan_ f (pp::pps) =
   212              | scan_ f (pp::pps) =
   233                if f pp then true else scan_ f pps;
   213                if f pp then true else scan_ f pps;
   234         in scan_ chk prepat end;
   214         in scan_ chk prepat end;
   235     (* apply the normal_form of a rev-set *)
   215     (* apply the normal_form of a rev-set *)
   236     fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   216     fun app_rev' thy (Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...}) t =
   237       if chk_prepat thy erls prepat t
   217       if chk_prepat thy erls prepat t
   238       then ((*tracing("### app_rev': t = "^(term2str t));*) normal_form t)
   218       then ((*tracing("### app_rev': t = "^(term2str t));*) normal_form t)
   239       else NONE;
   219       else NONE;
   240     val opt = app_rev' thy rrls t
   220     val opt = app_rev' thy rrls t
   241   in
   221   in
   270 
   250 
   271 (*.rewriting without argument [] for rew_ord.*)
   251 (*.rewriting without argument [] for rew_ord.*)
   272 (*WN.11.6.03: shouldnt asm<>[] lead to false ????*)
   252 (*WN.11.6.03: shouldnt asm<>[] lead to false ????*)
   273 fun eval_true thy terms rls = (snd o (eval__true thy 1 terms [])) rls;
   253 fun eval_true thy terms rls = (snd o (eval__true thy 1 terms [])) rls;
   274 
   254 
   275 
   255 (* rewriting without internal argument [] *)
   276 (*.rewriting without internal argument [] for rew_ord.*)
   256 fun rewrite_ thy rew_ord erls bool thm term = rewrite__ thy 1 [] rew_ord erls bool thm term;
   277 (* val (thy, rew_ord, erls, bool, thm, term) =
   257 fun rewrite_set_ thy bool rls term = rewrite__set_ thy 1 bool [] rls term;
   278        (thy, (assoc_rew_ord ro), rls', false, (assoc_thm' thy thm'), f);
       
   279    val (thy, rew_ord, erls, bool, thm, term) =
       
   280        (thy, rew_ord, erls, false, thm, t'');
       
   281    *)
       
   282 fun rewrite_ thy rew_ord erls bool thm term = 
       
   283     rewrite__ thy 1 [] rew_ord erls bool thm term;
       
   284 fun rewrite_set_ thy bool rls term =
       
   285 (* val (thy, bool, rls, term) = (thy, false, srls, t);
       
   286    *)
       
   287     rewrite__set_ thy 1 bool [] rls term;
       
   288 
       
   289 
   258 
   290 fun subs'2subst thy (s:subs') = 
   259 fun subs'2subst thy (s:subs') = 
   291     (((map (apfst (term_of o the o (parse thy)))) 
   260     (((map (apfst (term_of o the o (parse thy)))) 
   292      o (map (apsnd (term_of o the o (parse thy))))) s):subst;
   261      o (map (apsnd (term_of o the o (parse thy))))) s):subst;
   293 
   262