src/Tools/isac/BridgeLibisabelle/datatypes.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 11 Jan 2023 09:23:18 +0100
changeset 60649 b2ff1902420f
parent 60642 33977a136810
child 60664 ed6f9e67349d
permissions -rw-r--r--
eliminate use of Thy_Info 12: prep.arg. ctxt in TermC, UnparseC
wneuper@59250
     1
(* convert sml-datatypes to xml for libisabelle and for kbase.
wneuper@59250
     2
   authors: Walther Neuper 2003, 2016
neuper@37906
     3
   (c) due to copyright terms
walther@60257
     4
walther@60258
     5
This code remains in order to
walther@60258
     6
keep tests of interaction Java-frontend <-> Kernel running.
neuper@37906
     7
*)
neuper@37906
     8
wneuper@59250
     9
(*** convert sml-datatypes to xml for kbase ***)
wneuper@59250
    10
wneuper@59250
    11
fun filterpbl str =
wneuper@59250
    12
  let fun filt [] = []
wneuper@59250
    13
        | filt ((s, (t1, t2)) :: ps) = 
wneuper@59250
    14
	  if str = s then (t1 $ t2) :: filt ps else filt ps
wneuper@59250
    15
  in filt end;
wneuper@59250
    16
fun pattern2xml j p where_ =
wneuper@59250
    17
    (case filterpbl "#Given" p of
wneuper@59250
    18
	[] =>  (indt j) ^ "<GIVEN>  </GIVEN>\n"
wneuper@59250
    19
(* val gis = filterpbl "#Given" p;
wneuper@59250
    20
   *)
wneuper@59250
    21
      | gis => (indt j) ^ "<GIVEN>\n" ^ terms2xml' j gis ^
wneuper@59250
    22
	       (indt j) ^ "</GIVEN>\n")
wneuper@59250
    23
    ^ 
wneuper@59250
    24
    (case where_ of
wneuper@59250
    25
	 [] =>  (indt j) ^ "<WHERE>  </WHERE>\n"
wneuper@59250
    26
       | whs => (indt j) ^ "<WHERE>\n" ^ terms2xml' j whs ^
wneuper@59250
    27
		(indt j) ^ "</WHERE>\n")
wneuper@59250
    28
    ^ 
wneuper@59250
    29
    (case filterpbl "#Find" p of
wneuper@59250
    30
	 [] =>  (indt j) ^ "<FIND>  </FIND>\n"
wneuper@59250
    31
       | fis => (indt j) ^ "<FIND>\n" ^ terms2xml' j fis ^
wneuper@59250
    32
		(indt j) ^ "</FIND>\n")
wneuper@59250
    33
    ^ 
wneuper@59250
    34
    (case filterpbl "#Relate" p of
wneuper@59250
    35
	 [] =>  (indt j) ^ "<RELATE>  </RELATE>\n"
wneuper@59250
    36
       | res => (indt j) ^ "<RELATE>\n" ^ terms2xml' j res ^
wneuper@59250
    37
		(indt j) ^ "</RELATE>\n");
wneuper@59250
    38
wneuper@59250
    39
fun prepa12xml j (terms, term) =
wneuper@59250
    40
    indt j ^"<PREPAT>\n"^
wneuper@59250
    41
    indt (j+i) ^"<PRECONDS>\n"^
wneuper@59250
    42
    terms2xml (j+2*i) terms ^
wneuper@59250
    43
    indt (j+i) ^"</PRECONDS>\n"^
wneuper@59250
    44
    indt (j+i) ^"<PATTERN>\n"^
wneuper@59250
    45
    term2xml (j+2*i) term ^
wneuper@59250
    46
    indt (j+i) ^"</PATTERN>\n"^
wneuper@59405
    47
    indt j ^"</PREPAT>\n";
wneuper@59250
    48
fun prepat2xml _ [] = ""
wneuper@59405
    49
  | prepat2xml j (p::ps) = prepa12xml j p ^ prepat2xml j ps;
wneuper@59250
    50
wneuper@59250
    51
wneuper@59250
    52
(*** convert sml-datatypes to xml for libisabelle ***)
wneuper@59250
    53
walther@60258
    54
(** general types: lists, etc **)
neuper@37906
    55
wneuper@59124
    56
