src/Tools/isac/BridgeLibisabelle/thy-hierarchy.sml
author Walther Neuper <walther.neuper@jku.at>
Sun, 19 Apr 2020 12:22:37 +0200
changeset 59887 4616b145b1cd
parent 59886 106e7d8723ca
child 59890 ba0757da0dc8
permissions -rw-r--r--
rename KEStore to Know_Store, replace respect.part of Celem with Celem1

note: the latter was NOT restricted to Know_Store, because redefinition in calcelems.sml
(i.e. "datatype 'a ptyp = Celem1.Ptyp of ... Celem.ptyp") did not work.
wneuper@59600
     1
(* title: "/thy-hierarchy.sml"
wneuper@59549
     2
     export theory-data "thehier" to xml
neuper@37906
     3
   author: Walther Neuper 0601
neuper@37906
     4
   (c) isac-team
neuper@55490
     5
*)
neuper@37906
     6
walther@59874
     7
signature THEORY_HIERARCHY =
walther@59874
     8
sig
walther@59879
     9
  val collect_cals: string * ThyC.id -> (Celem.theID * Celem.thydata) list
walther@59874
    10
  val collect_isab: string -> string * thm -> Celem.theID * Celem.thydata
walther@59879
    11
  val collect_ords: 'a * ThyC.id -> (Celem.theID * Celem.thydata) list
walther@59874
    12
  val collect_part: string -> theory -> theory list -> (Celem.theID * Celem.thydata) list
walther@59879
    13
  val collect_rlss: string -> (Rule_Set.id * (ThyC.id * Rule_Set.T)) list -> theory list ->
walther@59874
    14
    (Celem.theID * Celem.thydata) list
walther@59874
    15
  val collect_thms: string -> theory -> (Celem.theID * Celem.thydata) list
walther@59874
    16
walther@59887
    17
  val hierarchy_guh: 'a Celem1.ptyp list -> string
walther@59874
    18
  val insert_errpatIDs: 'a -> Celem.theID -> Error_Fill_Def.errpatID list ->
walther@59874
    19
    Celem.thydata * Celem.theID
walther@59874
    20
walther@59879
    21
  val makeHcal: string * ThyC.id -> string * Rule_Def.calc -> Celem.theID * Celem.thydata
walther@59879
    22
  val makeHord: string * ThyC.id -> string * ((term * term) list -> term * term -> bool) ->
walther@59874
    23
    Celem.theID * Celem.thydata
walther@59879
    24
  val makeHrls: string -> Rule_Set.id * (ThyC.id * Rule_Def.rule_set) ->
walther@59874
    25
    Celem.theID * Celem.thydata
walther@59879
    26
  val makeHthm: string * ThyC.id -> thm -> Celem.theID * Celem.thydata
walther@59874
    27
  val make_cal: theory -> Rule_Def.calc -> Celem.authors -> Celem.thydata * Celem.theID
walther@59879
    28
  val make_isa: theory -> ThyC.id * ThyC.id -> Celem.authors ->
walther@59874
    29
    Celem.thydata * Celem.theID
walther@59874
    30
  val make_ord: theory -> ((term * term) list -> term * term -> bool) -> Celem.authors ->
walther@59874
    31
    Celem.thydata * Celem.theID
walther@59874
    32
  val make_rls: theory -> Rule_Def.rule_set -> Celem.authors -> Celem.thydata * Celem.theID
walther@59874
    33
  val make_thm: theory -> string -> string * thm -> Celem.authors -> Celem.thydata * Celem.theID
walther@59874
    34
  val make_thy: theory -> Celem.authors -> Celem.thydata * Celem.theID
walther@59874
    35
walther@59874
    36
  val show_thes: unit -> unit
walther@59874
    37
walther@59874
    38
  val thes2file: Celem.filepath -> unit
walther@59874
    39
  val thms_of: theory -> thm list
walther@59879
    40
  val thms_of_rlss: theory -> (Rule_Set.id * (ThyC.id * Rule_Def.rule_set)) list ->
