src/Tools/isac/calcelems.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 10 Oct 2016 18:24:14 +0200
changeset 59250 727dff4f6b2c
parent 59183 9a8f9093e160
child 59251 241e06eb9c04
permissions -rw-r--r--
transport terms in theorems to frontend

Note: tests missing for both..
# Test_Isac.thy
# pbl/met/thy_hierarchy2file + genhtml to/from ~/tmp
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@55481
    20
(* TODO CLEANUP Thm:
neuper@55487
    21
type rule = 
neuper@55487
    22
Thm (string, thm): (a) needs string to identify sym_thmID for handling in front-end;
neuper@55487
    23
                   (b) investigate if ""RS sym" attaches a [.]" still occurs: string_of_thmI
neuper@55487
    24
thmID            : type for data from user input + program
neuper@55487
    25
thmDeriv         : type for thy_hierarchy ONLY
neuper@55487
    26
obsolete types   : thm' (SEE "ad thm'"), thm''. 
wneuper@59250
    27
revise funs      : id_of_thm, thm_of_thm, rep_thm_G', eq_thmI, eq_thmI', thm''_of_thm thm.
neuper@55487
    28
activate         : thmID_of_derivation_name'
neuper@55481
    29
*)
wneuper@59172
    30
type iterID = int;
wneuper@59172
    31
type calcID = int;
wneuper@59172
    32
neuper@42399
    33
type thmID = string;    (* identifier for a thm (the shortest possible identifier)       *)
neuper@42434
    34
type thmDeriv = string; (* WN120524 deprecated
neuper@42434
    35
  thyID ^"."^ xxx ^"."^ thmID, see fun thmID_of_derivation_name 
neuper@42434
    36
  WN120524: dont use Thm.derivation_name, this is destroyed by num_str;
neuper@42434
    37
  Thm.get_name_hint survives num_str and seems perfectly reliable *)
neuper@42399
    38
neuper@37906
    39
type thm' = thmID * cterm';(*WN060610 deprecated in favour of thm''*)
wneuper@59250
    40
type thm'' = thmID * term; (* only for transport via libisabelle isac-java <--- ME *)
neuper@37906
    41
type rls' = string;
neuper@42399
    42
neuper@40836
    43
(*.a 'guh'='globally unique handle' is a string unique for each element
neuper@37906
    44
   of isac's KEStore and persistent over time
neuper@37906
    45
   (in particular under shifts within the respective hierarchy);
neuper@40836
    46
   specialty for thys:
neuper@37906
    47
   # guh NOT resistant agains shifts from one thy to another
neuper@37906
    48
   (which is the price for Isabelle's design: thy's overwrite ids of subthy's)
neuper@37906
    49
   # requirement for matchTheory: induce guh from tac + current thy
neuper@37906
    50
   (see 'fun thy_containing_thm', 'fun thy_containing_rls' etc.)
neuper@40836
    51
   TODO: introduce to pbl, met.*)
neuper@37906
    52
type guh = string;
neuper@37906
    53
val e_guh = "e_guh":guh;
neuper@37906
    54
wneuper@59141
    55
type xml = string; (* rm together with old code replaced by XML.tree *)
wneuper@59141
    56
fun string_to_bool "true" = true
wneuper@59141
    57
  | string_to_bool "false" = false
wneuper@59141
    58
  | string_to_bool str = error ("string_to_bool: arg = " ^ str)
neuper@37906
    59
neuper@52154
    60
(* eval function calling sml code during rewriting.
neuper@52154
    61
Unifying "type cal" and "type calc" would make Lucas-Interpretation more efficient,
neuper@52154
    62
  see "fun rule2stac": instead of 
neuper@52154
    63
    Calc: calID * eval_fn -> rule
neuper@52154
    64
  would be better
neuper@52154
    65
    Calc: prog_calcID * (calID * eval_fn)) -> rule*)
neuper@37906
    66
type eval_fn = (string -> term -> theory -> (string * term) option);
neuper@37906
    67
