src/Tools/isac/BridgeLibisabelle/thy-hierarchy.sml
author wneuper <Walther.Neuper@jku.at>
Sat, 08 Oct 2022 19:17:24 +0200
changeset 60563 fb5fce693420
parent 60309 70a1d102660d
child 60586 007ef64dbb08
permissions -rw-r--r--
follow up 5d: Error_Pattern.fill_in also included to adapt_to_type
walther@59974
     1
(* Title: "BridgeLibisabelle/thy-hierarchy.sml"
walther@59974
     2
   Author: Walther Neuper 0601
walther@59974
     3
   (c) due to copyright terms
walther@59974
     4
walther@60258
     5
Tools to Build_Thydata of theorems and rule-sets defined in Knowledge/ and ProgLang/
neuper@55490
     6
*)
neuper@37906
     7
walther@59874
     8
signature THEORY_HIERARCHY =
walther@59874
     9
sig
walther@59917
    10
  val collect_cals: string * ThyC.id -> (Thy_Write.theID * Thy_Write.thydata) list
walther@59917
    11
  val collect_isab: string -> string * thm -> Thy_Write.theID * Thy_Write.thydata
walther@59917
    12
  val collect_ords: 'a * ThyC.id -> (Thy_Write.theID * Thy_Write.thydata) list
walther@59917
    13
  val collect_part: string -> theory -> theory list -> (Thy_Write.theID * Thy_Write.thydata) list
walther@59879
    14
  val collect_rlss: string -> (Rule_Set.id * (ThyC.id * Rule_Set.T)) list -> theory list ->
walther@59917
    15
    (Thy_Write.theID * Thy_Write.thydata) list
walther@59917
    16
  val collect_thms: string -> theory -> (Thy_Write.theID * Thy_Write.thydata) list
walther@59874
    17
walther@59917
    18
  val insert_errpatIDs: 'a -> Thy_Write.theID -> Error_Pattern_Def.id list ->
walther@59917
    19
    Thy_Write.thydata * Thy_Write.theID
walther@59874
    20
walther@60258
    21
  val thms_of_rls : Rule_Set.T -> Rule.rule list
walther@59879
    22
  val thms_of_rlss: theory -> (Rule_Set.id * (ThyC.id * Rule_Def.rule_set)) list ->
walther@59874
    23
    (string * thm) list
walther@60255
    24
wenzelm@60223
    25
\<^isac_test>\<open>
walther@60258
    26
  val hierarchy_guh: 'a Store.T -> string
walther@59971
    27
  val get_fun_ids: theory -> (string * typ) list
wenzelm@60223
    28
\<close>
walther@59874
    29
end
walther@59874
    30
walther@59874
    31
(**)
walther@59874
    32
structure Thy_Hierarchy(**): THEORY_HIERARCHY(**) =
walther@59874
    33
struct
walther@59874
    34
(**)
wneuper@59507
    35
open TermC (* allows contains_one_of to be infix *)
wneuper@59507
    36
walther@60154
    37
fun fun_id_of ({scr = prog, ...} : MethodC.T) = 
walther@59971
    38
  case prog of
walther@59971
    39
    Rule.Empty_Prog => NONE
walther@59971
    40
  | Rule.Prog t => 
walther@59971
    41
    (case t of
wenzelm@60309
    42
      Const (\<^const_name>\<open>HOL.eq\<close>, _) $ Free ("t", _) $ Free ("t", _) (*=@{thm refl}*) => NONE
walther@59971
    43
    | _ => SOME (Program.get_fun_id t))
walther@59971
    44
  | Rule.Rfuns _ => NONE
walther@59971
    45
fun get_fun_ids thy =
walther@59971
    46
  let 
walther@59971
    47
    val mets = Store.get_all (KEStore_Elems.get_mets thy)
walther@59971
    48
    (* all mets of the respective theory PLUS of all ancestor theories*)
walther@59971
    49
    val fun_ids = mets |> map fun_id_of |> filter is_some |> map the
walther@59971
    50
  in 
walther@59971
    51
    filter (fn (str, _) => ThyC.cut_id str = Context.theory_name thy) fun_ids 
walther@59971
    52
  end
walther@59971
    53
wneuper@59203
    54
(* copy from mutabelle_extra.ML, fun thms_of *)
wneuper@59507
    55
fun thms_of thy = (* das ist zu verbessern *)
wneuper@59507
    56
  let
walther@59971
    57
    val fun_ids = get_fun_ids thy
wneuper@59507
    58
  in