walther@59874
    41
    (string * thm) list
walther@59874
    42
  val thy_hierarchy2file: Celem.filepath -> unit
walther@59874
    43
  val thydata2file: Celem.filepath -> Pos.pos -> Celem.theID -> Celem.thydata -> unit
walther@59874
    44
  val thydata2xml: Celem.theID * Celem.thydata -> Celem.xml
walther@59874
    45
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59874
    46
  (*NONE*)
walther@59886
    47
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59887
    48
  val thenode: Celem.filepath -> string list -> Pos.pos -> (Celem.filepath -> Pos.pos ->
walther@59887
    49
    string list -> 'a -> 'b) -> 'a Celem1.ptyp -> unit
walther@59887
    50
  val thenodes: Celem.filepath -> string list -> Pos.pos -> (Celem.filepath -> Pos.pos ->
walther@59887
    51
    string list -> 'a -> 'b) -> 'a Celem1.ptyp list -> unit
walther@59886
    52
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59874
    53
end
walther@59874
    54
walther@59874
    55
(**)
walther@59874
    56
structure Thy_Hierarchy(**): THEORY_HIERARCHY(**) =
walther@59874
    57
struct
walther@59874
    58
(**)
wneuper@59507
    59
open TermC (* allows contains_one_of to be infix *)
wneuper@59507
    60
wneuper@59203
    61
(* copy from mutabelle_extra.ML, fun thms_of *)
wneuper@59507
    62
fun thms_of thy = (* das ist zu verbessern *)
wneuper@59507
    63
  let
wneuper@59507
    64
    val fun_ids = Specify.get_fun_ids thy
wneuper@59507
    65
  in
wneuper@59507
    66
    filter (fn thm => Context.theory_name (Thm.theory_of_thm thm) = Context.theory_name thy
wneuper@59549
    67
        andalso not (thm contains_one_of fun_ids)
walther@59876
    68
        andalso not (ThmC.id_of_thm thm = "mono"))
wneuper@59549
    69
        (* created in Biegelinie by partial_function ^^^^^^*)
wneuper@59507
    70
      (map snd (Global_Theory.all_thms_of thy false))
wneuper@59507
    71
  end
wneuper@59203
    72
neuper@55490
    73
(* wrap theory-data into the uniform type thydata *)
neuper@37906
    74
walther@59879
    75
fun makeHthm (part : string, thyID : ThyC.id) (thm : thm) =
walther@59876
    76
    let val theID = [part, thyID, "Theorems"] @ [ThmC.id_of_thm thm] : Celem.theID
wneuper@59405
    77
    in (theID, Celem.Hthm {guh = Celem.theID2guh theID, coursedesign = [], 
neuper@42429
    78
		     mathauthors = ["isac-team"], fillpats = [], thm = thm})
neuper@37906
    79
    end;
walther@59879
    80