fun xml_of_bool b = XML.Elem (("BOOL", []), [XML.Text (bool2str b)])
wneuper@59156
    57
wneuper@59124
    58
fun xml_of_str str = XML.Elem (("STRING", []), [XML.Text str])
wneuper@59124
    59
fun xml_of_strs strs = XML.Elem (("STRINGLIST", []), map xml_of_str strs)
wneuper@59124
    60
wneuper@59124
    61
fun xml_of_int i = XML.Elem (("INT", []), [XML.Text (string_of_int i)])
wneuper@59250
    62
fun xml_of_ints is =
wneuper@59124
    63
  XML.Elem (("INTLIST", []), map xml_of_int is)
wneuper@59124
    64
neuper@37906
    65
(** isac datatypes **)
walther@60258
    66
wneuper@59249
    67
fun xml_of_pos tag (is, pp) =
wneuper@59124
    68
  XML.Elem ((tag, []), [
wneuper@59124
    69
    xml_of_ints is,
walther@59694
    70
    XML.Elem (("POS", []), [XML.Text (Pos.pos_2str pp)])])
wneuper@59124
    71
walther@59747
    72
fun xml_of_auto (Solve.Steps i) = 
wneuper@59124
    73
      XML.Elem (("AUTO", []), [XML.Text "Step", XML.Text (string_of_int i)])
walther@60258
    74
  | xml_of_auto Solve.CompleteModel = XML.Elem (("AUTO", []), [XML.Text "CompleteModel"])
walther@60258
    75
  | xml_of_auto Solve.CompleteCalcHead = XML.Elem (("AUTO", []), [XML.Text "CompleteCalcHead"])
walther@60258
    76
  | xml_of_auto Solve.CompleteToSubpbl = XML.Elem (("AUTO", []), [XML.Text "CompleteToSubpbl"])
walther@60258
    77
  | xml_of_auto Solve.CompleteSubpbl = XML.Elem (("AUTO", []), [XML.Text "CompleteSubpbl"])
walther@60258
    78
  | xml_of_auto Solve.CompleteCalc = XML.Elem (("AUTO", []), [XML.Text "CompleteCalc"])
neuper@37906
    79
neuper@37906
    80
fun filterpbl str =
neuper@37906
    81
  let fun filt [] = []
neuper@37906
    82
        | filt ((s, (t1, t2)) :: ps) = 
neuper@37906
    83
	  if str = s then (t1 $ t2) :: filt ps else filt ps
neuper@37906
    84
  in filt end;
neuper@37906
    85
walther@59943
    86
