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