src/Tools/isac/xmlsrc/thy-hierarchy.sml
author Walther Neuper <neuper@ist.tugraz.at>
Sun, 22 Sep 2013 18:09:05 +0200
changeset 52125 6f1d3415dc68
parent 48887 227924cdba6a
child 52142 e7febad0c988
permissions -rw-r--r--
add functions accessing Theory_Data in parallel to those accessing "ruleset' = Unsynchronized.ref"

updates have been done incrementally following Build_Isac.thy:
# ./bin/isabelle jedit -l HOL src/Tools/isac/ProgLang/ProgLang.thy &
# ./bin/isabelle jedit -l HOL src/Tools/isac/Interpret/Interpret.thy &
# ./bin/isabelle jedit -l HOL src/Tools/isac/xmlsrc/xmlsrc.thy &
# ./bin/isabelle jedit -l HOL src/Tools/isac/Frontend/Frontend.thy &

Note, that the original access function "fun assoc_rls" is still outcommented;
so the old and new functionality is established in parallel.
     1 (*.export theory-data to xml
     2    author: Walther Neuper 0601
     3    (c) isac-team
     4 
     5    FIXME.WN0602: re-engineer this file for analogy to pbl-met-hierarchy
     6    as follows:
     7    # 'fun collect_thydata': unit -> string list * thydata list
     8                                     ^^^^^^^^^^^ hierarchy-key
     9    # 'fun thys2file': from this ^^^ datastructure (^^^^^^^^^^^ for free!)
    10    # map 'fun store_pbt' (NEW!) over ^^^ into 'thy_data ptyp'
    11    # from 'thy_data ptyp' create 'thy_hierarchy'
    12 
    13 use"xmlsrc/thy-hierarchy.sml";
    14 use"thy-hierarchy.sml";
    15 .*)
    16 
    17 
    18 (**.collect data and build intermediate structure for hierarchy:
    19     theorems, rulesets and Calc's, (TODO rew_ord's etc) defined in isac 
    20     and Isabelle-thms used in rulesets;
    21     this code binds ref-var's and must be after IsacKnowledge .**)
    22 
    23 (*.collect all theorems defined in in a theory and insert the guh.*)
    24 fun makeHthm (part:string, thyID:thyID) (thmID:thmID, thm:term) =
    25     let val theID = [part, thyID, "Theorems"] @ [strip_thy thmID] : theID
    26     in (theID, Hthm {guh = theID2guh theID, coursedesign = [], 
    27 		     mathauthors = ["isac-team"], fillpats = [], thm = thm})
    28     end;
    29 fun makeHrls (part:string) (rls':rls', thy_rls as (thyID, rls): thyID * rls) =
    30     let val theID = [part, thyID,"Rulesets"] @ [rls'] : theID
    31     in (theID, Hrls {guh = theID2guh theID, coursedesign=[], 
    32 		     mathauthors=["isac-team"], thy_rls = thy_rls})
    33     end;
    34 fun makeHcal (part:string, thyID:thyID) (calID, cal) =
    35     let val theID = [part, thyID,"Operations"] @ [calID] : theID
    36     in (theID, Hcal {guh = theID2guh theID, coursedesign=[], 
    37 		     mathauthors=["isac-team"], calc = cal})
    38     end;
    39 fun makeHord (part:string, thyID:thyID) (ordID, ord) =
    40     let val theID = [part, thyID,"TODO-Orders"] @ [ordID] : theID
    41     in (theID, Hord  {guh = theID2guh theID, coursedesign=[], 
    42 		      mathauthors=["isac-team"], ord = ord})
    43     end;
    44 
    45 
    46 fun collect_thms' (part, thy') =
    47     let val thy = assoc_thy (thyID2theory' thy')
    48     in map (makeHthm (part, thy')) (Theory.axioms_of thy) end;
    49     
    50 (*.collect all rulesets defined in in a theory and insert the guh.*)
    51 fun collect_rlss (part, thy') = 
    52     let val rlss = filter ((curry op= thy') o 
    53 			   ((#1 o #2):(rls' * (theory' * rls)) -> theory')) 
    54 			  (!ruleset') (*SWITCH KEStore_Elems.get_rlss (Thy_Info.get_theory "Isac")*)
    55     in map (makeHrls part) rlss end;
    56 
    57 (*.collect all calcs defined in in a theory.*)
    58 fun collect_cals (part, thy') =
    59     let val cals = [] (*FIXXXXXXXXXXME.WN060713 add thyID: (thyID, cal)*)
    60     in map (makeHcal (part, thy')) cals end;
    61 
    62 
    63 (*.collect all rew_ord's defined in in a theory.*)
    64 fun collect_ords (part, thy') =
    65     let val thy = assoc_thy (thyID2theory' thy')
    66     in [(*TODO.WN060120 rew_ord, Calc*)]:(theID * thydata) list end;
    67 
    68 (*.collect all data for a thy TODO.WN060120 rew_ord, Calc.*)
    69 (* val thy' = nth 1 scri_thys;
    70    *)
    71 fun collect_thy part(*IsacScripts|IsacKnowledge*) (thy': theory') =
    72     ((collect_thms' (part, thy')) @ (collect_rlss (part, thy')) @ 
    73      (collect_cals (part, thy')) @ (collect_ords (part, thy')))
    74     : (theID * thydata) list;
    75 
    76 (*.collect theorems defined in Isabelle (before Isac is evaluated above).*)
    77 fun collect_isab isa (thmDeriv, thm) =
    78     let val theID =
    79       [isa, thyID_of_derivation_name thmDeriv, "Theorems", thmID_of_derivation_name thmDeriv]
    80     in (theID:theID, Hthm {guh = theID2guh theID, 
    81 		     mathauthors = ["Isabelle team, TU Munich"],
    82 		     coursedesign = [],
    83 		     fillpats = [],
    84 		     thm = thm}) end;
    85 
    86 val isabelle_page = (["Isabelle"] : theID,
    87 		     Html {guh = theID2guh ["Isabelle"],
    88 			   html = "",
    89 			   mathauthors = ["Isabelle team, TU Munich"],
    90 			   coursedesign = []});
    91 
    92 (*.create a list with all thydata=thyelements=the;
    93    this list is used by 'fun the_hier' to create the hierarchy .*)
    94 fun collect_thydata () =
    95   [isabelle_page] @
    96   (map (collect_isab "Isabelle") (! isab_thm_thy)) @ 
    97   ((flat o (map (collect_thy "IsacScripts"))) (map Context.theory_name (! progthys))) @
    98   ((flat o (map (collect_thy "IsacKnowledge"))) (map Context.theory_name (! knowthys))): 
    99 (theID * thydata) list; 
   100 
   101 fun show_thes () = (writeln o format_pblIDl o (scan [])) (! thehier);
   102 
   103 
   104 
   105 (***.create the xml-format for the hierarchy.***)
   106 
   107 (**.make a hierarchy from (theID * thydata) list created by 'fun collect_thy';
   108     use the same mechanism as for pbl_hierarchy and met_hierarchy;
   109     but check, if a thydata is already there (for auto-gen. Isabelle).**)
   110 
   111 (*.for preserving elements created by 'fun store_thy'.*)
   112 fun exist_the (theID:theID) (thy_hie:thehier) =
   113     let fun node theID ids (Ptyp (id,_,ns)) =
   114 	    if theID = ids @ [id] then true
   115 	    else nodes theID (ids @ [id]) ns
   116 	and nodes _ _ [] = false
   117 	  | nodes theID ids (n::ns) = if node theID ids n then true
   118 				      else  nodes theID ids ns
   119     in nodes theID [] thy_hie end;
   120 
   121 (*.insrt requires a parent; see 'fun fill_parents'.*)
   122 fun can_insert (theID:theID) (thy_hie:thehier) = 
   123     (insrt theID e_thydata theID thy_hie; true)
   124     handle _ => false;
   125 
   126 (*.cut 'theID', the ID of theory elements from tail to head
   127    until insertion into the hierarchy of theory elements 'th' is possible
   128    (the hierarchy requires the parentnode to exist for insertion).*)
   129 fun cut_theID th ([]:theID) = 
   130     error "could not insert into thy-hierarchy"
   131   | cut_theID th theID  = 
   132     if can_insert theID th
   133     then theID else cut_theID th (drop_last theID);
   134 
   135 (*.insert empty parents 'Html' into the hierarchy of theory elements 'th'
   136    until the actual node can be inserted with key 'theID'.*)
   137 fun fill_parents th cutID theID =
   138     let val cutID' = cut_theID th cutID
   139     in if cutID' = theID
   140        then th
   141        else let val th' = insrt cutID' (Html {guh=theID2guh theID,
   142 					      coursedesign=["isac team 2006"],
   143 					      mathauthors=[],
   144 					      html=""}) cutID' th
   145 		val cutID_ = cutID' @ [nth ((length cutID') + 1) theID]
   146 	    in fill_parents th' cutID_ theID end
   147     end;
   148 
   149 (*.create the hierarchy from a list (generated automatically);
   150    thus, missing parents of list-elems are inserted 
   151    (causing msgs '*** insert: not found');
   152    elemes already store_*d in some *.ML are NOT overwritten.*)
   153 fun the_hier th ([]: (theID * thydata) list) = th
   154   | the_hier th ((theID, thydata)::ths) =
   155     if can_insert theID th 
   156     then
   157       let
   158         val th' =
   159           if exist_the theID th
   160 		      then (writeln ("*** insert: preserved " ^ strs2str theID); th)
   161 		      else insrt theID thydata theID th
   162 		    in the_hier th' ths end
   163     else
   164       let
   165         val th' = fill_parents th theID theID (*..*** insert: not found*)
   166         val th' = insrt theID thydata theID th'
   167       in the_hier th' ths end;
   168 
   169 
   170 (*these files shall contain 'invisible' html
   171 val thydatafilename = "thy_datafile.xml"; (*for "Theorems"|...*)
   172 fun partfilename str = "thy_" ^ str ^ ".xml"; (*for "Isabelle"|...*)*)
   173 
   174 (* create an xml-hierarchy where the filname is created from the guh *)
   175 (*ad DTD: a NODE contains an ID and zero or more NODEs*)
   176 fun hierarchy_guh h =
   177   let
   178     val i = indentation
   179     val j = indentation
   180     fun node i p theID (Ptyp (id,_,ns)) = 
   181 	        let
   182 	          val p' = lev_on p
   183 	          val theID' = theID @ [id]
   184 	        in
   185 	          (indt i) ^ "<NODE>\n" ^ 
   186 	          (indt (i+j)) ^ "<ID> " ^ id ^ " </ID>\n" ^ 
   187 	          (indt (i+j)) ^ "<NO> " (*on this level*) ^ (string_of_int o last_elem) p' ^ " </NO>\n" ^ 
   188 	          (indt (i+j)) ^ "<CONTENTREF> " ^ theID2guh theID' ^ " </CONTENTREF>\n" ^
   189 	          (nodes (i+j) (lev_dn p') theID' ns) ^ 
   190 	          (indt i) ^ "</NODE>\n"
   191 	        end
   192 	  and nodes _ _ _ [] = ""
   193 	    | nodes i p theID (n::ns) =
   194 	        (node i p theID n) ^ (nodes i (lev_on p) theID ns);
   195   in nodes j [0] [] h end;
   196 
   197 fun thy_hierarchy2file (path:path) = 
   198     str2file (path ^ "thy_hierarchy.xml") 
   199 	     ("<NODE>\n" ^
   200 	      "  <ID> theory hierarchy </ID>\n" ^
   201 	      "  <NO> 1 </NO>\n" ^
   202 	      "  <CONTENTREF> thy_ROOT </CONTENTREF>\n" ^
   203 	     (hierarchy_guh (! thehier)) ^
   204 	     "</NODE>");
   205 
   206 
   207 (** create the xml-files for the theory-data from the hierarchy **)
   208 
   209 val i = indentation;
   210 (* analoguous to 'fun met2xml' *)
   211 fun thydata2xml (theID:theID, Html {guh, coursedesign, mathauthors, html}) =
   212       "<HTMLDATA>\n" ^
   213       indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
   214       id2xml i theID ^
   215       indt i ^ "<EXPLANATIONS> " ^ html ^ "</EXPLANATIONS>\n" ^
   216       authors2xml i "MATHAUTHORS" mathauthors ^
   217       authors2xml i "COURSEDESIGNS" coursedesign ^
   218       "</HTMLDATA>\n" : xml
   219   | thydata2xml (theID:theID, Hthm {guh, coursedesign, mathauthors, fillpats(*TODO?*), thm}) =
   220       "<THEOREMDATA>\n" ^
   221       indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
   222       id2xml i theID ^
   223       term2xml i thm ^
   224       indt i ^ "<PROOF>\n" ^
   225       extref2xml (i+i) "Proof of the theorem" 
   226 	      ("http://www.ist.tugraz.at/projects/isac/www/kbase/thy/browser_info/HOL/HOL-Real/Isac/" ^
   227 	    nth 2 theID ^ ".html") ^
   228 	    indt i ^  "</PROOF>\n" ^
   229 	    indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
   230 	    authors2xml i "MATHAUTHORS" mathauthors ^
   231 	    authors2xml i "COURSEDESIGNS" coursedesign ^
   232 	    "</THEOREMDATA>\n"
   233   | thydata2xml (theID, Hrls {guh, coursedesign, mathauthors, thy_rls}) =
   234       "<RULESETDATA>\n" ^
   235       indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
   236       id2xml i theID ^
   237       indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
   238       authors2xml i "MATHAUTHORS" mathauthors ^
   239     authors2xml i "COURSEDESIGNS" coursedesign ^
   240       "</RULESETDATA>\n"
   241   | thydata2xml (theID, Hcal {guh, coursedesign, mathauthors, calc}) =
   242       "<RULESETDATA>\n" ^
   243       indt i ^ "<GUH> "^ guh ^" </GUH>\n" ^
   244       id2xml i theID ^
   245       calc2xml i (theID2thyID theID, calc) ^
   246       indt i ^ "<EXPLANATIONS> </EXPLANATIONS>\n" ^
   247       authors2xml i "MATHAUTHORS" mathauthors ^
   248       authors2xml i "COURSEDESIGNS" coursedesign ^
   249       "</RULESETDATA>\n"
   250   | thydata2xml (theID, _) =
   251       error ("thydata2xml: not implemented for "^ strs2str' theID);
   252 
   253 (* analoguous to 'fun met2file' *)
   254 fun thydata2file (xmldata:path) (pos:pos) (theID:theID) thydata =
   255   (writeln ("### thes2file: id = " ^ strs2str theID);
   256     str2file (xmldata ^ theID2filename theID) (thydata2xml (theID:theID, thydata)));
   257 
   258 (* analoguous to 'fun node' *)
   259 fun thenode (pa:path) ids po wfn (Ptyp (id, [n], ns)) = 
   260     let val po' = lev_on po
   261     in wfn pa po' (ids @ [id]) n; thenodes pa (ids @ [id]) ((lev_dn po'): pos) wfn ns end
   262 and thenodes _ _ _ _ [] = ()
   263   | thenodes pa ids po wfn (n::ns) =
   264       (thenode pa ids po wfn n; thenodes pa ids (lev_on po) wfn ns);
   265 
   266 (* analoguous to 'fun mets2file' *)
   267 fun thes2file (p : path) = thenodes p [] [0] thydata2file (! thehier);
   268 
   269 
   270 (***.store a single theory element in the hierarchy.***)
   271 
   272 (*.for mathauthors only, other html is added to xml exported from here.*)
   273 (* val (theID, mathauthors) = 
   274        (["IsacKnowledge", theory2thyID Biegelinie.thy, "Theorems"],
   275 	["Walther Neuper 2005 supported by a grant from NMI Austria"]);
   276    *)
   277 fun store_isa (theID : theID) (mathauthors : authors) =
   278     let val guh = case theID of
   279 		      [part] => part2guh theID
   280 		    | [part, thyID, thypart] => thypart2guh theID
   281 	val theID = guh2theID guh
   282 	val the = Html {guh = guh, 
   283 			coursedesign = [], 
   284 			mathauthors = mathauthors,
   285 			html = ""}
   286     in (*needs no (!check_guhs_unique) because guh is generated automatically*)
   287 	thehier := insrt theID the theID (! thehier) end;
   288 				 
   289 fun store_thy thy (mathauthors : authors) =
   290     let val guh = thy2guh ["IsacKnowledge", theory2thyID thy]
   291 	val theID = guh2theID guh
   292 	val the = Html {guh = guh, 
   293 			coursedesign = [], 
   294 			mathauthors = mathauthors,
   295 			html = ""}
   296     in (*needs no (!check_guhs_unique) because guh is generated automatically*)
   297 	thehier := insrt theID the theID (! thehier) end;
   298 				 
   299 fun store_thm thy part (thmID : thmID, thm) (mathauthors : authors) =
   300   let
   301     val guh = thm2guh (part, theory2thyID thy) thmID
   302     val theID = guh2theID guh
   303     val the = Hthm {guh = guh, 
   304 			coursedesign = [], (*done at xml exported from here*)
   305 			mathauthors = mathauthors,
   306 			fillpats = [],
   307 			thm = thm}
   308   in (*needs no (!check_guhs_unique) because guh is generated automatically*)
   309 	  thehier := insrt theID the theID (! thehier) end;
   310 
   311 (*WN120512 DESIGN TODO: build thehier already in respective theories*)
   312 fun insert_fillpats thy part (thmID : thmID, thm) fillpats =
   313   let
   314     val guh = thm2guh (part, theory2thyID thy) thmID
   315     val theID = guh2theID guh
   316     val Hthm {guh, coursedesign, mathauthors, thm, ...} = get_the theID
   317     val the = Hthm {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
   318       fillpats = fillpats, thm = thm}
   319   in thehier := insrt theID the theID (! thehier) end;
   320 
   321 fun store_rls thy rls (mathauthors : authors) =
   322   let 
   323     val guh = rls2guh ("IsacKnowledge", theory2thyID thy) ((#id o rep_rls) rls)
   324     val theID = guh2theID guh
   325     val the = Hrls {guh = guh, coursedesign = [], mathauthors = mathauthors,
   326 			thy_rls = (theory2thyID thy, rls)}
   327 	  (*needs no (!check_guhs_unique) because guh is generated automatically*)
   328 	in thehier := insrt theID the theID (! thehier) end;
   329 
   330 fun store_cal thy cal (mathauthors : authors) =
   331     let val guh = cal2guh ("IsacKnowledge", theory2thyID thy)
   332 			  ("TODO store_cal")
   333 	val theID = guh2theID guh
   334 	val the = Hcal {guh = guh,
   335 			coursedesign = [],
   336 			mathauthors = mathauthors,
   337 			calc = cal}
   338     in (*needs no (!check_guhs_unique) because guh is generated automatically*)
   339 	thehier := insrt theID the theID (! thehier) end;
   340 
   341 fun store_ord thy ord (mathauthors : authors) =
   342     let val guh = ord2guh ("IsacKnowledge", theory2thyID thy)
   343 			  ("TODO store_ord")
   344 	val theID = guh2theID guh
   345 	val the = Hord {guh = guh,
   346 			coursedesign = [],
   347 			mathauthors = mathauthors,
   348 			ord = ord}
   349     in (*needs no (!check_guhs_unique) because guh is generated automatically*)
   350 	thehier := insrt theID the theID (! thehier) end;
   351 
   352 fun update_hthm (Hthm {guh, coursedesign, mathauthors, thm, ...}) fillpats' =
   353   Hthm {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
   354     fillpats = fillpats', thm = thm}
   355   | update_hthm _ _ = raise ERROR "wrong data";
   356 
   357 (* for interface for dialog-authoring *)
   358 fun update_hrls (Hrls {guh, coursedesign, mathauthors, thy_rls = (thyID, rls)}) errpatIDs =
   359   let
   360     val rls' = 
   361       case rls of
   362         Rls {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...} =>
   363           Rls {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
   364             calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
   365       | Seq {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...} =>
   366             Seq {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
   367               calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
   368       | Rrls {id, prepat, rew_ord, erls, calc, scr, ...} =>
   369             Rrls {id = id, prepat = prepat, rew_ord = rew_ord, erls = erls, calc = calc,
   370               scr = scr, errpatts = errpatIDs}
   371       | Erls => Erls
   372   in
   373     Hrls {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
   374       thy_rls = (thyID, rls')}
   375   end
   376   | update_hrls _ _ = raise ERROR "wrong data";
   377 
   378 (* interface for dialog-authoring: call in Buld_Thydata.thy only ! *)
   379 fun insert_fillpats theID fillpats =
   380   let
   381     val hthm = get_the theID
   382     val hthm' = update_hthm hthm fillpats
   383       handle ERROR _ => error ("insert_fillpats: " ^ strs2str theID ^ "must address a theorem")
   384   in thehier := insrt theID hthm' theID (! thehier) end;
   385 
   386 fun insert_errpatIDs theID errpatIDs =
   387   let
   388     val hrls = get_the theID
   389     val hrls' = update_hrls hrls errpatIDs
   390       handle ERROR _ => error ("insert_errpatIDs: " ^ strs2str theID ^ "must address a rule-set")
   391   in thehier := insrt theID hrls' theID (! thehier) end;