fun xml_of_itm_ (I_Model.Cor (dts, _)) =
walther@59953
    87
    XML.Elem (("ITEM", [("status", "correct")]), [xml_of_term (Input_Descript.join' dts)])
walther@59943
    88
  | xml_of_itm_ (I_Model.Syn c) =
wneuper@59127
    89
    XML.Elem (("ITEM", [("status", "syntaxerror")]), [XML.Text c])
walther@59943
    90
  | xml_of_itm_ (I_Model.Typ c) =
wneuper@59127
    91
    XML.Elem (("ITEM", [("status", "typeerror")]), [XML.Text c])
wneuper@59127
    92
  (*type item also has 'False of cterm' set in preconds2xml WN 050618*)
walther@59943
    93
  | xml_of_itm_ (I_Model.Inc (dts, _)) = 
walther@59953
    94
    XML.Elem (("ITEM", [("status", "incomplete")]), [xml_of_term (Input_Descript.join' dts)])
walther@59943
    95
  | xml_of_itm_ (I_Model.Sup dts) = 
walther@59953
    96
    XML.Elem (("ITEM", [("status", "superfluous")]), [xml_of_term (Input_Descript.join' dts)])
walther@59943
    97
  | xml_of_itm_ (I_Model.Mis (d, pid)) = 
wneuper@59127
    98
    XML.Elem (("ITEM", [("status", "missing")]), [xml_of_term (d $ pid)])
walther@59962
    99
  | xml_of_itm_ _ = raise ERROR "xml_of_itm_: wrong argument"
wneuper@59127
   100
fun xml_of_itms itms =
wneuper@59127
   101
  let 
wneuper@59127
   102
    fun extract (_, _, _, _, itm_) = itm_
wneuper@59127
   103
  in map (xml_of_itm_ o extract) itms end
neuper@37906
   104
wneuper@59127
   105
fun xml_of_precond (status, term) =
wneuper@59127
   106
    XML.Elem (("ITEM", [("status", if status then "correct" else "false")]), [xml_of_term term])
wneuper@59127
   107
fun xml_of_preconds ps = map xml_of_precond ps
neuper@37906
   108
wneuper@59127
   109
fun xml_of_model itms where_ =
wneuper@59127
   110
  let
wneuper@59127
   111
    fun eq str (_, _, _,field, _) = str = field
wneuper@59127
   112
  in 
wneuper@59127
   113
    XML.Elem (("MODEL", []), [
wneuper@59127
   114
      XML.Elem (("GIVEN", []), 
wneuper@59127
   115
        filter (eq "#Given") itms |> xml_of_itms),
wneuper@59127
   116
      XML.Elem (("WHERE", []), 
wneuper@59127
   117
        xml_of_preconds where_),
wneuper@59127
   118
      XML.Elem (("FIND", []), 
wneuper@59127
   119
        filter (eq "#Find") itms |> xml_of_itms),
wneuper@59127
   120
      XML.Elem (("RELATE", []), 
wneuper@59127
   121
        filter (eq "#Relate") itms |> xml_of_itms)])
wneuper@59127
   122
  end 
neuper@37906
   123
wneuper@59124
   124
fun xml_of_spec (thyID, pblID, metID) =
wneuper@59124
   125
  XML.Elem (("SPECIFICATION", []), [
wneuper@59124
   126
    XML.Elem (("THEORYID", []), [XML.Text thyID]),
wneuper@59124
   127
    XML.Elem (("PROBLEMID", []), [xml_of_strs pblID]),
wneuper@59124
   128
    XML.Elem (("METHODID", []), [xml_of_strs metID])])
neuper@37906
   129
wneuper@59148
   130
fun xml_of_variant (items, spec) = 
wneuper@59148
   131
  XML.Elem (("VARIANT", []), [xml_of_strs items, xml_of_spec spec])
wneuper@59148
   132
walther@59941
   133
fun xml_of_fmz [] = xml_of_fmz [Formalise.empty]
wneuper@59148
   134
  | xml_of_fmz vs = XML.Elem (("FORMALIZATION", []), map xml_of_variant vs)
wneuper@59141
   135
Walther@60586
   136
fun xml_of_modspec ((b, p_, head, gfr, where_, spec): SpecificationC.T) =
wneuper@59129
   137
  XML.Elem (("CALCHEAD", [("status", if b then "correct" else "incorrect")]), [
wneuper@59223
   138
    XML.Elem (("HEAD", []), [xml_of_term_NEW head]),
Walther@60586
   139
    xml_of_model gfr where_,
wneuper@59129
   140
    XML.Elem (("BELONGSTO", []), [
walther@60258
   141
      XML.Text (case p_ of Pos.Pbl => "Pbl" | Pos.Met => "Met" | _ => "Und")]),
wneuper@59129
   142
    xml_of_spec spec])
wneuper@59129
   143
Walther@60586
   144
fun xml_of_posmodspec ((p: Pos.pos', (b, p_, head, gfr, where_, spec): SpecificationC.T)) =
wneuper@59150
   145
  XML.Elem (("CALCHEAD", [("status", if b then "correct" else "incorrect")]), [
wneuper@59150
   146
    xml_of_pos "POSITION" p,
wneuper@59223
   147
    XML.Elem (("HEAD", []), [xml_of_term_NEW head]),
Walther@60586
   148
    xml_of_model gfr where_,
wneuper@59150
   149
    XML.Elem (("BELONGSTO", []), [
walther@60258
   150
      XML.Text (case p_ of Pos.Pbl => "Pbl" | Pos.Met => "Met" | _ => "Und")]),
wneuper@59150
   151
    xml_of_spec spec])
neuper@37906
   152
wneuper@59134
   153
fun xml_of_sub (id, value) =
wneuper@59134
   154
  XML.Elem (("PAIR", []), [
wneuper@59134
   155
    XML.Elem (("VARIABLE", []), [xml_of_term id]),
wneuper@59134
   156
    XML.Elem (("VALUE", []), [xml_of_term value])])
walther@59911
   157
fun xml_of_subs (subs : Subst.input) =
Walther@60500
   158
  XML.Elem (("SUBSTITUTION", []), 
Walther@60574
   159
    map xml_of_sub (Tactic.subst_adapt_to_type (ThyC.id_to_ctxt "Isac_Knowledge") subs))
wneuper@59301
   160
fun xml_of_sube sube =
Walther@60500
   161
  XML.Elem (("SUBSTITUTION", []), 
Walther@60649
   162
    map xml_of_sub (Subst.T_from_string_eqs (Know_Store.get_via_last_thy "Isac_Knowledge") sube))
wneuper@59134
   163
wneuper@59250
   164
fun thm''2xml j (thm : thm) =
wneuper@59250
   165
    indt j ^ "<THEOREM>\n" ^
walther@59876
   166
    indt (j+i) ^ "<ID> " ^ ThmC.id_of_thm thm ^ " </ID>\n"^
wneuper@59250
   167
    term2xml j (Thm.prop_of thm) ^ "\n" ^
wneuper@59405
   168
    indt j ^ "</THEOREM>\n";
wneuper@59405
   169
fun xml_of_thm' (ID, form) =
wneuper@59134
   170
  XML.Elem (("THEOREM", []), [
wneuper@59134
   171
    XML.Elem (("ID", []), [XML.Text ID]),
wneuper@59158
   172
    XML.Elem (("FORMULA", []), [
wneuper@59158
   173
      XML.Text form])])           (* repair for MathML: use term instead String *)
wneuper@59250
   174
(* at the front-end theorems can be shown by their term, so term is transported isac-java <--- ME *)
wneuper@59405
   175
fun xml_of_thm'' (ID, thm) =
wneuper@59252
   176
  XML.Elem (("THEOREM", []), [
wneuper@59252
   177
    XML.Elem (("ID", []), [XML.Text ID]),
wneuper@59252
   178
    XML.Elem (("FORMULA", []), [
walther@59868
   179
      XML.Text ((UnparseC.term o Thm.prop_of) thm)])])           (* repair for MathML: use term instead String *)
wneuper@59252
   180
walther@59878
   181
fun xml_of_src Rule.Empty_Prog =
wneuper@59176
   182
    XML.Elem (("NOCODE", []), [XML.Text "empty"])
wneuper@59416
   183
  | xml_of_src (Rule.Prog term) =
wneuper@59176
   184
    XML.Elem (("CODE", []), [
walther@59878
   185
      if term = TermC.empty then xml_of_src Rule.Empty_Prog
wneuper@59393
   186
      else xml_of_term (TermC.inst_abs term)])
wneuper@59416
   187
  | xml_of_src (Rule.Rfuns _) =
wneuper@59176
   188
    XML.Elem (("NOCODE", []), [XML.Text "reverse rewrite functions"])
neuper@37906
   189
wneuper@59249
   190
(*.convert a tactic into xml-format .*)
wneuper@59571
   191
fun xml_of_tac (Tactic.Subproblem (dI, pI)) =
wneuper@59134
   192
    XML.Elem (("SUBPROBLEMTACTIC", [("name", "Subproblem")]), [
wneuper@59134
   193
      XML.Elem (("THEORY", []), [XML.Text dI]),
wneuper@59134
   194
      XML.Elem (("PROBLEM", []), [xml_of_strs pI])])
wneuper@59571
   195
  | xml_of_tac (Tactic.Substitute cterms) =
walther@59912
   196
    (*Substitute: sube -> tac; Subst.string_eqs_empty: cterm' list; UNCLEAR HOW TO INPUT ON FRONTEND*)
wneuper@59162
   197
    XML.Elem (("STRINGLISTTACTIC", [("name", "Substitute")]), [xml_of_sube cterms])
wneuper@59160
   198
    (*----- Rewrite* -----------------------------------------------------*)
wneuper@59571
   199
  | xml_of_tac (Tactic.Rewrite thm'') =
wneuper@59250
   200
    XML.Elem (("REWRITETACTIC", [("name", "Rewrite")]), [xml_of_thm'' thm''])
wneuper@59571
   201
  | xml_of_tac (Tactic.Rewrite_Inst (subs, thm'')) =
wneuper@59134
   202
    XML.Elem (("REWRITEINSTTACTIC", [("name", "Rewrite_Inst")]), (
wneuper@59134
   203
      xml_of_subs subs ::
wneuper@59250
   204
      xml_of_thm'' thm'' :: []))
wneuper@59571
   205
  | xml_of_tac (Tactic.Rewrite_Set rls') =
wneuper@59134
   206
    XML.Elem (("REWRITESETTACTIC", [("name", "Rewrite_Set")]), [XML.Text rls'])
wneuper@59571
   207
  | xml_of_tac (Tactic.Rewrite_Set_Inst (subs, rls')) =
wneuper@59161
   208
    XML.Elem (("REWRITESETINSTTACTIC", [("name", "Rewrite_Set_Inst")]), ([
wneuper@59161
   209
      xml_of_subs subs,
wneuper@59161
   210
      XML.Elem (("RULESET", []), [XML.Text rls'])]))
wneuper@59161
   211
    (*----- FORMTACTIC ---------------------------------------------------*)
wneuper@59571
   212
  | xml_of_tac (Tactic.Add_Find ct) =
wneuper@59161
   213
    XML.Elem (("FORMTACTIC", [("name", "Add_Find")]), [xml_of_cterm ct])
wneuper@59571
   214
  | xml_of_tac (Tactic.Add_Given ct) =
wneuper@59161
   215
    XML.Elem (("FORMTACTIC", [("name", "Add_Given")]), [xml_of_cterm ct])
wneuper@59571
   216
  | xml_of_tac (Tactic.Add_Relation ct) =
wneuper@59161
   217
    XML.Elem (("FORMTACTIC", [("name", "Add_Relation")]), [xml_of_cterm ct])
wneuper@59571
   218
  | xml_of_tac (Tactic.Check_elementwise ct) =
wneuper@59161
   219
    XML.Elem (("FORMTACTIC", [("name", "Check_elementwise")]), [xml_of_cterm ct])
wneuper@59571
   220
  | xml_of_tac (Tactic.Take ct) =
wneuper@59161
   221
    XML.Elem (("FORMTACTIC", [("name", "Take")]), [xml_of_cterm ct])
wneuper@59160
   222
    (*----- SIMPLETACTIC -------------------------------------------------*)
wneuper@59571
   223
  | xml_of_tac (Tactic.Calculate opstr) =
wneuper@59160
   224
    XML.Elem (("SIMPLETACTIC", [("name", "Calculate")]), [XML.Text opstr])
wneuper@59571
   225
  | xml_of_tac (Tactic.Or_to_List) =
wneuper@59160
   226
    XML.Elem (("SIMPLETACTIC", [("name", "Or_to_List")]), [(*//////////*)])
wneuper@59571
   227
  | xml_of_tac (Tactic.Specify_Theory ct) =
wneuper@59160
   228
    XML.Elem (("SIMPLETACTIC", [("name", "Specify_Theory")]), [XML.Text ct])
wneuper@59160
   229
    (*----- STRINGLISTTACTIC ---------------------------------------------*)
wneuper@59571
   230
  | xml_of_tac (Tactic.Apply_Method mI) =
wneuper@59160
   231
    XML.Elem (("STRINGLISTTACTIC", [("name", "Apply_Method")]), [xml_of_strs mI])
wneuper@59571
   232
  | xml_of_tac (Tactic.Check_Postcond pI) =
wneuper@59160
   233
    XML.Elem (("STRINGLISTTACTIC", [("name", "Check_Postcond")]), [xml_of_strs pI])
walther@60258
   234
  | xml_of_tac Tactic.Model_Problem =
wneuper@59160
   235
    XML.Elem (("STRINGLISTTACTIC", [("name", "Model_Problem")]), [])
wneuper@59571
   236
  | xml_of_tac (Tactic.Refine_Tacitly pI) =
wneuper@59160
   237
    XML.Elem (("STRINGLISTTACTIC", [("name", "Refine_Tacitly")]), [xml_of_strs pI])
wneuper@59571
   238
  | xml_of_tac (Tactic.Specify_Method ct) =
wneuper@59160
   239
    XML.Elem (("STRINGLISTTACTIC", [("name", "Specify_Method")]), [xml_of_strs ct])
wneuper@59571
   240
  | xml_of_tac (Tactic.Specify_Problem ct) =
wneuper@59160
   241
    XML.Elem (("STRINGLISTTACTIC", [("name", "Specify_Problem")]), [xml_of_strs ct])
Walther@60628
   242
  | xml_of_tac tac =
Walther@60628
   243
    let
Walther@60628
   244
      val ctxt = Proof_Context.init_global (Know_Store.get_via_last_thy "Isac_Knowledge")
Walther@60628
   245
    in
Walther@60628
   246
      raise ERROR ("xml_of_tac: not impl. for " ^ Tactic.input_to_string ctxt tac)
Walther@60628
   247
    end;
wneuper@59160
   248
Walther@60422
   249
val e_pblterm = TermC.parseNEW'' @{theory Prog_Tac}
walther@59997
   250
		    ("Problem (" ^ ThyC.id_empty ^ ", " ^ strs2str' Problem.id_empty ^ ")");
neuper@37906
   251
neuper@37906
   252
(*WN051224 minimal adaption to exporting Formulae _only_ by getFormulaeFromTo*)
wneuper@59516
   253
fun xml_of_posterm (p, t, _) =
wneuper@59225
   254
  let val input_request = Free ("________________________________________________", dummyT)
wneuper@59225
   255
  in 
wneuper@59225
   256
    XML.Elem (("CALCFORMULA", []),
wneuper@59225
   257
      [xml_of_pos "POSITION" p,
wneuper@59225
   258
       if t = e_pblterm (*headline in pbl is e_ <- _root_pbl for CAS-command*)
wneuper@59225
   259
       then xml_of_term_NEW input_request
wneuper@59225
   260
       else xml_of_term_NEW t])
wneuper@59225
   261
  end
neuper@37906
   262
wneuper@59133
   263
fun xml_of_asm_val (asm, vl) =
wneuper@59133
   264
  XML.Elem (("ASMEVALUATED", []),[
wneuper@59133
   265
    XML.Elem (("ASM", []), [xml_of_term asm]),
wneuper@59133
   266
    XML.Elem (("VALUE", []), [xml_of_term vl])])
neuper@37906
   267
Walther@60642
   268
fun xml_of_matchpbl (model_ok, _(*pI*), hdl, pbl, where_) =
wneuper@59172
   269
  XML.Elem (("CONTEXTDATA", []), [
Walther@60556
   270
    XML.Elem (("GUH", []), [(XML.Text "would-need-ctxt")(*XML.Text (Ptool.pblID2guh pI)*)]),
wneuper@59172
   271
    XML.Elem (("STATUS", []), [
wneuper@59172
   272
      XML.Text ((if model_ok then "correct" else "incorrect"))]),
wneuper@59223
   273
    XML.Elem (("HEAD", []), [xml_of_term_NEW hdl]),
Walther@60586
   274
    xml_of_model pbl where_])
wneuper@59172
   275
Walther@60586
   276
fun xml_of_matchmet (model_ok, pI, program, pbl, where_) =
wneuper@59172
   277
  XML.Elem (("CONTEXTDATA", []), [
walther@59974
   278
    XML.Elem (("GUH", []), [XML.Text (Ptool.metID2guh pI)]),
wneuper@59172
   279
    XML.Elem (("STATUS", []), [
wneuper@59172
   280
      XML.Text ((if model_ok then "correct" else "incorrect"))]),
Walther@60586
   281
    XML.Elem (("PROGRAM", []), [xml_of_src program]),
Walther@60586
   282
    xml_of_model pbl where_])
neuper@37906
   283
walther@59616
   284
fun xml_of_calcchanged calcid tag old del new =
wneuper@59131
   285
  XML.Elem ((tag, []),[
wneuper@59131
   286
    XML.Elem (("CALCID", []), [XML.Text (string_of_int calcid)]),
wneuper@59131
   287
    XML.Elem (("CALCCHANGED", []), [
wneuper@59131
   288
      xml_of_pos "UNCHANGED" old,
wneuper@59131
   289
      xml_of_pos "DELETED" del,
wneuper@59131
   290
      xml_of_pos "GENERATED" new])])
wneuper@59175
   291
wneuper@59175
   292
(* for checking output at Frontend *)
wneuper@59175
   293
fun cut_xml xml i = xml |> xmlstr 0 |> Symbol.explode |> curry take i |> implode