src/Tools/isac/xmlsrc/thy-hierarchy.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 15 Mar 2018 10:17:44 +0100
changeset 59405 49d7d410b83c
parent 59380 8b08d9296654
child 59416 229e5c9cf78b
permissions -rw-r--r--
separate structure Celem: CALC_ELEMENT, all but Knowledge/
neuper@55490
     1
(* export theory-data "thehier" to xml
neuper@37906
     2
   author: Walther Neuper 0601
neuper@37906
     3
   (c) isac-team
neuper@55490
     4
*)
neuper@37906
     5
wneuper@59203
     6
(* copy from mutabelle_extra.ML, fun thms_of *)
wneuper@59203
     7
fun thms_of thy =
wneuper@59203
     8
  filter (fn th => Context.theory_name (Thm.theory_of_thm th) = Context.theory_name thy)
wneuper@59203
     9
    (map snd (Global_Theory.all_thms_of thy false))
wneuper@59203
    10
neuper@55490
    11
(* wrap theory-data into the uniform type thydata *)
neuper@37906
    12
wneuper@59405
    13
fun makeHthm (part : string, thyID : Celem.thyID) (thm : thm) =
wneuper@59405
    14
    let val theID = [part, thyID, "Theorems"] @ [Celem.thmID_of_derivation_name' thm] : Celem.theID
wneuper@59405
    15
    in (theID, Celem.Hthm {guh = Celem.theID2guh theID, coursedesign = [], 
neuper@42429
    16
		     mathauthors = ["isac-team"], fillpats = [], thm = thm})
neuper@37906
    17
    end;
wneuper@59405
    18
fun makeHrls (part : string) (rls' : Celem.rls', thy_rls as (thyID, rls): Celem.thyID * Celem.rls) =
wneuper@59405
    19
    let val theID = [part, thyID,"Rulesets"] @ [rls'] : Celem.theID
wneuper@59405
    20
    in (theID, Celem.Hrls {guh = Celem.theID2guh theID, coursedesign = [], 
neuper@55490
    21
		     mathauthors = ["isac-team"], thy_rls = thy_rls})
neuper@37906
    22
    end;
wneuper@59405
    23
fun makeHcal (part : string, thyID : Celem.thyID) (calID, cal) =
wneuper@59405
    24
    let val theID = [part, thyID,"Operations"] @ [calID] : Celem.theID
wneuper@59405
    25
    in (theID, Celem.Hcal {guh = Celem.theID2guh theID, coursedesign=[], 
neuper@55490
    26
		     mathauthors = ["isac-team"], calc = cal})
neuper@37906
    27
    end;
wneuper@59405
    28
fun makeHord (part : string, thyID : Celem.thyID) (ordID, ord) =
wneuper@59405
    29
    let val theID = [part, thyID,"TODO-Orders"] @ [ordID] : Celem.theID
wneuper@59405
    30
    in (theID, Celem.Hord  {guh = Celem.theID2guh theID, coursedesign=[], 
neuper@55490
    31
		      mathauthors = ["isac-team"], ord = ord})
neuper@37906
    32
    end;
neuper@37906
    33
wneuper@59405
    34
fun revert_sym thy (Celem.Thm (thmID, thm)) =
wneuper@59405
    35
  if (Rtools.is_sym o Celem.thmID_of_derivation_name) thmID
neuper@55484
    36
  then 
neuper@55484
    37
    let 
wneuper@59263
    38
      val thmID' = Rtools.sym_drop thmID
wneuper@59380
    39
      val thm' = Rewrite.assoc_thm' thy (thmID',"")
neuper@55484
    40
      val thmDeriv' = Thm.get_name_hint thm'
wneuper@59405
    41
    in Celem.Thm (thmDeriv', thm') end
wneuper@59405
    42
  else Celem.Thm (Thm.get_name_hint thm, thm)
neuper@37906
    43
neuper@55490
    44
(* get all theorems from the list of rule-sets (defined in Knowledge) *)
wneuper@59405
    45
fun thms_of_rlss thy rlss = (rlss : (Celem.rls' * (Celem.theory' * Celem.rls)) list)
wneuper@59263
    46
  |> map (Rtools.thms_of_rls o #2 o #2)
neuper@55405
    47
  |> flat
neuper@55484
    48
  |> map (revert_sym thy)
wneuper@59405
    49
  |> map (fn Celem.Thm (thmID, thm) => (thmID, thm))
neuper@55484
    50
  |> gen_distinct (fn ((thmID1, _), (thmID2, _)) => thmID1 = thmID2) 
wneuper@59405
    51
  : (Celem.thmID * thm) list                              
neuper@55490
    52
neuper@55490
    53
(* collect all thydata defined in in a theory *)
neuper@55405
    54
neuper@55448
    55
fun collect_thms part thy =
wneuper@59203
    56
  map (makeHthm (part, Context.theory_name thy)) (rev (thms_of thy))
wneuper@59405
    57
fun collect_rlss part rlss thys = (rlss : (Celem.rls' * (Celem.thyID * Celem.rls)) list)
neuper@55405
    58
  |> filter (fn (_, (thyID, _)) => member (op=) (map Context.theory_name thys) thyID)
neuper@55405
    59
  |> map (makeHrls part)
neuper@37906
    60
fun collect_cals (part, thy') =
neuper@55490
    61
  let val cals = [] (*FIXXXXXXXXXXME.WN060713 add thyID: (thyID, cal)*)
neuper@55490
    62
  in map (makeHcal (part, thy')) cals end;
neuper@37906
    63
fun collect_ords (part, thy') =
wneuper@59405
    64
    let val thy = Celem.assoc_thy (Celem.thyID2theory' thy')
wneuper@59405
    65
    in [(*TODO.WN060120 rew_ord, Calc*)]:(Celem.theID * Celem.thydata) list end;
neuper@37906
    66
neuper@55490
    67
(* parts are: Isabelle | IsacKnowledge | IsacScripts, see KEStore.thy *)
neuper@55405
    68
fun collect_part part parent thys =
neuper@55448
    69
  (flat (map (collect_thms part) thys)) @
neuper@55448
    70
  (collect_rlss part (KEStore_Elems.get_rlss parent) thys) @ 
neuper@55405
    71
  [(*TODO: collect_cals, collect_ords*)]
neuper@37906
    72
neuper@55490
    73
(* collect theorems defined in Isabelle *)
neuper@42400
    74
fun collect_isab isa (thmDeriv, thm) =
neuper@55490
    75
  let val theID =
wneuper@59405
    76
    [isa, Celem.thyID_of_derivation_name thmDeriv, "Theorems", Celem.thmID_of_derivation_name thmDeriv]
neuper@55490
    77
  in 
wneuper@59405
    78
    (theID: Celem.theID, Celem.Hthm {guh = Celem.theID2guh theID, 
neuper@55490
    79
      mathauthors = ["Isabelle team, TU Munich"],
neuper@55490
    80
      coursedesign = [],
neuper@55490
    81
      fillpats = [],
neuper@55490
    82
      thm = thm}) 
neuper@55490
    83
  end
neuper@37906
    84
wneuper@59269
    85
fun show_thes () = (writeln o Specify.format_pblIDl o (Specify.scan [])) (get_thes ());
neuper@37906
    86
neuper@37906
    87
neuper@55490
    88
(** create an xml representation for thehier: hierarchy of entries + files per entry **)
neuper@37906
    89
neuper@55490
    90
(* create an xml-hierarchy where the filname is created from the guh; 
neuper@55490
    91
  ad DTD: a NODE contains an ID and zero or more NODEs*)
neuper@37906
    92
fun hierarchy_guh h =
neuper@42407
    93
  let
neuper@42407
    94
    val i = indentation
neuper@42407
    95
    val j = indentation
wneuper@59405
    96
    fun node i p theID (Celem.Ptyp (id, _, ns)) = 
neuper@55490
    97
        let
wneuper@59276
    98
          val p' = Ctree.lev_on p
neuper@55490
    99
          val theID' = theID @ [id]
neuper@55490
   100
        in
neuper@55490
   101
        (indt i) ^ "<NODE>\n" ^ 
neuper@55490
   102
        (indt (i+j)) ^ "<ID> " ^ id ^ " </ID>\n" ^ 
neuper@55490
   103
        (indt (i+j)) ^ "<NO> " (*on this level*) ^ (string_of_int o last_elem) p' ^ " </NO>\n" ^ 
wneuper@59405
   104
        (indt (i+j)) ^ "<CONTENTREF> " ^ Celem.theID2guh theID' ^ " </CONTENTREF>\n" ^
wneuper@59276
   105
        (nodes (i+j) (Ctree.lev_dn p') theID' ns) ^ 
neuper@55490
   106
        (indt i) ^ "</NODE>\n"
neuper@55490
   107
        end
neuper@55490
   108
    and nodes _ _ _ [] = ""
wneuper@59276
   109
      | nodes i p theID (n :: ns) = (node i p theID n) ^ (nodes i (Ctree.lev_on p) theID ns);
neuper@42407
   110
  in nodes j [0] [] h end;
neuper@37906
   111
wneuper@59405
   112
fun thy_hierarchy2file (path: Celem.path) = 
neuper@55490
   113
  str2file (path ^ "thy_hierarchy.xml") 
neuper@55490
   114
    ("<NODE>\n" ^
neuper@55490
   115
    "  <ID> theory hierarchy </ID>\n" ^
neuper@55490
   116
    "  <NO> 1 </NO>\n" ^
neuper@55490
   117
    "  <CONTENTREF> thy_ROOT </CONTENTREF>\n" ^
neuper@55490
   118
    (hierarchy_guh (get_thes ())) ^
neuper@55490
   119
    "</NODE>");
neuper@37906
   120
neuper@55490
   121
(* create the xml-files for the thydata in the hierarchy *)
neuper@37906
   122
val i = indentation;
neuper@42407
   123
(* analoguous to 'fun met2xml' *)
wneuper@59405
   124
fun thydata2xml (theID: Celem.theID, Celem.Html {guh, coursedesign, mathauthors, html}) =
neuper@42407
   125
      "<HTMLDATA>\n" ^
neuper@42407
   126
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   127
      id2xml i theID ^
neuper@42407
   128
      indt i ^ "<EXPLANATIONS> " ^ html ^ "</EXPLANATIONS>\n" ^
neuper@42407
   129
      authors2xml i "MATHAUTHORS" mathauthors ^
neuper@42407
   130
      authors2xml i "COURSEDESIGNS" coursedesign ^
wneuper@59405
   131
      "</HTMLDATA>\n" : Celem.xml
wneuper@59405
   132
  | thydata2xml (theID, Celem.Hthm {guh, coursedesign, mathauthors, fillpats(*TODO?*), thm}) =
neuper@42407
   133
      "<THEOREMDATA>\n" ^
neuper@42407
   134
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   135
      id2xml i theID ^
neuper@55489
   136
      thm''2xml i thm ^
neuper@42407
   137
      indt i ^ "<PROOF>\n" ^
neuper@55490
   138
      extref2xml (i+i) "Proof of the theorem" (* TODO "Unsorted"vvv: distinguish Isabelle | Isac *)
neuper@55495
   139
	      ("http://www.ist.tugraz.at/projects/isac/www/kbase/thy/browser_info/HOL/HOL-Real/Isac/" ^
neuper@55490
   140
	        nth 2 theID ^ ".html") ^
neuper@42407
   141
	    indt i ^  "</PROOF>\n" ^
neuper@42407
   142
	    indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
neuper@42407
   143
	    authors2xml i "MATHAUTHORS" mathauthors ^
neuper@42407
   144
	    authors2xml i "COURSEDESIGNS" coursedesign ^
neuper@42407
   145
	    "</THEOREMDATA>\n"
wneuper@59405
   146
  | thydata2xml (theID, Celem.Hrls {guh, coursedesign, mathauthors, thy_rls}) =
neuper@42407
   147
      "<RULESETDATA>\n" ^
neuper@42407
   148
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   149
      id2xml i theID ^
neuper@55492
   150
      rls2xml i thy_rls ^
neuper@42407
   151
      indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
neuper@42407
   152
      authors2xml i "MATHAUTHORS" mathauthors ^
neuper@37906
   153
    authors2xml i "COURSEDESIGNS" coursedesign ^
neuper@42407
   154
      "</RULESETDATA>\n"
wneuper@59405
   155
  | thydata2xml (theID, Celem.Hcal {guh, coursedesign, mathauthors, calc}) =
neuper@42407
   156
      "<RULESETDATA>\n" ^
neuper@42407
   157
      indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
neuper@42407
   158
      id2xml i theID ^
wneuper@59405
   159
      calc2xml i (Celem.theID2thyID theID, calc) ^
neuper@42407
   160
      indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
neuper@42407
   161
      authors2xml i "MATHAUTHORS" mathauthors ^
neuper@42407
   162
      authors2xml i "COURSEDESIGNS" coursedesign ^
neuper@42407
   163
      "</RULESETDATA>\n"
neuper@37906
   164
  | thydata2xml (theID, _) =
neuper@42407
   165
      error ("thydata2xml: not implemented for "^ strs2str' theID);
neuper@37906
   166
neuper@42407
   167
(* analoguous to 'fun met2file' *)
wneuper@59405
   168
fun thydata2file (path : Celem.path) (pos : Ctree.pos) (theID : Celem.theID) thydata =
neuper@42407
   169
  (writeln ("### thes2file: id = " ^ strs2str theID);
wneuper@59263
   170
    str2file (path ^ Rtools.theID2filename theID) (thydata2xml (theID, thydata)));
neuper@37906
   171
neuper@42407
   172
(* analoguous to 'fun node' *)
wneuper@59405
   173
fun thenode (pa : Celem.path) ids po wfn (Celem.Ptyp (id, [n], ns)) = 
wneuper@59276
   174
    let val po' = Ctree.lev_on po
wneuper@59276
   175
    in wfn pa po' (ids @ [id]) n; thenodes pa (ids @ [id]) (Ctree.lev_dn po') wfn ns end
neuper@37906
   176
and thenodes _ _ _ _ [] = ()
neuper@42407
   177
  | thenodes pa ids po wfn (n::ns) =
wneuper@59276
   178
      (thenode pa ids po wfn n; thenodes pa ids (Ctree.lev_on po) wfn ns);
neuper@37906
   179
neuper@42407
   180
(* analoguous to 'fun mets2file' *)
wneuper@59405
   181
fun thes2file (p : Celem.path) = thenodes p [] [0] thydata2file (get_thes ());
neuper@37906
   182
neuper@37906
   183
neuper@37906
   184
(***.store a single theory element in the hierarchy.***)
neuper@37906
   185
neuper@37906
   186
(*.for mathauthors only, other html is added to xml exported from here.*)
wneuper@59405
   187
fun make_isa thy (part, thypart) (mathauthors : Celem.authors) =
neuper@55490
   188
  let 
wneuper@59405
   189
    val theID = [part, Celem.string_of_thy thy, thypart]
neuper@55490
   190
    val guh = case theID of
wneuper@59405
   191
        [part] => Celem.part2guh theID
wneuper@59405
   192
      | [part, thyID, thypart] => Celem.thypart2guh theID
wneuper@59263
   193
    val theID = Rtools.guh2theID guh
wneuper@59405
   194
    val the = Celem.Html {guh = guh, coursedesign = [], mathauthors = mathauthors, html = ""}
neuper@55420
   195
  in (the, theID) end
neuper@55420
   196
wneuper@59405
   197
fun make_thy thy (mathauthors : Celem.authors) =
neuper@55490
   198
  let 
wneuper@59405
   199
    val guh = Celem.thy2guh ["IsacKnowledge", Celem.theory2thyID thy]
wneuper@59263
   200
    val theID = Rtools.guh2theID guh
wneuper@59405
   201
    val the = Celem.Html {guh = guh, coursedesign = [], mathauthors = mathauthors, html = ""}
neuper@55420
   202
  in (the, theID) end
neuper@55420
   203
wneuper@59405
   204
fun make_thm thy part (thmID : Celem.thmID, thm) (mathauthors : Celem.authors) =
neuper@42411
   205
  let
wneuper@59405
   206
    val guh = Celem.thm2guh (part, Celem.theory2thyID thy) thmID
wneuper@59263
   207
    val theID = Rtools.guh2theID guh
wneuper@59405
   208
    val the = Celem.Hthm {guh = guh, coursedesign = [(*inserted in xml after export*)], 
neuper@55490
   209
			mathauthors = mathauthors, fillpats = [], thm = thm}
neuper@55420
   210
  in (the, theID) end
neuper@37906
   211
wneuper@59405
   212
fun make_rls thy rls (mathauthors : Celem.authors) =
neuper@48887
   213
  let 
wneuper@59405
   214
    val guh = Celem.rls2guh ("IsacKnowledge", Celem.theory2thyID thy) ((#id o Celem.rep_rls) rls)
wneuper@59263
   215
    val theID = Rtools.guh2theID guh
wneuper@59405
   216
    val the = Celem.Hrls {guh = guh, coursedesign = [], mathauthors = mathauthors,
wneuper@59405
   217
			thy_rls = (Celem.theory2thyID thy, rls)}
neuper@48887
   218
	  (*needs no (!check_guhs_unique) because guh is generated automatically*)
neuper@55420
   219
  in (the, theID) end
neuper@37906
   220
wneuper@59405
   221
fun make_cal thy cal (mathauthors : Celem.authors) =
neuper@55490
   222
  let
wneuper@59405
   223
    val guh = Celem.cal2guh ("IsacKnowledge", Celem.theory2thyID thy) ("TODO store_cal")
wneuper@59263
   224
    val theID = Rtools.guh2theID guh
wneuper@59405
   225
    val the = Celem.Hcal {guh = guh, coursedesign = [], mathauthors = mathauthors, calc = cal}
neuper@55420
   226
  in (the, theID) end
neuper@37906
   227
wneuper@59405
   228
fun make_ord thy ord (mathauthors : Celem.authors) =
neuper@55490
   229
  let 
wneuper@59405
   230
    val guh = Celem.ord2guh ("IsacKnowledge", Celem.theory2thyID thy) ("TODO store_ord")
wneuper@59263
   231
    val theID = Rtools.guh2theID guh
wneuper@59405
   232
    val the = Celem.Hord {guh = guh, coursedesign = [], mathauthors = mathauthors, ord = ord}
neuper@55420
   233
  in (the, theID) end
neuper@55420
   234
neuper@55463
   235
fun insert_errpatIDs thy theID errpatIDs = (* TODO: redo like insert_fillpatts *)
neuper@42452
   236
  let
wneuper@59269
   237
    val hrls = Specify.get_the theID
wneuper@59405
   238
    val hrls' = Celem.update_hrls hrls errpatIDs
neuper@42452
   239
      handle ERROR _ => error ("insert_errpatIDs: " ^ strs2str theID ^ "must address a rule-set")
neuper@55420
   240
  in (hrls', theID) end
neuper@55420
   241