Celem: separate structure Rule
authorWalther Neuper <wneuper@ist.tugraz.at>
Sun, 25 Mar 2018 13:59:57 +0200
changeset 59415d1b52fcd4023
parent 59414 97790a8e6ef2
child 59416 229e5c9cf78b
Celem: separate structure Rule
src/Tools/isac/KEStore.thy
src/Tools/isac/ThydataC/rule.sml
src/Tools/isac/calcelems.sml
     1.1 --- a/src/Tools/isac/KEStore.thy	Sun Mar 25 11:57:33 2018 +0200
     1.2 +++ b/src/Tools/isac/KEStore.thy	Sun Mar 25 13:59:57 2018 +0200
     1.3 @@ -3,11 +3,17 @@
     1.4  *)
     1.5  
     1.6  theory KEStore 
     1.7 -imports "~~/src/HOL/Complex_Main"
     1.8 +  imports "~~/src/HOL/Complex_Main"
     1.9 +
    1.10  begin
    1.11    ML_file "~~/src/Tools/isac/library.sml"
    1.12 +  ML_file "~~/src/Tools/isac/ThydataC/rule.sml"
    1.13    ML_file "~~/src/Tools/isac/calcelems.sml"
    1.14  
    1.15 +ML {*
    1.16 +*} ML {*
    1.17 +*}
    1.18 +
    1.19  section {* Knowledge elements for problems and methods *}
    1.20  ML {*
    1.21  (* Knowledge (and Exercises) are held by "KEStore" in Isac's Java front-end.
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/Tools/isac/ThydataC/rule.sml	Sun Mar 25 13:59:57 2018 +0200
     2.3 @@ -0,0 +1,464 @@
     2.4 +(* rules guiding stepwise execution of methods in the LUCAS_INTERPRETER.
     2.5 +   Author: Walther Neuper 2018 (code gathered from other Isac source)
     2.6 +   (c) copyright due to lincense terms
     2.7 +*)
     2.8 +
     2.9 +signature RULE =
    2.10 +  sig
    2.11 +    eqtype calID
    2.12 +    type eval_fn = string -> term -> theory -> (string * term) option
    2.13 +    val e_evalfn: 'a -> term -> theory -> (string * term) option
    2.14 +    type cal = calID * eval_fn
    2.15 +    eqtype prog_calcID
    2.16 +    type calc = prog_calcID * cal
    2.17 +    eqtype cterm'                                              (* shift up in sequence of defs *)
    2.18 +    type subs' = (cterm' * cterm') list                        (* shift up in sequence of defs *)
    2.19 +    type subst = (term * term) list                            (* shift up in sequence of defs *)
    2.20 +    val dummy_ord: subst -> term * term -> bool                (* shift up in sequence of defs *)
    2.21 +    val e_rew_ord: subst -> term * term -> bool                (* shift up in sequence of defs *)
    2.22 +    eqtype rew_ord'                                            (* shift up in sequence of defs *)
    2.23 +    type rew_ord_ = subst -> term * term -> bool               (* shift up in sequence of defs *)
    2.24 +    type rew_ord = rew_ord' * rew_ord_                         (* shift up in sequence of defs *)
    2.25 +    val e_rew_ord': rew_ord'                                   (* shift up in sequence of defs *)
    2.26 +    val e_rew_ordX: rew_ord                                    (* shift up in sequence of defs *)
    2.27 +    val e_rew_ord_: rew_ord_                                   (* shift up in sequence of defs *)
    2.28 +    eqtype errpatID
    2.29 +    type errpat = errpatID * term list * thm list
    2.30 +    datatype rls
    2.31 +      =  Erls
    2.32 +       | Rls of {calc: calc list, erls: rls, errpatts: errpatID list, id: string, preconds: term list, rew_ord: rew_ord, rules: rule list, scr: scr, srls: rls}
    2.33 +       | Rrls of {calc: calc list, erls: rls, errpatts: errpatID list, id: string, prepat: (term list * term) list, rew_ord: rew_ord, scr: scr}
    2.34 +       | Seq of {calc: calc list, erls: rls, errpatts: errpatID list, id: string, preconds: term list, rew_ord: rew_ord, rules: rule list, scr: scr, srls: rls}
    2.35 +    and rule = Cal1 of string * eval_fn | Calc of string * eval_fn | Erule | Rls_ of rls | Thm of string * thm
    2.36 +    and scr
    2.37 +      = EmptyScr
    2.38 +       | Prog of term
    2.39 +       | Rfuns of
    2.40 +           {attach_form: rule list list -> term -> term -> (rule * (term * term list)) list,
    2.41 +            init_state: term -> term * term * rule list list * (rule * (term * term list)) list,
    2.42 +            locate_rule: rule list list -> term -> rule -> (rule * (term * term list)) list, next_rule: rule list list -> term -> rule option, normal_form: term -> (term * term list) option}
    2.43 +    val rule2str: rule -> string
    2.44 +    val rule2str': rule -> string
    2.45 +    val get_rules: rls -> rule list
    2.46 +    val id_rule: rule -> string
    2.47 +    val eq_rule: rule * rule -> bool
    2.48 +
    2.49 +    val scr2str: scr -> string
    2.50 +
    2.51 +    val e_rls: rls
    2.52 +    val rls2str: rls -> string
    2.53 +    val id_rls: rls -> string
    2.54 +    val append_rls: string -> rls -> rule list -> rls
    2.55 +    val merge_rls: string -> rls -> rls -> rls
    2.56 +    val remove_rls: string -> rls -> rule list -> rls
    2.57 +
    2.58 +    val e_rrls: rls
    2.59 +
    2.60 +    val string_of_thmI: thm -> string                          (* shift up in sequence of defs *)
    2.61 +    val term_to_string': Proof.context -> term -> string       (* shift up in sequence of defs *)
    2.62 +    val thy2ctxt': string -> Proof.context                     (* shift up in sequence of defs *)
    2.63 +    val Thy_Info_get_theory: string -> theory                  (* shift up in sequence of defs *)
    2.64 +    eqtype thyID                                               (* shift up in sequence of defs *)
    2.65 +    eqtype domID                                               (* shift up in sequence of defs *)
    2.66 +    val e_domID: domID                                         (* shift up in sequence of defs *)
    2.67 +    eqtype theory'                                             (* shift up in sequence of defs *)
    2.68 +    val theory'2thyID: theory' -> theory'                      (* shift up in sequence of defs *)
    2.69 +    val theory2theory': theory -> theory'                      (* shift up in sequence of defs *)
    2.70 +    val term2str: term -> string                               (* shift up in sequence of defs *)
    2.71 +    val termopt2str: term option -> string                     (* shift up in sequence of defs *)
    2.72 +    val theory2str: theory -> theory'                          (* shift up in sequence of defs *)
    2.73 +    val terms2str: term list -> string                         (* shift up in sequence of defs *)
    2.74 +    val string_of_thy: theory -> theory'                       (* shift up in sequence of defs *)
    2.75 +    val theory2domID: theory -> theory'
    2.76 +
    2.77 +    val Isac: 'a -> theory                                     (* shift up in sequence of defs *)
    2.78 +    val term_to_string'': thyID -> term -> string              (* shift up in sequence of defs *)
    2.79 +    val term_to_string''': theory -> term -> string            (* shift up in sequence of defs *)
    2.80 +    val ts2str: theory -> term list -> string                  (* shift up in sequence of defs *)
    2.81 +    val theory2thyID: theory -> thyID                          (* shift up in sequence of defs *)
    2.82 +    val thyID2theory': thyID -> thyID                          (* shift up in sequence of defs *)
    2.83 +    val string_of_typ: typ -> string                           (* shift up in sequence of defs *)
    2.84 +    val string_of_typ_thy: thyID -> typ -> string              (* shift up in sequence of defs *)
    2.85 +
    2.86 +(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    2.87 +    val terms2str': term list -> string
    2.88 +(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\*)
    2.89 +    val string_of_thm': theory -> thm -> string
    2.90 +    val string_of_thm: thm -> string
    2.91 +(*\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    2.92 +
    2.93 +(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
    2.94 +
    2.95 +  end
    2.96 +
    2.97 +(**)
    2.98 +structure Rule(**): RULE(**) =
    2.99 +struct
   2.100 +(**)
   2.101 +
   2.102 +(* eval function calling sml code during rewriting.
   2.103 +Unifying "type cal" and "type calc" would make Lucas-Interpretation more efficient,
   2.104 +  see "fun rule2stac": instead of 
   2.105 +    Calc: calID * eval_fn -> rule
   2.106 +  would be better
   2.107 +    Calc: prog_calcID * (calID * eval_fn)) -> rule*)
   2.108 +type eval_fn = (string -> term -> theory -> (string * term) option);
   2.109 +fun e_evalfn (_ : 'a) (_ : term) (_ : theory) = NONE : (string * term) option;
   2.110 +
   2.111 +(*TODO.WN060610 make use of "type rew_ord" total*)
   2.112 +type rew_ord' = string;
   2.113 +val e_rew_ord' = "e_rew_ord" : rew_ord';
   2.114 +
   2.115 +type cterm' = string;
   2.116 +
   2.117 +(*WN180324 TODO clean types subst, *)
   2.118 +type subs' = (cterm' * cterm') list; (*16.11.00 for FE-KE*)
   2.119 +type subst = (term * term) list; (*here for ets2str*)
   2.120 +
   2.121 +type rew_ord_ = subst -> Term.term * Term.term -> bool;
   2.122 +fun dummy_ord (_:subst) (_:term,_:term) = true;
   2.123 +val e_rew_ord_ = dummy_ord;
   2.124 +type rew_ord = rew_ord' * rew_ord_;
   2.125 +val e_rew_ord = dummy_ord; (* TODO.WN071231 clarify identifiers..e_rew_ordX*)
   2.126 +val e_rew_ordX = (e_rew_ord', e_rew_ord_) : rew_ord;
   2.127 +
   2.128 +(* error patterns and fill patterns *)
   2.129 +type errpatID = string
   2.130 +type errpat =
   2.131 +  errpatID    (* one identifier for a list of patterns                       
   2.132 +                 DESIGN ?TODO: errpatID list for hierarchy of errpats ?      *)
   2.133 +  * term list (* error patterns                                              *)
   2.134 +  * thm list  (* thms related to error patterns; note that respective lhs 
   2.135 +                 do not match (which reflects student's error).
   2.136 +                 fillpatterns are stored with these thms.                    *)
   2.137 +
   2.138 +(* op in isa-term "Const(op,_)" *)
   2.139 +type calID = string;
   2.140 +type cal = calID * eval_fn;
   2.141 +type prog_calcID = string;
   2.142 +type calc = (prog_calcID * cal);
   2.143 +
   2.144 +datatype rule =
   2.145 +  Erule                (*.the empty rule                     .*)
   2.146 +| Thm of (string * Basic_Thm.thm) (* see TODO CLEANUP Thm     *)
   2.147 +| Calc of string *     (*.sml-code manipulating a (sub)term  .*)
   2.148 +	  eval_fn
   2.149 +| Cal1 of string *     (*.sml-code applied only to whole term
   2.150 +                          or left/right-hand-side of eqality .*)
   2.151 +	  eval_fn
   2.152 +| Rls_ of rls          (*.ie. rule sets may be nested.*)
   2.153 +and scr =
   2.154 +    EmptyScr
   2.155 +  | Prog of term (* a leaf is either a tactic or an 'exp' in 'let v = expr' 
   2.156 +                    where 'exp' does not contain a tactic. *)
   2.157 +  | Rfuns of     (* for Rrls, usage see rational.sml ----- reverse rewrite -----      *)
   2.158 +    {init_state : (* initialise for reverse rewriting by the Interpreter              *)
   2.159 +      term ->         (* for this the rrlsstate is initialised:                       *)
   2.160 +      term *          (* the current formula: goes locate_gen -> next_tac via istate  *)
   2.161 +      term *          (* the final formula                                            *)
   2.162 +      rule list       (* of reverse rewrite set (#1#)                                 *)
   2.163 +        list *        (*   may be serveral, eg. in norm_rational                      *)
   2.164 +      ( rule *        (* Thm (+ Thm generated from Calc) resulting in ...             *)
   2.165 +        (term *       (*   ... rewrite with ...                                       *)
   2.166 +        term list))   (*   ... assumptions                                            *)
   2.167 +      list,           (* derivation from given term to normalform
   2.168 +                         in reverse order with sym_thm;
   2.169 +                                                (#1#) could be extracted from here #1 *)  
   2.170 +	  normal_form:  (* the function which drives the Rrls ##############################*)
   2.171 +	    term -> (term * term list) option,
   2.172 +	  locate_rule:  (* checks a rule R for being a cancel-rule, and if it is,
   2.173 +                     then return the list of rules (+ the terms they are rewriting to)
   2.174 +                     which need to be applied before R should be applied.
   2.175 +                     precondition: the rule is applicable to the argument-term.       *)
   2.176 +	    rule list list -> (* the reverse rule list                                      *)
   2.177 +	    term ->         (* ... to which the rule shall be applied                       *)
   2.178 +	    rule ->         (* ... to be applied to term                                    *)
   2.179 +	    ( rule *        (* value: a rule rewriting to ...                               *)
   2.180 +	      (term *       (*   ... the resulting term ...                                 *)
   2.181 +	      term list))   (*   ... with the assumptions ( //#0)                           *)
   2.182 +	    list,           (*   there may be several such rules; the list is empty,          
   2.183 +                           if the rule has nothing to do with e.g. cancelation        *)
   2.184 +	  next_rule:    (* for a given term return the next rules to be done for cancelling *)
   2.185 +	    rule list list->(* the reverse rule list                                        *)
   2.186 +	    term ->         (* the term for which ...                                       *)
   2.187 +	    rule option,    (* ... this rule is appropriate for cancellation;
   2.188 +		                     there may be no such rule (if the term is eg.canceled already*)
   2.189 +	  attach_form:  (* checks an input term TI, if it may belong to e.g. a current 
   2.190 +                     cancellation, by trying to derive it from the given term TG.     
   2.191 +                     NOT IMPLEMENTED                                                  *)
   2.192 +	    rule list list->(**)
   2.193 +	    term ->         (* TG, the last one agreed upon by user + math-eng              *)
   2.194 +	    term ->         (* TI, the next one input by the user                           *)
   2.195 +	    ( rule *        (* the rule to be applied in order to reach TI                  *) 
   2.196 +	      (term *       (* ... obtained by applying the rule ...                        *)
   2.197 +	      term list))   (* ... and the respective assumptions                           *) 
   2.198 +	    list}           (* there may be several such rules; the list is empty, if the 
   2.199 +                         users term does not belong to e.g. a cancellation of the term 
   2.200 +                         last agreed upon.                                            *)
   2.201 +and rls =
   2.202 +    Erls                          (*for init e_rls*)
   2.203 +
   2.204 +  | Rls of (*a confluent and terminating ruleset, in general                         *)
   2.205 +    {id : string,          (*for trace_rewrite:=true                                 *)
   2.206 +     preconds : term list, (*unused WN020820                                         *)
   2.207 +     (*WN060616 for efficiency...                                                    
   2.208 +      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)                    
   2.209 +     rew_ord  : rew_ord,   (*for rules*)                                             
   2.210 +     erls     : rls,       (*for the conditions in rules                             *)
   2.211 +     srls     : rls,       (*for evaluation of list_fns in script                    *)
   2.212 +     calc     : calc list, (*for Calculate in scr, set by prep_rls'                  *)
   2.213 +     rules    : rule list,                                                           
   2.214 +     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy                *)
   2.215 +     scr      : scr}       (*Prog term: generating intermed.steps  *)                
   2.216 +  | Seq of (*a sequence of rules to be tried only once                               *)
   2.217 +    {id : string,          (*for trace_rewrite:=true                                 *)
   2.218 +     preconds : term list, (*unused 20.8.02                                          *)
   2.219 +     (*WN060616 for efficiency...                                                    
   2.220 +      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)                    
   2.221 +     rew_ord  : rew_ord,   (*for rules                                               *)
   2.222 +     erls     : rls,       (*for the conditions in rules                             *)
   2.223 +     srls     : rls,       (*for evaluation of list_fns in script                    *)
   2.224 +     calc     : calc list, (*for Calculate in scr, set by prep_rls'                  *)
   2.225 +     rules    : rule list,
   2.226 +     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
   2.227 +     scr      : scr}  (*Prog term  (how to restrict type ???)*)
   2.228 +
   2.229 +  (*Rrls call SML-code and simulate an rls
   2.230 +    difference: there is always _ONE_ redex rewritten in 1 call,
   2.231 +    thus wrap Rrls by: Rls (Rls_ ...)*)
   2.232 +  | Rrls of (* SML-functions within rewriting; step-wise execution provided;   
   2.233 +               Rrls simulate an rls
   2.234 +               difference: there is always _ONE_ redex rewritten in 1 call,
   2.235 +               thus wrap Rrls by: Rls (Rls_ ...)                                     *)
   2.236 +    {id : string,          (* for trace_rewrite := true                              *)
   2.237 +     prepat  : (term list *(* preconds, eval with subst from pattern;                
   2.238 +                              if [@{term True}], match decides alone                 *)
   2.239 +		            term )     (* pattern matched with current (sub)term                 *)
   2.240 +		   list,               (* meta-conjunction is or                                 *)
   2.241 +     rew_ord  : rew_ord,   (* for rules                                              *)
   2.242 +     erls     : rls,       (* for the conditions in rules and preconds               *)
   2.243 +     calc     : calc list, (* for Calculate in scr, set automatic.in prep_rls'       *)
   2.244 +     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)              
   2.245 +     scr      : scr};      (* Rfuns {...}  (how to restrict type ???)                *)
   2.246 +
   2.247 +fun id_rls Erls = "e_rls" (*WN060714 quick and dirty: recursive defs! TODO "Erls"*)
   2.248 +  | id_rls (Rls {id, ...}) = id
   2.249 +  | id_rls (Seq {id, ...}) = id
   2.250 +  | id_rls (Rrls {id, ...}) = id;
   2.251 +val rls2str = id_rls;
   2.252 +fun id_rule (Thm (id, _)) = id
   2.253 +  | id_rule (Calc (id, _)) = id
   2.254 +  | id_rule (Cal1 (id, _)) = id
   2.255 +  | id_rule (Rls_ rls) = id_rls rls
   2.256 +  | id_rule Erule = "Erule";
   2.257 +fun eq_rule (Thm (thm1, _), Thm (thm2, _)) = thm1 = thm2
   2.258 +  | eq_rule (Calc (id1, _), Calc (id2, _)) = id1 = id2
   2.259 +  | eq_rule (Rls_ rls1, Rls_ rls2) = id_rls rls1 = id_rls rls2
   2.260 +  | eq_rule _ = false;
   2.261 +
   2.262 +(* Since Isabelle2017 sessions in theory identifiers are enforced.
   2.263 +  However, we leave theory identifiers short, in particular in use as keys into KEStore. *)
   2.264 +fun Thy_Info_get_theory thyID = Thy_Info.get_theory ("Isac." ^ thyID)
   2.265 +fun thy2ctxt' thy' = Proof_Context.init_global (Thy_Info_get_theory thy');(*FIXXXME thy-ctxt*)
   2.266 +fun thy2ctxt thy = Proof_Context.init_global thy;(*FIXXXME thy-ctxt*)
   2.267 +fun Isac _ = Proof_Context.theory_of (thy2ctxt' "Isac"); (*@{theory "Isac"}*)
   2.268 +
   2.269 +fun term_to_string' ctxt t =
   2.270 +  let
   2.271 +    val ctxt' = Config.put show_markup false ctxt
   2.272 +  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
   2.273 +fun term_to_string'' thyID t =
   2.274 +  let
   2.275 +    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info_get_theory thyID))
   2.276 +  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
   2.277 +fun term_to_string''' thy t =
   2.278 +  let
   2.279 +    val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
   2.280 +  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
   2.281 +
   2.282 +
   2.283 +fun term2str t = term_to_string' (thy2ctxt' "Isac") t;
   2.284 +fun t2str thy t = term_to_string' (thy2ctxt thy) t;
   2.285 +fun ts2str thy ts = ts |> map (t2str thy) |> strs2str';
   2.286 +fun terms2strs ts = map term2str ts;     (* terms2strs [t1,t2] = ["1 + 2", "abc"];      *)
   2.287 +val terms2str = strs2str o terms2strs;   (* terms2str  [t1,t2] = "[\"1 + 2\",\"abc\"]"; *)
   2.288 +val terms2str' = strs2str' o terms2strs; (* terms2str' [t1,t2] = "[1 + 2,abc]";         *)
   2.289 +fun termopt2str (SOME t) = "(SOME " ^ term2str t ^ ")"
   2.290 +  | termopt2str NONE = "NONE";
   2.291 +
   2.292 +(*ad thm':
   2.293 +   there are two kinds of theorems ...
   2.294 +   (1) known by isabelle
   2.295 +   (2) not known, eg. calc_thm, instantiated rls
   2.296 +       the latter have a thmid "#..."
   2.297 +   and thus outside isa we ALWAYS transport both (thmID, string_of_thmI)
   2.298 +   and have a special assoc_thm / assoc_rls in this interface      *)
   2.299 +type theory' = string; (* = domID ^".thy" WN.101011 ABOLISH !*)
   2.300 +type domID = string;   (* domID ^".thy" = theory' WN.101011 replace by thyID*)
   2.301 +type thyID = string;   (* WN.3.11.03 TODO: replace domID with thyID*)
   2.302 +val e_domID = "e_domID" : domID;
   2.303 +
   2.304 +fun string_of_thy thy = Context.theory_name thy: theory';
   2.305 +val theory2domID = string_of_thy;
   2.306 +val theory2thyID = (get_thy o string_of_thy) : theory -> thyID;
   2.307 +val theory2theory' = string_of_thy;
   2.308 +val theory2str = string_of_thy; (*WN050903 ..most consistent naming*)
   2.309 +
   2.310 +fun thyID2theory' (thyID:thyID) = thyID;
   2.311 +fun theory'2thyID (theory':theory') = theory';
   2.312 +
   2.313 +fun type_to_string'' (thyID : thyID) t =
   2.314 +  let
   2.315 +    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info_get_theory thyID))
   2.316 +  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
   2.317 +fun type2str typ = type_to_string'' "Isac" typ; (*TODO legacy*)
   2.318 +val string_of_typ = type2str; (*legacy*)
   2.319 +fun string_of_typ_thy thy typ = type_to_string'' thy typ; (*legacy*)
   2.320 +
   2.321 +(*check for [.] as caused by "fun assoc_thm'"*)
   2.322 +fun string_of_thm thm = term_to_string' (thy2ctxt' "Isac") (Thm.prop_of thm)
   2.323 +fun string_of_thm' thy thm = term_to_string' (thy2ctxt thy) (Thm.prop_of thm)
   2.324 +fun string_of_thmI thm =
   2.325 +  let 
   2.326 +    val str = (de_quote o string_of_thm) thm
   2.327 +    val (a, b) = split_nlast (5, Symbol.explode str)
   2.328 +  in 
   2.329 +    case b of
   2.330 +      [" ", " ","[", ".", "]"] => implode a
   2.331 +    | _ => str
   2.332 +  end
   2.333 +
   2.334 +fun get_rules Erls = []
   2.335 +  | get_rules (Rls {rules, ...}) = rules
   2.336 +  | get_rules (Seq {rules, ...}) = rules
   2.337 +  | get_rules (Rrls _) = [];
   2.338 +fun rule2str Erule = "Erule"
   2.339 +  | rule2str (Thm (str, thm)) = "Thm (\""^str^"\","^(string_of_thmI thm)^")"
   2.340 +  | rule2str (Calc (str, _)) = "Calc (\""^str^"\",fn)"
   2.341 +  | rule2str (Cal1 (str, _)) = "Cal1 (\""^str^"\",fn)"
   2.342 +  | rule2str (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
   2.343 +fun rule2str' Erule = "Erule"
   2.344 +  | rule2str' (Thm (str, _)) = "Thm (\""^str^"\",\"\")"
   2.345 +  | rule2str' (Calc (str, _)) = "Calc (\""^str^"\",fn)"
   2.346 +  | rule2str' (Cal1 (str, _)) = "Cal1 (\""^str^"\",fn)"
   2.347 +  | rule2str' (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
   2.348 +fun scr2str EmptyScr = "EmptyScr"
   2.349 +  | scr2str (Prog s) = "Prog " ^ term2str s
   2.350 +  | scr2str (Rfuns _)  = "Rfuns";
   2.351 +
   2.352 +
   2.353 +(*//---------------- vvv TODO signature ---------------------------------\\*)
   2.354 +
   2.355 +
   2.356 +val e_type = Type ("empty",[]);
   2.357 +val e_term = Const ("empty", e_type);
   2.358 +val e_rule = Thm ("refl", @{thm refl});
   2.359 +val e_rrlsstate = (e_term,e_term, [[e_rule]], [(e_rule, (e_term, []))]);
   2.360 +val e_term = Const ("empty", Type("'a", []));
   2.361 +
   2.362 +
   2.363 +
   2.364 +type calc_elem =  (* fun calculate_ fetches the evaluation-function via this list *)
   2.365 +  prog_calcID *   (* a simple identifier used in programs                    *)
   2.366 +  (calID *        (* a long identifier used in Const                         *)
   2.367 +    eval_fn)      (* an ML function                                          *)
   2.368 +fun calc_eq ((pi1, (ci1, _)), (pi2, (ci2, _))) =
   2.369 +  if pi1 = pi2
   2.370 +  then if ci1 = ci2 then true else error ("calc_eq: " ^ ci1 ^ " <> " ^ ci2)
   2.371 +  else false
   2.372 +
   2.373 +local
   2.374 +    fun ii (_: term) = e_rrlsstate;
   2.375 +    fun no (_: term) = SOME (e_term, [e_term]);
   2.376 +    fun lo (_: rule list list) (_: term) (_: rule) = [(e_rule, (e_term, [e_term]))];
   2.377 +    fun ne (_: rule list list) (_: term) = SOME e_rule;
   2.378 +    fun fo (_: rule list list) (_: term) (_: term) = [(e_rule, (e_term, [e_term]))];
   2.379 +in
   2.380 +val e_rfuns = Rfuns {init_state = ii, normal_form = no, locate_rule = lo,
   2.381 +		  next_rule = ne, attach_form = fo};
   2.382 +end;
   2.383 +val e_rls =
   2.384 +  Rls {id = "e_rls", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
   2.385 +    srls = Erls, calc = [], rules = [], errpatts = [], scr = EmptyScr}: rls;
   2.386 +val e_rrls =
   2.387 +  Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
   2.388 +    calc = [], errpatts = [], scr = e_rfuns}:rls;
   2.389 +
   2.390 +fun rep_rls Erls = rep_rls e_rls
   2.391 +  | rep_rls (Rls {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
   2.392 +    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
   2.393 +      calc = calc, rules = rules, scr = scr}
   2.394 +  | rep_rls (Seq {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
   2.395 +    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
   2.396 +      calc = calc, rules = rules, scr = scr}
   2.397 +  | rep_rls (Rrls _)  = rep_rls e_rls
   2.398 +
   2.399 +fun append_rls id Erls _ = raise ERROR ("append_rls: with \"" ^ id ^ "\" not for Erls")
   2.400 +  | append_rls id (Rls {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.401 +			rules = rs, errpatts = errpatts, scr = sc}) r =
   2.402 +    Rls {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.403 +      rules = rs @ r, errpatts = errpatts, scr = sc}
   2.404 +  | append_rls id (Seq {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.405 +			rules = rs, errpatts = errpatts, scr = sc}) r =
   2.406 +    Seq {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.407 +      rules = rs @ r, errpatts = errpatts, scr = sc}
   2.408 +  | append_rls id (Rrls _) _ = raise ERROR ("append_rls: not for reverse-rewrite-rule-set " ^ id);
   2.409 +
   2.410 +
   2.411 +fun merge_ids rls1 rls2 =
   2.412 +  let 
   2.413 +    val id1 = (#id o rep_rls) rls1
   2.414 +    val id2 = (#id o rep_rls) rls2
   2.415 +  in
   2.416 +    if id1 = id2 then id1 else "merged_" ^ id1 ^ "_" ^ id2
   2.417 +  end
   2.418 +fun merge_rls _ Erls rls = rls
   2.419 +  | merge_rls _ rls Erls = rls
   2.420 +  | merge_rls _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
   2.421 +  | merge_rls _ _ (Rrls x) = Rrls x
   2.422 +  | merge_rls id
   2.423 +	  (Rls {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
   2.424 +	    rules = rs1, errpatts = eps1, scr = sc1, ...})
   2.425 +	  (Rls {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
   2.426 +	    rules = rs2, errpatts = eps2, ...})
   2.427 +    =
   2.428 +	  Rls {id = id, rew_ord = ro1, scr = sc1,
   2.429 +	    preconds = union (op =) pc1 pc2,	    
   2.430 +	    erls = merge_rls (merge_ids er1 er2) er1 er2,
   2.431 +	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
   2.432 +	    calc = union calc_eq ca1 ca2,
   2.433 +		  rules = union eq_rule rs1 rs2,
   2.434 +      errpatts = union (op =) eps1 eps2}
   2.435 +  | merge_rls id
   2.436 +	  (Seq {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
   2.437 +	    rules = rs1, errpatts = eps1, scr = sc1, ...})
   2.438 +	  (Seq {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
   2.439 +	    rules = rs2, errpatts = eps2, ...})
   2.440 +    =
   2.441 +	  Seq {id = id, rew_ord = ro1, scr = sc1,
   2.442 +	    preconds = union (op =) pc1 pc2,	    
   2.443 +	    erls = merge_rls (merge_ids er1 er2) er1 er2,
   2.444 +	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
   2.445 +	    calc = union calc_eq ca1 ca2,
   2.446 +		  rules = union eq_rule rs1 rs2,
   2.447 +      errpatts = union (op =) eps1 eps2}
   2.448 +  | merge_rls id _ _ = error ("merge_rls: \"" ^ id ^ 
   2.449 +    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Seq");
   2.450 +
   2.451 +(* used only for one hack TODO remove *)
   2.452 +fun remove_rls id (Rls {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.453 +		  rules = rs, errpatts = eps, scr = sc}) r =
   2.454 +    Rls {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.455 +	    rules = gen_rems eq_rule (rs, r),
   2.456 +	    errpatts = eps,
   2.457 +	    scr = sc}
   2.458 +  | remove_rls id (Seq {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.459 +		  rules = rs, errpatts = eps, scr = sc}) r =
   2.460 +    Seq {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   2.461 +	    rules = gen_rems eq_rule (rs, r),
   2.462 +	    errpatts = eps,
   2.463 +	    scr = sc}
   2.464 +  | remove_rls id (Rrls _) _ = raise ERROR ("remove_rls: not for reverse-rewrite-rule-set "^id)
   2.465 +  | remove_rls _ rls _ = raise ERROR ("remove_rls called with " ^ rls2str rls);
   2.466 +
   2.467 +end (*struct*)
     3.1 --- a/src/Tools/isac/calcelems.sml	Sun Mar 25 11:57:33 2018 +0200
     3.2 +++ b/src/Tools/isac/calcelems.sml	Sun Mar 25 13:59:57 2018 +0200
     3.3 @@ -13,7 +13,7 @@
     3.4      type rew_ord
     3.5      eqtype errpatID
     3.6      type calc
     3.7 -        datatype rule = Cal1 of string * eval_fn | Calc of string * eval_fn | Erule | Rls_ of rls 
     3.8 +    datatype rule = Cal1 of string * eval_fn | Calc of string * eval_fn | Erule | Rls_ of rls 
     3.9        | Thm of string * thm
    3.10      and scr
    3.11        = EmptyScr
    3.12 @@ -34,7 +34,6 @@
    3.13          id: string, prepat: (term list * term) list, rew_ord: rew_ord, scr: scr}
    3.14  
    3.15      eqtype rls'
    3.16 -    eqtype theory'
    3.17      eqtype prog_calcID
    3.18      eqtype calID
    3.19      type cas_elem
    3.20 @@ -54,12 +53,11 @@
    3.21      val subst2str: subst -> string
    3.22      val subst2str': subst -> string
    3.23  
    3.24 -    eqtype thyID
    3.25      type fillpat
    3.26      datatype thydata
    3.27        = Hcal of {calc: calc, coursedesign: authors, guh: guh, mathauthors: authors}
    3.28        | Hord of {coursedesign: authors, guh: guh, mathauthors: authors, ord: subst -> term * term -> bool}
    3.29 -      | Hrls of {coursedesign: authors, guh: guh, mathauthors: authors, thy_rls: thyID * rls}
    3.30 +      | Hrls of {coursedesign: authors, guh: guh, mathauthors: authors, thy_rls: Rule.thyID * rls}
    3.31        | Hthm of {coursedesign: authors, fillpats: fillpat list, guh: guh, mathauthors: authors, thm: thm}
    3.32        | Html of {coursedesign: authors, guh: guh, html: string, mathauthors: authors}
    3.33      type theID
    3.34 @@ -81,20 +79,13 @@
    3.35      val get_py: 'a ptyp list -> pblID -> string list -> 'a
    3.36      val update_hthm: thydata -> fillpat list -> thydata
    3.37      val update_ptyps: string list -> string list -> 'a -> 'a ptyp list -> 'a ptyp list
    3.38 -    val e_rls: rls
    3.39 -    val e_rrls: rls
    3.40      val part2guh: theID -> guh
    3.41      val spec2str: string * string list * string list -> string
    3.42 -    val term_to_string''': theory -> term -> string
    3.43      val linefeed: string -> string
    3.44      val pbts2str: pbt list -> string
    3.45      val thes2str: thydata list -> string
    3.46      val theID2str: string list -> string
    3.47      val the2str: thydata -> string
    3.48 -    val Thy_Info_get_theory: string -> theory
    3.49 -    val string_of_typ: typ -> string
    3.50 -    val string_of_typ_thy: thyID -> typ -> string
    3.51 -    val term2str: term -> string
    3.52      val thy2ctxt: theory -> Proof.context
    3.53      val trace_calc: bool Unsynchronized.ref
    3.54      eqtype thmID
    3.55 @@ -103,35 +94,23 @@
    3.56      val trace_rewrite: bool Unsynchronized.ref
    3.57      val depth: int Unsynchronized.ref
    3.58      val t2str: theory -> term -> string
    3.59 -    val ts2str: theory -> term list -> string
    3.60 -    val terms2str: term list -> string
    3.61      val id_rls: rls -> string
    3.62      val rls2str: rls -> string
    3.63      type errpat
    3.64      val rep_rls: rls -> {calc: calc list, erls: rls, errpats: errpatID list, id: string,
    3.65        preconds: term list, rew_ord: rew_ord, rules: rule list, scr: scr, srls: rls}
    3.66 -    val string_of_thmI: thm -> string
    3.67 -    val rule2str: rule -> string
    3.68      val e_term: term
    3.69      val dummy_ord: subst -> term * term -> bool
    3.70 -    val theory2domID: theory -> theory'
    3.71 -    val assoc_thy: theory' -> theory
    3.72 -    val append_rls: string -> rls -> rule list -> rls
    3.73 -    eqtype domID
    3.74 -    val get_rules: rls -> rule list
    3.75 +    val assoc_thy: Rule.theory' -> theory
    3.76      val id_rule: rule -> string
    3.77      eqtype cterm'
    3.78 -    val term_to_string': Proof.context -> term -> string
    3.79 -    val thy2ctxt': string -> Proof.context
    3.80      type rrlsstate = term * term * rule list list * (rule * (term * term list)) list
    3.81      type loc_
    3.82      val loc_2str: loc_ -> string
    3.83 -    val termopt2str: term option -> string
    3.84      eqtype rew_ord'
    3.85      type thm''
    3.86      type rew_ord_
    3.87      val metID2str: string list -> string
    3.88 -    val e_domID: domID
    3.89      val e_pblID: pblID
    3.90      val e_metID: metID
    3.91      val empty_spec: spec
    3.92 @@ -144,65 +123,50 @@
    3.93      val coll_metguhs: met ptyp list -> guh list
    3.94      type pat
    3.95      val e_type: typ
    3.96 -    val theory2str: theory -> theory'
    3.97      val pats2str: pat list -> string
    3.98 -    val string_of_thy: theory -> theory'
    3.99 -    val theory2theory': theory -> theory'
   3.100      val maxthy: theory -> theory -> theory
   3.101      val e_evalfn: 'a -> term -> theory -> (string * term) option
   3.102      val assoc_rew_ord: string -> subst -> term * term -> bool
   3.103      eqtype filename
   3.104 -    val rule2str': rule -> string
   3.105      val lim_deriv: int Unsynchronized.ref
   3.106      val id_of_thm: rule -> string
   3.107      val isabthys: unit -> theory list
   3.108      val thyID_of_derivation_name: string -> string
   3.109 -    val partID': theory' -> string
   3.110 -    val Isac: 'a -> theory
   3.111 -    val theory'2thyID: theory' -> theory'
   3.112 -    val thm2guh: string * thyID -> thmID -> guh
   3.113 +    val partID': Rule.theory' -> string
   3.114 +    val thm2guh: string * Rule.thyID -> thmID -> guh
   3.115      val thmID_of_derivation_name: string -> string
   3.116 -    val rls2guh: string * thyID -> rls' -> guh
   3.117 +    val rls2guh: string * Rule.thyID -> rls' -> guh
   3.118      val eq_rule: rule * rule -> bool
   3.119      val e_rew_ordX: rew_ord
   3.120      val theID2guh: theID -> guh
   3.121 -    val thyID2theory': thyID -> thyID
   3.122      val e_rule: rule
   3.123 -    val scr2str: scr -> string
   3.124      val type2str: typ -> string
   3.125      eqtype fillpatID
   3.126      type pbt_ = string * (term * term)
   3.127      val e_rew_ord: subst -> term * term -> bool
   3.128      eqtype xml
   3.129 -    val cal2guh: string * thyID -> string -> guh
   3.130 +    val cal2guh: string * Rule.thyID -> string -> guh
   3.131      val ketype2str': ketype -> string
   3.132      val str2ketype': string -> ketype
   3.133      val thmID_of_derivation_name': thm -> string
   3.134      eqtype path
   3.135 -    val theID2thyID: theID -> thyID
   3.136 +    val theID2thyID: theID -> Rule.thyID
   3.137      val thy2guh: theID -> guh
   3.138 -    val theory2thyID: theory -> thyID
   3.139      val thypart2guh: theID -> guh
   3.140 -    val ord2guh: string * thyID -> rew_ord' -> guh
   3.141 +    val ord2guh: string * Rule.thyID -> rew_ord' -> guh
   3.142      val update_hrls: thydata -> errpatID list -> thydata
   3.143      eqtype iterID
   3.144      eqtype calcID
   3.145      val thm''_of_thm: thm -> thm''
   3.146      val rew_ord': (rew_ord' * (subst -> term * term -> bool)) list Unsynchronized.ref
   3.147 -    val merge_rls: string -> rls -> rls -> rls
   3.148      val e_rrlsstate: rrlsstate
   3.149      val thm_of_thm: rule -> thm
   3.150 -    val remove_rls: string -> rls -> rule list -> rls
   3.151  (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
   3.152      val thm2str: thm -> string
   3.153 -    val term_to_string'': thyID -> term -> string
   3.154 -    val terms2str': term list -> string
   3.155      val insert_fillpats: thydata ptyp list -> (pblID * fillpat list) list -> thydata ptyp list ->
   3.156        thydata ptyp list
   3.157  (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
   3.158      val terms2strs: term list -> string list
   3.159 -    val string_of_thm': theory -> thm -> string
   3.160 -    val string_of_thm: thm -> string
   3.161      val knowthys: unit -> theory list
   3.162      val e_pbt: pbt
   3.163  ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
   3.164 @@ -213,14 +177,13 @@
   3.165  
   3.166  
   3.167  (**)
   3.168 -structure Celem(**): CALC_ELEMENT(**) =
   3.169 +structure Celem(*: CALC_ELEMENT*) =
   3.170  struct
   3.171  (**)
   3.172  
   3.173  val linefeed = (curry op^) "\n"; (* ?\<longrightarrow> libraryC ?*)
   3.174  type authors = string list;
   3.175  
   3.176 -type cterm' = string;
   3.177  type iterID = int;
   3.178  type calcID = int;
   3.179  
   3.180 @@ -240,7 +203,7 @@
   3.181    WN120524: dont use Thm.derivation_name, this is destroyed by num_str;
   3.182    Thm.get_name_hint survives num_str and seems perfectly reliable *)
   3.183  
   3.184 -type thm' = thmID * cterm';(*WN060610 deprecated in favour of thm''; WN180324: used in TODO review:
   3.185 +type thm' = thmID * Rule.cterm';(*WN060610 deprecated in favour of thm''; WN180324: used in TODO review:
   3.186  val thm'2xml : int -> Celem.thm' -> Celem.xml
   3.187  val assoc_thm': theory -> Celem.thm' -> thm
   3.188  | Calculate' of Celem.theory' * string * term * (term * Celem.thm')
   3.189 @@ -268,51 +231,6 @@
   3.190  
   3.191  type xml = string; (* rm together with old code replaced by XML.tree *)
   3.192  
   3.193 -(* eval function calling sml code during rewriting.
   3.194 -Unifying "type cal" and "type calc" would make Lucas-Interpretation more efficient,
   3.195 -  see "fun rule2stac": instead of 
   3.196 -    Calc: calID * eval_fn -> rule
   3.197 -  would be better
   3.198 -    Calc: prog_calcID * (calID * eval_fn)) -> rule*)
   3.199 -type eval_fn = (string -> term -> theory -> (string * term) option);
   3.200 -fun e_evalfn (_ : 'a) (_ : term) (_ : theory) = NONE : (string * term) option;
   3.201 -(* op in isa-term "Const(op,_)" *)
   3.202 -type calID = string;
   3.203 -type cal = calID * eval_fn;
   3.204 -type prog_calcID = string;
   3.205 -type calc = (prog_calcID * cal);
   3.206 -type calc_elem =  (* fun calculate_ fetches the evaluation-function via this list *)
   3.207 -  prog_calcID *   (* a simple identifier used in programs *)
   3.208 -  (calID *        (* a long identifier used in Const *)
   3.209 -    eval_fn)      (* an ML function *)
   3.210 -fun calc_eq ((pi1, (ci1, _)), (pi2, (ci2, _))) =
   3.211 -  if pi1 = pi2
   3.212 -  then if ci1 = ci2 then true else error ("calc_eq: " ^ ci1 ^ " <> " ^ ci2)
   3.213 -  else false
   3.214 -
   3.215 -(*WN180324 TODO clean types subst, *)
   3.216 -type subs' = (cterm' * cterm') list; (*16.11.00 for FE-KE*)
   3.217 -type subst = (term * term) list; (*here for ets2str*)
   3.218 -
   3.219 -(*TODO.WN060610 make use of "type rew_ord" total*)
   3.220 -type rew_ord' = string;
   3.221 -val e_rew_ord' = "e_rew_ord" : rew_ord';
   3.222 -type rew_ord_ = subst -> Term.term * Term.term -> bool;
   3.223 -fun dummy_ord (_:subst) (_:term,_:term) = true;
   3.224 -val e_rew_ord_ = dummy_ord;
   3.225 -type rew_ord = rew_ord' * rew_ord_;
   3.226 -val e_rew_ord = dummy_ord; (* TODO.WN071231 clarify identifiers..e_rew_ordX*)
   3.227 -val e_rew_ordX = (e_rew_ord', e_rew_ord_) : rew_ord;
   3.228 -
   3.229 -(* error patterns and fill patterns *)
   3.230 -type errpatID = string
   3.231 -type errpat =
   3.232 -  errpatID    (* one identifier for a list of patterns                       
   3.233 -                 DESIGN ?TODO: errpatID list for hierarchy of errpats ?      *)
   3.234 -  * term list (* error patterns                                              *)
   3.235 -  * thm list  (* thms related to error patterns; note that respective lhs 
   3.236 -                 do not match (which reflects student's error).
   3.237 -                 fillpatterns are stored with these thms.                    *)
   3.238  
   3.239  (* for (at least) 2 kinds of access:
   3.240    (1) given an errpatID, find the respective fillpats (e.g. in fun find_fill_pats)
   3.241 @@ -321,153 +239,10 @@
   3.242  type fillpat =
   3.243    fillpatID   (* DESIGN ?TODO: give an order w.r.t difficulty ? *)
   3.244    * term      (* the pattern with fill-in gaps                  *)
   3.245 -  * errpatID; (* which the fillpat would be a help for          
   3.246 +  * Rule.errpatID; (* which the fillpat would be a help for          
   3.247                   DESIGN ?TODO: list for several patterns ?      *)
   3.248  
   3.249 -datatype rule =
   3.250 -  Erule                (*.the empty rule                     .*)
   3.251 -| Thm of (string * Basic_Thm.thm) (* see TODO CLEANUP Thm     *)
   3.252 -| Calc of string *     (*.sml-code manipulating a (sub)term  .*)
   3.253 -	  eval_fn
   3.254 -| Cal1 of string *     (*.sml-code applied only to whole term
   3.255 -                          or left/right-hand-side of eqality .*)
   3.256 -	  eval_fn
   3.257 -| Rls_ of rls          (*.ie. rule sets may be nested.*)
   3.258 -and scr =
   3.259 -    EmptyScr
   3.260 -  | Prog of term (* a leaf is either a tactic or an 'exp' in 'let v = expr' 
   3.261 -                    where 'exp' does not contain a tactic. *)
   3.262 -  | Rfuns of     (* for Rrls, usage see rational.sml ----- reverse rewrite -----      *)
   3.263 -    {init_state : (* initialise for reverse rewriting by the Interpreter              *)
   3.264 -      term ->         (* for this the rrlsstate is initialised:                       *)
   3.265 -      term *          (* the current formula: goes locate_gen -> next_tac via istate  *)
   3.266 -      term *          (* the final formula                                            *)
   3.267 -      rule list       (* of reverse rewrite set (#1#)                                 *)
   3.268 -        list *        (*   may be serveral, eg. in norm_rational                      *)
   3.269 -      ( rule *        (* Thm (+ Thm generated from Calc) resulting in ...             *)
   3.270 -        (term *       (*   ... rewrite with ...                                       *)
   3.271 -        term list))   (*   ... assumptions                                            *)
   3.272 -      list,           (* derivation from given term to normalform
   3.273 -                         in reverse order with sym_thm;
   3.274 -                                                (#1#) could be extracted from here #1 *)  
   3.275 -	  normal_form:  (* the function which drives the Rrls ##############################*)
   3.276 -	    term -> (term * term list) option,
   3.277 -	  locate_rule:  (* checks a rule R for being a cancel-rule, and if it is,
   3.278 -                     then return the list of rules (+ the terms they are rewriting to)
   3.279 -                     which need to be applied before R should be applied.
   3.280 -                     precondition: the rule is applicable to the argument-term.       *)
   3.281 -	    rule list list -> (* the reverse rule list                                      *)
   3.282 -	    term ->         (* ... to which the rule shall be applied                       *)
   3.283 -	    rule ->         (* ... to be applied to term                                    *)
   3.284 -	    ( rule *        (* value: a rule rewriting to ...                               *)
   3.285 -	      (term *       (*   ... the resulting term ...                                 *)
   3.286 -	      term list))   (*   ... with the assumptions ( //#0)                           *)
   3.287 -	    list,           (*   there may be several such rules; the list is empty,          
   3.288 -                           if the rule has nothing to do with e.g. cancelation        *)
   3.289 -	  next_rule:    (* for a given term return the next rules to be done for cancelling *)
   3.290 -	    rule list list->(* the reverse rule list                                        *)
   3.291 -	    term ->         (* the term for which ...                                       *)
   3.292 -	    rule option,    (* ... this rule is appropriate for cancellation;
   3.293 -		                     there may be no such rule (if the term is eg.canceled already*)
   3.294 -	  attach_form:  (* checks an input term TI, if it may belong to e.g. a current 
   3.295 -                     cancellation, by trying to derive it from the given term TG.     
   3.296 -                     NOT IMPLEMENTED                                                 *)
   3.297 -	    rule list list->(**)
   3.298 -	    term ->         (* TG, the last one agreed upon by user + math-eng              *)
   3.299 -	    term ->         (* TI, the next one input by the user                           *)
   3.300 -	    ( rule *        (* the rule to be applied in order to reach TI                  *) 
   3.301 -	      (term *       (* ... obtained by applying the rule ...                        *)
   3.302 -	      term list))   (* ... and the respective assumptions                           *) 
   3.303 -	    list}           (* there may be several such rules; the list is empty, if the 
   3.304 -                         users term does not belong to e.g. a cancellation of the term 
   3.305 -                         last agreed upon.                                            *)
   3.306 -and rls =
   3.307 -    Erls                          (*for init e_rls*)
   3.308  
   3.309 -  | Rls of (*a confluent and terminating ruleset, in general         *)
   3.310 -    {id : string,          (*for trace_rewrite:=true                 *)
   3.311 -     preconds : term list, (*unused WN020820                         *)
   3.312 -     (*WN060616 for efficiency...
   3.313 -      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)
   3.314 -     rew_ord  : rew_ord,   (*for rules*)
   3.315 -     erls     : rls,       (*for the conditions in rules             *)
   3.316 -     srls     : rls,       (*for evaluation of list_fns in script    *)
   3.317 -     calc     : calc list, (*for Calculate in scr, set by prep_rls'   *)
   3.318 -     rules    : rule list,
   3.319 -     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
   3.320 -     scr      : scr}       (*Prog term: generating intermed.steps  *)
   3.321 -  | Seq of (*a sequence of rules to be tried only once               *)
   3.322 -    {id : string,          (*for trace_rewrite:=true                 *)
   3.323 -     preconds : term list, (*unused 20.8.02                          *)
   3.324 -     (*WN060616 for efficiency...
   3.325 -      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)
   3.326 -     rew_ord  : rew_ord,   (*for rules                               *)
   3.327 -     erls     : rls,       (*for the conditions in rules             *)
   3.328 -     srls     : rls,       (*for evaluation of list_fns in script    *)
   3.329 -     calc     : calc list, (*for Calculate in scr, set by prep_rls'   *)
   3.330 -     rules    : rule list,
   3.331 -     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
   3.332 -     scr      : scr}  (*Prog term  (how to restrict type ???)*)
   3.333 -
   3.334 -  (*Rrls call SML-code and simulate an rls
   3.335 -    difference: there is always _ONE_ redex rewritten in 1 call,
   3.336 -    thus wrap Rrls by: Rls (Rls_ ...)*)
   3.337 -  | Rrls of (* SML-functions within rewriting; step-wise execution provided;   
   3.338 -               Rrls simulate an rls
   3.339 -               difference: there is always _ONE_ redex rewritten in 1 call,
   3.340 -               thus wrap Rrls by: Rls (Rls_ ...)                              *)
   3.341 -    {id : string,          (* for trace_rewrite := true                       *)
   3.342 -     prepat  : (term list *(* preconds, eval with subst from pattern;
   3.343 -                              if [@{term True}], match decides alone          *)
   3.344 -		            term )     (* pattern matched with current (sub)term          *)
   3.345 -		   list,               (* meta-conjunction is or                          *)
   3.346 -     rew_ord  : rew_ord,   (* for rules                                       *)
   3.347 -     erls     : rls,       (* for the conditions in rules and preconds        *)
   3.348 -     calc     : calc list, (* for Calculate in scr, set automatic.in prep_rls' *)
   3.349 -     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
   3.350 -     scr      : scr};      (* Rfuns {...}  (how to restrict type ???)         *)
   3.351 -fun id_rls Erls = "e_rls" (*WN060714 quick and dirty: recursive defs! TODO "Erls" *)
   3.352 -  | id_rls (Rls {id, ...}) = id
   3.353 -  | id_rls (Seq {id, ...}) = id
   3.354 -  | id_rls (Rrls {id, ...}) = id;
   3.355 -val rls2str = id_rls;
   3.356 -fun id_rule (Thm (id, _)) = id
   3.357 -  | id_rule (Calc (id, _)) = id
   3.358 -  | id_rule (Cal1 (id, _)) = id
   3.359 -  | id_rule (Rls_ rls) = id_rls rls
   3.360 -  | id_rule Erule = "Erule";
   3.361 -fun eq_rule (Thm (thm1, _), Thm (thm2, _)) = thm1 = thm2
   3.362 -  | eq_rule (Calc (id1, _), Calc (id2, _)) = id1 = id2
   3.363 -  | eq_rule (Rls_ rls1, Rls_ rls2) = id_rls rls1 = id_rls rls2
   3.364 -  | eq_rule _ = false;
   3.365 -
   3.366 -(*ad thm':
   3.367 -   there are two kinds of theorems ...
   3.368 -   (1) known by isabelle
   3.369 -   (2) not known, eg. calc_thm, instantiated rls
   3.370 -       the latter have a thmid "#..."
   3.371 -   and thus outside isa we ALWAYS transport both (thmID, string_of_thmI)
   3.372 -   and have a special assoc_thm / assoc_rls in this interface      *)
   3.373 -type theory' = string; (* = domID ^".thy" WN.101011 ABOLISH !*)
   3.374 -type domID = string;   (* domID ^".thy" = theory' WN.101011 replace by thyID*)
   3.375 -type thyID = string;   (* WN.3.11.03 TODO: replace domID with thyID*)
   3.376 -val e_domID = "e_domID" : domID;
   3.377 -
   3.378 -(* Since Isabelle2017 sessions in theory identifiers are enforced.
   3.379 -  However, we leave theory identifiers short, in particular in use as keys into KEStore. *)
   3.380 -fun Thy_Info_get_theory thyID = Thy_Info.get_theory ("Isac." ^ thyID)
   3.381 -fun thy2ctxt' thy' = Proof_Context.init_global (Thy_Info_get_theory thy');(*FIXXXME thy-ctxt*)
   3.382 -fun thy2ctxt thy = Proof_Context.init_global thy;(*FIXXXME thy-ctxt*)
   3.383 -fun Isac _ = Proof_Context.theory_of (thy2ctxt' "Isac"); (*@{theory "Isac"}*)
   3.384 -
   3.385 -fun string_of_thy thy = Context.theory_name thy: theory';
   3.386 -val theory2domID = string_of_thy;
   3.387 -val theory2thyID = (get_thy o string_of_thy) : theory -> thyID;
   3.388 -val theory2theory' = string_of_thy;
   3.389 -val theory2str = string_of_thy; (*WN050903 ..most consistent naming*)
   3.390 -
   3.391 -fun thyID2theory' (thyID:thyID) = thyID;
   3.392 -fun theory'2thyID (theory':theory') = theory';
   3.393  (*  WN0509 discussion:
   3.394  #############################################################################
   3.395  #   How to manage theorys in subproblems wrt. the requirement,              #
   3.396 @@ -495,50 +270,16 @@
   3.397      # ???
   3.398  *)
   3.399  
   3.400 -
   3.401 -
   3.402 -
   3.403 -
   3.404  fun thm2str thm =
   3.405    let
   3.406      val t = Thm.prop_of thm
   3.407      val ctxt = Proof_Context.init_global (Thy_Info.get_theory ("Isac.Isac"))
   3.408      val ctxt' = Config.put show_markup false ctxt
   3.409    in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
   3.410 -fun term_to_string' ctxt t =
   3.411 -  let
   3.412 -    val ctxt' = Config.put show_markup false ctxt
   3.413 -  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
   3.414 -fun term_to_string'' thyID t =
   3.415 -  let
   3.416 -    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info_get_theory thyID))
   3.417 -  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
   3.418 -fun term_to_string''' thy t =
   3.419 -  let
   3.420 -    val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
   3.421 -  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
   3.422 -
   3.423 -fun term2str t = term_to_string' (thy2ctxt' "Isac") t;
   3.424 -fun t2str thy t = term_to_string' (thy2ctxt thy) t;
   3.425 -fun ts2str thy ts = ts |> map (t2str thy) |> strs2str';
   3.426 -fun terms2strs ts = map term2str ts;     (* terms2strs [t1,t2] = ["1 + 2", "abc"];      *)
   3.427 -val terms2str = strs2str o terms2strs;   (* terms2str  [t1,t2] = "[\"1 + 2\",\"abc\"]"; *)
   3.428 -val terms2str' = strs2str' o terms2strs; (* terms2str' [t1,t2] = "[1 + 2,abc]";         *)
   3.429 -fun termopt2str (SOME t) = "(SOME " ^ term2str t ^ ")"
   3.430 -  | termopt2str NONE = "NONE";
   3.431 -
   3.432 -fun type_to_string'' (thyID : thyID) t =
   3.433 -  let
   3.434 -    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info_get_theory thyID))
   3.435 -  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
   3.436 -fun type2str typ = type_to_string'' "Isac" typ; (*TODO legacy*)
   3.437 -val string_of_typ = type2str; (*legacy*)
   3.438 -fun string_of_typ_thy thy typ = type_to_string'' thy typ; (*legacy*)
   3.439                   
   3.440 -val e_rule = Thm ("refl", @{thm refl});
   3.441 -fun id_of_thm (Thm (id, _)) = id  (* TODO re-arrange code for rule2str *)
   3.442 +fun id_of_thm (Rule.Thm (id, _)) = id  (* TODO re-arrange code for rule2str *)
   3.443    | id_of_thm _ = raise ERROR ("id_of_thm: uncovered case " (* ^ rule2str r *))
   3.444 -fun thm_of_thm (Thm (_, thm)) = thm  (* TODO re-arrange code for rule2str *)
   3.445 +fun thm_of_thm (Rule.Thm (_, thm)) = thm  (* TODO re-arrange code for rule2str *)
   3.446    | thm_of_thm _ = raise ERROR ("thm_of_thm: uncovered case " (* ^ rule2str r *))
   3.447  
   3.448  fun thmID_of_derivation_name dn = last_elem (space_explode "." dn);
   3.449 @@ -546,41 +287,9 @@
   3.450  fun thyID_of_derivation_name dn = hd (space_explode "." dn);
   3.451  fun thm''_of_thm thm = (thmID_of_derivation_name' thm, thm) : thm''
   3.452  
   3.453 -(*check for [.] as caused by "fun assoc_thm'"*)
   3.454 -fun string_of_thm thm = term_to_string' (thy2ctxt' "Isac") (Thm.prop_of thm)
   3.455 -fun string_of_thm' thy thm = term_to_string' (thy2ctxt thy) (Thm.prop_of thm)
   3.456 -fun string_of_thmI thm =
   3.457 -  let 
   3.458 -    val str = (de_quote o string_of_thm) thm
   3.459 -    val (a, b) = split_nlast (5, Symbol.explode str)
   3.460 -  in 
   3.461 -    case b of
   3.462 -      [" ", " ","[", ".", "]"] => implode a
   3.463 -    | _ => str
   3.464 -  end
   3.465 +type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
   3.466 +  (term * term * Rule.rule list list * (Rule.rule * (term * term list)) list);
   3.467  
   3.468 -fun get_rules Erls = []
   3.469 -  | get_rules (Rls {rules, ...}) = rules
   3.470 -  | get_rules (Seq {rules, ...}) = rules
   3.471 -  | get_rules (Rrls _) = [];
   3.472 -fun rule2str Erule = "Erule"
   3.473 -  | rule2str (Thm (str, thm)) = "Thm (\""^str^"\","^(string_of_thmI thm)^")"
   3.474 -  | rule2str (Calc (str, _)) = "Calc (\""^str^"\",fn)"
   3.475 -  | rule2str (Cal1 (str, _)) = "Cal1 (\""^str^"\",fn)"
   3.476 -  | rule2str (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
   3.477 -fun rule2str' Erule = "Erule"
   3.478 -  | rule2str' (Thm (str, _)) = "Thm (\""^str^"\",\"\")"
   3.479 -  | rule2str' (Calc (str, _)) = "Calc (\""^str^"\",fn)"
   3.480 -  | rule2str' (Cal1 (str, _)) = "Cal1 (\""^str^"\",fn)"
   3.481 -  | rule2str' (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
   3.482 -
   3.483 -type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
   3.484 -  (term * term * rule list list * (rule * (term * term list)) list);
   3.485 -
   3.486 -val e_type = Type ("empty",[]);
   3.487 -val e_term = Const ("empty", e_type);
   3.488 -val e_rrlsstate = (e_term,e_term, [[e_rule]], [(e_rule, (e_term, []))]);
   3.489 -val e_term = Const ("empty", Type("'a", []));
   3.490  
   3.491  (*the key into the hierarchy ob theory elements*)
   3.492  type theID = string list;
   3.493 @@ -595,12 +304,12 @@
   3.494  
   3.495  (*the key into the hierarchy ob methods*)
   3.496  type metID = string list;
   3.497 -type spec = domID * pblID * metID;
   3.498 +type spec = Rule.domID * pblID * metID;
   3.499  fun spec2str (dom, pbl, met) = 
   3.500    "(" ^ quote dom  ^ ", " ^ strs2str pbl ^ ", " ^ strs2str met ^ ")";
   3.501  val e_metID = ["e_metID"];
   3.502  val metID2str = strs2str;
   3.503 -val empty_spec = (e_domID, e_pblID, e_metID);
   3.504 +val empty_spec = (Rule.e_domID, e_pblID, e_metID);
   3.505  val e_spec = empty_spec;
   3.506  
   3.507  (* association list with cas-commands, for generating a complete calc-head *)
   3.508 @@ -638,9 +347,9 @@
   3.509  (* rewrite orders, also stored in 'type met' and type 'and rls'
   3.510    The association list is required for 'rewrite.."rew_ord"..' *)
   3.511  val rew_ord' = Unsynchronized.ref
   3.512 -  ([("e_rew_ord", e_rew_ord), ("dummy_ord", dummy_ord)]
   3.513 -	: (rew_ord' *         (* the key for the association list         *)
   3.514 -	    (subst 	          (* the bound variables - they get high order*)
   3.515 +  ([("e_rew_ord", Rule.e_rew_ord), ("dummy_ord", Rule.dummy_ord)]
   3.516 +	: (Rule.rew_ord' *    (* the key for the association list         *)
   3.517 +	    (Rule.subst 	    (* the bound variables - they get high order*)
   3.518  	     -> (term * term) (* (t1, t2) to be compared                  *)
   3.519  	     -> bool))        (* if t1 <= t2 then true else false         *)
   3.520  		list);              (* association list                         *)
   3.521 @@ -661,10 +370,10 @@
   3.522    Html of {guh: guh, coursedesign: authors, mathauthors: authors, html: string}
   3.523  | Hthm of {guh: guh, coursedesign: authors, mathauthors: authors, fillpats: fillpat list,
   3.524     thm: thm} (* here no sym_thm, thus no thmID required *)
   3.525 -| Hrls of {guh: guh, coursedesign: authors, mathauthors: authors, thy_rls: (thyID * rls)}
   3.526 -| Hcal of {guh: guh, coursedesign: authors, mathauthors: authors, calc: calc}
   3.527 +| Hrls of {guh: guh, coursedesign: authors, mathauthors: authors, thy_rls: (Rule.thyID * Rule.rls)}
   3.528 +| Hcal of {guh: guh, coursedesign: authors, mathauthors: authors, calc: Rule.calc}
   3.529  | Hord of {guh: guh, coursedesign: authors, mathauthors: authors,
   3.530 -    ord: (subst -> (term * term) -> bool)};
   3.531 +    ord: (Rule.subst -> (term * term) -> bool)};
   3.532  fun the2str (Html {guh, ...}) = guh
   3.533    | the2str (Hthm {guh, ...}) = guh
   3.534    | the2str (Hrls {guh, ...}) = guh
   3.535 @@ -710,30 +419,30 @@
   3.536  (* convert the data got via contextToThy to a globally unique handle.
   3.537     there is another way to get the guh: get out of the 'theID' in the hierarchy *)
   3.538  fun thm2guh (isa, thyID) thmID = case isa of
   3.539 -    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID : guh
   3.540 -  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
   3.541 -  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
   3.542 +    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID : guh
   3.543 +  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
   3.544 +  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
   3.545    | _ => raise ERROR
   3.546      ("thm2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for thm = \"" ^ thmID ^ "\"");
   3.547  
   3.548  fun rls2guh (isa, thyID) rls' = case isa of
   3.549 -    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-rls-" ^ rls' : guh
   3.550 -  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-rls-" ^ rls'
   3.551 -  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-rls-" ^ rls'
   3.552 +    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-rls-" ^ rls' : guh
   3.553 +  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-rls-" ^ rls'
   3.554 +  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-rls-" ^ rls'
   3.555    | _ => raise ERROR
   3.556      ("rls2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for rls = \"" ^ rls' ^ "\"");
   3.557  			  
   3.558  fun cal2guh (isa, thyID) calID = case isa of
   3.559 -    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-cal-" ^ calID : guh
   3.560 -  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-cal-" ^ calID
   3.561 -  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-cal-" ^ calID
   3.562 +    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-cal-" ^ calID : guh
   3.563 +  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-cal-" ^ calID
   3.564 +  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-cal-" ^ calID
   3.565    | _ => raise ERROR
   3.566      ("cal2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for cal = \"" ^ calID ^ "\"");
   3.567  			  
   3.568  fun ord2guh (isa, thyID) rew_ord' = case isa of
   3.569 -    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord' : guh
   3.570 -  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord'
   3.571 -  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord'
   3.572 +    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-ord-" ^ rew_ord' : guh
   3.573 +  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-ord-" ^ rew_ord'
   3.574 +  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-ord-" ^ rew_ord'
   3.575    | _ => raise ERROR
   3.576      ("ord2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for ord = \"" ^ rew_ord' ^ "\"");
   3.577  
   3.578 @@ -772,105 +481,13 @@
   3.579  type path = string;
   3.580  type filename = string;
   3.581  
   3.582 -local
   3.583 -    fun ii (_: term) = e_rrlsstate;
   3.584 -    fun no (_: term) = SOME (e_term, [e_term]);
   3.585 -    fun lo (_: rule list list) (_: term) (_: rule) = [(e_rule, (e_term, [e_term]))];
   3.586 -    fun ne (_: rule list list) (_: term) = SOME e_rule;
   3.587 -    fun fo (_: rule list list) (_: term) (_: term) = [(e_rule, (e_term, [e_term]))];
   3.588 -in
   3.589 -val e_rfuns = Rfuns {init_state = ii, normal_form = no, locate_rule = lo,
   3.590 -		  next_rule = ne, attach_form = fo};
   3.591 -end;
   3.592 -val e_rls =
   3.593 -  Rls {id = "e_rls", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
   3.594 -    srls = Erls, calc = [], rules = [], errpatts = [], scr = EmptyScr}: rls;
   3.595 -val e_rrls =
   3.596 -  Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
   3.597 -    calc = [], errpatts = [], scr=e_rfuns}:rls;
   3.598  
   3.599 -fun rep_rls Erls = rep_rls e_rls
   3.600 -  | rep_rls (Rls {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
   3.601 -    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
   3.602 -      calc = calc, rules = rules, scr = scr}
   3.603 -  | rep_rls (Seq {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
   3.604 -    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
   3.605 -      calc = calc, rules = rules, scr = scr}
   3.606 -  | rep_rls (Rrls _)  = rep_rls e_rls
   3.607 -
   3.608 -fun append_rls id Erls _ = raise ERROR ("append_rls: with \"" ^ id ^ "\" not for Erls")
   3.609 -  | append_rls id (Rls {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.610 -			rules = rs, errpatts = errpatts, scr = sc}) r =
   3.611 -    Rls {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.612 -      rules = rs @ r, errpatts = errpatts, scr = sc}
   3.613 -  | append_rls id (Seq {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.614 -			rules = rs, errpatts = errpatts, scr = sc}) r =
   3.615 -    Seq {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.616 -      rules = rs @ r, errpatts = errpatts, scr = sc}
   3.617 -  | append_rls id (Rrls _) _ = raise ERROR ("append_rls: not for reverse-rewrite-rule-set " ^ id);
   3.618 -
   3.619 -
   3.620 -fun merge_ids rls1 rls2 =
   3.621 -  let 
   3.622 -    val id1 = (#id o rep_rls) rls1
   3.623 -    val id2 = (#id o rep_rls) rls2
   3.624 -  in
   3.625 -    if id1 = id2 then id1 else "merged_" ^ id1 ^ "_" ^ id2
   3.626 -  end
   3.627 -fun merge_rls _ Erls rls = rls
   3.628 -  | merge_rls _ rls Erls = rls
   3.629 -  | merge_rls _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
   3.630 -  | merge_rls _ _ (Rrls x) = Rrls x
   3.631 -  | merge_rls id
   3.632 -	  (Rls {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
   3.633 -	    rules = rs1, errpatts = eps1, scr = sc1, ...})
   3.634 -	  (Rls {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
   3.635 -	    rules = rs2, errpatts = eps2, ...})
   3.636 -    =
   3.637 -	  Rls {id = id, rew_ord = ro1, scr = sc1,
   3.638 -	    preconds = union (op =) pc1 pc2,	    
   3.639 -	    erls = merge_rls (merge_ids er1 er2) er1 er2,
   3.640 -	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
   3.641 -	    calc = union calc_eq ca1 ca2,
   3.642 -		  rules = union eq_rule rs1 rs2,
   3.643 -      errpatts = union (op =) eps1 eps2}
   3.644 -  | merge_rls id
   3.645 -	  (Seq {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
   3.646 -	    rules = rs1, errpatts = eps1, scr = sc1, ...})
   3.647 -	  (Seq {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
   3.648 -	    rules = rs2, errpatts = eps2, ...})
   3.649 -    =
   3.650 -	  Seq {id = id, rew_ord = ro1, scr = sc1,
   3.651 -	    preconds = union (op =) pc1 pc2,	    
   3.652 -	    erls = merge_rls (merge_ids er1 er2) er1 er2,
   3.653 -	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
   3.654 -	    calc = union calc_eq ca1 ca2,
   3.655 -		  rules = union eq_rule rs1 rs2,
   3.656 -      errpatts = union (op =) eps1 eps2}
   3.657 -  | merge_rls id _ _ = error ("merge_rls: \"" ^ id ^ 
   3.658 -    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Seq");
   3.659 -
   3.660 -(* used only for one hack TODO remove *)
   3.661 -fun remove_rls id (Rls {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.662 -		  rules = rs, errpatts = eps, scr = sc}) r =
   3.663 -    Rls {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.664 -	    rules = gen_rems eq_rule (rs, r),
   3.665 -	    errpatts = eps,
   3.666 -	    scr = sc}
   3.667 -  | remove_rls id (Seq {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.668 -		  rules = rs, errpatts = eps, scr = sc}) r =
   3.669 -    Seq {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
   3.670 -	    rules = gen_rems eq_rule (rs, r),
   3.671 -	    errpatts = eps,
   3.672 -	    scr = sc}
   3.673 -  | remove_rls id (Rrls _) _ = raise ERROR ("remove_rls: not for reverse-rewrite-rule-set "^id)
   3.674 -  | remove_rls _ rls _ = raise ERROR ("remove_rls called with " ^ rls2str rls);
   3.675  
   3.676  (* datastructure for KEStore_Elems, intermediate for thehier *)
   3.677  type rlss_elem = 
   3.678 -  (rls' *     (* identifier unique within Isac *)
   3.679 -  (theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
   3.680 -    rls))     (* ((#id o rep_rls) rls) = rls'   by coding discipline *)
   3.681 +  (rls' *          (* identifier unique within Isac *)
   3.682 +  (Rule.theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
   3.683 +    Rule.rls))     (* ((#id o rep_rls) rls) = rls'   by coding discipline *)
   3.684  fun rls_eq ((id1, (_, _)), (id2, (_, _))) = id1 = id2
   3.685  
   3.686  fun insert_merge_rls (re as (id, (thyID, r1)) : rlss_elem) ys = 
   3.687 @@ -878,7 +495,7 @@
   3.688        NONE => re :: ys
   3.689      | SOME (i, (_, (_, r2))) => 
   3.690        let
   3.691 -        val r12 = merge_rls id r1 r2
   3.692 +        val r12 = Rule.merge_rls id r1 r2
   3.693        in list_update ys i (id, (thyID, r12)) end
   3.694  fun merge_rlss (s1, s2) = fold insert_merge_rls s1 s2;
   3.695  
   3.696 @@ -888,8 +505,8 @@
   3.697  
   3.698  fun assoc_thy thy =
   3.699      if thy = "e_domID"
   3.700 -    then (Thy_Info_get_theory "Script") (*lower bound of Knowledge*)
   3.701 -    else (Thy_Info_get_theory thy) handle _ => error ("ME_Isa: thy \"" ^ thy ^ "\" not in system");
   3.702 +    then (Rule.Thy_Info_get_theory "Script") (*lower bound of Knowledge*)
   3.703 +    else (Rule.Thy_Info_get_theory thy) handle _ => error ("ME_Isa: thy \"" ^ thy ^ "\" not in system");
   3.704  
   3.705  (* overwrite an element in an association list and pair it with a thyID
   3.706     in order to create the thy_hierarchy;
   3.707 @@ -901,8 +518,8 @@
   3.708     in scripts...
   3.709     actually a hack to get alltogether run again with minimal effort *)
   3.710  fun insthy thy' (rls', rls) = (rls', (thy', rls));
   3.711 -fun overwritelthy thy (al, bl:(rls' * rls) list) =
   3.712 -    let val bl' = map (insthy ((get_thy o theory2theory') thy)) bl
   3.713 +fun overwritelthy thy (al, bl:(rls' * Rule.rls) list) =
   3.714 +    let val bl' = map (insthy ((get_thy o Rule.theory2theory') thy)) bl
   3.715      in overwritel (al, bl') end;
   3.716  
   3.717  fun assoc_rew_ord ro = ((the o assoc') (!rew_ord',ro))
   3.718 @@ -911,17 +528,13 @@
   3.719  fun subst2str s =
   3.720      (strs2str o
   3.721        (map (
   3.722 -        linefeed o pair2str o (apsnd term2str) o (apfst term2str)))) s;
   3.723 -fun subst2str' (s:subst) =
   3.724 +        linefeed o pair2str o (apsnd Rule.term2str) o (apfst Rule.term2str)))) s;
   3.725 +fun subst2str' s =
   3.726      (strs2str' o
   3.727       (map (
   3.728 -       pair2str o (apsnd term2str) o (apfst term2str)))) s;
   3.729 +       pair2str o (apsnd Rule.term2str) o (apfst Rule.term2str)))) s;
   3.730  val env2str = subst2str;
   3.731  
   3.732 -fun scr2str EmptyScr = "EmptyScr"
   3.733 -  | scr2str (Prog s) = "Prog " ^ term2str s
   3.734 -  | scr2str (Rfuns _)  = "Rfuns";
   3.735 -
   3.736  fun maxthy thy1 thy2 = if Context.subthy (thy1, thy2) then thy2 else thy1;
   3.737  
   3.738  
   3.739 @@ -956,7 +569,7 @@
   3.740  	  (term *     (* description         *)
   3.741  	     term))   (* id | arbitrary term *);
   3.742  fun pat2str ((field, (dsc, id)) : pat) = 
   3.743 -  pair2str (field, pair2str (term2str dsc, term2str id))
   3.744 +  pair2str (field, pair2str (Rule.term2str dsc, Rule.term2str id))
   3.745  fun pats2str pats = (strs2str o (map pat2str)) pats
   3.746  
   3.747  (* types for problems models (TODO rename to specification models) *)
   3.748 @@ -974,7 +587,7 @@
   3.749                          because applied terms may be from 'subthy' as well as from super;
   3.750                          thus we take 'maxthy'; see match_ags !                           *)
   3.751    cas : term option,  (* 'CAS-command'                                                   *)
   3.752 -  prls : rls,         (* for preds in where_                                             *)
   3.753 +  prls : Rule.rls,    (* for preds in where_                                             *)
   3.754    where_ : term list, (* where - predicates                                              *)
   3.755    ppc : pat list,     (* this is the model-pattern; 
   3.756                           it contains "#Given","#Where","#Find","#Relate"-patterns
   3.757 @@ -982,14 +595,14 @@
   3.758    met : metID list}   (* methods solving the pbt                                         *)
   3.759  
   3.760  val e_pbt = {guh = "pbl_empty", mathauthors = [], init = e_pblID, thy = Thy_Info.get_theory "Pure",
   3.761 -  cas = NONE, prls = Erls, where_ = [], ppc = [], met = []}
   3.762 +  cas = NONE, prls = Rule.Erls, where_ = [], ppc = [], met = []}
   3.763  fun pbt2str ({cas = cas', guh = guh', init = init', mathauthors = ma', met = met', ppc = ppc',
   3.764        prls = prls', thy = thy', where_ = w'} : pbt)
   3.765 -    = "{cas = " ^ (termopt2str cas') ^  ", guh = \"" ^ guh'  ^ "\", init = "
   3.766 +    = "{cas = " ^ (Rule.termopt2str cas') ^  ", guh = \"" ^ guh'  ^ "\", init = "
   3.767        ^ (strs2str init') ^ ", mathauthors = " ^ (strs2str ma' |> quote) ^ ", met = "
   3.768        ^ (strslist2strs met') ^ ", ppc = " ^ pats2str ppc' ^ ", prls = "
   3.769 -      ^ (rls2str prls' |> quote) ^ ", thy = {" ^ (theory2str thy') ^ "}, where_ = "
   3.770 -      ^ (terms2str w') ^ "}" |> linefeed;
   3.771 +      ^ (Rule.rls2str prls' |> quote) ^ ", thy = {" ^ (Rule.theory2str thy') ^ "}, where_ = "
   3.772 +      ^ (Rule.terms2str w') ^ "}" |> linefeed;
   3.773  fun pbts2str pbts = map pbt2str pbts |> list2str;
   3.774  
   3.775  val e_Ptyp = Ptyp ("e_pblID",[e_pbt],[])
   3.776 @@ -1053,32 +666,32 @@
   3.777  
   3.778  (* data for methods stored in 'methods'-database*)
   3.779  type met = 
   3.780 -     {guh        : guh,        (*unique within this isac-knowledge           *)
   3.781 -      mathauthors: string list,(*copyright                                   *)
   3.782 -      init       : pblID,      (*WN060721 introduced mistakenly--TODO.REMOVE!*)
   3.783 -      rew_ord'   : rew_ord',   (*for rules in Detail
   3.784 -			                           TODO.WN0509 store fun itself, see 'type pbt'*)
   3.785 -      erls       : rls,        (*the eval_rls for cond. in rules FIXME "rls'
   3.786 -				                         instead erls in "fun prep_met"              *)
   3.787 -      srls       : rls,        (*for evaluating list expressions in scr      *)
   3.788 -      prls       : rls,        (*for evaluating predicates in modelpattern   *)
   3.789 -      crls       : rls,        (*for check_elementwise, ie. formulae in calc.*)
   3.790 -      nrls       : rls,        (*canonical simplifier specific for this met  *)
   3.791 -      errpats    : errpat list,(*error patterns expected in this method      *)
   3.792 -      calc       : calc list,  (*Theory_Data in fun prep_met                 *)
   3.793 +     {guh        : guh,             (*unique within this isac-knowledge           *)
   3.794 +      mathauthors: string list,     (*copyright                                   *)
   3.795 +      init       : pblID,           (*WN060721 introduced mistakenly--TODO.REMOVE!*)
   3.796 +      rew_ord'   : Rule.rew_ord',   (*for rules in Detail
   3.797 +			                                TODO.WN0509 store fun itself, see 'type pbt'*)
   3.798 +      erls       : Rule.rls,        (*the eval_rls for cond. in rules FIXME "rls'
   3.799 +				                              instead erls in "fun prep_met"              *)
   3.800 +      srls       : Rule.rls,        (*for evaluating list expressions in scr      *)
   3.801 +      prls       : Rule.rls,        (*for evaluating predicates in modelpattern   *)
   3.802 +      crls       : Rule.rls,        (*for check_elementwise, ie. formulae in calc.*)
   3.803 +      nrls       : Rule.rls,        (*canonical simplifier specific for this met  *)
   3.804 +      errpats    : Rule.errpat list,(*error patterns expected in this method      *)
   3.805 +      calc       : Rule.calc list,  (*Theory_Data in fun prep_met                 *)
   3.806        (*branch   : TransitiveB set in append_problem at generation ob pblobj
   3.807 -          FIXXXME.0308: set branch from met in Apply_Method ?                *)
   3.808 +         FIXXXME.0308: set branch from met in Apply_Method ?                      *)
   3.809        ppc        : pat list,   (*.items in given, find, relate;
   3.810  	      items (in "#Find") which need not occur in the arg-list of a SubProblem
   3.811          are 'copy-named' with an identifier "*'.'".
   3.812          copy-named items are 'generating' if they are NOT "*'''" ?WN120516??
   3.813 -        see ME/calchead.sml 'fun is_copy_named'.                              *)
   3.814 -      pre        : term list,  (*preconditions in where                       *)
   3.815 -      scr        : scr         (*prep_met gets progam or string "empty_script"*)
   3.816 +        see ME/calchead.sml 'fun is_copy_named'.                                  *)
   3.817 +      pre        : term list,  (*preconditions in where                           *)
   3.818 +      scr        : Rule.scr    (*prep_met gets progam or string "empty_script"    *)
   3.819  	   };
   3.820  val e_met = {guh = "met_empty", mathauthors = [], init = e_metID, rew_ord' = "e_rew_ord'",
   3.821 -	erls = e_rls, srls = e_rls, prls = e_rls, calc = [], crls = e_rls, errpats = [], nrls= e_rls,
   3.822 -	ppc = [], pre = [], scr = EmptyScr};
   3.823 +	erls = Rule.e_rls, srls = Rule.e_rls, prls = Rule.e_rls, calc = [], crls = Rule.e_rls,
   3.824 +	errpats = [], nrls = Rule.e_rls, ppc = [], pre = [], scr = Rule.EmptyScr};
   3.825  val e_Mets = Ptyp ("e_metID", [e_met],[]);
   3.826  
   3.827  type mets = (met ptyp) list;
   3.828 @@ -1128,14 +741,14 @@
   3.829      let
   3.830        val rls' = 
   3.831          case rls of
   3.832 -          Rls {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
   3.833 -          => Rls {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
   3.834 +          Rule.Rls {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
   3.835 +          => Rule.Rls {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
   3.836                 calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
   3.837 -        | Seq {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
   3.838 -          => Seq {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
   3.839 +        | Rule.Seq {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
   3.840 +          => Rule.Seq {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
   3.841                 calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
   3.842 -        | Rrls {id, prepat, rew_ord, erls, calc, scr, ...}
   3.843 -          => Rrls {id = id, prepat = prepat, rew_ord = rew_ord, erls = erls, calc = calc,
   3.844 +        | Rule.Rrls {id, prepat, rew_ord, erls, calc, scr, ...}
   3.845 +          => Rule.Rrls {id = id, prepat = prepat, rew_ord = rew_ord, erls = erls, calc = calc,
   3.846                 scr = scr, errpatts = errpatIDs}
   3.847          | Erls => Erls
   3.848      in
   3.849 @@ -1174,7 +787,7 @@
   3.850    section "Get and group the theories defined in Isac" *) 
   3.851  fun isabthys () = (*["Complex_Main", "Taylor", .., "Pure"]*)
   3.852    let
   3.853 -    val allthys = Theory.ancestors_of (Thy_Info_get_theory "Build_Thydata")
   3.854 +    val allthys = Theory.ancestors_of (Rule.Thy_Info_get_theory "Build_Thydata")
   3.855    in
   3.856      drop ((find_index (curry Context.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), allthys)
   3.857    end
   3.858 @@ -1183,15 +796,15 @@
   3.859      fun isacthys () = (* ["Isac", .., "KEStore"] without Build_Isac thys: "Interpret" etc *)
   3.860        let
   3.861          val allthys = filter_out (member Context.eq_thy
   3.862 -          [(*Thy_Info_get_theory "ProgLang",*) Thy_Info_get_theory "Interpret", 
   3.863 -            Thy_Info_get_theory "xmlsrc", Thy_Info_get_theory "Frontend"]) 
   3.864 -          (Theory.ancestors_of (Thy_Info_get_theory "Build_Thydata"))
   3.865 +          [(*Thy_Info_get_theory "ProgLang",*) Rule.Thy_Info_get_theory "Interpret", 
   3.866 +            Rule.Thy_Info_get_theory "xmlsrc", Rule.Thy_Info_get_theory "Frontend"]) 
   3.867 +          (Theory.ancestors_of (Rule.Thy_Info_get_theory "Build_Thydata"))
   3.868        in
   3.869          take ((find_index (curry Context.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), 
   3.870          allthys)
   3.871        end
   3.872      val isacthys' = isacthys ()
   3.873 -    val proglang_parent = Thy_Info_get_theory "ProgLang"
   3.874 +    val proglang_parent = Rule.Thy_Info_get_theory "ProgLang"
   3.875    in
   3.876      take ((find_index (curry Context.eq_thy proglang_parent) isacthys'), isacthys')
   3.877    end
   3.878 @@ -1199,17 +812,17 @@
   3.879  fun progthys () = (*["Isac", .., "Descript", "Delete"]*)
   3.880    let
   3.881      fun isacthys () = (* ["Isac", .., "KEStore"] without Build_Isac thys: "Interpret" etc *)
   3.882 -      let
   3.883 +      let                                                        
   3.884          val allthys = filter_out (member Context.eq_thy
   3.885 -          [(*Thy_Info_get_theory "ProgLang",*) Thy_Info_get_theory "Interpret", 
   3.886 -            Thy_Info_get_theory "xmlsrc", Thy_Info_get_theory "Frontend"]) 
   3.887 -          (Theory.ancestors_of (Thy_Info_get_theory "Build_Thydata"))
   3.888 +          [(*Thy_Info_get_theory "ProgLang",*) Rule.Thy_Info_get_theory "Interpret", 
   3.889 +            Rule.Thy_Info_get_theory "xmlsrc", Rule.Thy_Info_get_theory "Frontend"]) 
   3.890 +          (Theory.ancestors_of (Rule.Thy_Info_get_theory "Build_Thydata"))
   3.891        in
   3.892          take ((find_index (curry Context.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), 
   3.893          allthys)
   3.894        end
   3.895      val isacthys' = isacthys ()
   3.896 -    val proglang_parent = Thy_Info_get_theory "ProgLang"
   3.897 +    val proglang_parent = Rule.Thy_Info_get_theory "ProgLang"
   3.898    in
   3.899      drop ((find_index (curry Context.eq_thy proglang_parent) isacthys') + 1(*ProgLang*), isacthys')
   3.900    end
   3.901 @@ -1218,8 +831,8 @@
   3.902    if member Context.eq_thy (knowthys ()) thy then "IsacKnowledge"
   3.903    else if member Context.eq_thy (progthys ()) thy then "IsacScripts"
   3.904    else if member Context.eq_thy (isabthys ()) thy then "Isabelle"
   3.905 -  else error ("closure of thys in Isac is broken by " ^ string_of_thy thy)
   3.906 -fun partID' (thy' : theory') = partID (Thy_Info_get_theory thy')
   3.907 +  else error ("closure of thys in Isac is broken by " ^ Rule.string_of_thy thy)
   3.908 +fun partID' thy' = partID (Rule.Thy_Info_get_theory thy')
   3.909  
   3.910  end (*struct*)
   3.911