fun makeHrls (part : string) (rls' : Rule_Set.id, thy_rls as (thyID, rls): ThyC.id * Rule_Set.T) =
wneuper@59405
    81
    let val theID = [part, thyID,"Rulesets"] @ [rls'] : Celem.theID
wneuper@59405
    82
    in (theID, Celem.Hrls {guh = Celem.theID2guh theID, coursedesign = [], 
neuper@55490
    83
		     mathauthors = ["isac-team"], thy_rls = thy_rls})
neuper@37906
    84
    end;
walther@59879
    85
fun makeHcal (part : string, thyID : ThyC.id) (calID, cal) =
wneuper@59405
    86
    let val theID = [part, thyID,"Operations"] @ [calID] : Celem.theID
wneuper@59405
    87
    in (theID, Celem.Hcal {guh = Celem.theID2guh theID, coursedesign=[], 
neuper@55490
    88
		     mathauthors = ["isac-team"], calc = cal})
neuper@37906
    89
    end;
walther@59879
    90
fun makeHord (part : string, thyID : ThyC.id) (ordID, ord) =
wneuper@59405
    91
    let val theID = [part, thyID,"TODO-Orders"] @ [ordID] : Celem.theID
wneuper@59405
    92
    in (theID, Celem.Hord  {guh = Celem.theID2guh theID, coursedesign=[], 
neuper@55490
    93
		      mathauthors = ["isac-team"], ord = ord})
neuper@37906
    94
    end;
neuper@37906
    95
neuper@55490
    96
(* get all theorems from the list of rule-sets (defined in Knowledge) *)
walther@59879
    97
fun thms_of_rlss thy rlss = (rlss : (Rule_Set.id * (ThyC.id * Rule_Set.T)) list)
wneuper@59263
    98
  |> map (Rtools.thms_of_rls o #2 o #2)
neuper@55405
    99
  |> flat
walther@59876
   100
  |> map (ThmC.revert_sym_rule thy)
wneuper@59416
   101
  |> map (fn Rule.Thm (thmID, thm) => (thmID, thm))
neuper@55484
   102
  |> gen_distinct (fn ((thmID1, _), (thmID2, _)) => thmID1 = thmID2) 
walther@59874
   103
  : ThmC.T list                              
neuper@55490
   104
neuper@55490
   105
(* collect all thydata defined in in a theory *)
neuper@55405
   106
neuper@55448
   107
fun collect_thms part thy =
wneuper@59203
   108
  map (makeHthm (part, Context.theory_name thy)) (rev (thms_of thy))
walther@59879
   109
fun collect_rlss part rlss thys = (rlss : (Rule_Set.id * (ThyC.id * Rule_Set.T)) list)
neuper@55405
   110
  |> filter (fn (_, (thyID, _)) => member (op=) (map Context.theory_name thys) thyID)
neuper@55405
   111
  |> map (makeHrls part)
neuper@37906
   112
fun collect_cals (part, thy') =
neuper@55490
   113
  let val cals = [] (*FIXXXXXXXXXXME.WN060713 add thyID: (thyID, cal)*)
neuper@55490
   114
  in map (makeHcal (part, thy')) cals end;
neuper@37906
   115
fun collect_ords (part, thy') =
walther@59881
   116
    let val thy = ThyC.get_theory thy'
walther@59878
   117
    in [(*TODO.WN060120 rew_ord, Eval*)]:(Celem.theID * Celem.thydata) list end;
neuper@37906
   118
walther@59887
   119
(* parts are: Isabelle | IsacKnowledge | IsacScripts, see Know_Store.thy *)
neuper@55405
   120
fun collect_part part parent thys =
neuper@55448
   121
  (flat (map (collect_thms part) thys)) @
neuper@55448
   122
  (collect_rlss part (KEStore_Elems.get_rlss parent) thys) @ 
neuper@55405
   123
  [(*TODO: collect_cals, collect_ords*)]
neuper@37906
   124
neuper@55490
   125
(* collect theorems defined in Isabelle *)
neuper@42400
   126
fun collect_isab isa (thmDeriv, thm) =
neuper@55490
   127
  let val theID =
walther@59879
   128
    [isa, ThyC.cut_id thmDeriv, "Theorems", ThmC.cut_id thmDeriv]
neuper@55490
   129
  in 
wneuper@59405
   130
    (theID: Celem.theID, Celem.Hthm {guh = Celem.theID2guh theID, 
neuper@55490
   131
      mathauthors = ["Isabelle team, TU Munich"],
neuper@55490
   132
      coursedesign = [],
neuper@55490
   133
      fillpats = [],
neuper@55490
   134
      thm = thm}) 
neuper@55490
   135
  end
neuper@37906
   136
wneuper@59269
   137
fun show_thes () = (writeln o Specify.format_pblIDl o (Specify.scan [])) (get_thes ());
neuper@37906
   138
neuper@37906
   139
neuper@55490
   140
(** create an xml representation for thehier: hierarchy of entries + files per entry **)
neuper@37906
   141
neuper@55490
   142
(* create an xml-hierarchy where the filname is created from the guh; 
neuper@55490
   143
  ad DTD: a NODE contains an ID and zero or more NODEs*)
neuper@37906
   144
fun hierarchy_guh h =
neuper@42407
   145
  let
neuper@42407
   146
    val i = indentation
neuper@42407
   147
    val j = indentation
walther@59887
   148
    fun node i p theID (Celem1.Ptyp (id, _, ns)) = 
neuper@55490
   149
        let
walther@59757
   150
          val p' = Pos.lev_on p
neuper@55490
   151
          val theID' = theID @ [id]
neuper@55490
   152
        in
neuper@55490
   153
        (indt i) ^ "<NODE>\n" ^ 
neuper@55490
   154
        (indt (i+j)) ^ "<ID> " ^ id ^ " </ID>\n" ^ 
neuper@55490
   155
        (indt (i+j)) ^ "<NO> " (*on this level*) ^ (string_of_int o last_elem) p' ^ " </NO>\n" ^ 
wneuper@59405
   156
        (indt (i+j)) ^ "<CONTENTREF> " ^ Celem.theID2guh theID' ^ " </CONTENTREF>\n" ^
walther@59757
   157
        (nodes (i+j) (Pos.lev_dn p') theID' ns) ^ 
neuper@55490
   158
        (indt i) ^ "</NODE>\n"
neuper@55490
   159
        end
neuper@55490
   160
    and nodes _ _ _ [] = ""
walther@59757
   161
      | nodes i p theID (n :: ns) = (node i p theID n) ^ (nodes i (Pos.lev_on p) theID ns);
neuper@42407
   162
  in nodes j [0] [] h end;
neuper@37906
   163
walther@59687
   164
fun thy_hierarchy2file (path: Celem.filepath) = 
neuper@55490
   165
  str2file (path ^ "thy_hierarchy.xml") 
neuper@55490
   166
    ("<NODE>\n" ^
neuper@55490
   167
    "  <ID> theory hierarchy </ID>\n" ^
neuper@55490
   168
    "  <NO> 1 </NO>\n" ^
neuper@55490
   169
    "  <CONTENTREF> thy_ROOT </CONTENTREF>\n" ^
neuper@55490
   170
    (hierarchy_guh (get_thes ())) ^
neuper@55490
   171
    "</NODE>");
neuper@37906
   172
neuper@55490
   173
(* create the xml-files for the thydata in the hierarchy *)
neuper@37906
   174
val i = indentation;
neuper@42407
   175
(* analoguous to 'fun met2xml' *)
wneuper@59405
   176
fun thydata2xml (theID: Celem.theID, Celem.Html {guh, coursedesign, mathauthors, html}) =
neuper@42407
   177
      "<HTMLDATA>\n" ^
neuper@42407
   178
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   179
      id2xml i theID ^
neuper@42407
   180
      indt i ^ "<EXPLANATIONS> " ^ html ^ "</EXPLANATIONS>\n" ^
neuper@42407
   181
      authors2xml i "MATHAUTHORS" mathauthors ^
neuper@42407
   182
      authors2xml i "COURSEDESIGNS" coursedesign ^
wneuper@59405
   183
      "</HTMLDATA>\n" : Celem.xml
wneuper@59405
   184
  | thydata2xml (theID, Celem.Hthm {guh, coursedesign, mathauthors, fillpats(*TODO?*), thm}) =
neuper@42407
   185
      "<THEOREMDATA>\n" ^
neuper@42407
   186
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   187
      id2xml i theID ^
neuper@55489
   188
      thm''2xml i thm ^
neuper@42407
   189
      indt i ^ "<PROOF>\n" ^
neuper@55490
   190
      extref2xml (i+i) "Proof of the theorem" (* TODO "Unsorted"vvv: distinguish Isabelle | Isac *)
neuper@55495
   191
	      ("http://www.ist.tugraz.at/projects/isac/www/kbase/thy/browser_info/HOL/HOL-Real/Isac/" ^
neuper@55490
   192
	        nth 2 theID ^ ".html") ^
neuper@42407
   193
	    indt i ^  "</PROOF>\n" ^
neuper@42407
   194
	    indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
neuper@42407
   195
	    authors2xml i "MATHAUTHORS" mathauthors ^
neuper@42407
   196
	    authors2xml i "COURSEDESIGNS" coursedesign ^
neuper@42407
   197
	    "</THEOREMDATA>\n"
wneuper@59405
   198
  | thydata2xml (theID, Celem.Hrls {guh, coursedesign, mathauthors, thy_rls}) =
neuper@42407
   199
      "<RULESETDATA>\n" ^
neuper@42407
   200
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   201
      id2xml i theID ^
neuper@55492
   202
      rls2xml i thy_rls ^
neuper@42407
   203
      indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
neuper@42407
   204
      authors2xml i "MATHAUTHORS" mathauthors ^
neuper@37906
   205
    authors2xml i "COURSEDESIGNS" coursedesign ^
neuper@42407
   206
      "</RULESETDATA>\n"
wneuper@59405
   207
  | thydata2xml (theID, Celem.Hcal {guh, coursedesign, mathauthors, calc}) =
neuper@42407
   208
      "<RULESETDATA>\n" ^
neuper@42407
   209
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   210
      id2xml i theID ^
wneuper@59405
   211
      calc2xml i (Celem.theID2thyID theID, calc) ^
neuper@42407
   212
      indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
neuper@42407
   213
      authors2xml i "MATHAUTHORS" mathauthors ^
neuper@42407
   214
      authors2xml i "COURSEDESIGNS" coursedesign ^
neuper@42407
   215
      "</RULESETDATA>\n"
neuper@37906
   216
  | thydata2xml (theID, _) =
neuper@42407
   217
      error ("thydata2xml: not implemented for "^ strs2str' theID);
neuper@37906
   218
neuper@42407
   219
(* analoguous to 'fun met2file' *)
walther@59846
   220
fun thydata2file (path : Celem.filepath) (pos : Pos.pos) (theID : Celem.theID) thydata =
neuper@42407
   221
  (writeln ("### thes2file: id = " ^ strs2str theID);
wneuper@59263
   222
    str2file (path ^ Rtools.theID2filename theID) (thydata2xml (theID, thydata)));
neuper@37906
   223
neuper@42407
   224
(* analoguous to 'fun node' *)
walther@59887
   225
fun thenode (pa : Celem.filepath) ids po wfn (Celem1.Ptyp (id, [n], ns)) = 
walther@59757
   226
    let val po' = Pos.lev_on po
walther@59757
   227
    in wfn pa po' (ids @ [id]) n; thenodes pa (ids @ [id]) (Pos.lev_dn po') wfn ns end
neuper@37906
   228
and thenodes _ _ _ _ [] = ()
neuper@42407
   229
  | thenodes pa ids po wfn (n::ns) =
walther@59757
   230
      (thenode pa ids po wfn n; thenodes pa ids (Pos.lev_on po) wfn ns);
neuper@37906
   231
neuper@42407
   232
(* analoguous to 'fun mets2file' *)
walther@59687
   233
fun thes2file (p : Celem.filepath) = thenodes p [] [0] thydata2file (get_thes ());
neuper@37906
   234
neuper@37906
   235
neuper@37906
   236
(***.store a single theory element in the hierarchy.***)
neuper@37906
   237
neuper@37906
   238
(*.for mathauthors only, other html is added to xml exported from here.*)
wneuper@59405
   239
fun make_isa thy (part, thypart) (mathauthors : Celem.authors) =
neuper@55490
   240
  let 
walther@59880
   241
    val theID = [part, Context.theory_name thy, thypart]
neuper@55490
   242
    val guh = case theID of
wneuper@59405
   243
        [part] => Celem.part2guh theID
wneuper@59405
   244
      | [part, thyID, thypart] => Celem.thypart2guh theID
wneuper@59263
   245
    val theID = Rtools.guh2theID guh
wneuper@59405
   246
    val the = Celem.Html {guh = guh, coursedesign = [], mathauthors = mathauthors, html = ""}
neuper@55420
   247
  in (the, theID) end
neuper@55420
   248
wneuper@59405
   249
fun make_thy thy (mathauthors : Celem.authors) =
neuper@55490
   250
  let 
walther@59880
   251
    val guh = Celem.thy2guh ["IsacKnowledge", Context.theory_name thy]
wneuper@59263
   252
    val theID = Rtools.guh2theID guh
wneuper@59405
   253
    val the = Celem.Html {guh = guh, coursedesign = [], mathauthors = mathauthors, html = ""}
neuper@55420
   254
  in (the, theID) end
neuper@55420
   255
walther@59874
   256
fun make_thm thy part (thmID : ThmC.id, thm) (mathauthors : Celem.authors) =
neuper@42411
   257
  let
walther@59880
   258
    val guh = Celem.thm2guh (part, Context.theory_name thy) thmID
wneuper@59263
   259
    val theID = Rtools.guh2theID guh
wneuper@59405
   260
    val the = Celem.Hthm {guh = guh, coursedesign = [(*inserted in xml after export*)], 
neuper@55490
   261
			mathauthors = mathauthors, fillpats = [], thm = thm}
neuper@55420
   262
  in (the, theID) end
neuper@37906
   263
wneuper@59405
   264
fun make_rls thy rls (mathauthors : Celem.authors) =
neuper@48887
   265
  let 
walther@59880
   266
    val guh = Celem.rls2guh ("IsacKnowledge", Context.theory_name thy) ((#id o Rule_Set.rep) rls)
wneuper@59263
   267
    val theID = Rtools.guh2theID guh
wneuper@59405
   268
    val the = Celem.Hrls {guh = guh, coursedesign = [], mathauthors = mathauthors,
walther@59880
   269
			thy_rls = (Context.theory_name thy, rls)}
neuper@48887
   270
	  (*needs no (!check_guhs_unique) because guh is generated automatically*)
neuper@55420
   271
  in (the, theID) end
neuper@37906
   272
wneuper@59405
   273
fun make_cal thy cal (mathauthors : Celem.authors) =
neuper@55490
   274
  let
walther@59880
   275
    val guh = Celem.cal2guh ("IsacKnowledge", Context.theory_name thy) ("TODO store_cal")
wneuper@59263
   276
    val theID = Rtools.guh2theID guh
wneuper@59405
   277
    val the = Celem.Hcal {guh = guh, coursedesign = [], mathauthors = mathauthors, calc = cal}
neuper@55420
   278
  in (the, theID) end
neuper@37906
   279
wneuper@59405
   280
fun make_ord thy ord (mathauthors : Celem.authors) =
neuper@55490
   281
  let 
walther@59880
   282
    val guh = Celem.ord2guh ("IsacKnowledge", Context.theory_name thy) ("TODO store_ord")
wneuper@59263
   283
    val theID = Rtools.guh2theID guh
wneuper@59405
   284
    val the = Celem.Hord {guh = guh, coursedesign = [], mathauthors = mathauthors, ord = ord}
neuper@55420
   285
  in (the, theID) end
neuper@55420
   286
neuper@55463
   287
fun insert_errpatIDs thy theID errpatIDs = (* TODO: redo like insert_fillpatts *)
neuper@42452
   288
  let
wneuper@59269
   289
    val hrls = Specify.get_the theID
wneuper@59405
   290
    val hrls' = Celem.update_hrls hrls errpatIDs
neuper@42452
   291
      handle ERROR _ => error ("insert_errpatIDs: " ^ strs2str theID ^ "must address a rule-set")
neuper@55420
   292
  in (hrls', theID) end
neuper@55420
   293
walther@59874
   294
(**)end(**)
walther@59874
   295
open Thy_Hierarchy