src/Tools/isac/calcelems.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Sat, 22 Jun 2019 13:15:52 +0200
changeset 59550 2e7631381921
parent 59540 98298342fb6d
child 59562 d50fe358f04a
permissions -rw-r--r--
funpack: repair remaining test/../partial_fractions.sml, inverse_z_transform.sml
wneuper@59540
     1
(* ~~/src/Tools/isac/calcelems.sml
wneuper@59540
     2
   elements of calculations.
neuper@37906
     3
   they are partially held in association lists as ref's for
wneuper@59405
     4
   switching language levels (meta-string, object-values).                                  
wneuper@59405
     5
   in order to keep these ref's during re-evaluation of code,                       
neuper@42399
     6
   they are defined here at the beginning of the code.
neuper@42399
     7
   Author: Walther Neuper 2003
neuper@42399
     8
   (c) copyright due to lincense terms
neuper@37906
     9
*)
neuper@37906
    10
wneuper@59405
    11
signature CALC_ELEMENT =
wneuper@59405
    12
  sig
wneuper@59413
    13
    type cas_elem
wneuper@59413
    14
    type pbt
wneuper@59413
    15
    type ptyps
wneuper@59413
    16
    type metID
wneuper@59413
    17
    type pblID
wneuper@59413
    18
    type mets
wneuper@59413
    19
    type met
wneuper@59405
    20
    datatype 'a ptyp = Ptyp of string * 'a list * 'a ptyp list
wneuper@59405
    21
wneuper@59413
    22
    type authors
wneuper@59405
    23
    type guh
wneuper@59416
    24
    val env2str: Rule.subst -> string
wneuper@59416
    25
    val subst2str: Rule.subst -> string
wneuper@59416
    26
    val subst2str': Rule.subst -> string
wneuper@59414
    27
wneuper@59413
    28
    type fillpat
wneuper@59405
    29
    datatype thydata
wneuper@59416
    30
      = Hcal of {calc: Rule.calc, coursedesign: authors, guh: guh, mathauthors: authors}
wneuper@59416
    31
      | Hord of {coursedesign: authors, guh: guh, mathauthors: authors, ord: Rule.subst -> term * term -> bool}
wneuper@59416
    32
      | Hrls of {coursedesign: authors, guh: guh, mathauthors: authors, thy_rls: Rule.thyID * Rule.rls}
wneuper@59405
    33
      | Hthm of {coursedesign: authors, fillpats: fillpat list, guh: guh, mathauthors: authors, thm: thm}
wneuper@59405
    34
      | Html of {coursedesign: authors, guh: guh, html: string, mathauthors: authors}
wneuper@59413
    35
    type theID
wneuper@59413
    36
    type rlss_elem
wneuper@59405
    37
    val merge_rlss: rlss_elem list * rlss_elem list -> rlss_elem list
wneuper@59405
    38
    val rls_eq: (''a * ('b * 'c)) * (''a * ('d * 'e)) -> bool
wneuper@59413
    39
    type spec
wneuper@59405
    40
    val cas_eq: cas_elem * cas_elem -> bool
wneuper@59405
    41
    val e_Ptyp: pbt ptyp
wneuper@59405
    42
    val merge_ptyps: 'a ptyp list * 'a ptyp list -> 'a ptyp list
wneuper@59405
    43
    val check_guhs_unique: bool Unsynchronized.ref
wneuper@59405
    44
    val check_pblguh_unique: guh -> pbt ptyp list -> unit
wneuper@59405
    45
    val insrt: pblID -> 'a -> string list -> 'a ptyp list -> 'a ptyp list
wneuper@59405
    46
    val e_Mets: met ptyp
wneuper@59405
    47
    val check_metguh_unique: guh -> met ptyp list -> unit
wneuper@59405
    48
    val add_thydata: string list * string list -> thydata -> thydata ptyp list -> thydata ptyp list
wneuper@59405
    49
    val get_py: 'a ptyp list -> pblID -> string list -> 'a
wneuper@59405
    50
    val update_hthm: thydata -> fillpat list -> thydata
wneuper@59405
    51
    val update_ptyps: string list -> string list -> 'a -> 'a ptyp list -> 'a ptyp list
wneuper@59405
    52
    val part2guh: theID -> guh
wneuper@59405
    53
    val spec2str: string * string list * string list -> string
wneuper@59405
    54
    val linefeed: string -> string
wneuper@59405
    55
    val pbts2str: pbt list -> string
wneuper@59405
    56
    val thes2str: thydata list -> string
wneuper@59405
    57
    val theID2str: string list -> string
wneuper@59405
    58
    val the2str: thydata -> string
wneuper@59405
    59
    val trace_calc: bool Unsynchronized.ref
wneuper@59405
    60
    eqtype thmID
wneuper@59413
    61
    type thm'
wneuper@59405
    62
    datatype lrd = D | L | R
wneuper@59405
    63
    val trace_rewrite: bool Unsynchronized.ref
wneuper@59405
    64
    val depth: int Unsynchronized.ref
wneuper@59415
    65
    val assoc_thy: Rule.theory' -> theory
wneuper@59413
    66
    type loc_
wneuper@59405
    67
    val loc_2str: loc_ -> string
wneuper@59413
    68
    type thm''
wneuper@59405
    69
    val metID2str: string list -> string
wneuper@59405
    70
    val e_pblID: pblID
wneuper@59405
    71
    val e_metID: metID
wneuper@59405
    72
    val empty_spec: spec
wneuper@59414
    73
    val e_spec: spec
wneuper@59405
    74
    datatype ketype = Exp_ | Met_ | Pbl_ | Thy_
wneuper@59413
    75
    type kestoreID
wneuper@59405
    76
    val app_py: 'a ptyp list -> ('a ptyp -> 'b) -> pblID -> string list -> 'b
wneuper@59405
    77
    val ketype2str: ketype -> string
wneuper@59405
    78
    val coll_pblguhs: pbt ptyp list -> guh list
wneuper@59405
    79
    val coll_metguhs: met ptyp list -> guh list
wneuper@59413
    80
    type pat
wneuper@59405
    81
    val pats2str: pat list -> string
wneuper@59405
    82
    val maxthy: theory -> theory -> theory
wneuper@59405
    83
    eqtype filename
wneuper@59405
    84
    val lim_deriv: int Unsynchronized.ref
wneuper@59416
    85
    val id_of_thm: Rule.rule -> string
wneuper@59405
    86
    val isabthys: unit -> theory list
wneuper@59405
    87
    val thyID_of_derivation_name: string -> string
wneuper@59415
    88
    val partID': Rule.theory' -> string
wneuper@59415
    89
    val thm2guh: string * Rule.thyID -> thmID -> guh
wneuper@59405
    90
    val thmID_of_derivation_name: string -> string
wneuper@59416
    91
    val rls2guh: string * Rule.thyID -> Rule.rls' -> guh
wneuper@59405
    92
    val theID2guh: theID -> guh
wneuper@59405
    93
    eqtype fillpatID
wneuper@59405
    94
    type pbt_ = string * (term * term)
wneuper@59405
    95
    eqtype xml
wneuper@59415
    96
    val cal2guh: string * Rule.thyID -> string -> guh
wneuper@59405
    97
    val ketype2str': ketype -> string
wneuper@59405
    98
    val str2ketype': string -> ketype
wneuper@59405
    99
    val thmID_of_derivation_name': thm -> string
wneuper@59405
   100
    eqtype path
wneuper@59415
   101
    val theID2thyID: theID -> Rule.thyID
