src/Tools/isac/calcelems.sml
author Mathias Lehnfeld <s1210629013@students.fh-hagenberg.at>
Wed, 22 Jan 2014 15:47:14 +0100
changeset 55344 2a421c3a8b47
parent 55339 cccd24e959ba
child 55345 dfc5633a7ddd
permissions -rw-r--r--
pbt2str functions updated
neuper@37906
     1
(* elements of calculations.
neuper@37906
     2
   they are partially held in association lists as ref's for
neuper@37906
     3
   switching language levels (meta-string, object-values).
neuper@37906
     4
   in order to keep these ref's during re-evaluation of code,
neuper@42399
     5
   they are defined here at the beginning of the code.
neuper@42399
     6
   Author: Walther Neuper 2003
neuper@42399
     7
   (c) copyright due to lincense terms
neuper@37906
     8
*)
neuper@37906
     9
neuper@38061
    10
(*
neuper@38061
    11
structure CalcElems =
neuper@38061
    12
struct
neuper@38061
    13
*)
neuper@37906
    14
val linefeed = (curry op^) "\n";
neuper@37906
    15
type authors = string list;
neuper@37906
    16
neuper@37906
    17
type cterm' = string;
neuper@37906
    18
val empty_cterm' = "empty_cterm'";
neuper@42399
    19
neuper@42399
    20
type thmID = string;    (* identifier for a thm (the shortest possible identifier)       *)
neuper@42434
    21
type thmDeriv = string; (* WN120524 deprecated
neuper@42434
    22
  thyID ^"."^ xxx ^"."^ thmID, see fun thmID_of_derivation_name 
neuper@42434
    23
  WN120524: dont use Thm.derivation_name, this is destroyed by num_str;
neuper@42434
    24
  Thm.get_name_hint survives num_str and seems perfectly reliable *)
neuper@42399
    25
neuper@37906
    26
type thm' = thmID * cterm';(*WN060610 deprecated in favour of thm''*)
neuper@37906
    27
type thm'' = thmID * term;
neuper@37906
    28
type rls' = string;
neuper@42399
    29
neuper@40836
    30
(*.a 'guh'='globally unique handle' is a string unique for each element
neuper@37906
    31
   of isac's KEStore and persistent over time
neuper@37906
    32
   (in particular under shifts within the respective hierarchy);
neuper@40836
    33
   specialty for thys:
neuper@37906
    34
   # guh NOT resistant agains shifts from one thy to another
neuper@37906
    35
   (which is the price for Isabelle's design: thy's overwrite ids of subthy's)
neuper@37906
    36
   # requirement for matchTheory: induce guh from tac + current thy
neuper@37906
    37
   (see 'fun thy_containing_thm', 'fun thy_containing_rls' etc.)
neuper@40836
    38
   TODO: introduce to pbl, met.*)
neuper@37906
    39
type guh = string;
neuper@37906
    40
val e_guh = "e_guh":guh;
neuper@37906
    41
neuper@37906
    42
type xml = string;
neuper@37906
    43
neuper@52154
    44
(* eval function calling sml code during rewriting.
neuper@52154
    45
Unifying "type cal" and "type calc" would make Lucas-Interpretation more efficient,
neuper@52154
    46
  see "fun rule2stac": instead of 
neuper@52154
    47
    Calc: calID * eval_fn -> rule
neuper@52154
    48
  would be better
neuper@52154
    49
    Calc: prog_calcID * (calID * eval_fn)) -> rule*)
neuper@37906
    50
type eval_fn = (string -> term -> theory -> (string * term) option);
neuper@37906
    51