wneuper@59507
    59
    filter (fn thm => Context.theory_name (Thm.theory_of_thm thm) = Context.theory_name thy
wneuper@59549
    60
        andalso not (thm contains_one_of fun_ids)
walther@59876
    61
        andalso not (ThmC.id_of_thm thm = "mono"))
wneuper@59549
    62
        (* created in Biegelinie by partial_function ^^^^^^*)
wneuper@59507
    63
      (map snd (Global_Theory.all_thms_of thy false))
wneuper@59507
    64
  end
wneuper@59203
    65
neuper@55490
    66
(* wrap theory-data into the uniform type thydata *)
neuper@37906
    67
walther@59879
    68
fun makeHthm (part : string, thyID : ThyC.id) (thm : thm) =
walther@59917
    69
    let val theID = [part, thyID, "Theorems"] @ [ThmC.id_of_thm thm] : Thy_Write.theID
walther@59917
    70
    in (theID, Thy_Write.Hthm {guh = Thy_Write.theID2guh theID, coursedesign = [], 
neuper@42429
    71
		     mathauthors = ["isac-team"], fillpats = [], thm = thm})
neuper@37906
    72
    end;
walther@60258
    73
fun makeHrls (part : string) (rls' : Rule_Set.id, thy_rls as (thyID, _): ThyC.id * Rule_Set.T) =
walther@59917
    74
    let val theID = [part, thyID,"Rulesets"] @ [rls'] : Thy_Write.theID
walther@59917
    75
    in (theID, Thy_Write.Hrls {guh = Thy_Write.theID2guh theID, coursedesign = [], 
neuper@55490
    76
		     mathauthors = ["isac-team"], thy_rls = thy_rls})
neuper@37906
    77
    end;
walther@59879
    78
fun makeHcal (part : string, thyID : ThyC.id) (calID, cal) =
walther@59917
    79
    let val theID = [part, thyID,"Operations"] @ [calID] : Thy_Write.theID
walther@59917
    80
    in (theID, Thy_Write.Hcal {guh = Thy_Write.theID2guh theID, coursedesign=[], 
neuper@55490
    81
		     mathauthors = ["isac-team"], calc = cal})
neuper@37906
    82
    end;
walther@60258
    83
walther@60258
    84
(* get all theorems in a rule set (recursivley containing rule sets) *)
walther@60258
    85
fun thm_of_rule Rule.Erule = []
walther@60258
    86
  | thm_of_rule (thm as Rule.Thm _) = [thm]
walther@60258
    87
  | thm_of_rule (Rule.Eval _) = []
walther@60258
    88
  | thm_of_rule (Rule.Cal1 _) = []
walther@60258
    89
  | thm_of_rule (Rule.Rls_ rls) = thms_of_rls rls
walther@60258
    90
and thms_of_rls Rule_Set.Empty = []
walther@60258
    91
  | thms_of_rls (Rule_Def.Repeat {rules,...}) = (flat o (map  thm_of_rule)) rules
walther@60258
    92
  | thms_of_rls (Rule_Set.Sequence {rules,...}) = (flat o (map  thm_of_rule)) rules
walther@60258
    93
  | thms_of_rls (Rule_Set.Rrls _) = []
neuper@37906
    94
neuper@55490
    95
(* get all theorems from the list of rule-sets (defined in Knowledge) *)
walther@59879
    96
fun thms_of_rlss thy rlss = (rlss : (Rule_Set.id * (ThyC.id * Rule_Set.T)) list)
walther@60258
    97
  |> map (thms_of_rls o #2 o #2)
neuper@55405
    98
  |> flat
walther@59876
    99
  |> map (ThmC.revert_sym_rule thy)
walther@60269
   100
  |> map (fn Rule.Thm (thmID, thm) => (thmID, thm)
walther@60269
   101
           | _ => raise ERROR "thms_of_rlss: uncovered case")
walther@60017
   102
  |> 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;
walther@60258
   115
fun collect_ords (_, _) =
walther@60258
   116
    [(*TODO.WN060120 rew_ord, Eval*)]:(Thy_Write.theID * Thy_Write.thydata) list
neuper@37906
   117
walther@59887
   118
(* parts are: Isabelle | IsacKnowledge | IsacScripts, see Know_Store.thy *)
neuper@55405
   119
fun collect_part part parent thys =
neuper@55448
   120
  (flat (map (collect_thms part) thys)) @
neuper@55448
   121
  (collect_rlss part (KEStore_Elems.get_rlss parent) thys) @ 
neuper@55405
   122
  [(*TODO: collect_cals, collect_ords*)]
neuper@37906
   123
neuper@55490
   124
(* collect theorems defined in Isabelle *)
neuper@42400
   125
fun collect_isab isa (thmDeriv, thm) =
neuper@55490
   126
  let val theID =
walther@59879
   127
    [isa, ThyC.cut_id thmDeriv, "Theorems", ThmC.cut_id thmDeriv]
neuper@55490
   128
  in 
walther@59917
   129
    (theID: Thy_Write.theID, Thy_Write.Hthm {guh = Thy_Write.theID2guh theID, 
neuper@55490
   130
      mathauthors = ["Isabelle team, TU Munich"],
neuper@55490
   131
      coursedesign = [],
neuper@55490
   132
      fillpats = [],
neuper@55490
   133
      thm = thm}) 
neuper@55490
   134
  end
neuper@37906
   135
neuper@37906
   136
walther@59918
   137
(* for dialog-authoring *)
walther@59918
   138
fun update_hrls (Thy_Write.Hrls {guh, coursedesign, mathauthors, thy_rls = (thyID, rls)}) errpatIDs =
walther@59918
   139
    let
walther@59918
   140
      val rls' = 
walther@59918
   141
        case rls of
walther@59918
   142
          Rule_Def.Repeat {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
walther@59918
   143
          => Rule_Def.Repeat {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
walther@59918
   144
               calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
walther@59918
   145
        | Rule_Set.Sequence {id, preconds, rew_ord, erls, srls, calc, rules, scr, ...}
walther@59918
   146
          => Rule_Set.Sequence {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls,
walther@59918
   147
               calc = calc, rules = rules, scr = scr, errpatts = errpatIDs}
walther@59918
   148
        | Rule_Set.Rrls {id, prepat, rew_ord, erls, calc, scr, ...}
walther@59918
   149
          => Rule_Set.Rrls {id = id, prepat = prepat, rew_ord = rew_ord, erls = erls, calc = calc,
walther@59918
   150
               scr = scr, errpatts = errpatIDs}
walther@59918
   151
        | Erls => Erls
walther@59918
   152
    in
walther@59918
   153
      Thy_Write.Hrls {guh = guh, coursedesign = coursedesign, mathauthors = mathauthors,
walther@59918
   154
        thy_rls = (thyID, rls')}
walther@59918
   155
    end
walther@59918
   156
  | update_hrls _ _ = raise ERROR "update_hrls: wrong arguments";
walther@59918
   157
walther@60258
   158
fun insert_errpatIDs _(*thy*) theID errpatIDs = (* TODO: redo like insert_fillpatts *)
neuper@42452
   159
  let
Walther@60563
   160
    val hrls = Thy_Read.for_thy_hierarchy theID
walther@59918
   161
    val hrls' = update_hrls hrls errpatIDs
walther@59962
   162
      handle ERROR _ => raise ERROR ("insert_errpatIDs: " ^ strs2str theID ^ "must address a rule-set")
neuper@55420
   163
  in (hrls', theID) end
neuper@55420
   164
walther@60258
   165
(*keep tests of interaction Java-frontend <-> Kernel running*)
walther@60258
   166
\<^isac_test>\<open>
walther@60258
   167
(* create an xml-hierarchy where the filname is created from the guh *)
walther@60258
   168
val indentation = 2;
walther@60258
   169
fun hierarchy_guh h =
walther@60258
   170
  let
walther@60258
   171
    val j = indentation
walther@60258
   172
    fun node i p theID (Store.Node (id, _, ns)) = 
walther@60258
   173
        let
walther@60258
   174
          val p' = Pos.lev_on p
walther@60258
   175
          val theID' = theID @ [id]
walther@60258
   176
        in
walther@60258
   177
        (indt i) ^ "<NODE>\n" ^ 
walther@60258
   178
        (indt (i+j)) ^ "<ID> " ^ id ^ " </ID>\n" ^ 
walther@60258
   179
        (indt (i+j)) ^ "<NO> " (*on this level*) ^ (string_of_int o last_elem) p' ^ " </NO>\n" ^ 
walther@60258
   180
        (indt (i+j)) ^ "<CONTENTREF> " ^ Thy_Write.theID2guh theID' ^ " </CONTENTREF>\n" ^
walther@60258
   181
        (nodes (i+j) (Pos.lev_dn p') theID' ns) ^ 
walther@60258
   182
        (indt i) ^ "</NODE>\n"
walther@60258
   183
        end
walther@60258
   184
    and nodes _ _ _ [] = ""
walther@60258
   185
      | nodes i p theID (n :: ns) = (node i p theID n) ^ (nodes i (Pos.lev_on p) theID ns);
walther@60258
   186
  in nodes j [0] [] h end;
walther@60258
   187
\<close>
walther@60258
   188
walther@59874
   189
(**)end(**)