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