fun e_evalfn (_:'a) (_:term) (_:theory) = NONE:(string * term) option;
neuper@37906
    52
(*. op in isa-term 'Const(op,_)' .*)
neuper@37906
    53
type calID = string;
neuper@52154
    54
neuper@52154
    55
(* *)
neuper@37906
    56
type cal = (calID * eval_fn);
neuper@37906
    57
(*. fun calculate_ fetches the evaluation-function via this list. *)
neuper@52117
    58
type prog_calcID = string;
neuper@52117
    59
type calc = (prog_calcID * cal);
neuper@52141
    60
type calc_elem =
neuper@52141
    61
  prog_calcID *   (* a simple identifier used in programs *)
neuper@52141
    62
  (calID *        (* a long identifier used in Const *)
neuper@52141
    63
    eval_fn)      (* an ML function *)
neuper@52141
    64
fun calc_eq ((pi1, (ci1, _)) : calc_elem, (pi2, (ci2, _)) : calc_elem) =
neuper@52141
    65
  if pi1 = pi2
neuper@52141
    66
  then if ci1 = ci2 then true else error ("calc_eq: " ^ ci1 ^ " <> " ^ ci2)
neuper@52141
    67
  else false
neuper@52141
    68
neuper@37906
    69
neuper@37906
    70
type subs' = (cterm' * cterm') list; (*16.11.00 for FE-KE*)
neuper@37906
    71
type subst = (term * term) list; (*here for ets2str*)
neuper@37906
    72
val e_subst = []:(term * term) list;
neuper@37906
    73
neuper@37906
    74
(*TODO.WN060610 make use of "type rew_ord" total*)
neuper@37906
    75
type rew_ord' = string;
neuper@37906
    76
val e_rew_ord' = "e_rew_ord" : rew_ord';
neuper@37906
    77
type rew_ord_ = subst -> Term.term * Term.term -> bool;
neuper@37906
    78
fun dummy_ord (_:subst) (_:term,_:term) = true;
neuper@37906
    79
val e_rew_ord_ = dummy_ord;
neuper@37906
    80
type rew_ord = rew_ord' * rew_ord_;
neuper@37906
    81
val e_rew_ord = dummy_ord; (* TODO.WN071231 clarify identifiers..e_rew_ordX*)
neuper@37906
    82
val e_rew_ordX = (e_rew_ord', e_rew_ord_) : rew_ord;
neuper@37906
    83
neuper@42451
    84
(* error patterns and fill patterns *)
neuper@42451
    85
type errpatID = string
neuper@42451
    86
type errpat =
neuper@42451
    87
  errpatID    (* one identifier for a list of patterns                       
neuper@42451
    88
                 DESIGN ?TODO: errpatID list for hierarchy of errpats ?      *)
neuper@42451
    89
  * term list (* error patterns                                              *)
neuper@42451
    90
  * thm list  (* thms related to error patterns; note that respective lhs 
neuper@42451
    91
                 do not match (which reflects student's error).
neuper@42451
    92
                 fillpatterns are stored with these thms.                    *)
neuper@42451
    93
neuper@42451
    94
(* for (at least) 2 kinds of access:
neuper@42451
    95
  (1) given an errpatID, find the respective fillpats (e.g. in fun find_fill_pats)
neuper@42451
    96
  (2) given a thm, find respective fillpats *)
neuper@42451
    97
type fillpatID = string
neuper@42451
    98
type fillpat =
neuper@42451
    99
  fillpatID   (* DESIGN ?TODO: give an order w.r.t difficulty ? *)
neuper@42451
   100
  * term      (* the pattern with fill-in gaps                  *)
neuper@42451
   101
  * errpatID; (* which the fillpat would be a help for          
neuper@42451
   102
                 DESIGN ?TODO: list for several patterns ?      *)
neuper@37906
   103
neuper@40836
   104
datatype rule =
neuper@37906
   105
  Erule                (*.the empty rule                     .*)
neuper@40836
   106
| Thm of (string * Basic_Thm.thm)(*.a theorem, ie (identifier, Thm.thm).*)
neuper@37906
   107
| Calc of string *     (*.sml-code manipulating a (sub)term  .*)
neuper@37906
   108
	  (string -> term -> theory -> (string * term) option)
neuper@37906
   109
| Cal1 of string *     (*.sml-code applied only to whole term
neuper@37906
   110
                          or left/right-hand-side of eqality .*)
neuper@37906
   111
	  (string -> term -> theory -> (string * term) option)
neuper@37906
   112
| Rls_ of rls          (*.ie. rule sets may be nested.*)
neuper@40836
   113
and scr =
neuper@40836
   114
    EmptyScr
neuper@52084
   115
  | Prog of term (* for met *)
neuper@52101
   116
  | Rfuns of     (* for Rrls, usage see rational.sml ----- reverse rewrite -----      *)
neuper@52084
   117
    {init_state : (* initialise for reverse rewriting by the Interpreter              *)
neuper@52084
   118
      term ->         (* for this the rrlsstate is initialised:                       *)
neuper@52084
   119
      term *          (* the current formula: goes locate_gen -> next_tac via istate  *)
neuper@52084
   120
      term *          (* the final formula                                            *)
neuper@52084
   121
      rule list       (* of reverse rewrite set (#1#)                                 *)
neuper@52084
   122
        list *        (*   may be serveral, eg. in norm_rational                      *)
neuper@52084
   123
      ( rule *        (* Thm (+ Thm generated from Calc) resulting in ...             *)
neuper@52084
   124
        (term *       (*   ... rewrite with ...                                       *)
neuper@52084
   125
        term list))   (*   ... assumptions                                            *)
neuper@52084
   126
      list,           (* derivation from given term to normalform
neuper@52084
   127
                         in reverse order with sym_thm;
neuper@52084
   128
                                                (#1#) could be extracted from here #1 *)  
neuper@52084
   129
	  normal_form:  (* the function which drives the Rrls ##############################*)
neuper@52084
   130
	    term -> (term * term list) option,
neuper@52084
   131
	  locate_rule:  (* checks a rule R for being a cancel-rule, and if it is,
neuper@52084
   132
                     then return the list of rules (+ the terms they are rewriting to)
neuper@52084
   133
                     which need to be applied before R should be applied.
neuper@52084
   134
                     precondition: the rule is applicable to the argument-term.       *)
neuper@52084
   135
	    rule list list -> (* the reverse rule list                                      *)
neuper@52084
   136
	    term ->         (* ... to which the rule shall be applied                       *)
neuper@52084
   137
	    rule ->         (* ... to be applied to term                                    *)
neuper@52084
   138
	    ( rule *        (* value: a rule rewriting to ...                               *)
neuper@52084
   139
	      (term *       (*   ... the resulting term ...                                 *)
neuper@52084
   140
	      term list))   (*   ... with the assumptions ( //#0)                           *)
neuper@52084
   141
	    list,           (*   there may be several such rules; the list is empty,          
neuper@52084
   142
                           if the rule has nothing to do with e.g. cancelation        *)
neuper@52084
   143
	  next_rule:    (* for a given term return the next rules to be done for cancelling *)
neuper@52084
   144
	    rule list list->(* the reverse rule list                                        *)
neuper@52084
   145
	    term ->         (* the term for which ...                                       *)
neuper@52084
   146
	    rule option,    (* ... this rule is appropriate for cancellation;
neuper@52084
   147
		                     there may be no such rule (if the term is eg.canceled already*)
neuper@52084
   148
	  attach_form:  (* checks an input term TI, if it may belong to e.g. a current 
neuper@52084
   149
                     cancellation, by trying to derive it from the given term TG.     
neuper@52084
   150
                     NOT IMPLEMENTED                                                 *)
neuper@52084
   151
	    rule list list->(**)
neuper@52084
   152
	    term ->         (* TG, the last one agreed upon by user + math-eng              *)
neuper@52084
   153
	    term ->         (* TI, the next one input by the user                           *)
neuper@52084
   154
	    ( rule *        (* the rule to be applied in order to reach TI                  *) 
neuper@52084
   155
	      (term *       (* ... obtained by applying the rule ...                        *)
neuper@52084
   156
	      term list))   (* ... and the respective assumptions                           *) 
neuper@52084
   157
	    list}           (* there may be several such rules; the list is empty, if the 
neuper@52084
   158
                         users term does not belong to e.g. a cancellation of the term 
neuper@52084
   159
                         last agreed upon.                                            *)
neuper@37906
   160
and rls =
neuper@37906
   161
    Erls                          (*for init e_rls*)
neuper@40836
   162
neuper@37906
   163
  | Rls of (*a confluent and terminating ruleset, in general         *)
neuper@37906
   164
    {id : string,          (*for trace_rewrite:=true                 *)
neuper@37906
   165
     preconds : term list, (*unused WN020820                         *)
neuper@37906
   166
     (*WN060616 for efficiency...
neuper@37906
   167
      bdvs    : false,       (*set in prep_rls for get_bdvs *)*)
neuper@37906
   168
     rew_ord  : rew_ord,   (*for rules*)
neuper@37906
   169
     erls     : rls,       (*for the conditions in rules             *)
neuper@37906
   170
     srls     : rls,       (*for evaluation of list_fns in script    *)
neuper@37906
   171
     calc     : calc list, (*for Calculate in scr, set by prep_rls   *)
neuper@37906
   172
     rules    : rule list,
neuper@42451
   173
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
neuper@48763
   174
     scr      : scr}       (*Prog term: generating intermed.steps  *)
neuper@37906
   175
  | Seq of (*a sequence of rules to be tried only once               *)
neuper@37906
   176
    {id : string,          (*for trace_rewrite:=true                 *)
neuper@37906
   177
     preconds : term list, (*unused 20.8.02                          *)
neuper@37906
   178
     (*WN060616 for efficiency...
neuper@37906
   179
      bdvs    : false,       (*set in prep_rls for get_bdvs *)*)
neuper@37906
   180
     rew_ord  : rew_ord,   (*for rules                               *)
neuper@37906
   181
     erls     : rls,       (*for the conditions in rules             *)
neuper@37906
   182
     srls     : rls,       (*for evaluation of list_fns in script    *)
neuper@40836
   183
     calc     : calc list, (*for Calculate in scr, set by prep_rls   *)
neuper@37906
   184
     rules    : rule list,
neuper@42451
   185
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
neuper@48763
   186
     scr      : scr}  (*Prog term  (how to restrict type ???)*)
neuper@52084
   187
neuper@37906
   188
  (*Rrls call SML-code and simulate an rls
neuper@37906
   189
    difference: there is always _ONE_ redex rewritten in 1 call,
neuper@37906
   190
    thus wrap Rrls by: Rls (Rls_ ...)*)
neuper@52084
   191
  | Rrls of (* SML-functions within rewriting; step-wise execution provided;   
neuper@52084
   192
               Rrls simulate an rls
neuper@52084
   193
               difference: there is always _ONE_ redex rewritten in 1 call,
neuper@52084
   194
               thus wrap Rrls by: Rls (Rls_ ...)                              *)
neuper@38036
   195
    {id : string,          (* for trace_rewrite := true                       *)
neuper@40836
   196
     prepat  : (term list *(* preconds, eval with subst from pattern;
neuper@52084
   197
                              if [@{term True}], match decides alone          *)
neuper@52084
   198
		            term )     (* pattern matched with current (sub)term          *)
neuper@52084
   199
		   list,               (* meta-conjunction is or                          *)
neuper@38036
   200
     rew_ord  : rew_ord,   (* for rules                                       *)
neuper@38036
   201
     erls     : rls,       (* for the conditions in rules and preconds        *)
neuper@38036
   202
     calc     : calc list, (* for Calculate in scr, set automatic.in prep_rls *)
neuper@42451
   203
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
neuper@38036
   204
     scr      : scr};      (* Rfuns {...}  (how to restrict type ???)         *)
neuper@37906
   205
neuper@48760
   206
fun thy2ctxt' thy' = Proof_Context.init_global (Thy_Info.get_theory thy');(*FIXXXME thy-ctxt*)
neuper@48760
   207
fun thy2ctxt thy = Proof_Context.init_global thy;(*FIXXXME thy-ctxt*)
neuper@37906
   208
neuper@48760
   209
fun Isac _ = Proof_Context.theory_of (thy2ctxt' "Isac"); (*@{theory "Isac"}*)
neuper@48760
   210
                                     
neuper@40836
   211
val e_rule = Thm ("refl", @{thm refl});
neuper@37906
   212
fun id_of_thm (Thm (id, _)) = id
neuper@52101
   213
  | id_of_thm _ = error "error id_of_thm";
neuper@37906
   214
fun thm_of_thm (Thm (_, thm)) = thm
neuper@52101
   215
  | thm_of_thm _ = error "error thm_of_thm";
neuper@37906
   216
fun rep_thm_G' (Thm (thmid, thm)) = (thmid, thm);
neuper@38061
   217
neuper@38061
   218
fun thmID_of_derivation_name dn = last_elem (space_explode "." dn);
neuper@38061
   219
fun thyID_of_derivation_name dn = hd (space_explode "." dn);
neuper@38061
   220
neuper@37906
   221
fun eq_thmI ((thmid1 : thmID, _ : thm), (thmid2 : thmID, _ : thm)) =
neuper@37906
   222
    (strip_thy thmid1) = (strip_thy thmid2);
neuper@42372
   223
(*WN120201 weakened*)
neuper@42372
   224
fun eq_thmI ((thmid1 : thmID, _ : thm), (thmid2 : thmID, _)) = thmid1 = thmid2;
neuper@38002
   225
(*version typed weaker WN100910*)
neuper@38002
   226
fun eq_thmI' ((thmid1, _), (thmid2, _)) =
neuper@38061
   227
    (thmID_of_derivation_name thmid1) = (thmID_of_derivation_name thmid2);
neuper@37906
   228
neuper@42437
   229
val string_of_thm =  Thm.get_name_hint; (*FIXME.2009*)
neuper@42437
   230
fun thm'_of_thm thm =
neuper@42437
   231
  ((thmID_of_derivation_name o Thm.get_name_hint) thm, ""): thm'
neuper@37906
   232
neuper@37906
   233
(*check for [.] as caused by "fun assoc_thm'"*)
neuper@37906
   234
fun string_of_thmI thm =
neuper@37906
   235
    let val ct' = (de_quote o string_of_thm) thm
neuper@40836
   236
	val (a, b) = split_nlast (5, Symbol.explode ct')
neuper@40836
   237
    in case b of
neuper@37906
   238
	   [" ", " ","[", ".", "]"] => implode a
neuper@37906
   239
	 | _ => ct'
neuper@37906
   240
    end;
neuper@37906
   241
neuper@37906
   242
(*.id requested for all, Rls,Seq,Rrls.*)
neuper@37906
   243
fun id_rls Erls = "e_rls" (*WN060714 quick and dirty: recursive defs!*)
neuper@37906
   244
  | id_rls (Rls {id,...}) = id
neuper@37906
   245
  | id_rls (Seq {id,...}) = id
neuper@37906
   246
  | id_rls (Rrls {id,...}) = id;
neuper@37906
   247
val rls2str = id_rls;
neuper@37906
   248
fun id_rule (Thm (id, _)) = id
neuper@37906
   249
  | id_rule (Calc (id, _)) = id
neuper@37906
   250
  | id_rule (Rls_ rls) = id_rls rls;
neuper@37906
   251
neuper@37906
   252
fun get_rules (Rls {rules,...}) = rules
neuper@37906
   253
  | get_rules (Seq {rules,...}) = rules
neuper@37906
   254
  | get_rules (Rrls _) = [];
neuper@37906
   255
neuper@37906
   256
fun rule2str Erule = "Erule"
neuper@37906
   257
  | rule2str (Thm (str, thm)) = "Thm (\""^str^"\","^(string_of_thmI thm)^")"
neuper@37906
   258
  | rule2str (Calc (str,f))  = "Calc (\""^str^"\",fn)"
neuper@37906
   259
  | rule2str (Cal1 (str,f))  = "Cal1 (\""^str^"\",fn)"
neuper@37906
   260
  | rule2str (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
neuper@37906
   261
fun rule2str' Erule = "Erule"
neuper@37906
   262
  | rule2str' (Thm (str, thm)) = "Thm (\""^str^"\",\"\")"
neuper@37906
   263
  | rule2str' (Calc (str,f))  = "Calc (\""^str^"\",fn)"
neuper@37906
   264
  | rule2str' (Cal1 (str,f))  = "Cal1 (\""^str^"\",fn)"
neuper@37906
   265
  | rule2str' (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
neuper@37906
   266
neuper@37906
   267
(*WN080102 compare eq_rule ?!?*)
neuper@37906
   268
fun eqrule (Thm (id1,_), Thm (id2,_)) = id1 = id2
neuper@37906
   269
  | eqrule (Calc (id1,_), Calc (id2,_)) = id1 = id2
neuper@37906
   270
  | eqrule (Cal1 (id1,_), Cal1 (id2,_)) = id1 = id2
neuper@37906
   271
  | eqrule (Rls_ _, Rls_ _) = false (*{id=id1}{id=id2} = id1 = id2 FIXXME*)
neuper@37906
   272
  | eqrule _ = false;
neuper@37906
   273
neuper@52084
   274
type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
neuper@52084
   275
  (term * term * rule list list * (rule * (term * term list)) list);
neuper@37906
   276
neuper@37906
   277
val e_type = Type("empty",[]);
neuper@37906
   278
val a_type = TFree("'a",[]);
neuper@37906
   279
val e_term = Const("empty",e_type);
neuper@37906
   280
val a_term = Free("empty",a_type);
neuper@37906
   281
val e_rrlsstate = (e_term,e_term,[[e_rule]],[(e_rule,(e_term,[]))]):rrlsstate;
neuper@37906
   282
neuper@37906
   283
val e_term = Const("empty", Type("'a", []));
neuper@48763
   284
val e_scr = Prog e_term;
neuper@37906
   285
neuper@37906
   286
(*ad thm':
neuper@37906
   287
   there are two kinds of theorems ...
neuper@37906
   288
   (1) known by isabelle
neuper@37906
   289
   (2) not known, eg. calc_thm, instantiated rls
neuper@37906
   290
       the latter have a thmid "#..."
neuper@37906
   291
   and thus outside isa we ALWAYS transport both (thmid,string_of_thmI)
neuper@37906
   292
   and have a special assoc_thm / assoc_rls in this interface      *)
neuper@38058
   293
type theory' = string; (* = domID ^".thy" WN.101011 ABOLISH !*)
neuper@38058
   294
type domID = string;   (* domID ^".thy" = theory' WN.101011 replace by thyID*)
neuper@37906
   295
type thyID = string;    (*WN.3.11.03 TODO: replace domID with thyID*)
neuper@37906
   296
neuper@37906
   297
fun string_of_thy thy = Context.theory_name thy: theory';
neuper@37906
   298
val theory2domID = string_of_thy;
neuper@37906
   299
val theory2thyID = (get_thy o string_of_thy) : theory -> thyID;
neuper@37906
   300
val theory2theory' = string_of_thy;
neuper@37906
   301
val theory2str = string_of_thy; (*WN050903 ..most consistent naming*)
neuper@40836
   302
val theory2str' = implode o (drop_last_n 4) o Symbol.explode o string_of_thy;
neuper@48795
   303
(* fun theory'2theory = fun thyID2thy ... see fun assoc_thy (...Thy_Info.get_theory string);
neuper@37906
   304
al it = "Isac" : string
neuper@37906
   305
*)
neuper@37906
   306
neuper@38058
   307
fun thyID2theory' (thyID:thyID) = thyID;
neuper@38058
   308
(*
neuper@40836
   309
    let val ss = Symbol.explode thyID
neuper@37906
   310
	val ext = implode (takelast (4, ss))
neuper@37906
   311
    in if ext = ".thy" then thyID : theory' (*disarm abuse of thyID*)
neuper@37906
   312
       else thyID ^ ".thy"
neuper@37906
   313
    end;
neuper@38058
   314
*)
neuper@37906
   315
(* thyID2theory' "Isac" (*ok*);
neuper@38050
   316
val it = "Isac" : theory'
neuper@38050
   317
 > thyID2theory' "Isac" (*abuse, goes ok...*);
neuper@38050
   318
val it = "Isac" : theory'
neuper@37906
   319
*)
neuper@48880
   320
neuper@38058
   321
fun theory'2thyID (theory':theory') = theory';
neuper@38058
   322
(*
neuper@40836
   323
    let val ss = Symbol.explode theory'
neuper@37906
   324
	val ext = implode (takelast (4, ss))
neuper@37906
   325
    in if ext = ".thy" then ((implode o (drop_last_n 4)) ss) : thyID
neuper@37906
   326
       else theory' (*disarm abuse of theory'*)
neuper@37906
   327
    end;
neuper@38058
   328
*)
neuper@38050
   329
(* theory'2thyID "Isac";
neuper@37906
   330
val it = "Isac" : thyID
neuper@37906
   331
> theory'2thyID "Isac";
neuper@37906
   332
val it = "Isac" : thyID*)
neuper@37906
   333
neuper@37906
   334
neuper@37906
   335
(*. WN0509 discussion:
neuper@37906
   336
#############################################################################
neuper@37906
   337
#   How to manage theorys in subproblems wrt. the requirement,              #
neuper@37906
   338
#   that scripts should be re-usable ?                                      #
neuper@37906
   339
#############################################################################
neuper@37906
   340
neuper@37988
   341
    eg. 'Script Solve_rat_equation' calls 'SubProblem (RatEq',..'
neuper@37906
   342
    which would not allow to 'solve (y'' = -M_b / EI, M_b)' by this script
neuper@40836
   343
    because Biegelinie.thy is subthy of RatEq.thy and thus Biegelinie.M_b
neuper@37906
   344
    is unknown in RatEq.thy and M_b cannot be parsed into the scripts guard
neuper@37906
   345
    (see match_ags).
neuper@37906
   346
neuper@37906
   347
    Preliminary solution:
neuper@38011
   348
    # the thy in 'SubProblem (thy', pbl, arglist)' is not taken automatically,
neuper@38011
   349
    # instead the 'maxthy (rootthy pt) thy' is taken for each subpbl
neuper@40836
   350
    # however, a thy specified by the user in the rootpbl may lead to
neuper@40836
   351
      errors in far-off subpbls (which are not yet reported properly !!!)
neuper@37906
   352
      and interactively specifiying thys in subpbl is not very relevant.
neuper@37906
   353
neuper@37906
   354
    Other solutions possible:
neuper@40836
   355
    # always parse and type-check with Thy_Info.get_theory "Isac"
neuper@37906
   356
      (rejected tue to the vague idea eg. to re-use equations for R in C etc.)
neuper@37906
   357
    # regard the subthy-relation in specifying thys of subpbls
neuper@38011
   358
    # specifically handle 'SubProblem (undefined, pbl, arglist)'
neuper@37906
   359
    # ???
neuper@37906
   360
.*)
neuper@40836
   361
(*WN0509 TODO "ProtoPure" ... would be more consistent
neuper@37906
   362
  with assoc_thy <--> theory2theory' +FIXME assoc_thy "e_domID" -> Script.thy*)
neuper@37906
   363
val e_domID = "e_domID":domID;
neuper@37906
   364
neuper@37906
   365
(*the key into the hierarchy ob theory elements*)
neuper@37906
   366
type theID = string list;
neuper@37906
   367
val e_theID = ["e_theID"];
neuper@37906
   368
val theID2str = strs2str;
neuper@37906
   369
(*theID eg. is ["IsacKnowledge", "Test", "Rulesets", "ac_plus_times"]*)
neuper@37906
   370
fun theID2thyID (theID:theID) =
neuper@37906
   371
    if length theID >= 3 then (last_elem o (drop_last_n 2)) theID : thyID
neuper@38031
   372
    else error ("theID2thyID called with "^ theID2str theID);
neuper@37906
   373
neuper@37906
   374
(*the key into the hierarchy ob problems*)
neuper@37906
   375
type pblID = string list; (* domID::...*)
neuper@37906
   376
val e_pblID = ["e_pblID"]:pblID;
neuper@37906
   377
val pblID2str = strs2str;
neuper@37906
   378
neuper@37906
   379
(*the key into the hierarchy ob methods*)
neuper@37906
   380
type metID = string list;
neuper@37906
   381
val e_metID = ["e_metID"]:metID;
neuper@37906
   382
val metID2str = strs2str;
neuper@37906
   383
s1210629013@52166
   384
type spec = 
s1210629013@52166
   385
     domID * (*WN.12.03: is replaced by thy from get_met ?FIXME? in:
s1210629013@52166
   386
	      specify (Init_Proof..), nxt_specify_init_calc,
s1210629013@52166
   387
	      assod (.SubProblem...), stac2tac (.SubProblem...)*)
s1210629013@52166
   388
     pblID * 
s1210629013@52166
   389
     metID;
s1210629013@52166
   390
s1210629013@52171
   391
fun spec2str ((dom,pbl,met)(*:spec*)) = 
s1210629013@52171
   392
  "(" ^ (quote dom) ^ ", " ^ (strs2str pbl) ^ 
s1210629013@52171
   393
  ", " ^ (strs2str met) ^ ")";
s1210629013@52171
   394
(*> spec2str empty_spec;
s1210629013@52171
   395
val it = "(\"\", [], (\"\", \"\"))" : string *)
s1210629013@52171
   396
val empty_spec = (e_domID,e_pblID,e_metID):spec;
s1210629013@52171
   397
val e_spec = empty_spec;
s1210629013@52171
   398
s1210629013@52166
   399
(*.association list with cas-commands, for generating a complete calc-head.*)
neuper@52169
   400
type generate_fn = 
neuper@52169
   401
  (term list ->    (* the arguments of the cas-command, eg. (x+1=2, x) *)
neuper@52169
   402
    (term *        (* description of an element                        *)
neuper@52169
   403
      term list)   (* value of the element (always put into a list)    *)
neuper@52169
   404
  list)            (* of elements in the formalization                 *)
neuper@52169
   405
type cas_elem = 
neuper@52169
   406
  (term *          (* cas-command, eg. 'solve'                         *)
neuper@52169
   407
    (spec *        (* theory, problem, method                          *)
s1210629013@52174
   408
    generate_fn))
neuper@52169
   409
fun cas_eq ((t1, (_, _)) : cas_elem, (t2, (_, _)) : cas_elem) = t1 = t2
s1210629013@52167
   410
neuper@37906
   411
(*either theID or pblID or metID*)
neuper@37906
   412
type kestoreID = string list;
neuper@37906
   413
val e_kestoreID = ["e_kestoreID"];
neuper@37906
   414
val kestoreID2str = strs2str;
neuper@37906
   415
neuper@48892
   416
(*for distinction of contexts WN130621: disambiguate with Isabelle's Context !*)
neuper@37906
   417
datatype ketype = Exp_ | Thy_ | Pbl_ | Met_;
neuper@37906
   418
fun ketype2str Exp_ = "Exp_"
neuper@40836
   419
  | ketype2str Thy_ = "Thy_"
neuper@40836
   420
  | ketype2str Pbl_ = "Pbl_"
neuper@37906
   421
  | ketype2str Met_ = "Met_";
neuper@37906
   422
fun ketype2str' Exp_ = "Example"
neuper@40836
   423
  | ketype2str' Thy_ = "Theory"
neuper@40836
   424
  | ketype2str' Pbl_ = "Problem"
neuper@37906
   425
  | ketype2str' Met_ = "Method";
neuper@37906
   426
neuper@37906
   427
(*see 'How to manage theorys in subproblems' at 'type thyID'*)
neuper@38006
   428
val theory'  = Unsynchronized.ref ([]:(theory' * theory) list);
neuper@37906
   429
neuper@42400
   430
(* theories for html representation: Isabelle, Knowledge, ProgLang *)
neuper@42400
   431
val isabthys = Unsynchronized.ref ([] : theory list);
neuper@42400
   432
val knowthys = Unsynchronized.ref ([] : theory list);
neuper@42400
   433
val progthys = Unsynchronized.ref ([] : theory list);
neuper@37906
   434
neuper@37906
   435
(*rewrite orders, also stored in 'type met' and type 'and rls'
neuper@37906
   436
  The association list is required for 'rewrite.."rew_ord"..'
neuper@37947
   437
  WN0509 tests not well-organized: see smltest/Knowledge/termorder.sml*)
neuper@38007
   438
val rew_ord' =
neuper@38007
   439
    Unsynchronized.ref
neuper@38007
   440
        ([]:(rew_ord' *        (*the key for the association list         *)
neuper@37906
   441
	     (subst 	       (*the bound variables - they get high order*)
neuper@37906
   442
	      -> (term * term) (*(t1, t2) to be compared                  *)
neuper@37906
   443
	      -> bool))        (*if t1 <= t2 then true else false         *)
neuper@37906
   444
		list);         (*association list                         *)
neuper@38061
   445
neuper@37906
   446
rew_ord' := overwritel (!rew_ord', [("e_rew_ord", e_rew_ord),
neuper@37906
   447
				    ("dummy_ord", dummy_ord)]);
neuper@37906
   448
neuper@37906
   449
neuper@48887
   450
(* A tree for storing data defined in different theories 
neuper@48887
   451
  for access from the Interpreter and from dialogue authoring 
neuper@48887
   452
  using a string list as key.
neuper@48887
   453
  'a is for pbt | met | thydata; after WN030424 naming became inappropriate *)
neuper@40836
   454
datatype 'a ptyp =
neuper@48887
   455
	Ptyp of string * (* element of the key *)
neuper@48887
   456
		'a list *      (* several pbts with different domIDs/thy TODO: select by subthy (isaref.p.69)
neuper@48887
   457
			                presently only _ONE_ elem FOR ALL KINDS OF CONTENT pbt | met | thydata *)
neuper@48887
   458
		('a ptyp) list;(* the children nodes *)
neuper@37906
   459
neuper@37906
   460
(*.datatype for collecting thydata for hierarchy.*)
neuper@37906
   461
(*WN060720 more consistent naming would be 'type thyelem' or 'thelem'*)
neuper@37906
   462
(*WN0606 Htxt contains html which does not belong to the sml-kernel*)
neuper@37906
   463
datatype thydata = Html of {guh: guh,
neuper@37906
   464
			    coursedesign: authors,
neuper@37906
   465
			    mathauthors: authors,
neuper@37906
   466
			    html: string} (*html; for demos before database*)
neuper@37906
   467
		 | Hthm of {guh: guh,
neuper@37906
   468
			    coursedesign: authors,
neuper@37906
   469
			    mathauthors: authors,
neuper@42429
   470
			    fillpats: fillpat list,
neuper@38002
   471
			    thm: term}
neuper@37906
   472
		 | Hrls of {guh: guh,
neuper@37906
   473
			    coursedesign: authors,
neuper@37906
   474
			    mathauthors: authors,
neuper@37906
   475
			    thy_rls: (thyID * rls)}
neuper@37906
   476
		 | Hcal of {guh: guh,
neuper@37906
   477
			    coursedesign: authors,
neuper@37906
   478
			    mathauthors: authors,
neuper@37906
   479
			    calc: calc}
neuper@37906
   480
		 | Hord of {guh: guh,
neuper@37906
   481
			    coursedesign: authors,
neuper@37906
   482
			    mathauthors: authors,
neuper@37906
   483
			    ord: (subst -> (term * term) -> bool)};
neuper@37906
   484
val e_thydata = Html {guh="e_guh", coursedesign=[], mathauthors=[], html=""};
neuper@37906
   485
neuper@37906
   486
type thehier = (thydata ptyp) list;
neuper@38061
   487
val thehier = Unsynchronized.ref ([] : thehier); (*WN101011 make argument, del*)
neuper@37906
   488
neuper@40836
   489
(* an association list, gets the value once in Isac.ML.
neuper@38061
   490
   stores Isabelle's thms as terms for compatibility with Theory.axioms_of.
neuper@38061
   491
   WN1-1-28 make this data arguments and del ref ?*)
neuper@42400
   492
val isab_thm_thy = Unsynchronized.ref ([] : (thmDeriv * term) list);
neuper@42400
   493
val isabthys = Unsynchronized.ref ([] : theory list);
neuper@42400
   494
neuper@42400
   495
val first_ProgLang_thy = Unsynchronized.ref (@{theory Pure});
neuper@42400
   496
val first_Knowledge_thy = Unsynchronized.ref (@{theory Pure});
neuper@37906
   497
neuper@37906
   498
neuper@37906
   499
type path = string;
neuper@37906
   500
type filename = string;
neuper@37906
   501
neuper@37906
   502
(*val xxx = fn: a b => (a,b);   ??? fun-definition ???*)
neuper@37906
   503
local
neuper@37906
   504
    fun ii (_:term) = e_rrlsstate;
neuper@37906
   505
    fun no (_:term) = SOME (e_term,[e_term]);
neuper@37906
   506
    fun lo (_:rule list list) (_:term) (_:rule) = [(e_rule,(e_term,[e_term]))];
neuper@37906
   507
    fun ne (_:rule list list) (_:term) = SOME e_rule;
neuper@37906
   508
    fun fo (_:rule list list) (_:term) (_:term) = [(e_rule,(e_term,[e_term]))];
neuper@37906
   509
in
neuper@37906
   510
val e_rfuns = Rfuns {init_state=ii,normal_form=no,locate_rule=lo,
neuper@37906
   511
		     next_rule=ne,attach_form=fo};
neuper@37906
   512
end;
neuper@37906
   513
neuper@42458
   514
val e_rls =
neuper@42458
   515
  Rls {id = "e_rls", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
neuper@42458
   516
    srls = Erls, calc = [], rules = [], errpatts = [], scr = EmptyScr}: rls;
neuper@42458
   517
val e_rrls =
neuper@42458
   518
  Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
neuper@42458
   519
    calc = [], errpatts = [], scr=e_rfuns}:rls;
neuper@42451
   520
neuper@42451
   521
fun rep_rls (Rls {id,preconds,rew_ord,erls,srls,calc,errpatts,rules,scr}) =
neuper@37906
   522
  {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
neuper@37906
   523
   (*asm_thm=asm_thm,*)rules=rules,scr=scr}
neuper@42451
   524
  | rep_rls (Seq {id,preconds,rew_ord,erls,srls,calc,errpatts,rules,scr}) =
neuper@37906
   525
  {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
neuper@37906
   526
   (*asm_thm=asm_thm,*)rules=rules,scr=scr}
neuper@37906
   527
  | rep_rls Erls = rep_rls e_rls
neuper@37906
   528
  | rep_rls (Rrls {id,...})  = rep_rls e_rls
neuper@38031
   529
    (*error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id)*);
neuper@40836
   530
(*| rep_rls (Seq {id,...})  =
neuper@38031
   531
    error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id);
neuper@37906
   532
--1.7.03*)
neuper@42451
   533
fun rep_rrls (Rrls {id, calc, erls, prepat, rew_ord, errpatts,
neuper@42451
   534
	      scr = Rfuns {attach_form, init_state, locate_rule, next_rule, normal_form}}) =
neuper@42451
   535
  {id=id, calc=calc, erls=erls, prepat=prepat,
neuper@42451
   536
     rew_ord=rew_ord, errpatts=errpatts, attach_form=attach_form, init_state=init_state,
neuper@37906
   537
     locate_rule=locate_rule, next_rule=next_rule, normal_form=normal_form}
neuper@40836
   538
  | rep_rrls (Rls {id,...}) =
neuper@38031
   539
    error ("rep_rrls doesn't take apart (normal) rule-sets: "^id)
neuper@40836
   540
  | rep_rrls (Seq {id,...}) =
neuper@38031
   541
    error ("rep_rrls doesn't take apart (normal) rule-sets: "^id);
neuper@37906
   542
neuper@37906
   543
fun append_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   544
			rules =rs, errpatts=errpatts, scr=sc}) r =
neuper@37906
   545
    (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   546
	 rules = rs @ r, errpatts=errpatts, scr=sc}:rls)
neuper@37906
   547
  | append_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   548
			rules =rs, errpatts=errpatts, scr=sc}) r =
neuper@37906
   549
    (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   550
	 rules = rs @ r, errpatts=errpatts, scr=sc}:rls)
neuper@40836
   551
  | append_rls id (Rrls _) _ =
neuper@38031
   552
    error ("append_rls: not for reverse-rewrite-rule-set "^id);
neuper@37906
   553
neuper@37906
   554
(*. are _atomic_ rules equal ?.*)
neuper@37906
   555
(*WN080102 compare eqrule ?!?*)
neuper@37906
   556
fun eq_rule (Thm (thm1,_), Thm (thm2,_)) = thm1 = thm2
neuper@37906
   557
  | eq_rule (Calc (id1,_), Calc (id2,_)) = id1 = id2
neuper@37906
   558
  | eq_rule (Rls_ rls1, Rls_ rls2) = id_rls rls1 = id_rls rls2
neuper@37906
   559
  (*id_rls checks for Rls, Seq, Rrls*)
neuper@37906
   560
  | eq_rule _ = false;
neuper@37906
   561
neuper@52141
   562
fun merge_ids rls1 rls2 =
neuper@52141
   563
  let 
neuper@52141
   564
    val id1 = (#id o rep_rls) rls1
neuper@52141
   565
    val id2 = (#id o rep_rls) rls2
neuper@52141
   566
  in
neuper@52141
   567
    if id1 = id2 then id1
neuper@52141
   568
    else "merged_" ^ id1 ^ "_" ^ id2
neuper@52141
   569
  end
neuper@37906
   570
fun merge_rls _ Erls rls = rls
neuper@37906
   571
  | merge_rls _ rls Erls = rls
neuper@52141
   572
  | merge_rls _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
neuper@52141
   573
  | merge_rls _ _ (Rrls x) = Rrls x
neuper@37906
   574
  | merge_rls id
neuper@52141
   575
	  (Rls {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
neuper@52141
   576
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
neuper@52141
   577
	  (Rls {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
neuper@52141
   578
	    rules = rs2, errpatts = eps2, ...})
neuper@52141
   579
    =
neuper@52141
   580
	  (Rls {id = id, rew_ord = ro1, scr = sc1,
neuper@52141
   581
	    preconds = union (op =) pc1 pc2,	    
neuper@52141
   582
	    erls = merge_rls (merge_ids er1 er2) er1 er2,
neuper@52141
   583
	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
neuper@52141
   584
	    calc = union calc_eq ca1 ca2,
neuper@52141
   585
		  rules = union eq_rule rs1 rs2,
neuper@52141
   586
      errpatts = union (op =) eps1 eps2} : rls)
neuper@37906
   587
  | merge_rls id
neuper@52141
   588
	  (Seq {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
neuper@52141
   589
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
neuper@52141
   590
	  (Seq {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
neuper@52141
   591
	    rules = rs2, errpatts = eps2, ...})
neuper@52141
   592
    =
neuper@52141
   593
	  (Seq {id = id, rew_ord = ro1, scr = sc1,
neuper@52141
   594
	    preconds = union (op =) pc1 pc2,	    
neuper@52141
   595
	    erls = merge_rls (merge_ids er1 er2) er1 er2,
neuper@52141
   596
	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
neuper@52141
   597
	    calc = union calc_eq ca1 ca2,
neuper@52141
   598
		  rules = union eq_rule rs1 rs2,
neuper@52141
   599
      errpatts = union (op =) eps1 eps2} : rls)
neuper@52141
   600
  | merge_rls id _ _ = error ("merge_rls: \"" ^ id ^ 
neuper@52141
   601
    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Seq");
neuper@42451
   602
neuper@37906
   603
fun remove_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   604
		     rules=rs, errpatts=eps, scr=sc}) r =
neuper@37906
   605
    (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   606
	   rules = gen_rems eq_rule (rs, r),
neuper@42451
   607
	   errpatts = eps(*gen_rems op= (eps, TODO)*),
neuper@37906
   608
	 scr=sc}:rls)
neuper@37906
   609
  | remove_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   610
		     rules =rs, errpatts=eps, scr=sc}) r =
neuper@37906
   611
    (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   612
	   rules = gen_rems eq_rule (rs, r),
neuper@42451
   613
	   errpatts = eps(*gen_rems op= (eps, TODO)*),
neuper@37906
   614
	 scr=sc}:rls)
neuper@40836
   615
  | remove_rls id (Rrls _) _ = error
neuper@37906
   616
                   ("remove_rls: not for reverse-rewrite-rule-set "^id);
neuper@37906
   617
neuper@52141
   618
(* datastructure for KEStore_Elems, intermediate for thehier *)
neuper@52141
   619
type rlss_elem = 
neuper@52141
   620
  (rls' *     (* identifier unique within Isac *)
neuper@52141
   621
  (theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
neuper@52141
   622
    rls))     (* ((#id o rep_rls) rls) = rls'   by coding discipline *)
neuper@52141
   623
fun rls_eq ((id1, (_, _)), (id2, (_, _))) = id1 = id2
neuper@37906
   624
neuper@52141
   625
fun insert_merge_rls (re as (id, (thyID, r1)) : rlss_elem) ys = 
neuper@52141
   626
  case get_index (fn y => if curry rls_eq re y then SOME y else NONE) ys of
neuper@52141
   627
    NONE => re :: ys
neuper@52141
   628
  | SOME (i, (_, (_, r2))) => 
neuper@52141
   629
      let
neuper@52141
   630
        val r12 = merge_rls id r1 r2
neuper@52141
   631
      in list_update ys i (id, (thyID, r12)) end
neuper@52141
   632
neuper@52141
   633
fun merge_rlss (s1, s2) = fold insert_merge_rls s1 s2;
neuper@52141
   634
neuper@52141
   635
neuper@37906
   636
fun memrls r (Rls {rules,...}) = gen_mem eqrule (r, rules)
neuper@37906
   637
  | memrls r (Seq {rules,...}) = gen_mem eqrule (r, rules)
neuper@38031
   638
  | memrls r _ = error ("memrls: incomplete impl. r= "^(rule2str r));
neuper@37906
   639
neuper@38031
   640
fun assoc' ([], key) = error ("ME_Isa: '"^key^"' not known")
neuper@37906
   641
  | assoc' ((keyi, xi) :: pairs, key) =
neuper@37906
   642
      if key = keyi then SOME xi else assoc' (pairs, key);
neuper@37906
   643
neuper@37927
   644
(*100818 fun assoc_thy (thy:theory') = ((the o assoc')(!theory',thy))
neuper@38031
   645
  handle _ => error ("ME_Isa: thy '"^thy^"' not in system");*)
neuper@38056
   646
fun assoc_thy (thy:theory') =
neuper@40836
   647
    if thy = "e_domID" then (Thy_Info.get_theory "Script") (*lower bound of Knowledge*)
neuper@40836
   648
    else (Thy_Info.get_theory thy)
neuper@38056
   649
         handle _ => error ("ME_Isa: thy '" ^ thy ^ "' not in system");
neuper@40836
   650
neuper@37906
   651
(*.overwrite an element in an association list and pair it with a thyID
neuper@37906
   652
   in order to create the thy_hierarchy;
neuper@37906
   653
   overwrites existing rls' even if they are defined in a different thy;
neuper@37906
   654
   this is related to assoc_rls, TODO.WN060120: assoc_rew_ord, assoc_calc;.*)
neuper@40836
   655
(*WN060120 ...these are NOT compatible to "fun assoc_thm'" in that
neuper@37906
   656
   they do NOT handle overlays by re-using an identifier in different thys;
neuper@37906
   657
   "thyID.rlsID" would be a good solution, if the "." would be possible
neuper@37906
   658
   in scripts...
neuper@37906
   659
   actually a hack to get alltogether run again with minimal effort*)
neuper@37906
   660
fun insthy thy' (rls', rls) = (rls', (thy', rls));
neuper@37906
   661
fun overwritelthy thy (al, bl:(rls' * rls) list) =
neuper@37906
   662
    let val bl' = map (insthy ((get_thy o theory2theory') thy)) bl
neuper@37906
   663
    in overwritel (al, bl') end;
neuper@37906
   664
neuper@37906
   665
fun assoc_rew_ord ro = ((the o assoc') (!rew_ord',ro))
neuper@38031
   666
  handle _ => error ("ME_Isa: rew_ord '"^ro^"' not in system");
neuper@37906
   667
(*get the string for stac from rule*)
neuper@37906
   668
neuper@52070
   669
fun term_to_string' ctxt t =
neuper@52065
   670
  let
neuper@52070
   671
    val ctxt' = Config.put show_markup false ctxt
neuper@52070
   672
  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
neuper@52070
   673
fun term_to_string'' (thyID : thyID) t =
neuper@52070
   674
  let
neuper@52070
   675
    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info.get_theory thyID))
neuper@52070
   676
  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
neuper@52070
   677
fun term_to_string''' thy t =
neuper@52070
   678
  let
neuper@52070
   679
    val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
neuper@52065
   680
  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
neuper@38051
   681
neuper@52070
   682
fun term_to_string t = term_to_string'' "Isac" t;
neuper@52070
   683
fun term2str t = term_to_string'' "Isac" t; (*legacy*)
neuper@38022
   684
fun terms2str ts = (strs2str o (map term2str )) ts;
neuper@38022
   685
(*terms2str [t1,t2] = "[\"1 + 2\",\"abc\"]";*)
neuper@38022
   686
fun terms2str' ts = (strs2str' o (map term2str )) ts;
neuper@38022
   687
(*terms2str' [t1,t2] = "[1 + 2,abc]";*)
neuper@38022
   688
fun terms2strs ts = (map term2str) ts;
neuper@38022
   689
(*terms2strs [t1,t2] = ["1 + 2", "abc"];*)
neuper@38013
   690
neuper@38053
   691
fun termopt2str (SOME t) = "SOME " ^ term2str t
neuper@38053
   692
  | termopt2str NONE = "NONE";
neuper@38053
   693
neuper@52070
   694
fun type_to_string' ctxt t =
neuper@52070
   695
  let
neuper@52070
   696
    val ctxt' = Config.put show_markup false ctxt
neuper@52070
   697
  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
neuper@52070
   698
fun type_to_string'' (thyID : thyID) t =
neuper@52070
   699
  let
neuper@52070
   700
    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info.get_theory thyID))
neuper@52070
   701
  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
neuper@52070
   702
fun type_to_string''' thy t =
neuper@52070
   703
  let
neuper@52070
   704
    val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
neuper@52070
   705
  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
neuper@37906
   706
neuper@52070
   707
fun type2str typ = type_to_string'' "Isac" typ; (*legacy*)
neuper@52070
   708
val string_of_typ = type2str; (*legacy*)
neuper@52070
   709
fun string_of_typ_thy thy typ = type_to_string'' thy typ; (*legacy*)
neuper@52070
   710
                 
neuper@40836
   711
fun subst2str (s:subst) =
neuper@40836
   712
    (strs2str o
neuper@37906
   713
     (map (linefeed o pair2str o
neuper@40836
   714
	   (apsnd term2str) o
neuper@37906
   715
	   (apfst term2str)))) s;
neuper@40836
   716
fun subst2str' (s:subst) =
neuper@40836
   717
    (strs2str' o
neuper@37906
   718
     (map (pair2str o
neuper@40836
   719
	   (apsnd term2str) o
neuper@37906
   720
	   (apfst term2str)))) s;
neuper@37906
   721
(*> subst2str' [(str2term "bdv", str2term "x"),
neuper@37906
   722
		(str2term "bdv_2", str2term "y")];
neuper@37906
   723
val it = "[(bdv, x)]" : string
neuper@37906
   724
*)
neuper@37906
   725
val env2str = subst2str;
neuper@37906
   726
neuper@37906
   727
neuper@37906
   728
(*recursive defs:*)
neuper@48763
   729
fun scr2str (Prog s) = "Prog " ^ term2str s
neuper@37906
   730
  | scr2str (Rfuns _)  = "Rfuns";
neuper@37906
   731
neuper@37906
   732
neuper@37906
   733
fun maxthy thy1 thy2 = if Theory.subthy (thy1, thy2) then thy2 else thy1;
neuper@37906
   734
neuper@37906
   735
neuper@37906
   736
(*.trace internal steps of isac's rewriter*)
neuper@38006
   737
val trace_rewrite = Unsynchronized.ref false;
neuper@37906
   738
(*.depth of recursion in traces of the rewriter, if trace_rewrite:=true.*)
neuper@38006
   739
val depth = Unsynchronized.ref 99999;
neuper@37906
   740
(*.no of rewrites exceeding this int -> NO rewrite.*)
neuper@37906
   741
(*WN060829 still unused...*)
neuper@38006
   742
val lim_rewrite = Unsynchronized.ref 99999;
neuper@37906
   743
(*.no of derivation-elements exceeding this int -> SOME derivation-elements.*)
neuper@38006
   744
val lim_deriv = Unsynchronized.ref 100;
neuper@37906
   745
(*.switch for checking guhs unique before storing a pbl or met;
neuper@37906
   746
   set true at startup (done at begin of ROOT.ML)
neuper@37906
   747
   set false for editing IsacKnowledge (done at end of ROOT.ML).*)
neuper@41905
   748
val check_guhs_unique = Unsynchronized.ref true;
neuper@37906
   749
neuper@37906
   750
neuper@37906
   751
datatype lrd = (*elements of a path (=loc_) into an Isabelle term*)
neuper@40836
   752
	 L     (*go left at $*)
neuper@37906
   753
       | R     (*go right at $*)
neuper@37906
   754
       | D;     (*go down at Abs*)
neuper@37906
   755
type loc_ = lrd list;
neuper@37906
   756
fun ldr2str L = "L"
neuper@37906
   757
  | ldr2str R = "R"
neuper@37906
   758
  | ldr2str D = "D";
neuper@37906
   759
fun loc_2str (k:loc_) = (strs2str' o (map ldr2str)) k;
neuper@37906
   760
neuper@55335
   761
neuper@55335
   762
(* the pattern for an item of a problems model or a methods guard *)
neuper@55335
   763
type pat =
neuper@55335
   764
  (string *     (* field               *)
neuper@55335
   765
	  (term *     (* description         *)
neuper@55335
   766
	     term))   (* id | arbitrary term *);
neuper@55335
   767
fun pat2str ((field, (dsc, id)) : pat) = 
neuper@55335
   768
  pair2str (field, pair2str (term2str dsc, term2str id))
neuper@55335
   769
fun pats2str pats = (strs2str o (map pat2str)) pats
neuper@55335
   770
neuper@55335
   771
(* types for problems models *)
neuper@55335
   772
type pbt_ =
neuper@55335
   773
  (string *   (* field "#Given",..*)(*deprecated due to 'type pat'*)
neuper@55335
   774
    (term *   (* description      *)
neuper@55335
   775
      term)); (* id | struct-var  *)
neuper@55335
   776
val e_pbt_ = ("#Undef", (e_term, e_term)) : pbt_
neuper@55335
   777
type pbt = 
neuper@55335
   778
  {guh : guh,         (* unique within this isac-knowledge *)
neuper@55335
   779
  mathauthors : string list, (* copyright *)
neuper@55335
   780
  init : pblID,       (* to start refinement with *)
neuper@55335
   781
  thy  : theory,      (* which allows to compile that pbt
neuper@55335
   782
                      TODO: search generalized for subthy (ref.p.69*)
neuper@55335
   783
                      (*^^^ WN050912 NOT used during application of the problem,
neuper@55335
   784
                      because applied terms may be from 'subthy' as well as from super;
neuper@55335
   785
                      thus we take 'maxthy'; see match_ags !*)
neuper@55335
   786
  cas : term option,  (* 'CAS-command'*)
neuper@55335
   787
  prls : rls,         (* for preds in where_*)
neuper@55335
   788
  where_ : term list, (* where - predicates*)
neuper@55335
   789
  ppc : pat list,     (* this is the model-pattern; 
neuper@55335
   790
                       it contains "#Given","#Where","#Find","#Relate"-patterns
neuper@55335
   791
                       for constraints on identifiers see "fun cpy_nam" *)
neuper@55335
   792
  met : metID list}   (* methods solving the pbt*)
neuper@55335
   793
neuper@55335
   794
val e_pbt = {guh = "pbl_empty", mathauthors = [], init = e_pblID, thy = Thy_Info.get_theory "Pure",
neuper@55335
   795
  cas = NONE, prls = Erls, where_ = [], ppc = [], met = []} : pbt
s1210629013@55344
   796
fun pbt2str
s1210629013@55344
   797
        ({cas = cas', guh = guh', init = init', mathauthors = ma', met = met', ppc = ppc',
s1210629013@55344
   798
          prls = prls', thy = thy', where_ = where'} : pbt) =
s1210629013@55344
   799
      "{cas = " ^ (termopt2str cas') ^  ", guh = \"" ^ guh'  ^ "\", init = "
s1210629013@55344
   800
      ^ (strs2str init') ^ ", mathauthors = \"" ^ (strs2str ma') ^ "\", met = "
s1210629013@55344
   801
      ^ (strslist2strs met') ^ ", ppc = " ^ pats2str ppc' ^ ", prls = \""
s1210629013@55344
   802
      ^ (rls2str prls') ^ "\", thy = {" ^ Context.theory_name thy'
s1210629013@55344
   803
      ^ "}, where_ = " ^ (terms2str where') ^ "}" |> linefeed;
s1210629013@55344
   804
fun pbts2str pbts = map pbt2str pbts |> strs2str;
neuper@55335
   805
neuper@55335
   806
val e_Ptyp = Ptyp ("e_pblID",[e_pbt],[])
neuper@55335
   807
type ptyps = (pbt ptyp) list
neuper@55335
   808
val ptyps = Unsynchronized.ref ([e_Ptyp] : ptyps);
neuper@55335
   809
s1210629013@55339
   810
s1210629013@55339
   811
fun insrt _ pbt [k] [] = [Ptyp (k, [pbt],[])]
s1210629013@55339
   812
  | insrt d pbt [k] ((Ptyp (k', [p], ps))::pys) =
s1210629013@55339
   813
((*tracing("### insert 1: ks = "^(strs2str [k])^"    k'= "^k');*)
s1210629013@55339
   814
     if k=k'
s1210629013@55339
   815
     then ((Ptyp (k', [pbt], ps))::pys)
s1210629013@55339
   816
     else (*ev.newly added pbt is free _only_ with 'last_elem pblID'*)
s1210629013@55339
   817
	 ((Ptyp (k', [p], ps))::(insrt d pbt [k] pys))
s1210629013@55339
   818
)			 
s1210629013@55339
   819
  | insrt d pbt (k::ks) ((Ptyp (k', [p], ps))::pys) =
s1210629013@55339
   820
((*tracing("### insert 2: ks = "^(strs2str (k::ks))^"    k'= "^k');*)
s1210629013@55339
   821
     if k=k'
s1210629013@55339
   822
     then ((Ptyp (k', [p], insrt d pbt ks ps))::pys)
s1210629013@55339
   823
     else 
s1210629013@55339
   824
	 if length pys = 0
s1210629013@55339
   825
	 then error ("insert: not found "^(strs2str (d:pblID)))
s1210629013@55339
   826
	 else ((Ptyp (k', [p], ps))::(insrt d pbt (k::ks) pys))
s1210629013@55339
   827
);
s1210629013@55339
   828
s1210629013@55339
   829
(* this function only works wrt. the way Isabelle evaluates Theories and is not a general merge
s1210629013@55339
   830
  function for trees / ptyps *)
s1210629013@55339
   831
fun merge_ptyps ([], pt) = pt
s1210629013@55339
   832
  | merge_ptyps (pt, []) = pt
s1210629013@55339
   833
  | merge_ptyps ((Ptyp (k, x, ps)::xs), (ys' as Ptyp (k', y, ps')::ys)) =
s1210629013@55339
   834
      if k = k' then
s1210629013@55339
   835
        (Ptyp (k, y, merge_ptyps (ps, ps')) :: merge_ptyps (xs, ys)) else
s1210629013@55339
   836
        (Ptyp (k, x, ps) :: merge_ptyps (xs, ys'));
s1210629013@55339
   837
neuper@55335
   838
(* types for methods *)
neuper@55335
   839
neuper@55335
   840
neuper@38061
   841
(*
neuper@38061
   842
end (*struct*)
neuper@38061
   843
*)