fun e_evalfn (_:'a) (_:term) (_:theory) = NONE:(string * term) option;
neuper@37906
    68
(*. op in isa-term 'Const(op,_)' .*)
neuper@37906
    69
type calID = string;
neuper@52154
    70
neuper@52154
    71
(* *)
neuper@37906
    72
type cal = (calID * eval_fn);
neuper@37906
    73
(*. fun calculate_ fetches the evaluation-function via this list. *)
neuper@52117
    74
type prog_calcID = string;
neuper@52117
    75
type calc = (prog_calcID * cal);
neuper@52141
    76
type calc_elem =
neuper@52141
    77
  prog_calcID *   (* a simple identifier used in programs *)
neuper@52141
    78
  (calID *        (* a long identifier used in Const *)
neuper@52141
    79
    eval_fn)      (* an ML function *)
neuper@52141
    80
fun calc_eq ((pi1, (ci1, _)) : calc_elem, (pi2, (ci2, _)) : calc_elem) =
neuper@52141
    81
  if pi1 = pi2
neuper@52141
    82
  then if ci1 = ci2 then true else error ("calc_eq: " ^ ci1 ^ " <> " ^ ci2)
neuper@52141
    83
  else false
neuper@52141
    84
neuper@37906
    85
neuper@37906
    86
type subs' = (cterm' * cterm') list; (*16.11.00 for FE-KE*)
neuper@37906
    87
type subst = (term * term) list; (*here for ets2str*)
neuper@37906
    88
val e_subst = []:(term * term) list;
neuper@37906
    89
neuper@37906
    90
(*TODO.WN060610 make use of "type rew_ord" total*)
neuper@37906
    91
type rew_ord' = string;
neuper@37906
    92
val e_rew_ord' = "e_rew_ord" : rew_ord';
neuper@37906
    93
type rew_ord_ = subst -> Term.term * Term.term -> bool;
neuper@37906
    94
fun dummy_ord (_:subst) (_:term,_:term) = true;
neuper@37906
    95
val e_rew_ord_ = dummy_ord;
neuper@37906
    96
type rew_ord = rew_ord' * rew_ord_;
neuper@37906
    97
val e_rew_ord = dummy_ord; (* TODO.WN071231 clarify identifiers..e_rew_ordX*)
neuper@37906
    98
val e_rew_ordX = (e_rew_ord', e_rew_ord_) : rew_ord;
neuper@37906
    99
neuper@42451
   100
(* error patterns and fill patterns *)
neuper@42451
   101
type errpatID = string
neuper@42451
   102
type errpat =
neuper@42451
   103
  errpatID    (* one identifier for a list of patterns                       
neuper@42451
   104
                 DESIGN ?TODO: errpatID list for hierarchy of errpats ?      *)
neuper@42451
   105
  * term list (* error patterns                                              *)
neuper@42451
   106
  * thm list  (* thms related to error patterns; note that respective lhs 
neuper@42451
   107
                 do not match (which reflects student's error).
neuper@42451
   108
                 fillpatterns are stored with these thms.                    *)
neuper@42451
   109
neuper@42451
   110
(* for (at least) 2 kinds of access:
neuper@42451
   111
  (1) given an errpatID, find the respective fillpats (e.g. in fun find_fill_pats)
neuper@42451
   112
  (2) given a thm, find respective fillpats *)
neuper@42451
   113
type fillpatID = string
neuper@42451
   114
type fillpat =
neuper@42451
   115
  fillpatID   (* DESIGN ?TODO: give an order w.r.t difficulty ? *)
neuper@42451
   116
  * term      (* the pattern with fill-in gaps                  *)
neuper@42451
   117
  * errpatID; (* which the fillpat would be a help for          
neuper@42451
   118
                 DESIGN ?TODO: list for several patterns ?      *)
neuper@37906
   119
neuper@40836
   120
datatype rule =
neuper@37906
   121
  Erule                (*.the empty rule                     .*)
neuper@55481
   122
| Thm of (string * Basic_Thm.thm) (* see TODO CLEANUP Thm     *)
neuper@37906
   123
| Calc of string *     (*.sml-code manipulating a (sub)term  .*)
neuper@55487
   124
	  eval_fn
neuper@37906
   125
| Cal1 of string *     (*.sml-code applied only to whole term
neuper@37906
   126
                          or left/right-hand-side of eqality .*)
neuper@55487
   127
	  eval_fn
neuper@37906
   128
| Rls_ of rls          (*.ie. rule sets may be nested.*)
neuper@40836
   129
and scr =
neuper@40836
   130
    EmptyScr
neuper@52084
   131
  | Prog of term (* for met *)
neuper@52101
   132
  | Rfuns of     (* for Rrls, usage see rational.sml ----- reverse rewrite -----      *)
neuper@52084
   133
    {init_state : (* initialise for reverse rewriting by the Interpreter              *)
neuper@52084
   134
      term ->         (* for this the rrlsstate is initialised:                       *)
neuper@52084
   135
      term *          (* the current formula: goes locate_gen -> next_tac via istate  *)
neuper@52084
   136
      term *          (* the final formula                                            *)
neuper@52084
   137
      rule list       (* of reverse rewrite set (#1#)                                 *)
neuper@52084
   138
        list *        (*   may be serveral, eg. in norm_rational                      *)
neuper@52084
   139
      ( rule *        (* Thm (+ Thm generated from Calc) resulting in ...             *)
neuper@52084
   140
        (term *       (*   ... rewrite with ...                                       *)
neuper@52084
   141
        term list))   (*   ... assumptions                                            *)
neuper@52084
   142
      list,           (* derivation from given term to normalform
neuper@52084
   143
                         in reverse order with sym_thm;
neuper@52084
   144
                                                (#1#) could be extracted from here #1 *)  
neuper@52084
   145
	  normal_form:  (* the function which drives the Rrls ##############################*)
neuper@52084
   146
	    term -> (term * term list) option,
neuper@52084
   147
	  locate_rule:  (* checks a rule R for being a cancel-rule, and if it is,
neuper@52084
   148
                     then return the list of rules (+ the terms they are rewriting to)
neuper@52084
   149
                     which need to be applied before R should be applied.
neuper@52084
   150
                     precondition: the rule is applicable to the argument-term.       *)
neuper@52084
   151
	    rule list list -> (* the reverse rule list                                      *)
neuper@52084
   152
	    term ->         (* ... to which the rule shall be applied                       *)
neuper@52084
   153
	    rule ->         (* ... to be applied to term                                    *)
neuper@52084
   154
	    ( rule *        (* value: a rule rewriting to ...                               *)
neuper@52084
   155
	      (term *       (*   ... the resulting term ...                                 *)
neuper@52084
   156
	      term list))   (*   ... with the assumptions ( //#0)                           *)
neuper@52084
   157
	    list,           (*   there may be several such rules; the list is empty,          
neuper@52084
   158
                           if the rule has nothing to do with e.g. cancelation        *)
neuper@52084
   159
	  next_rule:    (* for a given term return the next rules to be done for cancelling *)
neuper@52084
   160
	    rule list list->(* the reverse rule list                                        *)
neuper@52084
   161
	    term ->         (* the term for which ...                                       *)
neuper@52084
   162
	    rule option,    (* ... this rule is appropriate for cancellation;
neuper@52084
   163
		                     there may be no such rule (if the term is eg.canceled already*)
neuper@52084
   164
	  attach_form:  (* checks an input term TI, if it may belong to e.g. a current 
neuper@52084
   165
                     cancellation, by trying to derive it from the given term TG.     
neuper@52084
   166
                     NOT IMPLEMENTED                                                 *)
neuper@52084
   167
	    rule list list->(**)
neuper@52084
   168
	    term ->         (* TG, the last one agreed upon by user + math-eng              *)
neuper@52084
   169
	    term ->         (* TI, the next one input by the user                           *)
neuper@52084
   170
	    ( rule *        (* the rule to be applied in order to reach TI                  *) 
neuper@52084
   171
	      (term *       (* ... obtained by applying the rule ...                        *)
neuper@52084
   172
	      term list))   (* ... and the respective assumptions                           *) 
neuper@52084
   173
	    list}           (* there may be several such rules; the list is empty, if the 
neuper@52084
   174
                         users term does not belong to e.g. a cancellation of the term 
neuper@52084
   175
                         last agreed upon.                                            *)
neuper@37906
   176
and rls =
neuper@37906
   177
    Erls                          (*for init e_rls*)
neuper@40836
   178
neuper@37906
   179
  | Rls of (*a confluent and terminating ruleset, in general         *)
neuper@37906
   180
    {id : string,          (*for trace_rewrite:=true                 *)
neuper@37906
   181
     preconds : term list, (*unused WN020820                         *)
neuper@37906
   182
     (*WN060616 for efficiency...
s1210629013@55444
   183
      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)
neuper@37906
   184
     rew_ord  : rew_ord,   (*for rules*)
neuper@37906
   185
     erls     : rls,       (*for the conditions in rules             *)
neuper@37906
   186
     srls     : rls,       (*for evaluation of list_fns in script    *)
s1210629013@55444
   187
     calc     : calc list, (*for Calculate in scr, set by prep_rls'   *)
neuper@37906
   188
     rules    : rule list,
neuper@42451
   189
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
neuper@48763
   190
     scr      : scr}       (*Prog term: generating intermed.steps  *)
neuper@37906
   191
  | Seq of (*a sequence of rules to be tried only once               *)
neuper@37906
   192
    {id : string,          (*for trace_rewrite:=true                 *)
neuper@37906
   193
     preconds : term list, (*unused 20.8.02                          *)
neuper@37906
   194
     (*WN060616 for efficiency...
s1210629013@55444
   195
      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)
neuper@37906
   196
     rew_ord  : rew_ord,   (*for rules                               *)
neuper@37906
   197
     erls     : rls,       (*for the conditions in rules             *)
neuper@37906
   198
     srls     : rls,       (*for evaluation of list_fns in script    *)
s1210629013@55444
   199
     calc     : calc list, (*for Calculate in scr, set by prep_rls'   *)
neuper@37906
   200
     rules    : rule list,
neuper@42451
   201
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
neuper@48763
   202
     scr      : scr}  (*Prog term  (how to restrict type ???)*)
neuper@52084
   203
neuper@37906
   204
  (*Rrls call SML-code and simulate an rls
neuper@37906
   205
    difference: there is always _ONE_ redex rewritten in 1 call,
neuper@37906
   206
    thus wrap Rrls by: Rls (Rls_ ...)*)
neuper@52084
   207
  | Rrls of (* SML-functions within rewriting; step-wise execution provided;   
neuper@52084
   208
               Rrls simulate an rls
neuper@52084
   209
               difference: there is always _ONE_ redex rewritten in 1 call,
neuper@52084
   210
               thus wrap Rrls by: Rls (Rls_ ...)                              *)
neuper@38036
   211
    {id : string,          (* for trace_rewrite := true                       *)
neuper@40836
   212
     prepat  : (term list *(* preconds, eval with subst from pattern;
neuper@52084
   213
                              if [@{term True}], match decides alone          *)
neuper@52084
   214
		            term )     (* pattern matched with current (sub)term          *)
neuper@52084
   215
		   list,               (* meta-conjunction is or                          *)
neuper@38036
   216
     rew_ord  : rew_ord,   (* for rules                                       *)
neuper@38036
   217
     erls     : rls,       (* for the conditions in rules and preconds        *)
s1210629013@55444
   218
     calc     : calc list, (* for Calculate in scr, set automatic.in prep_rls' *)
neuper@42451
   219
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
neuper@38036
   220
     scr      : scr};      (* Rfuns {...}  (how to restrict type ???)         *)
neuper@37906
   221
neuper@55477
   222
(*ad thm':
neuper@55477
   223
   there are two kinds of theorems ...
neuper@55477
   224
   (1) known by isabelle
neuper@55477
   225
   (2) not known, eg. calc_thm, instantiated rls
neuper@55477
   226
       the latter have a thmid "#..."
neuper@55481
   227
   and thus outside isa we ALWAYS transport both (thmID, string_of_thmI)
neuper@55477
   228
   and have a special assoc_thm / assoc_rls in this interface      *)
neuper@55477
   229
type theory' = string; (* = domID ^".thy" WN.101011 ABOLISH !*)
neuper@55477
   230
type domID = string;   (* domID ^".thy" = theory' WN.101011 replace by thyID*)
neuper@55477
   231
type thyID = string;    (*WN.3.11.03 TODO: replace domID with thyID*)
neuper@55477
   232
     
neuper@55477
   233
fun term_to_string' ctxt t =
neuper@55477
   234
  let
neuper@55477
   235
    val ctxt' = Config.put show_markup false ctxt
neuper@55477
   236
  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
neuper@55477
   237
fun term_to_string'' (thyID : thyID) t =
neuper@55477
   238
  let
neuper@55477
   239
    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info.get_theory thyID))
neuper@55477
   240
  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
neuper@55477
   241
fun term_to_string''' thy t =
neuper@55477
   242
  let
neuper@55477
   243
    val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
neuper@55477
   244
  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
neuper@55477
   245
neuper@48760
   246
fun thy2ctxt' thy' = Proof_Context.init_global (Thy_Info.get_theory thy');(*FIXXXME thy-ctxt*)
neuper@48760
   247
fun thy2ctxt thy = Proof_Context.init_global thy;(*FIXXXME thy-ctxt*)
neuper@37906
   248
neuper@55477
   249
fun term2str t = term_to_string' (thy2ctxt' "Isac") t;
neuper@55477
   250
fun terms2strs ts = map term2str ts;
neuper@55477
   251
(*terms2strs [t1,t2] = ["1 + 2", "abc"];*)
neuper@55477
   252
val terms2str = strs2str o terms2strs;
neuper@55477
   253
(*terms2str [t1,t2] = "[\"1 + 2\",\"abc\"]";*)
neuper@55477
   254
val terms2str' = strs2str' o terms2strs;
neuper@55477
   255
(*terms2str' [t1,t2] = "[1 + 2,abc]";*)
neuper@55477
   256
neuper@55477
   257
fun termopt2str (SOME t) = "(SOME " ^ term2str t ^ ")"
neuper@55477
   258
  | termopt2str NONE = "NONE";
neuper@55477
   259
neuper@55477
   260
fun type_to_string' ctxt t =
neuper@55477
   261
  let
neuper@55477
   262
    val ctxt' = Config.put show_markup false ctxt
neuper@55477
   263
  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
neuper@55477
   264
fun type_to_string'' (thyID : thyID) t =
neuper@55477
   265
  let
neuper@55477
   266
    val ctxt' = Config.put show_markup false (Proof_Context.init_global (Thy_Info.get_theory thyID))
neuper@55477
   267
  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
neuper@55477
   268
fun type_to_string''' thy t =
neuper@55477
   269
  let
neuper@55477
   270
    val ctxt' = Config.put show_markup false (Proof_Context.init_global thy)
neuper@55477
   271
  in Print_Mode.setmp [] (Syntax.string_of_typ ctxt') t end;
neuper@55477
   272
neuper@55477
   273
fun type2str typ = type_to_string'' "Isac" typ; (*legacy*)
neuper@55477
   274
val string_of_typ = type2str; (*legacy*)
neuper@55477
   275
fun string_of_typ_thy thy typ = type_to_string'' thy typ; (*legacy*)
neuper@55477
   276
                 
neuper@48760
   277
fun Isac _ = Proof_Context.theory_of (thy2ctxt' "Isac"); (*@{theory "Isac"}*)
neuper@48760
   278
                                     
neuper@40836
   279
val e_rule = Thm ("refl", @{thm refl});
neuper@37906
   280
fun id_of_thm (Thm (id, _)) = id
neuper@52101
   281
  | id_of_thm _ = error "error id_of_thm";
neuper@37906
   282
fun thm_of_thm (Thm (_, thm)) = thm
neuper@52101
   283
  | thm_of_thm _ = error "error thm_of_thm";
neuper@37906
   284
fun rep_thm_G' (Thm (thmid, thm)) = (thmid, thm);
neuper@38061
   285
neuper@38061
   286
fun thmID_of_derivation_name dn = last_elem (space_explode "." dn);
neuper@55481
   287
fun thmID_of_derivation_name' thm = (thmID_of_derivation_name o Thm.get_name_hint) thm
neuper@38061
   288
fun thyID_of_derivation_name dn = hd (space_explode "." dn);
wneuper@59250
   289
fun thm''_of_thm thm = (thmID_of_derivation_name' thm, Thm.prop_of thm) : thm''
neuper@38061
   290
neuper@37906
   291
fun eq_thmI ((thmid1 : thmID, _ : thm), (thmid2 : thmID, _ : thm)) =
neuper@37906
   292
    (strip_thy thmid1) = (strip_thy thmid2);
neuper@42372
   293
(*WN120201 weakened*)
neuper@42372
   294
fun eq_thmI ((thmid1 : thmID, _ : thm), (thmid2 : thmID, _)) = thmid1 = thmid2;
neuper@38002
   295
(*version typed weaker WN100910*)
neuper@38002
   296
fun eq_thmI' ((thmid1, _), (thmid2, _)) =
neuper@38061
   297
    (thmID_of_derivation_name thmid1) = (thmID_of_derivation_name thmid2);
neuper@37906
   298
neuper@37906
   299
neuper@37906
   300
(*check for [.] as caused by "fun assoc_thm'"*)
wneuper@59183
   301
fun string_of_thm thm = term_to_string' (thy2ctxt' "Isac") (Thm.prop_of thm)
wneuper@59183
   302
fun string_of_thm' thy thm = term_to_string' (thy2ctxt thy) (Thm.prop_of thm)
neuper@55480
   303
fun string_of_thmI thm =
neuper@55480
   304
  let 
neuper@55480
   305
    val str = (de_quote o string_of_thm) thm
neuper@55480
   306
    val (a, b) = split_nlast (5, Symbol.explode str)
neuper@55480
   307
  in 
neuper@55480
   308
    case b of
neuper@55480
   309
      [" ", " ","[", ".", "]"] => implode a
neuper@55480
   310
    | _ => str
neuper@55480
   311
  end
neuper@55481
   312
neuper@37906
   313
(*.id requested for all, Rls,Seq,Rrls.*)
neuper@37906
   314
fun id_rls Erls = "e_rls" (*WN060714 quick and dirty: recursive defs!*)
neuper@37906
   315
  | id_rls (Rls {id,...}) = id
neuper@37906
   316
  | id_rls (Seq {id,...}) = id
neuper@37906
   317
  | id_rls (Rrls {id,...}) = id;
neuper@37906
   318
val rls2str = id_rls;
neuper@37906
   319
fun id_rule (Thm (id, _)) = id
neuper@37906
   320
  | id_rule (Calc (id, _)) = id
neuper@37906
   321
  | id_rule (Rls_ rls) = id_rls rls;
neuper@37906
   322
neuper@37906
   323
fun get_rules (Rls {rules,...}) = rules
neuper@37906
   324
  | get_rules (Seq {rules,...}) = rules
neuper@37906
   325
  | get_rules (Rrls _) = [];
neuper@37906
   326
neuper@37906
   327
fun rule2str Erule = "Erule"
neuper@37906
   328
  | rule2str (Thm (str, thm)) = "Thm (\""^str^"\","^(string_of_thmI thm)^")"
neuper@37906
   329
  | rule2str (Calc (str,f))  = "Calc (\""^str^"\",fn)"
neuper@37906
   330
  | rule2str (Cal1 (str,f))  = "Cal1 (\""^str^"\",fn)"
neuper@37906
   331
  | rule2str (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
neuper@37906
   332
fun rule2str' Erule = "Erule"
neuper@37906
   333
  | rule2str' (Thm (str, thm)) = "Thm (\""^str^"\",\"\")"
neuper@37906
   334
  | rule2str' (Calc (str,f))  = "Calc (\""^str^"\",fn)"
neuper@37906
   335
  | rule2str' (Cal1 (str,f))  = "Cal1 (\""^str^"\",fn)"
neuper@37906
   336
  | rule2str' (Rls_ rls) = "Rls_ (\""^id_rls rls^"\")";
neuper@37906
   337
neuper@37906
   338
(*WN080102 compare eq_rule ?!?*)
neuper@37906
   339
fun eqrule (Thm (id1,_), Thm (id2,_)) = id1 = id2
neuper@37906
   340
  | eqrule (Calc (id1,_), Calc (id2,_)) = id1 = id2
neuper@37906
   341
  | eqrule (Cal1 (id1,_), Cal1 (id2,_)) = id1 = id2
neuper@37906
   342
  | eqrule (Rls_ _, Rls_ _) = false (*{id=id1}{id=id2} = id1 = id2 FIXXME*)
neuper@37906
   343
  | eqrule _ = false;
neuper@37906
   344
neuper@52084
   345
type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
neuper@52084
   346
  (term * term * rule list list * (rule * (term * term list)) list);
neuper@37906
   347
neuper@37906
   348
val e_type = Type("empty",[]);
neuper@37906
   349
val a_type = TFree("'a",[]);
neuper@37906
   350
val e_term = Const("empty",e_type);
neuper@37906
   351
val a_term = Free("empty",a_type);
neuper@37906
   352
val e_rrlsstate = (e_term,e_term,[[e_rule]],[(e_rule,(e_term,[]))]):rrlsstate;
neuper@37906
   353
neuper@37906
   354
val e_term = Const("empty", Type("'a", []));
neuper@48763
   355
val e_scr = Prog e_term;
neuper@37906
   356
neuper@37906
   357
fun string_of_thy thy = Context.theory_name thy: theory';
neuper@37906
   358
val theory2domID = string_of_thy;
neuper@37906
   359
val theory2thyID = (get_thy o string_of_thy) : theory -> thyID;
neuper@37906
   360
val theory2theory' = string_of_thy;
neuper@37906
   361
val theory2str = string_of_thy; (*WN050903 ..most consistent naming*)
neuper@40836
   362
val theory2str' = implode o (drop_last_n 4) o Symbol.explode o string_of_thy;
neuper@48795
   363
(* fun theory'2theory = fun thyID2thy ... see fun assoc_thy (...Thy_Info.get_theory string);
neuper@37906
   364
al it = "Isac" : string
neuper@37906
   365
*)
neuper@37906
   366
neuper@38058
   367
fun thyID2theory' (thyID:thyID) = thyID;
neuper@38058
   368
(*
neuper@40836
   369
    let val ss = Symbol.explode thyID
neuper@37906
   370
	val ext = implode (takelast (4, ss))
neuper@37906
   371
    in if ext = ".thy" then thyID : theory' (*disarm abuse of thyID*)
neuper@37906
   372
       else thyID ^ ".thy"
neuper@37906
   373
    end;
neuper@38058
   374
*)
neuper@37906
   375
(* thyID2theory' "Isac" (*ok*);
neuper@38050
   376
val it = "Isac" : theory'
neuper@38050
   377
 > thyID2theory' "Isac" (*abuse, goes ok...*);
neuper@38050
   378
val it = "Isac" : theory'
neuper@37906
   379
*)
neuper@48880
   380
neuper@38058
   381
fun theory'2thyID (theory':theory') = theory';
neuper@38058
   382
(*
neuper@40836
   383
    let val ss = Symbol.explode theory'
neuper@37906
   384
	val ext = implode (takelast (4, ss))
neuper@37906
   385
    in if ext = ".thy" then ((implode o (drop_last_n 4)) ss) : thyID
neuper@37906
   386
       else theory' (*disarm abuse of theory'*)
neuper@37906
   387
    end;
neuper@38058
   388
*)
neuper@38050
   389
(* theory'2thyID "Isac";
neuper@37906
   390
val it = "Isac" : thyID
neuper@37906
   391
> theory'2thyID "Isac";
neuper@37906
   392
val it = "Isac" : thyID*)
neuper@37906
   393
neuper@37906
   394
neuper@37906
   395
(*. WN0509 discussion:
neuper@37906
   396
#############################################################################
neuper@37906
   397
#   How to manage theorys in subproblems wrt. the requirement,              #
neuper@37906
   398
#   that scripts should be re-usable ?                                      #
neuper@37906
   399
#############################################################################
neuper@37906
   400
neuper@37988
   401
    eg. 'Script Solve_rat_equation' calls 'SubProblem (RatEq',..'
neuper@37906
   402
    which would not allow to 'solve (y'' = -M_b / EI, M_b)' by this script
neuper@40836
   403
    because Biegelinie.thy is subthy of RatEq.thy and thus Biegelinie.M_b
neuper@37906
   404
    is unknown in RatEq.thy and M_b cannot be parsed into the scripts guard
neuper@37906
   405
    (see match_ags).
neuper@37906
   406
neuper@37906
   407
    Preliminary solution:
neuper@38011
   408
    # the thy in 'SubProblem (thy', pbl, arglist)' is not taken automatically,
neuper@38011
   409
    # instead the 'maxthy (rootthy pt) thy' is taken for each subpbl
neuper@40836
   410
    # however, a thy specified by the user in the rootpbl may lead to
neuper@40836
   411
      errors in far-off subpbls (which are not yet reported properly !!!)
neuper@37906
   412
      and interactively specifiying thys in subpbl is not very relevant.
neuper@37906
   413
neuper@37906
   414
    Other solutions possible:
neuper@40836
   415
    # always parse and type-check with Thy_Info.get_theory "Isac"
neuper@37906
   416
      (rejected tue to the vague idea eg. to re-use equations for R in C etc.)
neuper@37906
   417
    # regard the subthy-relation in specifying thys of subpbls
neuper@38011
   418
    # specifically handle 'SubProblem (undefined, pbl, arglist)'
neuper@37906
   419
    # ???
neuper@37906
   420
.*)
neuper@40836
   421
(*WN0509 TODO "ProtoPure" ... would be more consistent
neuper@37906
   422
  with assoc_thy <--> theory2theory' +FIXME assoc_thy "e_domID" -> Script.thy*)
neuper@37906
   423
val e_domID = "e_domID":domID;
neuper@37906
   424
neuper@37906
   425
(*the key into the hierarchy ob theory elements*)
neuper@37906
   426
type theID = string list;
neuper@37906
   427
val e_theID = ["e_theID"];
neuper@37906
   428
val theID2str = strs2str;
neuper@37906
   429
(*theID eg. is ["IsacKnowledge", "Test", "Rulesets", "ac_plus_times"]*)
neuper@37906
   430
fun theID2thyID (theID:theID) =
neuper@37906
   431
    if length theID >= 3 then (last_elem o (drop_last_n 2)) theID : thyID
neuper@38031
   432
    else error ("theID2thyID called with "^ theID2str theID);
neuper@37906
   433
neuper@37906
   434
(*the key into the hierarchy ob problems*)
neuper@37906
   435
type pblID = string list; (* domID::...*)
neuper@37906
   436
val e_pblID = ["e_pblID"]:pblID;
neuper@37906
   437
val pblID2str = strs2str;
neuper@37906
   438
neuper@37906
   439
(*the key into the hierarchy ob methods*)
neuper@37906
   440
type metID = string list;
neuper@37906
   441
val e_metID = ["e_metID"]:metID;
neuper@37906
   442
val metID2str = strs2str;
neuper@37906
   443
s1210629013@52166
   444
type spec = 
s1210629013@52166
   445
     domID * (*WN.12.03: is replaced by thy from get_met ?FIXME? in:
s1210629013@52166
   446
	      specify (Init_Proof..), nxt_specify_init_calc,
s1210629013@52166
   447
	      assod (.SubProblem...), stac2tac (.SubProblem...)*)
s1210629013@52166
   448
     pblID * 
s1210629013@52166
   449
     metID;
s1210629013@52166
   450
s1210629013@52171
   451
fun spec2str ((dom,pbl,met)(*:spec*)) = 
s1210629013@52171
   452
  "(" ^ (quote dom) ^ ", " ^ (strs2str pbl) ^ 
s1210629013@52171
   453
  ", " ^ (strs2str met) ^ ")";
s1210629013@52171
   454
(*> spec2str empty_spec;
s1210629013@52171
   455
val it = "(\"\", [], (\"\", \"\"))" : string *)
s1210629013@52171
   456
val empty_spec = (e_domID,e_pblID,e_metID):spec;
s1210629013@52171
   457
val e_spec = empty_spec;
s1210629013@52171
   458
s1210629013@52166
   459
(*.association list with cas-commands, for generating a complete calc-head.*)
neuper@52169
   460
type generate_fn = 
neuper@52169
   461
  (term list ->    (* the arguments of the cas-command, eg. (x+1=2, x) *)
neuper@52169
   462
    (term *        (* description of an element                        *)
neuper@52169
   463
      term list)   (* value of the element (always put into a list)    *)
neuper@52169
   464
  list)            (* of elements in the formalization                 *)
neuper@52169
   465
type cas_elem = 
neuper@52169
   466
  (term *          (* cas-command, eg. 'solve'                         *)
neuper@52169
   467
    (spec *        (* theory, problem, method                          *)
s1210629013@52174
   468
    generate_fn))
neuper@52169
   469
fun cas_eq ((t1, (_, _)) : cas_elem, (t2, (_, _)) : cas_elem) = t1 = t2
s1210629013@52167
   470
neuper@37906
   471
(*either theID or pblID or metID*)
neuper@37906
   472
type kestoreID = string list;
neuper@37906
   473
val e_kestoreID = ["e_kestoreID"];
neuper@37906
   474
val kestoreID2str = strs2str;
neuper@37906
   475
neuper@48892
   476
(*for distinction of contexts WN130621: disambiguate with Isabelle's Context !*)
neuper@37906
   477
datatype ketype = Exp_ | Thy_ | Pbl_ | Met_;
neuper@37906
   478
fun ketype2str Exp_ = "Exp_"
neuper@40836
   479
  | ketype2str Thy_ = "Thy_"
neuper@40836
   480
  | ketype2str Pbl_ = "Pbl_"
neuper@37906
   481
  | ketype2str Met_ = "Met_";
neuper@37906
   482
fun ketype2str' Exp_ = "Example"
neuper@40836
   483
  | ketype2str' Thy_ = "Theory"
neuper@40836
   484
  | ketype2str' Pbl_ = "Problem"
neuper@37906
   485
  | ketype2str' Met_ = "Method";
wneuper@59156
   486
fun str2ketype "Exp_" = Exp_
wneuper@59156
   487
  | str2ketype "Thy_" = Thy_
wneuper@59156
   488
  | str2ketype "Pbl_" = Pbl_
wneuper@59156
   489
  | str2ketype "Met_" = Met_
wneuper@59156
   490
  | str2ketype str = raise ERROR ("str2ketype: WRONG arg = " ^ str)
wneuper@59172
   491
(* for conversion from XML *)
wneuper@59172
   492
fun str2ketype' "exp" = Exp_
wneuper@59172
   493
  | str2ketype' "thy" = Thy_
wneuper@59172
   494
  | str2ketype' "pbl" = Pbl_
wneuper@59172
   495
  | str2ketype' "met" = Met_
wneuper@59172
   496
  | str2ketype' str = raise ERROR ("str2ketype': WRONG arg = " ^ str)
neuper@37906
   497
neuper@37906
   498
(*rewrite orders, also stored in 'type met' and type 'and rls'
neuper@37906
   499
  The association list is required for 'rewrite.."rew_ord"..'
neuper@37947
   500
  WN0509 tests not well-organized: see smltest/Knowledge/termorder.sml*)
neuper@55432
   501
val rew_ord' = Unsynchronized.ref
neuper@38007
   502
        ([]:(rew_ord' *        (*the key for the association list         *)
neuper@37906
   503
	     (subst 	       (*the bound variables - they get high order*)
neuper@37906
   504
	      -> (term * term) (*(t1, t2) to be compared                  *)
neuper@37906
   505
	      -> bool))        (*if t1 <= t2 then true else false         *)
neuper@37906
   506
		list);         (*association list                         *)
neuper@38061
   507
neuper@37906
   508
rew_ord' := overwritel (!rew_ord', [("e_rew_ord", e_rew_ord),
neuper@37906
   509
				    ("dummy_ord", dummy_ord)]);
neuper@37906
   510
neuper@37906
   511
neuper@48887
   512
(* A tree for storing data defined in different theories 
neuper@48887
   513
  for access from the Interpreter and from dialogue authoring 
neuper@48887
   514
  using a string list as key.
neuper@48887
   515
  'a is for pbt | met | thydata; after WN030424 naming became inappropriate *)
neuper@40836
   516
datatype 'a ptyp =
neuper@48887
   517
	Ptyp of string * (* element of the key *)
neuper@48887
   518
		'a list *      (* several pbts with different domIDs/thy TODO: select by subthy (isaref.p.69)
neuper@48887
   519
			                presently only _ONE_ elem FOR ALL KINDS OF CONTENT pbt | met | thydata *)
neuper@48887
   520
		('a ptyp) list;(* the children nodes *)
neuper@37906
   521
neuper@37906
   522
(*.datatype for collecting thydata for hierarchy.*)
neuper@37906
   523
(*WN060720 more consistent naming would be 'type thyelem' or 'thelem'*)
neuper@37906
   524
(*WN0606 Htxt contains html which does not belong to the sml-kernel*)
neuper@37906
   525
datatype thydata = Html of {guh: guh,
neuper@37906
   526
			    coursedesign: authors,
neuper@37906
   527
			    mathauthors: authors,
neuper@37906
   528
			    html: string} (*html; for demos before database*)
neuper@37906
   529
		 | Hthm of {guh: guh,
neuper@37906
   530
			    coursedesign: authors,
neuper@37906
   531
			    mathauthors: authors,
neuper@42429
   532
			    fillpats: fillpat list,
neuper@55491
   533
			    thm: thm} (* here no sym_thm, thus no thmID required *)
neuper@37906
   534
		 | Hrls of {guh: guh,
neuper@37906
   535
			    coursedesign: authors,
neuper@37906
   536
			    mathauthors: authors,
neuper@37906
   537
			    thy_rls: (thyID * rls)}
neuper@37906
   538
		 | Hcal of {guh: guh,
neuper@37906
   539
			    coursedesign: authors,
neuper@37906
   540
			    mathauthors: authors,
neuper@37906
   541
			    calc: calc}
neuper@37906
   542
		 | Hord of {guh: guh,
neuper@37906
   543
			    coursedesign: authors,
neuper@37906
   544
			    mathauthors: authors,
neuper@37906
   545
			    ord: (subst -> (term * term) -> bool)};
neuper@37906
   546
val e_thydata = Html {guh="e_guh", coursedesign=[], mathauthors=[], html=""};
neuper@55435
   547
fun the2str (Html {guh, coursedesign, mathauthors, html}) = guh : string
neuper@55435
   548
  | the2str (Hthm {guh, coursedesign, mathauthors, fillpats, thm}) = guh
neuper@55435
   549
  | the2str (Hrls {guh, coursedesign, mathauthors, thy_rls}) = guh
neuper@55435
   550
  | the2str (Hcal {guh, coursedesign, mathauthors, calc}) = guh
neuper@55435
   551
  | the2str (Hord {guh, coursedesign, mathauthors, ord}) = guh
neuper@55435
   552
fun thes2str thes = map the2str thes |> list2str;
neuper@37906
   553
neuper@55483
   554
(* notes on thehier concerning sym_thmID theorems (created in derivations, reverse rewriting)
neuper@55487
   555
     (a): thehier does not contain sym_thmID theorems
neuper@55485
   556
     (b): lookup for sym_thmID directly from Isabelle using sym_thm
neuper@55485
   557
          (within math-engine NO lookup in thehier -- within java in *.xml only!)
neuper@55483
   558
TODO (c): export from thehier to xml
neuper@55483
   559
TODO (c1)   creates one entry for "thmID" (and NONE for "sym_thmID") in the hierarchy
neuper@55483
   560
TODO (c2)   creates 2 files "thy_*-thm-thmID.xml" and "thy_*-thm-sym_thmID.xml"
neuper@55483
   561
TODO (d): 1 entry in the MiniBrowser's hierarchy (generated from xml)
neuper@55483
   562
          stands for both, "thmID" and "sym_thmID" 
neuper@55483
   563
TODO (d1)   lookup from calctxt          
neuper@55483
   564
TODO (d1)   lookup from from rule set in MiniBrowser *)
neuper@37906
   565
type thehier = (thydata ptyp) list;
neuper@55405
   566
(* required to determine sequence of main nodes of thehier in KEStore.thy *)
neuper@55405
   567
fun part2guh ([str]:theID) =
neuper@55405
   568
    (case str of
neuper@55405
   569
	"Isabelle" => "thy_isab_" ^ str ^ "-part" : guh
neuper@55405
   570
      | "IsacScripts" => "thy_scri_" ^ str ^ "-part"
neuper@55405
   571
      | "IsacKnowledge" => "thy_isac_" ^ str ^ "-part"
neuper@55405
   572
      | str => error ("thy2guh: called with '"^str^"'"))
neuper@55405
   573
  | part2guh theID = error ("part2guh called with theID = " ^ theID2str theID);
neuper@37906
   574
neuper@55437
   575
fun thy2guh ([part, thyID] : theID) = (case part of
neuper@55437
   576
      "Isabelle" => "thy_isab_" ^ thyID : guh
neuper@55437
   577
    | "IsacScripts" => "thy_scri_" ^ thyID
neuper@55437
   578
    | "IsacKnowledge" => "thy_isac_" ^ thyID
neuper@55437
   579
    | str => error ("thy2guh: called with '" ^ str ^ "'"))
neuper@55437
   580
  | thy2guh theID = error ("thy2guh called with '" ^ strs2str' theID ^ "'");
neuper@55437
   581
			
neuper@55437
   582
fun thypart2guh ([part, thyID, thypart] : theID) = case part of
neuper@55437
   583
    "Isabelle" => "thy_isab_" ^ thyID ^ "-" ^ thypart : guh
neuper@55437
   584
  | "IsacScripts" => "thy_scri_" ^ thyID ^ "-" ^ thypart
neuper@55437
   585
  | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-" ^ thypart
neuper@55437
   586
  | str => error ("thypart2guh: called with '" ^ str ^ "'");
neuper@55437
   587
  
neuper@55437
   588
(* convert the data got via contextToThy to a globally unique handle
neuper@55437
   589
   there is another way to get the guh out of the 'theID' in the hierarchy *)
neuper@55437
   590
fun thm2guh (isa, thyID:thyID) (thmID:thmID) = case isa of
neuper@55437
   591
    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID : guh
neuper@55437
   592
  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
neuper@55437
   593
  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
neuper@55437
   594
  | str => error ("thm2guh called with isa = '" ^ isa ^ "' for thm = " ^ thmID ^ "'");
neuper@55437
   595
neuper@55437
   596
fun rls2guh (isa, thyID:thyID) (rls':rls') = case isa of
neuper@55437
   597
    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-rls-" ^ rls' : guh
neuper@55437
   598
  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-rls-" ^ rls'
neuper@55437
   599
  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-rls-" ^ rls'
neuper@55437
   600
  | str => error ("rls2guh called with isa = '" ^ isa ^ "' for rls = '" ^ rls' ^ "'");
neuper@55437
   601
			  
neuper@55437
   602
fun cal2guh (isa, thyID:thyID) calID = case isa of
neuper@55437
   603
    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-cal-" ^ calID : guh
neuper@55437
   604
  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-cal-" ^ calID
neuper@55437
   605
  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-cal-" ^ calID
neuper@55437
   606
  | str => error ("cal2guh called with isa = '" ^ isa ^ "' for cal = '" ^ calID ^ "'");
neuper@55437
   607
			  
neuper@55437
   608
fun ord2guh (isa, thyID:thyID) (rew_ord':rew_ord') = case isa of
neuper@55437
   609
    "Isabelle" => "thy_isab_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord' : guh
neuper@55437
   610
  | "IsacKnowledge" => "thy_isac_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord'
neuper@55437
   611
  | "IsacScripts" => "thy_scri_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord'
neuper@55437
   612
  | str => error ("ord2guh called with isa = '" ^ isa ^ "' for ord = '" ^ rew_ord' ^ "'");
neuper@55437
   613
neuper@55437
   614
(* not only for thydata, but also for thy's etc *)
neuper@55437
   615
fun theID2guh (theID : theID) = case length theID of
neuper@55437
   616
    0 => error ("theID2guh: called with theID = " ^ strs2str' theID)
neuper@55437
   617
  | 1 => part2guh theID
neuper@55437
   618
  | 2 => thy2guh theID
neuper@55437
   619
  | 3 => thypart2guh theID
neuper@55437
   620
  | 4 => 
neuper@55437
   621
    let val [isa, thyID, typ, elemID] = theID
neuper@55437
   622
    in case typ of
neuper@55437
   623
        "Theorems" => thm2guh (isa, thyID) elemID
neuper@55437
   624
      | "Rulesets" => rls2guh (isa, thyID) elemID
neuper@55437
   625
      | "Calculations" => cal2guh (isa, thyID) elemID
neuper@55437
   626
      | "Orders" => ord2guh (isa, thyID) elemID
neuper@55437
   627
      | "Theorems" => thy2guh [isa, thyID]
neuper@55437
   628
      | str => error ("theID2guh: called with theID = " ^ strs2str' theID)
neuper@55437
   629
    end
neuper@55437
   630
  | n => error ("theID2guh called with theID = " ^ strs2str' theID);
neuper@55437
   631
neuper@37906
   632
type path = string;
neuper@37906
   633
type filename = string;
neuper@37906
   634
neuper@37906
   635
(*val xxx = fn: a b => (a,b);   ??? fun-definition ???*)
neuper@37906
   636
local
neuper@37906
   637
    fun ii (_:term) = e_rrlsstate;
neuper@37906
   638
    fun no (_:term) = SOME (e_term,[e_term]);
neuper@37906
   639
    fun lo (_:rule list list) (_:term) (_:rule) = [(e_rule,(e_term,[e_term]))];
neuper@37906
   640
    fun ne (_:rule list list) (_:term) = SOME e_rule;
neuper@37906
   641
    fun fo (_:rule list list) (_:term) (_:term) = [(e_rule,(e_term,[e_term]))];
neuper@37906
   642
in
neuper@37906
   643
val e_rfuns = Rfuns {init_state=ii,normal_form=no,locate_rule=lo,
neuper@37906
   644
		     next_rule=ne,attach_form=fo};
neuper@37906
   645
end;
neuper@37906
   646
neuper@42458
   647
val e_rls =
neuper@42458
   648
  Rls {id = "e_rls", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
neuper@42458
   649
    srls = Erls, calc = [], rules = [], errpatts = [], scr = EmptyScr}: rls;
neuper@42458
   650
val e_rrls =
neuper@42458
   651
  Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Erls,
neuper@42458
   652
    calc = [], errpatts = [], scr=e_rfuns}:rls;
neuper@42451
   653
neuper@42451
   654
fun rep_rls (Rls {id,preconds,rew_ord,erls,srls,calc,errpatts,rules,scr}) =
neuper@37906
   655
  {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
neuper@37906
   656
   (*asm_thm=asm_thm,*)rules=rules,scr=scr}
neuper@42451
   657
  | rep_rls (Seq {id,preconds,rew_ord,erls,srls,calc,errpatts,rules,scr}) =
neuper@37906
   658
  {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,srls=srls,calc=calc,
neuper@37906
   659
   (*asm_thm=asm_thm,*)rules=rules,scr=scr}
neuper@37906
   660
  | rep_rls Erls = rep_rls e_rls
neuper@37906
   661
  | rep_rls (Rrls {id,...})  = rep_rls e_rls
neuper@38031
   662
    (*error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id)*);
neuper@40836
   663
(*| rep_rls (Seq {id,...})  =
neuper@38031
   664
    error("rep_rls doesn't take apart reverse-rewrite-rule-sets: "^id);
neuper@37906
   665
--1.7.03*)
neuper@42451
   666
fun rep_rrls (Rrls {id, calc, erls, prepat, rew_ord, errpatts,
neuper@42451
   667
	      scr = Rfuns {attach_form, init_state, locate_rule, next_rule, normal_form}}) =
neuper@42451
   668
  {id=id, calc=calc, erls=erls, prepat=prepat,
neuper@42451
   669
     rew_ord=rew_ord, errpatts=errpatts, attach_form=attach_form, init_state=init_state,
neuper@37906
   670
     locate_rule=locate_rule, next_rule=next_rule, normal_form=normal_form}
neuper@40836
   671
  | rep_rrls (Rls {id,...}) =
neuper@38031
   672
    error ("rep_rrls doesn't take apart (normal) rule-sets: "^id)
neuper@40836
   673
  | rep_rrls (Seq {id,...}) =
neuper@38031
   674
    error ("rep_rrls doesn't take apart (normal) rule-sets: "^id);
neuper@37906
   675
neuper@37906
   676
fun append_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   677
			rules =rs, errpatts=errpatts, scr=sc}) r =
neuper@37906
   678
    (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   679
	 rules = rs @ r, errpatts=errpatts, scr=sc}:rls)
neuper@37906
   680
  | append_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   681
			rules =rs, errpatts=errpatts, scr=sc}) r =
neuper@37906
   682
    (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   683
	 rules = rs @ r, errpatts=errpatts, scr=sc}:rls)
neuper@40836
   684
  | append_rls id (Rrls _) _ =
neuper@38031
   685
    error ("append_rls: not for reverse-rewrite-rule-set "^id);
neuper@37906
   686
neuper@37906
   687
(*. are _atomic_ rules equal ?.*)
neuper@37906
   688
(*WN080102 compare eqrule ?!?*)
neuper@37906
   689
fun eq_rule (Thm (thm1,_), Thm (thm2,_)) = thm1 = thm2
neuper@37906
   690
  | eq_rule (Calc (id1,_), Calc (id2,_)) = id1 = id2
neuper@37906
   691
  | eq_rule (Rls_ rls1, Rls_ rls2) = id_rls rls1 = id_rls rls2
neuper@37906
   692
  (*id_rls checks for Rls, Seq, Rrls*)
neuper@37906
   693
  | eq_rule _ = false;
neuper@37906
   694
neuper@52141
   695
fun merge_ids rls1 rls2 =
neuper@52141
   696
  let 
neuper@52141
   697
    val id1 = (#id o rep_rls) rls1
neuper@52141
   698
    val id2 = (#id o rep_rls) rls2
neuper@52141
   699
  in
neuper@52141
   700
    if id1 = id2 then id1
neuper@52141
   701
    else "merged_" ^ id1 ^ "_" ^ id2
neuper@52141
   702
  end
neuper@37906
   703
fun merge_rls _ Erls rls = rls
neuper@37906
   704
  | merge_rls _ rls Erls = rls
neuper@52141
   705
  | merge_rls _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
neuper@52141
   706
  | merge_rls _ _ (Rrls x) = Rrls x
neuper@37906
   707
  | merge_rls id
neuper@52141
   708
	  (Rls {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
neuper@52141
   709
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
neuper@52141
   710
	  (Rls {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
neuper@52141
   711
	    rules = rs2, errpatts = eps2, ...})
neuper@52141
   712
    =
neuper@52141
   713
	  (Rls {id = id, rew_ord = ro1, scr = sc1,
neuper@52141
   714
	    preconds = union (op =) pc1 pc2,	    
neuper@52141
   715
	    erls = merge_rls (merge_ids er1 er2) er1 er2,
neuper@52141
   716
	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
neuper@52141
   717
	    calc = union calc_eq ca1 ca2,
neuper@52141
   718
		  rules = union eq_rule rs1 rs2,
neuper@52141
   719
      errpatts = union (op =) eps1 eps2} : rls)
neuper@37906
   720
  | merge_rls id
neuper@52141
   721
	  (Seq {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
neuper@52141
   722
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
neuper@52141
   723
	  (Seq {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
neuper@52141
   724
	    rules = rs2, errpatts = eps2, ...})
neuper@52141
   725
    =
neuper@52141
   726
	  (Seq {id = id, rew_ord = ro1, scr = sc1,
neuper@52141
   727
	    preconds = union (op =) pc1 pc2,	    
neuper@52141
   728
	    erls = merge_rls (merge_ids er1 er2) er1 er2,
neuper@52141
   729
	    srls = merge_rls (merge_ids sr1 sr2) sr1 sr2,
neuper@52141
   730
	    calc = union calc_eq ca1 ca2,
neuper@52141
   731
		  rules = union eq_rule rs1 rs2,
neuper@52141
   732
      errpatts = union (op =) eps1 eps2} : rls)
neuper@52141
   733
  | merge_rls id _ _ = error ("merge_rls: \"" ^ id ^ 
neuper@52141
   734
    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Seq");
neuper@42451
   735
neuper@37906
   736
fun remove_rls id (Rls {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   737
		     rules=rs, errpatts=eps, scr=sc}) r =
neuper@37906
   738
    (Rls{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   739
	   rules = gen_rems eq_rule (rs, r),
neuper@42451
   740
	   errpatts = eps(*gen_rems op= (eps, TODO)*),
neuper@37906
   741
	 scr=sc}:rls)
neuper@37906
   742
  | remove_rls id (Seq {id=_,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   743
		     rules =rs, errpatts=eps, scr=sc}) r =
neuper@37906
   744
    (Seq{id=id,preconds=pc,rew_ord=ro,erls=er,srls=sr,calc=ca,
neuper@42451
   745
	   rules = gen_rems eq_rule (rs, r),
neuper@42451
   746
	   errpatts = eps(*gen_rems op= (eps, TODO)*),
neuper@37906
   747
	 scr=sc}:rls)
neuper@40836
   748
  | remove_rls id (Rrls _) _ = error
neuper@37906
   749
                   ("remove_rls: not for reverse-rewrite-rule-set "^id);
neuper@37906
   750
neuper@52141
   751
(* datastructure for KEStore_Elems, intermediate for thehier *)
neuper@52141
   752
type rlss_elem = 
neuper@52141
   753
  (rls' *     (* identifier unique within Isac *)
neuper@52141
   754
  (theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
neuper@52141
   755
    rls))     (* ((#id o rep_rls) rls) = rls'   by coding discipline *)
neuper@52141
   756
fun rls_eq ((id1, (_, _)), (id2, (_, _))) = id1 = id2
neuper@37906
   757
neuper@52141
   758
fun insert_merge_rls (re as (id, (thyID, r1)) : rlss_elem) ys = 
neuper@52141
   759
  case get_index (fn y => if curry rls_eq re y then SOME y else NONE) ys of
neuper@52141
   760
    NONE => re :: ys
neuper@52141
   761
  | SOME (i, (_, (_, r2))) => 
neuper@52141
   762
      let
neuper@52141
   763
        val r12 = merge_rls id r1 r2
neuper@52141
   764
      in list_update ys i (id, (thyID, r12)) end
neuper@52141
   765
neuper@52141
   766
fun merge_rlss (s1, s2) = fold insert_merge_rls s1 s2;
neuper@52141
   767
neuper@52141
   768
neuper@37906
   769
fun memrls r (Rls {rules,...}) = gen_mem eqrule (r, rules)
neuper@37906
   770
  | memrls r (Seq {rules,...}) = gen_mem eqrule (r, rules)
neuper@38031
   771
  | memrls r _ = error ("memrls: incomplete impl. r= "^(rule2str r));
neuper@37906
   772
neuper@38031
   773
fun assoc' ([], key) = error ("ME_Isa: '"^key^"' not known")
neuper@37906
   774
  | assoc' ((keyi, xi) :: pairs, key) =
neuper@37906
   775
      if key = keyi then SOME xi else assoc' (pairs, key);
neuper@37906
   776
neuper@38056
   777
fun assoc_thy (thy:theory') =
neuper@40836
   778
    if thy = "e_domID" then (Thy_Info.get_theory "Script") (*lower bound of Knowledge*)
neuper@40836
   779
    else (Thy_Info.get_theory thy)
neuper@38056
   780
         handle _ => error ("ME_Isa: thy '" ^ thy ^ "' not in system");
neuper@40836
   781
neuper@37906
   782
(*.overwrite an element in an association list and pair it with a thyID
neuper@37906
   783
   in order to create the thy_hierarchy;
neuper@37906
   784
   overwrites existing rls' even if they are defined in a different thy;
neuper@37906
   785
   this is related to assoc_rls, TODO.WN060120: assoc_rew_ord, assoc_calc;.*)
neuper@40836
   786
(*WN060120 ...these are NOT compatible to "fun assoc_thm'" in that
neuper@37906
   787
   they do NOT handle overlays by re-using an identifier in different thys;
neuper@37906
   788
   "thyID.rlsID" would be a good solution, if the "." would be possible
neuper@37906
   789
   in scripts...
neuper@37906
   790
   actually a hack to get alltogether run again with minimal effort*)
neuper@37906
   791
fun insthy thy' (rls', rls) = (rls', (thy', rls));
neuper@37906
   792
fun overwritelthy thy (al, bl:(rls' * rls) list) =
neuper@37906
   793
    let val bl' = map (insthy ((get_thy o theory2theory') thy)) bl
neuper@37906
   794
    in overwritel (al, bl') end;
neuper@37906
   795
neuper@37906
   796
fun assoc_rew_ord ro = ((the o assoc') (!rew_ord',ro))
neuper@38031
   797
  handle _ => error ("ME_Isa: rew_ord '"^ro^"' not in system");
neuper@37906
   798
(*get the string for stac from rule*)
neuper@37906
   799
neuper@40836
   800
fun subst2str (s:subst) =
neuper@40836
   801
    (strs2str o
neuper@37906
   802
     (map (linefeed o pair2str o
neuper@40836
   803
	   (apsnd term2str) o
neuper@37906
   804
	   (apfst term2str)))) s;
neuper@40836
   805
fun subst2str' (s:subst) =
neuper@40836
   806
    (strs2str' o
neuper@37906
   807
     (map (pair2str o
neuper@40836
   808
	   (apsnd term2str) o
neuper@37906
   809
	   (apfst term2str)))) s;
neuper@37906
   810
(*> subst2str' [(str2term "bdv", str2term "x"),
neuper@37906
   811
		(str2term "bdv_2", str2term "y")];
neuper@37906
   812
val it = "[(bdv, x)]" : string
neuper@37906
   813
*)
neuper@37906
   814
val env2str = subst2str;
neuper@37906
   815
neuper@37906
   816
neuper@37906
   817
(*recursive defs:*)
neuper@48763
   818
fun scr2str (Prog s) = "Prog " ^ term2str s
neuper@37906
   819
  | scr2str (Rfuns _)  = "Rfuns";
neuper@37906
   820
neuper@37906
   821
neuper@37906
   822
fun maxthy thy1 thy2 = if Theory.subthy (thy1, thy2) then thy2 else thy1;
neuper@37906
   823
neuper@37906
   824
neuper@37906
   825
(*.trace internal steps of isac's rewriter*)
neuper@38006
   826
val trace_rewrite = Unsynchronized.ref false;
neuper@37906
   827
(*.depth of recursion in traces of the rewriter, if trace_rewrite:=true.*)
neuper@38006
   828
val depth = Unsynchronized.ref 99999;
neuper@37906
   829
(*.no of rewrites exceeding this int -> NO rewrite.*)
neuper@37906
   830
(*WN060829 still unused...*)
neuper@38006
   831
val lim_rewrite = Unsynchronized.ref 99999;
neuper@37906
   832
(*.no of derivation-elements exceeding this int -> SOME derivation-elements.*)
neuper@38006
   833
val lim_deriv = Unsynchronized.ref 100;
neuper@37906
   834
(*.switch for checking guhs unique before storing a pbl or met;
neuper@37906
   835
   set true at startup (done at begin of ROOT.ML)
neuper@37906
   836
   set false for editing IsacKnowledge (done at end of ROOT.ML).*)
neuper@41905
   837
val check_guhs_unique = Unsynchronized.ref true;
neuper@37906
   838
neuper@37906
   839
neuper@37906
   840
datatype lrd = (*elements of a path (=loc_) into an Isabelle term*)
neuper@40836
   841
	 L     (*go left at $*)
neuper@37906
   842
       | R     (*go right at $*)
neuper@37906
   843
       | D;     (*go down at Abs*)
neuper@37906
   844
type loc_ = lrd list;
neuper@37906
   845
fun ldr2str L = "L"
neuper@37906
   846
  | ldr2str R = "R"
neuper@37906
   847
  | ldr2str D = "D";
neuper@37906
   848
fun loc_2str (k:loc_) = (strs2str' o (map ldr2str)) k;
neuper@37906
   849
neuper@55335
   850
neuper@55335
   851
(* the pattern for an item of a problems model or a methods guard *)
neuper@55335
   852
type pat =
neuper@55335
   853
  (string *     (* field               *)
neuper@55335
   854
	  (term *     (* description         *)
neuper@55335
   855
	     term))   (* id | arbitrary term *);
neuper@55335
   856
fun pat2str ((field, (dsc, id)) : pat) = 
neuper@55335
   857
  pair2str (field, pair2str (term2str dsc, term2str id))
neuper@55335
   858
fun pats2str pats = (strs2str o (map pat2str)) pats
neuper@55335
   859
neuper@55357
   860
(* types for problems models (TODO rename to specification models) *)
neuper@55335
   861
type pbt_ =
neuper@55335
   862
  (string *   (* field "#Given",..*)(*deprecated due to 'type pat'*)
neuper@55335
   863
    (term *   (* description      *)
neuper@55335
   864
      term)); (* id | struct-var  *)
neuper@55335
   865
val e_pbt_ = ("#Undef", (e_term, e_term)) : pbt_
neuper@55335
   866
type pbt = 
neuper@55335
   867
  {guh : guh,         (* unique within this isac-knowledge *)
neuper@55335
   868
  mathauthors : string list, (* copyright *)
neuper@55335
   869
  init : pblID,       (* to start refinement with *)
neuper@55335
   870
  thy  : theory,      (* which allows to compile that pbt
neuper@55335
   871
                      TODO: search generalized for subthy (ref.p.69*)
neuper@55335
   872
                      (*^^^ WN050912 NOT used during application of the problem,
neuper@55335
   873
                      because applied terms may be from 'subthy' as well as from super;
neuper@55335
   874
                      thus we take 'maxthy'; see match_ags !*)
neuper@55335
   875
  cas : term option,  (* 'CAS-command'*)
neuper@55335
   876
  prls : rls,         (* for preds in where_*)
neuper@55335
   877
  where_ : term list, (* where - predicates*)
neuper@55335
   878
  ppc : pat list,     (* this is the model-pattern; 
neuper@55335
   879
                       it contains "#Given","#Where","#Find","#Relate"-patterns
neuper@55335
   880
                       for constraints on identifiers see "fun cpy_nam" *)
neuper@55335
   881
  met : metID list}   (* methods solving the pbt*)
neuper@55335
   882
neuper@55335
   883
val e_pbt = {guh = "pbl_empty", mathauthors = [], init = e_pblID, thy = Thy_Info.get_theory "Pure",
neuper@55335
   884
  cas = NONE, prls = Erls, where_ = [], ppc = [], met = []} : pbt
s1210629013@55344
   885
fun pbt2str
s1210629013@55344
   886
        ({cas = cas', guh = guh', init = init', mathauthors = ma', met = met', ppc = ppc',
s1210629013@55345
   887
          prls = prls', thy = thy', where_ = w'} : pbt) =
s1210629013@55344
   888
      "{cas = " ^ (termopt2str cas') ^  ", guh = \"" ^ guh'  ^ "\", init = "
s1210629013@55345
   889
        ^ (strs2str init') ^ ", mathauthors = " ^ (strs2str ma' |> quote) ^ ", met = "
s1210629013@55345
   890
        ^ (strslist2strs met') ^ ", ppc = " ^ pats2str ppc' ^ ", prls = "
s1210629013@55345
   891
        ^ (rls2str prls' |> quote) ^ ", thy = {" ^ (theory2str thy') ^ "}, where_ = "
s1210629013@55345
   892
        ^ (terms2str w') ^ "}" |> linefeed;
s1210629013@55345
   893
fun pbts2str pbts = map pbt2str pbts |> list2str;
neuper@55335
   894
neuper@55335
   895
val e_Ptyp = Ptyp ("e_pblID",[e_pbt],[])
neuper@55335
   896
type ptyps = (pbt ptyp) list
neuper@55335
   897
neuper@55357
   898
fun coll_pblguhs pbls =
neuper@55357
   899
    let fun node coll (Ptyp (_,[n],ns)) =
neuper@55357
   900
	    [(#guh : pbt -> guh) n] @ (nodes coll ns)
neuper@55357
   901
	and nodes coll [] = coll
neuper@55357
   902
	  | nodes coll (n::ns) = (node coll n) @ (nodes coll ns);
neuper@55357
   903
    in nodes [] pbls end;
neuper@55357
   904
fun check_pblguh_unique (guh:guh) (pbls: (pbt ptyp) list) =
neuper@55357
   905
    if member op = (coll_pblguhs pbls) guh
neuper@55357
   906
    then error ("check_guh_unique failed with '"^guh^"';\n"^
neuper@55357
   907
		      "use 'sort_pblguhs()' for a list of guhs;\n"^
neuper@55357
   908
		      "consider setting 'check_guhs_unique := false'")
neuper@55357
   909
    else ();
s1210629013@55339
   910
neuper@55453
   911
fun insrt _ pbt [k] [] = [Ptyp (k, [pbt], [])]
neuper@55453
   912
  | insrt d pbt [k] ((Ptyp (k', [p], ps)) :: pys) =
neuper@55453
   913
      ((*writeln ("### insert 1: ks = " ^ strs2str [k] ^ "    k'= " ^ k');*)
neuper@55453
   914
      if k = k'
neuper@55453
   915
      then ((Ptyp (k', [pbt], ps)) :: pys)
neuper@55453
   916
      else  ((Ptyp (k', [p], ps)) :: (insrt d pbt [k] pys))
neuper@55453
   917
      )			 
s1210629013@55339
   918
  | insrt d pbt (k::ks) ((Ptyp (k', [p], ps))::pys) =
neuper@55453
   919
      ((*writeln ("### insert 2: ks = "^(strs2str (k::ks))^"    k'= "^k');*)
neuper@55453
   920
      if k = k'
neuper@55453
   921
      then ((Ptyp (k', [p], insrt d pbt ks ps)) :: pys)
neuper@55453
   922
      else 
neuper@55453
   923
        if length pys = 0
neuper@55453
   924
        then error ("insert: not found " ^ (strs2str (d : pblID)))
neuper@55453
   925
        else ((Ptyp (k', [p], ps)) :: (insrt d pbt (k :: ks) pys))
neuper@55453
   926
      );
s1210629013@55339
   927
neuper@55448
   928
fun update_ptyps ID _ _ [] =
neuper@55448
   929
    error ("update_ptyps: " ^ strs2str' ID ^ " does not exist")
neuper@55448
   930
  | update_ptyps ID [i] data ((py as Ptyp (key, _, pys)) :: pyss) =
neuper@55448
   931
    if i = key
neuper@55448
   932
    then 
neuper@55448
   933
      if length pys = 0
neuper@55448
   934
      then ((Ptyp (key, [data], [])) :: pyss)
neuper@55448
   935
      else error ("update_ptyps: " ^ strs2str' ID ^ " has descendants")
neuper@55448
   936
    else py :: update_ptyps ID [i] data pyss
neuper@55448
   937
  | update_ptyps ID (i :: is) data ((py as Ptyp (key, d, pys)) :: pyss) =
neuper@55448
   938
    if i = key
neuper@55448
   939
    then ((Ptyp (key,  d, update_ptyps ID is data pys)) :: pyss)
neuper@55448
   940
    else (py :: (update_ptyps ID (i :: is) data pyss))
neuper@55448
   941
s1210629013@55339
   942
(* this function only works wrt. the way Isabelle evaluates Theories and is not a general merge
s1210629013@55339
   943
  function for trees / ptyps *)
s1210629013@55339
   944
fun merge_ptyps ([], pt) = pt
s1210629013@55339
   945
  | merge_ptyps (pt, []) = pt
s1210629013@55346
   946
  | merge_ptyps ((x' as Ptyp (k, x, ps)) :: xs, (xs' as Ptyp (k', y, ps') :: ys)) =
s1210629013@55346
   947
      if k = k'
s1210629013@55346
   948
      then Ptyp (k, y, merge_ptyps (ps, ps')) :: merge_ptyps (xs, ys)
s1210629013@55346
   949
      else x' :: merge_ptyps (xs, xs');
neuper@55448
   950
fun merge_ptyps' pt1 pt2 = merge_ptyps (pt1, pt2)
s1210629013@55372
   951
s1210629013@55372
   952
(* data for methods stored in 'methods'-database*)
s1210629013@55372
   953
type met = 
s1210629013@55372
   954
     {guh        : guh,        (*unique within this isac-knowledge           *)
s1210629013@55372
   955
      mathauthors: string list,(*copyright                                   *)
s1210629013@55372
   956
      init       : pblID,      (*WN060721 introduced mistakenly--TODO.REMOVE!*)
s1210629013@55372
   957
      rew_ord'   : rew_ord',   (*for rules in Detail
s1210629013@55372
   958
			                           TODO.WN0509 store fun itself, see 'type pbt'*)
s1210629013@55372
   959
      erls       : rls,        (*the eval_rls for cond. in rules FIXME "rls'
s1210629013@55372
   960
				                         instead erls in "fun prep_met"              *)
s1210629013@55372
   961
      srls       : rls,        (*for evaluating list expressions in scr      *)
s1210629013@55372
   962
      prls       : rls,        (*for evaluating predicates in modelpattern   *)
s1210629013@55372
   963
      crls       : rls,        (*for check_elementwise, ie. formulae in calc.*)
s1210629013@55372
   964
      nrls       : rls,        (*canonical simplifier specific for this met  *)
s1210629013@55372
   965
      errpats    : errpat list,(*error patterns expected in this method      *)
s1210629013@55372
   966
      calc       : calc list,  (*Theory_Data in fun prep_met                 *)
s1210629013@55372
   967
      (*branch   : TransitiveB set in append_problem at generation ob pblobj
s1210629013@55372
   968
          FIXXXME.0308: set branch from met in Apply_Method ?                *)
s1210629013@55372
   969
      ppc        : pat list,   (*.items in given, find, relate;
s1210629013@55372
   970
	      items (in "#Find") which need not occur in the arg-list of a SubProblem
s1210629013@55372
   971
        are 'copy-named' with an identifier "*'.'".
s1210629013@55372
   972
        copy-named items are 'generating' if they are NOT "*'''" ?WN120516??
s1210629013@55372
   973
        see ME/calchead.sml 'fun is_copy_named'.                              *)
s1210629013@55372
   974
      pre        : term list,  (*preconditions in where                       *)
s1210629013@55372
   975
      scr        : scr         (*prep_met gets progam or string "empty_script"*)
s1210629013@55372
   976
	   };
s1210629013@55372
   977
val e_met = {guh="met_empty",mathauthors=[],init=e_metID,
s1210629013@55372
   978
	     rew_ord' = "e_rew_ord'": rew_ord',
s1210629013@55372
   979
	      erls = e_rls, srls = e_rls, prls = e_rls,
s1210629013@55372
   980
	      calc = [], crls = e_rls, errpats = [], nrls= e_rls,
s1210629013@55372
   981
	      ppc = []: (string * (term * term)) list,
s1210629013@55372
   982
	      pre = []: term list,
s1210629013@55372
   983
	      scr = EmptyScr: scr}:met;
s1210629013@55372
   984
s1210629013@55372
   985
s1210629013@55372
   986
val e_Mets = Ptyp ("e_metID",[e_met],[]);
s1210629013@55372
   987
s1210629013@55372
   988
type mets = (met ptyp) list;
neuper@55335
   989
s1210629013@55373
   990
fun coll_metguhs mets =
s1210629013@55373
   991
    let fun node coll (Ptyp (_,[n],ns)) =
neuper@55428
   992
	    [(#guh : met -> guh) n] @ (nodes coll ns)
s1210629013@55373
   993
	and nodes coll [] = coll
s1210629013@55373
   994
	  | nodes coll (n::ns) = (node coll n) @ (nodes coll ns);
s1210629013@55373
   995
    in nodes [] mets end;
s1210629013@55373
   996
s1210629013@55373
   997
fun check_metguh_unique (guh:guh) (mets: (met ptyp) list) =
s1210629013@55373
   998
    if member op = (coll_metguhs mets) guh
s1210629013@55373
   999
    then error ("check_guh_unique failed with '"^guh^"';\n"^
s1210629013@55373
  1000
		      "use 'sort_metguhs()' for a list of guhs;\n"^
s1210629013@55373
  1001
		      "consider setting 'check_guhs_unique := false'")
s1210629013@55373
  1002
    else ();
s1210629013@55373
  1003
neuper@55450
  1004
fun Html_default exist = (Html {guh = theID2guh exist, 
neuper@55450
  1005
  coursedesign = ["isac team 2006"], mathauthors = [], html = ""})
neuper@55450
  1006
neuper@55450
  1007
fun fill_parents (exist, [i]) thydata = Ptyp (i, [thydata], [])
neuper@55450
  1008
  | fill_parents (exist, i :: is) thydata =
neuper@55450
  1009
    Ptyp (i, [Html_default (exist @ [i])], [fill_parents (exist @ [i], is) thydata])
neuper@55450
  1010
  | fill_parents _ _ = error "Html_default: avoid ML warning: Matches are not exhaustive"
neuper@55450
  1011
neuper@55450
  1012
fun add_thydata (exist, is) thydata [] = [fill_parents (exist, is) thydata]
neuper@55450
  1013
  | add_thydata (exist, [i]) data (pys as (py as Ptyp (key, _, _)) :: pyss) = 
neuper@55450
  1014
    if i = key
neuper@55450
  1015
    then pys (* preserve existing thydata *) 
neuper@55450
  1016
    else py :: add_thydata (exist, [i]) data pyss
neuper@55450
  1017
  | add_thydata (exist, iss as (i :: is)) data ((py as Ptyp (key, d, pys)) :: pyss) = 
neuper@55450
  1018
    if i = key
neuper@55450
  1019
    then       
neuper@55450
  1020
      if length pys = 0
neuper@55450
  1021
      then Ptyp (key, d, [fill_parents (exist @ [i], is) data]) :: pyss
neuper@55450
  1022
      else Ptyp (key, d, add_thydata (exist @ [i], is) data pys) :: pyss
neuper@55450
  1023
    else py :: add_thydata (exist, iss) data pyss
neuper@55450
  1024
  | add_thydata _ _ _ = error "add_thydata: avoid ML warning: Matches are not exhaustive"
neuper@55448
  1025
neuper@55448
  1026
fun update_hthm (Hthm {guh, coursedesign, mathauthors, thm, ...}) fillpats' =
neuper@55448
  1027
  Hthm {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
neuper@55448
  1028
    fillpats = fillpats', thm = thm}
neuper@55448
  1029
  | update_hthm _ _ = raise ERROR "wrong data";
neuper@55448
  1030
neuper@55448
  1031
(* for interface for dialog-authoring *)
neuper@55448
  1032
fun update_hrls (Hrls {guh, coursedesign, mathauthors, thy_rls = (thyID, rls)}) errpatIDs =
neuper@55448
  1033
  let
neuper@55448
  1034
    val rls' = 
neuper@55448
  1035
      case rls of
neuper@55448
  1036
        Rls {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...} =>
neuper@55448
  1037
          Rls {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
neuper@55448
  1038
            calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
neuper@55448
  1039
      | Seq {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...} =>
neuper@55448
  1040
            Seq {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
neuper@55448
  1041
              calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
neuper@55448
  1042
      | Rrls {id, prepat, rew_ord, erls, calc, scr, ...} =>
neuper@55448
  1043
            Rrls {id = id, prepat = prepat, rew_ord = rew_ord, erls = erls, calc = calc,
neuper@55448
  1044
              scr = scr, errpatts = errpatIDs}
neuper@55448
  1045
      | Erls => Erls
neuper@55448
  1046
  in
neuper@55448
  1047
    Hrls {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
neuper@55448
  1048
      thy_rls = (thyID, rls')}
neuper@55448
  1049
  end
neuper@55448
  1050
  | update_hrls _ _ = raise ERROR "wrong data";
neuper@55448
  1051
neuper@55448
  1052
fun app_py p f (d:pblID) (k(*:pblRD*)) = let
neuper@55448
  1053
    fun py_err _ =
neuper@55448
  1054
          error ("app_py: not found: " ^ (strs2str d));
neuper@55448
  1055
    fun app_py' _ [] = py_err ()
neuper@55448
  1056
      | app_py' [] _ = py_err ()
neuper@55448
  1057
      | app_py' [k0] ((p' as Ptyp (k', _, _  ))::ps) =
neuper@55448
  1058
          if k0 = k' then f p' else app_py' [k0] ps
neuper@55448
  1059
      | app_py' (k' as (k0::ks)) (Ptyp (k'', _, ps)::ps') =
neuper@55448
  1060
          if k0 = k'' then app_py' ks ps else app_py' k' ps';
neuper@55448
  1061
  in app_py' k p end;
neuper@55448
  1062
  
neuper@55448
  1063
fun get_py p = let
neuper@55448
  1064
        fun extract_py (Ptyp (_, [py], _)) = py
neuper@55448
  1065
          | extract_py _ = error ("extract_py: Ptyp has wrong format.");
neuper@55448
  1066
      in app_py p extract_py end;
neuper@55448
  1067
neuper@55448
  1068
fun (*KEStore_Elems.*)insert_fillpats th fis = (* for tests bypassing setup KEStore_Elems *)
neuper@55448
  1069
  let
neuper@55448
  1070
    fun update_elem th (theID, fillpats) =
neuper@55437
  1071
      let
neuper@55448
  1072
        val hthm = get_py th theID theID
neuper@55448
  1073
        val hthm' = update_hthm hthm fillpats
neuper@55448
  1074
          handle ERROR _ => error ("insert_fillpats: " ^ strs2str theID ^ "must address a theorem")
neuper@55448
  1075
      in update_ptyps theID theID hthm' end
neuper@55448
  1076
  in fold (update_elem th) fis end
neuper@55456
  1077
neuper@55456
  1078
(* group the theories defined in Isac, compare Build_Thydata:
neuper@55456
  1079
  section "Get and group the theories defined in Isac" *) 
neuper@55456
  1080
fun isabthys () = (*["Complex_Main", "Taylor", .., "Pure"]*)
neuper@55456
  1081
  let
neuper@55456
  1082
    val allthys = Theory.ancestors_of (Thy_Info.get_theory "Build_Thydata")
neuper@55456
  1083
  in
neuper@55456
  1084
    drop ((find_index (curry Theory.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), allthys)
neuper@55456
  1085
  end
neuper@55456
  1086
fun knowthys () = (*["Isac", .., "Descript", "Delete"]*)
neuper@55456
  1087
  let
neuper@55456
  1088
    fun isacthys () = (* ["Isac", .., "KEStore"] without Build_Isac thys: "Interpret" etc *)
neuper@55456
  1089
      let
neuper@55456
  1090
        val allthys = filter_out (member Theory.eq_thy
neuper@55456
  1091
          [(*Thy_Info.get_theory "ProgLang",*) Thy_Info.get_theory "Interpret", 
neuper@55456
  1092
            Thy_Info.get_theory "xmlsrc", Thy_Info.get_theory "Frontend"]) 
neuper@55456
  1093
          (Theory.ancestors_of (Thy_Info.get_theory "Build_Thydata"))
neuper@55456
  1094
      in
neuper@55456
  1095
        take ((find_index (curry Theory.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), 
neuper@55456
  1096
        allthys)
neuper@55456
  1097
      end
neuper@55456
  1098
    val isacthys' = isacthys ()
neuper@55456
  1099
    val proglang_parent = Thy_Info.get_theory "ProgLang"
neuper@55456
  1100
  in
neuper@55456
  1101
    take ((find_index (curry Theory.eq_thy proglang_parent) isacthys'), isacthys')
neuper@55456
  1102
  end
neuper@55456
  1103
fun progthys () = (*["Isac", .., "Descript", "Delete"]*)
neuper@55456
  1104
  let
neuper@55456
  1105
    fun isacthys () = (* ["Isac", .., "KEStore"] without Build_Isac thys: "Interpret" etc *)
neuper@55456
  1106
      let
neuper@55456
  1107
        val allthys = filter_out (member Theory.eq_thy
neuper@55456
  1108
          [(*Thy_Info.get_theory "ProgLang",*) Thy_Info.get_theory "Interpret", 
neuper@55456
  1109
            Thy_Info.get_theory "xmlsrc", Thy_Info.get_theory "Frontend"]) 
neuper@55456
  1110
          (Theory.ancestors_of (Thy_Info.get_theory "Build_Thydata"))
neuper@55456
  1111
      in
neuper@55456
  1112
        take ((find_index (curry Theory.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), 
neuper@55456
  1113
        allthys)
neuper@55456
  1114
      end
neuper@55456
  1115
    val isacthys' = isacthys ()
neuper@55456
  1116
    val proglang_parent = Thy_Info.get_theory "ProgLang"
neuper@55456
  1117
  in
neuper@55456
  1118
    drop ((find_index (curry Theory.eq_thy proglang_parent) isacthys') + 1(*ProgLang*), isacthys')
neuper@55456
  1119
  end
neuper@55456
  1120
neuper@55456
  1121
fun partID thy = 
neuper@55456
  1122
  if member Theory.eq_thy (knowthys ()) thy then "IsacKnowledge"
neuper@55456
  1123
  else if member Theory.eq_thy (progthys ()) thy then "IsacScripts"
neuper@55456
  1124
  else if member Theory.eq_thy (isabthys ()) thy then "Isabelle"
neuper@55456
  1125
  else error ("closure of thys in Isac is broken by " ^ string_of_thy thy)
neuper@55456
  1126
fun partID' (thy' : theory') = partID (Thy_Info.get_theory thy')
neuper@38061
  1127
(*
neuper@38061
  1128
end (*struct*)
neuper@38061
  1129
*)