wneuper@59405
   102
    val thy2guh: theID -> guh
wneuper@59405
   103
    val thypart2guh: theID -> guh
wneuper@59474
   104
    val ord2guh: string * Rule.theory' -> string -> string
wneuper@59416
   105
    val update_hrls: thydata -> Rule.errpatID list -> thydata
wneuper@59405
   106
    eqtype iterID
wneuper@59405
   107
    eqtype calcID
wneuper@59405
   108
    val thm''_of_thm: thm -> thm''
wneuper@59416
   109
    val thm_of_thm: Rule.rule -> thm
wneuper@59408
   110
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59414
   111
    val thm2str: thm -> string
wneuper@59540
   112
    val pats2str' : pat list -> string
wneuper@59414
   113
    val insert_fillpats: thydata ptyp list -> (pblID * fillpat list) list -> thydata ptyp list ->
wneuper@59414
   114
      thydata ptyp list
wneuper@59408
   115
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59411
   116
    val knowthys: unit -> theory list
wneuper@59411
   117
    val e_pbt: pbt
wneuper@59507
   118
    val e_met: met
wneuper@59408
   119
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59414
   120
wneuper@59414
   121
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59416
   122
val overwritelthy: theory -> (Rule.rls' * (string * Rule.rls)) list * (Rule.rls' * Rule.rls) list ->
wneuper@59416
   123
  (Rule.rls' * (string * Rule.rls)) list  end
wneuper@59414
   124
wneuper@59416
   125
               
wneuper@59540
   126
structure Celem(**): CALC_ELEMENT(**) =
neuper@38061
   127
struct
wneuper@59405
   128
wneuper@59414
   129
val linefeed = (curry op^) "\n"; (* ?\<longrightarrow> libraryC ?*)
neuper@37906
   130
type authors = string list;
neuper@37906
   131
wneuper@59414
   132
type iterID = int;
wneuper@59414
   133
type calcID = int;
neuper@42399
   134
neuper@55481
   135
(* TODO CLEANUP Thm:
neuper@55487
   136
Thm (string, thm): (a) needs string to identify sym_thmID for handling in front-end;
neuper@55487
   137
                   (b) investigate if ""RS sym" attaches a [.]" still occurs: string_of_thmI
neuper@55487
   138
thmID            : type for data from user input + program
neuper@55487
   139
thmDeriv         : type for thy_hierarchy ONLY
neuper@55487
   140
obsolete types   : thm' (SEE "ad thm'"), thm''. 
wneuper@59250
   141
revise funs      : id_of_thm, thm_of_thm, rep_thm_G', eq_thmI, eq_thmI', thm''_of_thm thm.
neuper@55487
   142
activate         : thmID_of_derivation_name'
neuper@55481
   143
*)
neuper@42399
   144
type thmID = string;    (* identifier for a thm (the shortest possible identifier)       *)
neuper@42434
   145
type thmDeriv = string; (* WN120524 deprecated
neuper@42434
   146
  thyID ^"."^ xxx ^"."^ thmID, see fun thmID_of_derivation_name 
neuper@42434
   147
  WN120524: dont use Thm.derivation_name, this is destroyed by num_str;
neuper@42434
   148
  Thm.get_name_hint survives num_str and seems perfectly reliable *)
neuper@42399
   149
wneuper@59415
   150
type thm' = thmID * Rule.cterm';(*WN060610 deprecated in favour of thm''; WN180324: used in TODO review:
wneuper@59414
   151
val thm'2xml : int -> Celem.thm' -> Celem.xml
wneuper@59414
   152
val assoc_thm': theory -> Celem.thm' -> thm
wneuper@59416
   153
| Calculate' of Rule.theory' * string * term * (term * Celem.thm')
wneuper@59414
   154
*)
wneuper@59251
   155
(* tricky combination of (string, term) for theorems in Isac:
wneuper@59251
   156
  * case 1 general: frontend + lucin, e.g. applicable_in..Rewrite: (thmID, _) --> (thmID, thm)
wneuper@59251
   157
    by Global_Theory.get_thm, special cases ("add_commute",..) see convert_metaview_to_thmid.
wneuper@59251
   158
  * case 2 "sym_..": Global_Theory.get_thm..RS sym
wneuper@59251
   159
  * case 3 ad-hoc thm "#..." mk_thm from ad-hoc term (numerals only) in calculate_:
wneuper@59255
   160
    from applicable_in..Calculate: opstr --calculate_/adhoc_thm--> (thmID, thm)
wneuper@59251
   161
*)
wneuper@59253
   162
type thm'' = thmID * thm; (* only for transport via libisabelle isac-java <--- ME *)
neuper@42399
   163
neuper@40836
   164
(*.a 'guh'='globally unique handle' is a string unique for each element
neuper@37906
   165
   of isac's KEStore and persistent over time
neuper@37906
   166
   (in particular under shifts within the respective hierarchy);
neuper@40836
   167
   specialty for thys:
neuper@37906
   168
   # guh NOT resistant agains shifts from one thy to another
neuper@37906
   169
   (which is the price for Isabelle's design: thy's overwrite ids of subthy's)
neuper@37906
   170
   # requirement for matchTheory: induce guh from tac + current thy
neuper@37906
   171
   (see 'fun thy_containing_thm', 'fun thy_containing_rls' etc.)
neuper@40836
   172
   TODO: introduce to pbl, met.*)
neuper@37906
   173
type guh = string;
neuper@37906
   174
wneuper@59141
   175
type xml = string; (* rm together with old code replaced by XML.tree *)
neuper@37906
   176
neuper@42451
   177
neuper@42451
   178
(* for (at least) 2 kinds of access:
neuper@42451
   179
  (1) given an errpatID, find the respective fillpats (e.g. in fun find_fill_pats)
neuper@42451
   180
  (2) given a thm, find respective fillpats *)
neuper@42451
   181
type fillpatID = string
neuper@42451
   182
type fillpat =
neuper@42451
   183
  fillpatID   (* DESIGN ?TODO: give an order w.r.t difficulty ? *)
neuper@42451
   184
  * term      (* the pattern with fill-in gaps                  *)
wneuper@59415
   185
  * Rule.errpatID; (* which the fillpat would be a help for          
neuper@42451
   186
                 DESIGN ?TODO: list for several patterns ?      *)
neuper@37906
   187
neuper@40836
   188
wneuper@59414
   189
(*  WN0509 discussion:
neuper@37906
   190
#############################################################################
neuper@37906
   191
#   How to manage theorys in subproblems wrt. the requirement,              #
neuper@37906
   192
#   that scripts should be re-usable ?                                      #
neuper@37906
   193
#############################################################################
neuper@37906
   194
neuper@37988
   195
    eg. 'Script Solve_rat_equation' calls 'SubProblem (RatEq',..'
neuper@37906
   196
    which would not allow to 'solve (y'' = -M_b / EI, M_b)' by this script
neuper@40836
   197
    because Biegelinie.thy is subthy of RatEq.thy and thus Biegelinie.M_b
neuper@37906
   198
    is unknown in RatEq.thy and M_b cannot be parsed into the scripts guard
neuper@37906
   199
    (see match_ags).
neuper@37906
   200
neuper@37906
   201
    Preliminary solution:
neuper@38011
   202
    # the thy in 'SubProblem (thy', pbl, arglist)' is not taken automatically,
neuper@38011
   203
    # instead the 'maxthy (rootthy pt) thy' is taken for each subpbl
neuper@40836
   204
    # however, a thy specified by the user in the rootpbl may lead to
neuper@40836
   205
      errors in far-off subpbls (which are not yet reported properly !!!)
neuper@37906
   206
      and interactively specifiying thys in subpbl is not very relevant.
neuper@37906
   207
neuper@37906
   208
    Other solutions possible:
wneuper@59352
   209
    # always parse and type-check with Thy_Info_get_theory "Isac"
wneuper@59414
   210
      (rejected due to the vague idea eg. to re-use equations for R in C etc.)
neuper@37906
   211
    # regard the subthy-relation in specifying thys of subpbls
neuper@38011
   212
    # specifically handle 'SubProblem (undefined, pbl, arglist)'
neuper@37906
   213
    # ???
wneuper@59414
   214
*)
wneuper@59414
   215
wneuper@59414
   216
fun thm2str thm =
wneuper@59414
   217
  let
wneuper@59414
   218
    val t = Thm.prop_of thm
wneuper@59414
   219
    val ctxt = Proof_Context.init_global (Thy_Info.get_theory ("Isac.Isac"))
wneuper@59414
   220
    val ctxt' = Config.put show_markup false ctxt
wneuper@59414
   221
  in Print_Mode.setmp [] (Syntax.string_of_term ctxt') t end;
wneuper@59414
   222
                 
wneuper@59415
   223
fun id_of_thm (Rule.Thm (id, _)) = id  (* TODO re-arrange code for rule2str *)
wneuper@59414
   224
  | id_of_thm _ = raise ERROR ("id_of_thm: uncovered case " (* ^ rule2str r *))
wneuper@59415
   225
fun thm_of_thm (Rule.Thm (_, thm)) = thm  (* TODO re-arrange code for rule2str *)
wneuper@59414
   226
  | thm_of_thm _ = raise ERROR ("thm_of_thm: uncovered case " (* ^ rule2str r *))
wneuper@59414
   227
wneuper@59414
   228
fun thmID_of_derivation_name dn = last_elem (space_explode "." dn);
wneuper@59414
   229
fun thmID_of_derivation_name' thm = (thmID_of_derivation_name o Thm.get_name_hint) thm
wneuper@59414
   230
fun thyID_of_derivation_name dn = hd (space_explode "." dn);
wneuper@59414
   231
fun thm''_of_thm thm = (thmID_of_derivation_name' thm, thm) : thm''
wneuper@59414
   232
wneuper@59414
   233
neuper@37906
   234
neuper@37906
   235
(*the key into the hierarchy ob theory elements*)
neuper@37906
   236
type theID = string list;
wneuper@59414
   237
val theID2str = strs2str; (*theID eg. is ["IsacKnowledge", "Test", "Rulesets", "ac_plus_times"]*)
wneuper@59414
   238
fun theID2thyID theID =
wneuper@59414
   239
  if length theID >= 3 then (last_elem o (drop_last_n 2)) theID
wneuper@59414
   240
  else error ("theID2thyID called with " ^ theID2str theID);
neuper@37906
   241
neuper@37906
   242
(*the key into the hierarchy ob problems*)
wneuper@59414
   243
type pblID = string list; (* domID :: ...*)
wneuper@59414
   244
val e_pblID = ["e_pblID"];
neuper@37906
   245
neuper@37906
   246
(*the key into the hierarchy ob methods*)
neuper@37906
   247
type metID = string list;
wneuper@59415
   248
type spec = Rule.domID * pblID * metID;
wneuper@59414
   249
fun spec2str (dom, pbl, met) = 
wneuper@59414
   250
  "(" ^ quote dom  ^ ", " ^ strs2str pbl ^ ", " ^ strs2str met ^ ")";
wneuper@59414
   251
val e_metID = ["e_metID"];
neuper@37906
   252
val metID2str = strs2str;
wneuper@59415
   253
val empty_spec = (Rule.e_domID, e_pblID, e_metID);
s1210629013@52171
   254
val e_spec = empty_spec;
s1210629013@52171
   255
wneuper@59414
   256
(* association list with cas-commands, for generating a complete calc-head *)
neuper@52169
   257
type generate_fn = 
neuper@52169
   258
  (term list ->    (* the arguments of the cas-command, eg. (x+1=2, x) *)
neuper@52169
   259
    (term *        (* description of an element                        *)
neuper@52169
   260
      term list)   (* value of the element (always put into a list)    *)
neuper@52169
   261
  list)            (* of elements in the formalization                 *)
neuper@52169
   262
type cas_elem = 
neuper@52169
   263
  (term *          (* cas-command, eg. 'solve'                         *)
neuper@52169
   264
    (spec *        (* theory, problem, method                          *)
s1210629013@52174
   265
    generate_fn))
neuper@52169
   266
fun cas_eq ((t1, (_, _)) : cas_elem, (t2, (_, _)) : cas_elem) = t1 = t2
s1210629013@52167
   267
neuper@37906
   268
(*either theID or pblID or metID*)
neuper@37906
   269
type kestoreID = string list;
neuper@37906
   270
wneuper@59414
   271
(* for distinction of contexts WN130621: disambiguate with Isabelle's Context *)
neuper@37906
   272
datatype ketype = Exp_ | Thy_ | Pbl_ | Met_;
neuper@37906
   273
fun ketype2str Exp_ = "Exp_"
neuper@40836
   274
  | ketype2str Thy_ = "Thy_"
neuper@40836
   275
  | ketype2str Pbl_ = "Pbl_"
neuper@37906
   276
  | ketype2str Met_ = "Met_";
neuper@37906
   277
fun ketype2str' Exp_ = "Example"
neuper@40836
   278
  | ketype2str' Thy_ = "Theory"
neuper@40836
   279
  | ketype2str' Pbl_ = "Problem"
neuper@37906
   280
  | ketype2str' Met_ = "Method";
wneuper@59172
   281
(* for conversion from XML *)
wneuper@59172
   282
fun str2ketype' "exp" = Exp_
wneuper@59172
   283
  | str2ketype' "thy" = Thy_
wneuper@59172
   284
  | str2ketype' "pbl" = Pbl_
wneuper@59172
   285
  | str2ketype' "met" = Met_
wneuper@59172
   286
  | str2ketype' str = raise ERROR ("str2ketype': WRONG arg = " ^ str)
neuper@37906
   287
neuper@48887
   288
(* A tree for storing data defined in different theories 
neuper@48887
   289
  for access from the Interpreter and from dialogue authoring 
neuper@48887
   290
  using a string list as key.
wneuper@59414
   291
  'a is for pbt | met | thydata; after WN030424 naming "pbt" became inappropriate *)
neuper@40836
   292
datatype 'a ptyp =
wneuper@59414
   293
	Ptyp of string *  (* element of the key                                                        *)
wneuper@59414
   294
		'a list *       (* several pbts with different domIDs/thy TODO: select by subthy (isaref.p.69)
wneuper@59414
   295
			                 presently only _ONE_ elem FOR ALL KINDS OF CONTENT pbt | met | thydata    *)
wneuper@59414
   296
		('a ptyp) list; (* the children nodes                                                        *)
neuper@37906
   297
wneuper@59414
   298
(* datatype for collecting thydata for hierarchy *)
neuper@37906
   299
(*WN060720 more consistent naming would be 'type thyelem' or 'thelem'*)
wneuper@59414
   300
datatype thydata =
wneuper@59414
   301
  Html of {guh: guh, coursedesign: authors, mathauthors: authors, html: string}
wneuper@59414
   302
| Hthm of {guh: guh, coursedesign: authors, mathauthors: authors, fillpats: fillpat list,
wneuper@59414
   303
   thm: thm} (* here no sym_thm, thus no thmID required *)
wneuper@59415
   304
| Hrls of {guh: guh, coursedesign: authors, mathauthors: authors, thy_rls: (Rule.thyID * Rule.rls)}
wneuper@59415
   305
| Hcal of {guh: guh, coursedesign: authors, mathauthors: authors, calc: Rule.calc}
wneuper@59414
   306
| Hord of {guh: guh, coursedesign: authors, mathauthors: authors,
wneuper@59415
   307
    ord: (Rule.subst -> (term * term) -> bool)};
wneuper@59414
   308
fun the2str (Html {guh, ...}) = guh
wneuper@59414
   309
  | the2str (Hthm {guh, ...}) = guh
wneuper@59414
   310
  | the2str (Hrls {guh, ...}) = guh
wneuper@59414
   311
  | the2str (Hcal {guh, ...}) = guh
wneuper@59414
   312
  | the2str (Hord {guh, ...}) = guh
neuper@55435
   313
fun thes2str thes = map the2str thes |> list2str;
neuper@37906
   314
neuper@55483
   315
(* notes on thehier concerning sym_thmID theorems (created in derivations, reverse rewriting)
neuper@55487
   316
     (a): thehier does not contain sym_thmID theorems
neuper@55485
   317
     (b): lookup for sym_thmID directly from Isabelle using sym_thm
neuper@55485
   318
          (within math-engine NO lookup in thehier -- within java in *.xml only!)
neuper@55483
   319
TODO (c): export from thehier to xml
neuper@55483
   320
TODO (c1)   creates one entry for "thmID" (and NONE for "sym_thmID") in the hierarchy
neuper@55483
   321
TODO (c2)   creates 2 files "thy_*-thm-thmID.xml" and "thy_*-thm-sym_thmID.xml"
neuper@55483
   322
TODO (d): 1 entry in the MiniBrowser's hierarchy (generated from xml)
neuper@55483
   323
          stands for both, "thmID" and "sym_thmID" 
neuper@55483
   324
TODO (d1)   lookup from calctxt          
neuper@55483
   325
TODO (d1)   lookup from from rule set in MiniBrowser *)
neuper@37906
   326
type thehier = (thydata ptyp) list;
neuper@55405
   327
(* required to determine sequence of main nodes of thehier in KEStore.thy *)
wneuper@59414
   328
fun part2guh [str] = (case str of
wneuper@59414
   329
	  "Isabelle" => "thy_isab_" ^ str ^ "-part" : guh
neuper@55405
   330
      | "IsacScripts" => "thy_scri_" ^ str ^ "-part"
neuper@55405
   331
      | "IsacKnowledge" => "thy_isac_" ^ str ^ "-part"
wneuper@59414
   332
      | str => raise ERROR ("thy2guh: called with \""^ str ^"\""))
wneuper@59414
   333
  | part2guh theID = raise ERROR ("part2guh called with theID = \"" ^ theID2str theID ^ "'");
neuper@37906
   334
wneuper@59414
   335
fun thy2guh [part, thyID] = (case part of
wneuper@59414
   336
      "Isabelle" => "thy_isab_" ^ thyID
neuper@55437
   337
    | "IsacScripts" => "thy_scri_" ^ thyID
neuper@55437
   338
    | "IsacKnowledge" => "thy_isac_" ^ thyID
wneuper@59414
   339
    | str => raise ERROR ("thy2guh: called with \"" ^ str ^ "\""))
wneuper@59414
   340
  | thy2guh theID = raise ERROR ("thy2guh called with \"" ^ strs2str' theID ^ "\"");
neuper@55437
   341
			
wneuper@59414
   342
fun thypart2guh ([part, thyID, thypart] : theID) = (case part of
wneuper@59414
   343
      "Isabelle" => "thy_isab_" ^ thyID ^ "-" ^ thypart : guh
wneuper@59414
   344
    | "IsacScripts" => "thy_scri_" ^ thyID ^ "-" ^ thypart
wneuper@59414
   345
    | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-" ^ thypart
wneuper@59414
   346
    | str => raise ERROR ("thypart2guh: called with '" ^ str ^ "'"))
wneuper@59414
   347
  | thypart2guh strs = raise ERROR ("thypart2guh called with \"" ^ strs2str' strs ^ "\"");
wneuper@59414
   348
neuper@55437
   349
  
wneuper@59414
   350
(* convert the data got via contextToThy to a globally unique handle.
wneuper@59414
   351
   there is another way to get the guh: get out of the 'theID' in the hierarchy *)
wneuper@59414
   352
fun thm2guh (isa, thyID) thmID = case isa of
wneuper@59415
   353
    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID : guh
wneuper@59415
   354
  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
wneuper@59415
   355
  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
wneuper@59414
   356
  | _ => raise ERROR
wneuper@59414
   357
    ("thm2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for thm = \"" ^ thmID ^ "\"");
neuper@55437
   358
wneuper@59414
   359
fun rls2guh (isa, thyID) rls' = case isa of
wneuper@59415
   360
    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-rls-" ^ rls' : guh
wneuper@59415
   361
  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-rls-" ^ rls'
wneuper@59415
   362
  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-rls-" ^ rls'
wneuper@59414
   363
  | _ => raise ERROR
wneuper@59414
   364
    ("rls2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for rls = \"" ^ rls' ^ "\"");
neuper@55437
   365
			  
wneuper@59414
   366
fun cal2guh (isa, thyID) calID = case isa of
wneuper@59415
   367
    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-cal-" ^ calID : guh
wneuper@59415
   368
  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-cal-" ^ calID
wneuper@59415
   369
  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-cal-" ^ calID
wneuper@59414
   370
  | _ => raise ERROR
wneuper@59414
   371
    ("cal2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for cal = \"" ^ calID ^ "\"");
neuper@55437
   372
			  
wneuper@59414
   373
fun ord2guh (isa, thyID) rew_ord' = case isa of
wneuper@59415
   374
    "Isabelle" => "thy_isab_" ^ Rule.theory'2thyID thyID ^ "-ord-" ^ rew_ord' : guh
wneuper@59415
   375
  | "IsacKnowledge" => "thy_isac_" ^ Rule.theory'2thyID thyID ^ "-ord-" ^ rew_ord'
wneuper@59415
   376
  | "IsacScripts" => "thy_scri_" ^ Rule.theory'2thyID thyID ^ "-ord-" ^ rew_ord'
wneuper@59414
   377
  | _ => raise ERROR
wneuper@59414
   378
    ("ord2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for ord = \"" ^ rew_ord' ^ "\"");
neuper@55437
   379
neuper@55437
   380
(* not only for thydata, but also for thy's etc *)
wneuper@59414
   381
(* TODO
wneuper@59414
   382
fun theID2guh theID = case length theID of
neuper@55437
   383
    0 => error ("theID2guh: called with theID = " ^ strs2str' theID)
neuper@55437
   384
  | 1 => part2guh theID
neuper@55437
   385
  | 2 => thy2guh theID
neuper@55437
   386
  | 3 => thypart2guh theID
neuper@55437
   387
  | 4 => 
neuper@55437
   388
    let val [isa, thyID, typ, elemID] = theID
neuper@55437
   389
    in case typ of
neuper@55437
   390
        "Theorems" => thm2guh (isa, thyID) elemID
neuper@55437
   391
      | "Rulesets" => rls2guh (isa, thyID) elemID
neuper@55437
   392
      | "Calculations" => cal2guh (isa, thyID) elemID
neuper@55437
   393
      | "Orders" => ord2guh (isa, thyID) elemID
neuper@55437
   394
      | "Theorems" => thy2guh [isa, thyID]
wneuper@59414
   395
      | str => raise ERROR ("theID2guh: called with theID = " ^ strs2str' theID)
neuper@55437
   396
    end
wneuper@59414
   397
  | n => raise ERROR ("theID2guh called with theID = " ^ strs2str' theID);
wneuper@59414
   398
*)
wneuper@59414
   399
(* not only for thydata, but also for thy's etc *)
wneuper@59414
   400
fun theID2guh [] = raise ERROR ("theID2guh: called with []")
wneuper@59414
   401
  | theID2guh [str] = part2guh [str]
wneuper@59414
   402
  | theID2guh [s1, s2] = thy2guh [s1, s2]
wneuper@59414
   403
  | theID2guh [s1, s2, s3] = thypart2guh [s1, s2, s3]
wneuper@59414
   404
  | theID2guh (strs as [isa, thyID, typ, elemID]) = (case typ of
wneuper@59414
   405
      "Theorems" => thm2guh (isa, thyID) elemID
wneuper@59414
   406
    | "Rulesets" => rls2guh (isa, thyID) elemID
wneuper@59414
   407
    | "Calculations" => cal2guh (isa, thyID) elemID
wneuper@59414
   408
    | "Orders" => ord2guh (isa, thyID) elemID
wneuper@59414
   409
    | _ => raise ERROR ("theID2guh: called with theID = " ^ strs2str' strs))
wneuper@59414
   410
  | theID2guh strs = raise ERROR ("theID2guh called with theID = " ^ strs2str' strs);
neuper@55437
   411
neuper@37906
   412
type path = string;
neuper@37906
   413
type filename = string;
neuper@37906
   414
neuper@42451
   415
neuper@37906
   416
neuper@52141
   417
(* datastructure for KEStore_Elems, intermediate for thehier *)
neuper@52141
   418
type rlss_elem = 
wneuper@59416
   419
  (Rule.rls' *     (* identifier unique within Isac *)
wneuper@59415
   420
  (Rule.theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
wneuper@59415
   421
    Rule.rls))     (* ((#id o rep_rls) rls) = rls'   by coding discipline *)
neuper@52141
   422
fun rls_eq ((id1, (_, _)), (id2, (_, _))) = id1 = id2
neuper@37906
   423
neuper@52141
   424
fun insert_merge_rls (re as (id, (thyID, r1)) : rlss_elem) ys = 
wneuper@59414
   425
    case get_index (fn y => if curry rls_eq re y then SOME y else NONE) ys of
wneuper@59414
   426
      NONE => re :: ys
wneuper@59414
   427
    | SOME (i, (_, (_, r2))) => 
neuper@52141
   428
      let
wneuper@59415
   429
        val r12 = Rule.merge_rls id r1 r2
neuper@52141
   430
      in list_update ys i (id, (thyID, r12)) end
neuper@52141
   431
fun merge_rlss (s1, s2) = fold insert_merge_rls s1 s2;
neuper@52141
   432
neuper@52141
   433
wneuper@59414
   434
fun assoc_thy thy =
wneuper@59414
   435
    if thy = "e_domID"
wneuper@59415
   436
    then (Rule.Thy_Info_get_theory "Script") (*lower bound of Knowledge*)
wneuper@59415
   437
    else (Rule.Thy_Info_get_theory thy) handle _ => error ("ME_Isa: thy \"" ^ thy ^ "\" not in system");
neuper@37906
   438
wneuper@59414
   439
(* overwrite an element in an association list and pair it with a thyID
neuper@37906
   440
   in order to create the thy_hierarchy;
neuper@37906
   441
   overwrites existing rls' even if they are defined in a different thy;
wneuper@59414
   442
   this is related to assoc_rls, TODO.WN060120: assoc_rew_ord, assoc_calc *)
wneuper@59414
   443
(* WN060120 ...these are NOT compatible to "fun assoc_thm'" in that
neuper@37906
   444
   they do NOT handle overlays by re-using an identifier in different thys;
neuper@37906
   445
   "thyID.rlsID" would be a good solution, if the "." would be possible
neuper@37906
   446
   in scripts...
wneuper@59414
   447
   actually a hack to get alltogether run again with minimal effort *)
neuper@37906
   448
fun insthy thy' (rls', rls) = (rls', (thy', rls));
wneuper@59416
   449
fun overwritelthy thy (al, bl: (Rule.rls' * Rule.rls) list) =
wneuper@59415
   450
    let val bl' = map (insthy ((get_thy o Rule.theory2theory') thy)) bl
neuper@37906
   451
    in overwritel (al, bl') end;
neuper@37906
   452
wneuper@59414
   453
fun subst2str s =
neuper@40836
   454
    (strs2str o
wneuper@59414
   455
      (map (
wneuper@59415
   456
        linefeed o pair2str o (apsnd Rule.term2str) o (apfst Rule.term2str)))) s;
wneuper@59415
   457
fun subst2str' s =
neuper@40836
   458
    (strs2str' o
wneuper@59414
   459
     (map (
wneuper@59415
   460
       pair2str o (apsnd Rule.term2str) o (apfst Rule.term2str)))) s;
neuper@37906
   461
val env2str = subst2str;
neuper@37906
   462
wneuper@59332
   463
fun maxthy thy1 thy2 = if Context.subthy (thy1, thy2) then thy2 else thy1;
neuper@37906
   464
neuper@37906
   465
wneuper@59387
   466
(* trace internal steps of isac's numeral calculations *)
wneuper@59387
   467
val trace_calc = Unsynchronized.ref false;
wneuper@59414
   468
(* trace internal steps of isac's rewriter *)
neuper@38006
   469
val trace_rewrite = Unsynchronized.ref false;
wneuper@59414
   470
(* depth of recursion in traces of the rewriter, if trace_rewrite:=true *)
neuper@38006
   471
val depth = Unsynchronized.ref 99999;
wneuper@59414
   472
(* no of rewrites exceeding this int -> NO rewrite *)
neuper@38006
   473
val lim_deriv = Unsynchronized.ref 100;
wneuper@59414
   474
(* switch for checking guhs unique before storing a pbl or met;
neuper@37906
   475
   set true at startup (done at begin of ROOT.ML)
wneuper@59414
   476
   set false for editing IsacKnowledge (done at end of ROOT.ML) *)
neuper@41905
   477
val check_guhs_unique = Unsynchronized.ref true;
neuper@37906
   478
neuper@37906
   479
wneuper@59414
   480
datatype lrd = (*elements of "type loc_" into an Isabelle term*)
wneuper@59414
   481
	L   (*go left at $*)
wneuper@59414
   482
| R   (*go right at $*)
wneuper@59414
   483
| D;  (*go down at Abs*)
neuper@37906
   484
type loc_ = lrd list;
neuper@37906
   485
fun ldr2str L = "L"
neuper@37906
   486
  | ldr2str R = "R"
neuper@37906
   487
  | ldr2str D = "D";
wneuper@59414
   488
fun loc_2str k = (strs2str' o (map ldr2str)) k;
neuper@37906
   489
neuper@55335
   490
neuper@55335
   491
(* the pattern for an item of a problems model or a methods guard *)
neuper@55335
   492
type pat =
neuper@55335
   493
  (string *     (* field               *)
neuper@55335
   494
	  (term *     (* description         *)
neuper@55335
   495
	     term))   (* id | arbitrary term *);
neuper@55335
   496
fun pat2str ((field, (dsc, id)) : pat) = 
wneuper@59415
   497
  pair2str (field, pair2str (Rule.term2str dsc, Rule.term2str id))
neuper@55335
   498
fun pats2str pats = (strs2str o (map pat2str)) pats
wneuper@59540
   499
fun pat2str' ((field, (dsc, id)) : pat) = 
wneuper@59540
   500
  pair2str (field, pair2str (Rule.term2str dsc, Rule.term2str id)) ^ "\n"
wneuper@59540
   501
fun pats2str' pats = (strs2str o (map pat2str')) pats
neuper@55335
   502
neuper@55357
   503
(* types for problems models (TODO rename to specification models) *)
neuper@55335
   504
type pbt_ =
neuper@55335
   505
  (string *   (* field "#Given",..*)(*deprecated due to 'type pat'*)
neuper@55335
   506
    (term *   (* description      *)
neuper@55335
   507
      term)); (* id | struct-var  *)
neuper@55335
   508
type pbt = 
wneuper@59414
   509
  {guh : guh,         (* unique within this isac-knowledge                               *)
wneuper@59414
   510
  mathauthors : string list, (* copyright                                                *)
wneuper@59414
   511
  init : pblID,       (* to start refinement with                                        *)
neuper@55335
   512
  thy  : theory,      (* which allows to compile that pbt
wneuper@59414
   513
                        TODO: search generalized for subthy (ref.p.69*)
wneuper@59414
   514
                        (*^^^ WN050912 NOT used during application of the problem,
wneuper@59414
   515
                        because applied terms may be from 'subthy' as well as from super;
wneuper@59414
   516
                        thus we take 'maxthy'; see match_ags !                           *)
wneuper@59414
   517
  cas : term option,  (* 'CAS-command'                                                   *)
wneuper@59415
   518
  prls : Rule.rls,    (* for preds in where_                                             *)
wneuper@59414
   519
  where_ : term list, (* where - predicates                                              *)
neuper@55335
   520
  ppc : pat list,     (* this is the model-pattern; 
wneuper@59414
   521
                         it contains "#Given","#Where","#Find","#Relate"-patterns
wneuper@59414
   522
                         for constraints on identifiers see "fun cpy_nam"                *)
wneuper@59414
   523
  met : metID list}   (* methods solving the pbt                                         *)
neuper@55335
   524
wneuper@59397
   525
val e_pbt = {guh = "pbl_empty", mathauthors = [], init = e_pblID, thy = Thy_Info.get_theory "Pure",
wneuper@59416
   526
  cas = NONE, prls = Rule.Erls, where_ = [], ppc = [], met = []} : pbt
wneuper@59414
   527
fun pbt2str ({cas = cas', guh = guh', init = init', mathauthors = ma', met = met', ppc = ppc',
wneuper@59414
   528
      prls = prls', thy = thy', where_ = w'} : pbt)
wneuper@59415
   529
    = "{cas = " ^ (Rule.termopt2str cas') ^  ", guh = \"" ^ guh'  ^ "\", init = "
wneuper@59414
   530
      ^ (strs2str init') ^ ", mathauthors = " ^ (strs2str ma' |> quote) ^ ", met = "
wneuper@59414
   531
      ^ (strslist2strs met') ^ ", ppc = " ^ pats2str ppc' ^ ", prls = "
wneuper@59415
   532
      ^ (Rule.rls2str prls' |> quote) ^ ", thy = {" ^ (Rule.theory2str thy') ^ "}, where_ = "
wneuper@59415
   533
      ^ (Rule.terms2str w') ^ "}" |> linefeed;
s1210629013@55345
   534
fun pbts2str pbts = map pbt2str pbts |> list2str;
neuper@55335
   535
wneuper@59416
   536
val e_Ptyp = Ptyp ("e_pblID", [e_pbt], [])
neuper@55335
   537
type ptyps = (pbt ptyp) list
neuper@55335
   538
neuper@55357
   539
fun coll_pblguhs pbls =
wneuper@59414
   540
  let
wneuper@59414
   541
    fun node coll (Ptyp (_, [n], ns)) = [(#guh : pbt -> guh) n] @ (nodes coll ns)
wneuper@59414
   542
      | node _ _ = raise ERROR "coll_pblguhs - node"
wneuper@59414
   543
	  and nodes coll [] = coll
wneuper@59414
   544
      | nodes coll (n :: ns) = (node coll n) @ (nodes coll ns);
wneuper@59414
   545
  in nodes [] pbls end;
wneuper@59414
   546
fun check_pblguh_unique guh pbls =
wneuper@59414
   547
  if member op = (coll_pblguhs pbls) guh
wneuper@59414
   548
  then error ("check_guh_unique failed with \""^ guh ^"\";\n"^
wneuper@59414
   549
	      "use \"sort_pblguhs()\" for a list of guhs;\n"^
wneuper@59414
   550
	      "consider setting \"check_guhs_unique := false\"")
wneuper@59414
   551
  else ();
s1210629013@55339
   552
neuper@55453
   553
fun insrt _ pbt [k] [] = [Ptyp (k, [pbt], [])]
neuper@55453
   554
  | insrt d pbt [k] ((Ptyp (k', [p], ps)) :: pys) =
wneuper@59414
   555
    ((*writeln ("### insert 1: ks = " ^ strs2str [k] ^ "    k'= " ^ k');*)
wneuper@59414
   556
    if k = k'
wneuper@59414
   557
    then ((Ptyp (k', [pbt], ps)) :: pys)
wneuper@59414
   558
    else  ((Ptyp (k', [p], ps)) :: (insrt d pbt [k] pys))
wneuper@59414
   559
    )			 
wneuper@59414
   560
  | insrt d pbt (k::ks) ((Ptyp (k', [p], ps)) :: pys) =
wneuper@59414
   561
    ((*writeln ("### insert 2: ks = "^(strs2str (k::ks))^"    k'= "^k');*)
wneuper@59414
   562
    if k = k'
wneuper@59414
   563
    then ((Ptyp (k', [p], insrt d pbt ks ps)) :: pys)
wneuper@59414
   564
    else 
wneuper@59414
   565
      if length pys = 0
wneuper@59414
   566
      then error ("insert: not found " ^ (strs2str (d : pblID)))
wneuper@59414
   567
      else ((Ptyp (k', [p], ps)) :: (insrt d pbt (k :: ks) pys))
wneuper@59414
   568
    )
wneuper@59414
   569
  | insrt _ _ _ _ = raise ERROR "";
s1210629013@55339
   570
neuper@55448
   571
fun update_ptyps ID _ _ [] =
neuper@55448
   572
    error ("update_ptyps: " ^ strs2str' ID ^ " does not exist")
neuper@55448
   573
  | update_ptyps ID [i] data ((py as Ptyp (key, _, pys)) :: pyss) =
neuper@55448
   574
    if i = key
neuper@55448
   575
    then 
neuper@55448
   576
      if length pys = 0
neuper@55448
   577
      then ((Ptyp (key, [data], [])) :: pyss)
neuper@55448
   578
      else error ("update_ptyps: " ^ strs2str' ID ^ " has descendants")
neuper@55448
   579
    else py :: update_ptyps ID [i] data pyss
neuper@55448
   580
  | update_ptyps ID (i :: is) data ((py as Ptyp (key, d, pys)) :: pyss) =
neuper@55448
   581
    if i = key
neuper@55448
   582
    then ((Ptyp (key,  d, update_ptyps ID is data pys)) :: pyss)
neuper@55448
   583
    else (py :: (update_ptyps ID (i :: is) data pyss))
wneuper@59414
   584
  | update_ptyps _ _ _ _ = raise ERROR "update_ptyps called with undef pattern.";
neuper@55448
   585
s1210629013@55339
   586
(* this function only works wrt. the way Isabelle evaluates Theories and is not a general merge
s1210629013@55339
   587
  function for trees / ptyps *)
s1210629013@55339
   588
fun merge_ptyps ([], pt) = pt
s1210629013@55339
   589
  | merge_ptyps (pt, []) = pt
wneuper@59414
   590
  | merge_ptyps ((x' as Ptyp (k, _, ps)) :: xs, (xs' as Ptyp (k', y, ps') :: ys)) =
s1210629013@55346
   591
      if k = k'
s1210629013@55346
   592
      then Ptyp (k, y, merge_ptyps (ps, ps')) :: merge_ptyps (xs, ys)
s1210629013@55346
   593
      else x' :: merge_ptyps (xs, xs');
s1210629013@55372
   594
s1210629013@55372
   595
(* data for methods stored in 'methods'-database*)
s1210629013@55372
   596
type met = 
wneuper@59429
   597
     {guh        : guh,             (* unique within this isac-knowledge             *)
wneuper@59429
   598
      mathauthors: string list,     (* copyright                                     *)
wneuper@59429
   599
      init       : pblID,           (* WN060721 introduced mistakenly--TODO.REMOVE!  *)
wneuper@59429
   600
      rew_ord'   : Rule.rew_ord',   (* for rules in Detail                           
wneuper@59429
   601
			                                 TODO.WN0509 store fun itself, see 'type pbt'  *)
wneuper@59429
   602
      erls       : Rule.rls,        (* the eval_rls for cond. in rules FIXME "rls'   
wneuper@59429
   603
				                               instead erls in "fun prep_met"                *)
wneuper@59429
   604
      srls       : Rule.rls,        (* for evaluating list expressions in scr        *)
wneuper@59429
   605
      prls       : Rule.rls,        (* for evaluating predicates in modelpattern     *)
wneuper@59429
   606
      crls       : Rule.rls,        (* for check_elementwise, ie. formulae in calc.  *)
wneuper@59429
   607
      nrls       : Rule.rls,        (* canonical simplifier specific for this met    *)
wneuper@59429
   608
      errpats    : Rule.errpat list,(* error patterns expected in this method        *)
wneuper@59429
   609
      calc       : Rule.calc list,  (* Theory_Data in fun prep_met                   *)
wneuper@59429
   610
      (*branch   : TransitiveB set in append_problem at generation ob pblobj         *)
wneuper@59429
   611
      ppc        : pat list,        (* items in given, find, relate;
s1210629013@55372
   612
	      items (in "#Find") which need not occur in the arg-list of a SubProblem
s1210629013@55372
   613
        are 'copy-named' with an identifier "*'.'".
s1210629013@55372
   614
        copy-named items are 'generating' if they are NOT "*'''" ?WN120516??
wneuper@59429
   615
        see ME/calchead.sml 'fun is_copy_named'.                                     *)
wneuper@59429
   616
      pre        : term list,       (* preconditions in where                        *)
wneuper@59550
   617
      scr        : Rule.scr         (* progam, empty as @{thm refl} or Rfuns         *)
s1210629013@55372
   618
	   };
wneuper@59414
   619
val e_met = {guh = "met_empty", mathauthors = [], init = e_metID, rew_ord' = "e_rew_ord'",
wneuper@59415
   620
	erls = Rule.e_rls, srls = Rule.e_rls, prls = Rule.e_rls, calc = [], crls = Rule.e_rls,
wneuper@59415
   621
	errpats = [], nrls = Rule.e_rls, ppc = [], pre = [], scr = Rule.EmptyScr};
wneuper@59414
   622
val e_Mets = Ptyp ("e_metID", [e_met],[]);
s1210629013@55372
   623
s1210629013@55372
   624
type mets = (met ptyp) list;
s1210629013@55373
   625
fun coll_metguhs mets =
wneuper@59414
   626
  let
wneuper@59414
   627
    fun node coll (Ptyp (_, [n], ns)) = [(#guh : met -> guh) n] @ (nodes coll ns)
wneuper@59414
   628
      | node _ _ = raise ERROR "coll_pblguhs - node"
wneuper@59414
   629
  	and nodes coll [] = coll
wneuper@59414
   630
  	  | nodes coll (n :: ns) = (node coll n) @ (nodes coll ns);
s1210629013@55373
   631
    in nodes [] mets end;
s1210629013@55373
   632
fun check_metguh_unique (guh:guh) (mets: (met ptyp) list) =
s1210629013@55373
   633
    if member op = (coll_metguhs mets) guh
wneuper@59414
   634
    then raise ERROR ("check_guh_unique failed with \"" ^ guh ^"\";\n"^
wneuper@59473
   635
		(*"use \"sort_metguhs()\" for a list of guhs;\n" ^        ...evaluates to [] ?!?*)
wneuper@59414
   636
		  "consider setting \"check_guhs_unique := false\"")
s1210629013@55373
   637
    else ();
s1210629013@55373
   638
neuper@55450
   639
fun Html_default exist = (Html {guh = theID2guh exist, 
neuper@55450
   640
  coursedesign = ["isac team 2006"], mathauthors = [], html = ""})
neuper@55450
   641
wneuper@59414
   642
fun fill_parents (_, [i]) thydata = Ptyp (i, [thydata], [])
neuper@55450
   643
  | fill_parents (exist, i :: is) thydata =
neuper@55450
   644
    Ptyp (i, [Html_default (exist @ [i])], [fill_parents (exist @ [i], is) thydata])
wneuper@59414
   645
  | fill_parents _ _ = raise ERROR "Html_default: avoid ML warning: Matches are not exhaustive"
neuper@55450
   646
neuper@55450
   647
fun add_thydata (exist, is) thydata [] = [fill_parents (exist, is) thydata]
neuper@55450
   648
  | add_thydata (exist, [i]) data (pys as (py as Ptyp (key, _, _)) :: pyss) = 
neuper@55450
   649
    if i = key
neuper@55450
   650
    then pys (* preserve existing thydata *) 
neuper@55450
   651
    else py :: add_thydata (exist, [i]) data pyss
neuper@55450
   652
  | add_thydata (exist, iss as (i :: is)) data ((py as Ptyp (key, d, pys)) :: pyss) = 
neuper@55450
   653
    if i = key
neuper@55450
   654
    then       
neuper@55450
   655
      if length pys = 0
neuper@55450
   656
      then Ptyp (key, d, [fill_parents (exist @ [i], is) data]) :: pyss
neuper@55450
   657
      else Ptyp (key, d, add_thydata (exist @ [i], is) data pys) :: pyss
neuper@55450
   658
    else py :: add_thydata (exist, iss) data pyss
wneuper@59414
   659
  | add_thydata _ _ _ = raise ERROR "add_thydata: avoid ML warning: Matches are not exhaustive"
neuper@55448
   660
neuper@55448
   661
fun update_hthm (Hthm {guh, coursedesign, mathauthors, thm, ...}) fillpats' =
neuper@55448
   662
  Hthm {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
neuper@55448
   663
    fillpats = fillpats', thm = thm}
wneuper@59414
   664
  | update_hthm _ _ = raise ERROR "update_hthm: wrong arguments";
neuper@55448
   665
wneuper@59414
   666
(* for dialog-authoring *)
neuper@55448
   667
fun update_hrls (Hrls {guh, coursedesign, mathauthors, thy_rls = (thyID, rls)}) errpatIDs =
wneuper@59414
   668
    let
wneuper@59414
   669
      val rls' = 
wneuper@59414
   670
        case rls of
wneuper@59415
   671
          Rule.Rls {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
wneuper@59415
   672
          => Rule.Rls {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
wneuper@59414
   673
               calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
wneuper@59415
   674
        | Rule.Seq {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
wneuper@59415
   675
          => Rule.Seq {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
wneuper@59414
   676
               calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
wneuper@59415
   677
        | Rule.Rrls {id, prepat, rew_ord, erls, calc, scr, ...}
wneuper@59415
   678
          => Rule.Rrls {id = id, prepat = prepat, rew_ord = rew_ord, erls = erls, calc = calc,
wneuper@59414
   679
               scr = scr, errpatts = errpatIDs}
wneuper@59414
   680
        | Erls => Erls
wneuper@59414
   681
    in
wneuper@59414
   682
      Hrls {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
wneuper@59414
   683
        thy_rls = (thyID, rls')}
wneuper@59414
   684
    end
wneuper@59414
   685
  | update_hrls _ _ = raise ERROR "update_hrls: wrong arguments";
wneuper@59414
   686
wneuper@59414
   687
fun app_py p f (d:pblID) (k(*:pblRD*)) =
neuper@55448
   688
  let
wneuper@59414
   689
    fun py_err _ = raise ERROR ("app_py: not found: " ^ strs2str d);
neuper@55448
   690
    fun app_py' _ [] = py_err ()
neuper@55448
   691
      | app_py' [] _ = py_err ()
wneuper@59414
   692
      | app_py' [k0] ((p' as Ptyp (k', _, _  )) :: ps) =
wneuper@59414
   693
        if k0 = k' then f p' else app_py' [k0] ps
wneuper@59414
   694
      | app_py' (k' as (k0 :: ks)) (Ptyp (k'', _, ps) :: ps') =
wneuper@59414
   695
        if k0 = k'' then app_py' ks ps else app_py' k' ps';
neuper@55448
   696
  in app_py' k p end;
wneuper@59414
   697
fun get_py p =
wneuper@59414
   698
  let
wneuper@59414
   699
    fun extract_py (Ptyp (_, [py], _)) = py
wneuper@59414
   700
      | extract_py _ = raise ERROR ("extract_py: Ptyp has wrong format.");
wneuper@59414
   701
  in app_py p extract_py end;
neuper@55448
   702
neuper@55448
   703
fun (*KEStore_Elems.*)insert_fillpats th fis = (* for tests bypassing setup KEStore_Elems *)
neuper@55448
   704
  let
neuper@55448
   705
    fun update_elem th (theID, fillpats) =
neuper@55437
   706
      let
neuper@55448
   707
        val hthm = get_py th theID theID
neuper@55448
   708
        val hthm' = update_hthm hthm fillpats
neuper@55448
   709
          handle ERROR _ => error ("insert_fillpats: " ^ strs2str theID ^ "must address a theorem")
neuper@55448
   710
      in update_ptyps theID theID hthm' end
neuper@55448
   711
  in fold (update_elem th) fis end
neuper@55456
   712
neuper@55456
   713
(* group the theories defined in Isac, compare Build_Thydata:
neuper@55456
   714
  section "Get and group the theories defined in Isac" *) 
wneuper@59397
   715
fun isabthys () = (*["Complex_Main", "Taylor", .., "Pure"]*)
neuper@55456
   716
  let
wneuper@59415
   717
    val allthys = Theory.ancestors_of (Rule.Thy_Info_get_theory "Build_Thydata")
neuper@55456
   718
  in
wneuper@59366
   719
    drop ((find_index (curry Context.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), allthys)
neuper@55456
   720
  end
neuper@55456
   721
fun knowthys () = (*["Isac", .., "Descript", "Delete"]*)
neuper@55456
   722
  let
neuper@55456
   723
    fun isacthys () = (* ["Isac", .., "KEStore"] without Build_Isac thys: "Interpret" etc *)
neuper@55456
   724
      let
wneuper@59332
   725
        val allthys = filter_out (member Context.eq_thy
wneuper@59415
   726
          [(*Thy_Info_get_theory "ProgLang",*) Rule.Thy_Info_get_theory "Interpret", 
wneuper@59415
   727
            Rule.Thy_Info_get_theory "xmlsrc", Rule.Thy_Info_get_theory "Frontend"]) 
wneuper@59415
   728
          (Theory.ancestors_of (Rule.Thy_Info_get_theory "Build_Thydata"))
neuper@55456
   729
      in
wneuper@59366
   730
        take ((find_index (curry Context.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), 
neuper@55456
   731
        allthys)
neuper@55456
   732
      end
neuper@55456
   733
    val isacthys' = isacthys ()
wneuper@59415
   734
    val proglang_parent = Rule.Thy_Info_get_theory "ProgLang"
neuper@55456
   735
  in
wneuper@59332
   736
    take ((find_index (curry Context.eq_thy proglang_parent) isacthys'), isacthys')
neuper@55456
   737
  end
wneuper@59414
   738
neuper@55456
   739
fun progthys () = (*["Isac", .., "Descript", "Delete"]*)
neuper@55456
   740
  let
neuper@55456
   741
    fun isacthys () = (* ["Isac", .., "KEStore"] without Build_Isac thys: "Interpret" etc *)
wneuper@59415
   742
      let                                                        
wneuper@59332
   743
        val allthys = filter_out (member Context.eq_thy
wneuper@59415
   744
          [(*Thy_Info_get_theory "ProgLang",*) Rule.Thy_Info_get_theory "Interpret", 
wneuper@59415
   745
            Rule.Thy_Info_get_theory "xmlsrc", Rule.Thy_Info_get_theory "Frontend"]) 
wneuper@59415
   746
          (Theory.ancestors_of (Rule.Thy_Info_get_theory "Build_Thydata"))
neuper@55456
   747
      in
wneuper@59366
   748
        take ((find_index (curry Context.eq_thy (Thy_Info.get_theory "Complex_Main")) allthys), 
neuper@55456
   749
        allthys)
neuper@55456
   750
      end
neuper@55456
   751
    val isacthys' = isacthys ()
wneuper@59415
   752
    val proglang_parent = Rule.Thy_Info_get_theory "ProgLang"
neuper@55456
   753
  in
wneuper@59332
   754
    drop ((find_index (curry Context.eq_thy proglang_parent) isacthys') + 1(*ProgLang*), isacthys')
neuper@55456
   755
  end
neuper@55456
   756
neuper@55456
   757
fun partID thy = 
wneuper@59332
   758
  if member Context.eq_thy (knowthys ()) thy then "IsacKnowledge"
wneuper@59332
   759
  else if member Context.eq_thy (progthys ()) thy then "IsacScripts"
wneuper@59332
   760
  else if member Context.eq_thy (isabthys ()) thy then "Isabelle"
wneuper@59415
   761
  else error ("closure of thys in Isac is broken by " ^ Rule.string_of_thy thy)
wneuper@59415
   762
fun partID' thy' = partID (Rule.Thy_Info_get_theory thy')
wneuper@59405
   763
neuper@38061
   764
end (*struct*)
wneuper@59405
   765