src/Tools/isac/BaseDefinitions/thy-write.sml
author wneuper <Walther.Neuper@jku.at>
Thu, 04 Aug 2022 12:48:37 +0200
changeset 60509 2e0b7ca391dc
parent 60272 0e69700b70d6
child 60537 f0305aeb010b
permissions -rw-r--r--
polish naming in Rewrite_Order
     1 (* Title:  BaseDefinitions/celem-8.sml
     2    Author: Walther Neuper
     3    (c) due to copyright terms
     4 
     5 Here is a minimum of code required for Know_Store.thy.
     6 For main code see structure Thy_Write and structure Thy_Present.
     7 
     8 The latter is legacy for HTML representation of theory data in Isac's Java front end.
     9 *)
    10 signature THEORY_DATA_STORE_WRITE =
    11 sig
    12   type authors
    13   datatype thydata
    14     = Hcal of {calc: Rule_Def.calc, coursedesign: authors, guh: Check_Unique.id, mathauthors: authors}
    15     | Hord of {coursedesign: authors, guh: Check_Unique.id, mathauthors: authors, ord: Rewrite_Ord.function}
    16     | Hrls of {coursedesign: authors, guh: Check_Unique.id, mathauthors: authors, thy_rls: ThyC.id * Rule_Set.T}
    17     | Hthm of {coursedesign: authors, fillpats: Error_Pattern_Def.fill_in list, guh: Check_Unique.id, mathauthors: authors, thm: thm}
    18     | Html of {coursedesign: authors, guh: Check_Unique.id, html: string, mathauthors: authors}
    19   type theID
    20   val theID2str: string list -> string
    21   val the2str: thydata -> string
    22   val thes2str: thydata list -> string
    23   val theID2thyID: theID -> ThyC.id
    24 
    25   val part2guh: theID -> Check_Unique.id
    26   val thy2guh: theID -> Check_Unique.id
    27   val thypart2guh: theID -> Check_Unique.id
    28   val thm2guh: string * ThyC.id -> ThmC_Def.id -> Check_Unique.id
    29   val rls2guh: string * ThyC.id -> Rule_Set.id -> Check_Unique.id
    30   val cal2guh: string * ThyC.id -> string -> Check_Unique.id
    31   val ord2guh: string * ThyC.id -> string -> Check_Unique.id
    32   val theID2guh: theID -> Check_Unique.id
    33 
    34   val add_thydata: string list * string list -> thydata -> thydata Store.T -> thydata Store.T
    35   val update_hthm: thydata -> Error_Pattern_Def.fill_in list -> thydata
    36 end
    37 
    38 (**)
    39 structure Thy_Write(**): THEORY_DATA_STORE_WRITE(**) =
    40 struct
    41 (**)
    42 
    43 (*the key into the hierarchy ob theory elements*)
    44 type theID = string list;
    45 val theID2str = strs2str; (*theID eg. is ["IsacKnowledge", "Test", "Rulesets", "ac_plus_times"]*)
    46 fun theID2thyID theID =
    47   if length theID >= 3 then (last_elem o (drop_last_n 2)) theID
    48   else raise ERROR ("theID2thyID called with " ^ theID2str theID);
    49 type authors = string list;
    50 (* datatype for collecting thydata for hierarchy *)
    51 (*WN060720 more consistent naming would be 'type thyelem' or 'thelem'*)
    52 datatype thydata =
    53   Html of {guh: Check_Unique.id, coursedesign: authors, mathauthors: authors, html: string}
    54 | Hthm of {guh: Check_Unique.id, coursedesign: authors, mathauthors: authors, fillpats: Error_Pattern_Def.fill_in list,
    55    thm: thm} (* here no sym_thm, thus no thmID required *)
    56 | Hrls of {guh: Check_Unique.id, coursedesign: authors, mathauthors: authors, thy_rls: (ThyC.id * Rule_Set.T)}
    57 | Hcal of {guh: Check_Unique.id, coursedesign: authors, mathauthors: authors, calc: Rule_Def.calc}
    58 | Hord of {guh: Check_Unique.id, coursedesign: authors, mathauthors: authors,
    59     ord: (subst -> (term * term) -> bool)};
    60 fun the2str (Html {guh, ...}) = guh
    61   | the2str (Hthm {guh, ...}) = guh
    62   | the2str (Hrls {guh, ...}) = guh
    63   | the2str (Hcal {guh, ...}) = guh
    64   | the2str (Hord {guh, ...}) = guh
    65 fun thes2str thes = map the2str thes |> list2str;
    66 
    67 (* notes on thehier concerning sym_thmID theorems (created in derivations, reverse rewriting)
    68      (a): thehier does not contain sym_thmID theorems
    69      (b): lookup for sym_thmID directly from Isabelle using sym_thm
    70           (within math-engine NO lookup in thehier -- within java in *.xml only!)
    71 TODO (c): export from thehier to xml
    72 TODO (c1)   creates one entry for "thmID" (and NONE for "sym_thmID") in the hierarchy
    73 TODO (c2)   creates 2 files "thy_*-thm-thmID.xml" and "thy_*-thm-sym_thmID.xml"
    74 TODO (d): 1 entry in the MiniBrowser's hierarchy (generated from xml)
    75           stands for both, "thmID" and "sym_thmID" 
    76 TODO (d1)   lookup from calctxt          
    77 TODO (d1)   lookup from from rule set in MiniBrowser *)
    78 type thehier = (thydata Store.node) list;
    79 (* required to determine sequence of main nodes of thehier in Know_Store.thy *)
    80 fun part2guh [str] = (case str of
    81 	  "Isabelle" => "thy_isab_" ^ str ^ "-part" : Check_Unique.id
    82       | "IsacScripts" => "thy_scri_" ^ str ^ "-part"
    83       | "IsacKnowledge" => "thy_isac_" ^ str ^ "-part"
    84       | str => raise ERROR ("thy2guh: called with \""^ str ^"\""))
    85   | part2guh theID = raise ERROR ("part2guh called with theID = \"" ^ theID2str theID ^ "'");
    86 
    87 fun thy2guh [part, thyID] = (case part of
    88       "Isabelle" => "thy_isab_" ^ thyID
    89     | "IsacScripts" => "thy_scri_" ^ thyID
    90     | "IsacKnowledge" => "thy_isac_" ^ thyID
    91     | str => raise ERROR ("thy2guh: called with \"" ^ str ^ "\""))
    92   | thy2guh theID = raise ERROR ("thy2guh called with \"" ^ strs2str' theID ^ "\"");
    93 			
    94 fun thypart2guh ([part, thyID, thypart] : theID) = (case part of
    95       "Isabelle" => "thy_isab_" ^ thyID ^ "-" ^ thypart : Check_Unique.id
    96     | "IsacScripts" => "thy_scri_" ^ thyID ^ "-" ^ thypart
    97     | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-" ^ thypart
    98     | str => raise ERROR ("thypart2guh: called with '" ^ str ^ "'"))
    99   | thypart2guh strs = raise ERROR ("thypart2guh called with \"" ^ strs2str' strs ^ "\"");
   100 
   101   
   102 (* convert the data got via contextToThy to a globally unique handle.
   103    there is another way to get the guh: get out of the 'theID' in the hierarchy *)
   104 fun thm2guh (isa, thyID) thmID = case isa of
   105     "Isabelle" => "thy_isab_" ^ thyID ^ "-thm-" ^ strip_thy thmID : Check_Unique.id
   106   | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-thm-" ^ strip_thy thmID
   107   | "IsacScripts" => "thy_scri_" ^ thyID ^ "-thm-" ^ strip_thy thmID
   108   | _ => raise ERROR
   109     ("thm2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for thm = \"" ^ thmID ^ "\"");
   110 
   111 fun rls2guh (isa, thyID) rls' = case isa of
   112     "Isabelle" => "thy_isab_" ^ thyID ^ "-rls-" ^ rls' : Check_Unique.id
   113   | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-rls-" ^ rls'
   114   | "IsacScripts" => "thy_scri_" ^ thyID ^ "-rls-" ^ rls'
   115   | _ => raise ERROR
   116     ("rls2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for rls = \"" ^ rls' ^ "\"");
   117 			  
   118 fun cal2guh (isa, thyID) calID = case isa of
   119     "Isabelle" => "thy_isab_" ^ thyID ^ "-cal-" ^ calID : Check_Unique.id
   120   | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-cal-" ^ calID
   121   | "IsacScripts" => "thy_scri_" ^ thyID ^ "-cal-" ^ calID
   122   | _ => raise ERROR
   123     ("cal2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for cal = \"" ^ calID ^ "\"");
   124 			  
   125 fun ord2guh (isa, thyID) rew_ord' = case isa of
   126     "Isabelle" => "thy_isab_" ^ thyID ^ "-ord-" ^ rew_ord' : Check_Unique.id
   127   | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-ord-" ^ rew_ord'
   128   | "IsacScripts" => "thy_scri_" ^ thyID ^ "-ord-" ^ rew_ord'
   129   | _ => raise ERROR
   130     ("ord2guh called with (isa, thyID) = (" ^ isa ^ ", " ^ thyID ^ ") for ord = \"" ^ rew_ord' ^ "\"");
   131 
   132 (* TODO
   133 fun theID2guh theID = case length theID of
   134     0 => raise ERROR ("theID2guh: called with theID = " ^ strs2str' theID)
   135   | 1 => part2guh theID
   136   | 2 => thy2guh theID
   137   | 3 => thypart2guh theID
   138   | 4 => 
   139     let val [isa, thyID, typ, elemID] = theID
   140     in case typ of
   141         "Theorems" => thm2guh (isa, thyID) elemID
   142       | "Rulesets" => rls2guh (isa, thyID) elemID
   143       | "Calculations" => cal2guh (isa, thyID) elemID
   144       | "Orders" => ord2guh (isa, thyID) elemID
   145       | "Theorems" => thy2guh [isa, thyID]
   146       | str => raise ERROR ("theID2guh: called with theID = " ^ strs2str' theID)
   147     end
   148   | n => raise ERROR ("theID2guh called with theID = " ^ strs2str' theID);
   149 *)
   150 (* not only for thydata, but also for thy's etc *)
   151 fun theID2guh [] = raise ERROR ("theID2guh: called with []")
   152   | theID2guh [str] = part2guh [str]
   153   | theID2guh [s1, s2] = thy2guh [s1, s2]
   154   | theID2guh [s1, s2, s3] = thypart2guh [s1, s2, s3]
   155   | theID2guh (strs as [isa, thyID, typ, elemID]) = (case typ of
   156       "Theorems" => thm2guh (isa, thyID) elemID
   157     | "Rulesets" => rls2guh (isa, thyID) elemID
   158     | "Calculations" => cal2guh (isa, thyID) elemID
   159     | "Orders" => ord2guh (isa, thyID) elemID
   160     | _ => raise ERROR ("theID2guh: called with theID = " ^ strs2str' strs))
   161   | theID2guh strs = raise ERROR ("theID2guh called with theID = " ^ strs2str' strs);
   162 
   163 fun Html_default exist = (Html {guh = theID2guh exist, 
   164   coursedesign = ["isac team 2006"], mathauthors = [], html = ""})
   165 
   166 fun fill_parents (_, [i]) thydata = Store.Node (i, [thydata], [])
   167   | fill_parents (exist, i :: is) thydata =
   168     Store.Node (i, [Html_default (exist @ [i])], [fill_parents (exist @ [i], is) thydata])
   169   | fill_parents _ _ = raise ERROR "Html_default: avoid ML warning: Matches are not exhaustive"
   170 
   171 fun add_thydata (exist, is) thydata [] = [fill_parents (exist, is) thydata]
   172   | add_thydata (exist, [i]) data (pys as (py as Store.Node (key, _, _)) :: pyss) = 
   173     if i = key
   174     then pys (* preserve existing thydata *) 
   175     else py :: add_thydata (exist, [i]) data pyss
   176   | add_thydata (exist, iss as (i :: is)) data ((py as Store.Node (key, d, pys)) :: pyss) = 
   177     if i = key
   178     then       
   179       if length pys = 0
   180       then Store.Node (key, d, [fill_parents (exist @ [i], is) data]) :: pyss
   181       else Store.Node (key, d, add_thydata (exist @ [i], is) data pys) :: pyss
   182     else py :: add_thydata (exist, iss) data pyss
   183   | add_thydata _ _ _ = raise ERROR "add_thydata: avoid ML warning: Matches are not exhaustive"
   184 
   185 fun update_hthm (Hthm {guh, coursedesign, mathauthors, thm, ...}) fillpats' =
   186   Hthm {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
   187     fillpats = fillpats', thm = thm}
   188   | update_hthm _ _ = raise ERROR "update_hthm: wrong arguments";
   189 
   190 (**)end(**)