src/Tools/isac/Specify/p-spec.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 11 Jan 2023 09:23:18 +0100
changeset 60649 b2ff1902420f
parent 60590 35846e25713e
child 60653 fff1c0f0a9e7
permissions -rw-r--r--
eliminate use of Thy_Info 12: prep.arg. ctxt in TermC, UnparseC
walther@59822
     1
(* Title:  Specify/input-calchead.sml
walther@59822
     2
   Author: Walther Neuper
walther@59822
     3
   (c) due to copyright terms
walther@59960
     4
walther@60111
     5
This will be dropped at switch to Isabelle/PIDE .
walther@59960
     6
*)
walther@59822
     7
walther@59984
     8
signature PRESENTATION_SPECIFICATION =
walther@59822
     9
sig
walther@60132
    10
  type record
walther@59995
    11
(*/------- rename -------\*)
walther@59960
    12
  datatype iitem =
walther@59960
    13
      Find of TermC.as_string list
walther@59960
    14
    | Given of TermC.as_string list
walther@59960
    15
    | Relate of TermC.as_string list
walther@59822
    16
  type imodel = iitem list
walther@59976
    17
  type icalhd = Pos.pos' * TermC.as_string * imodel * Pos.pos_ * References.T
walther@59995
    18
  val empty: icalhd
walther@60097
    19
  val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * SpecificationC.T
wenzelm@60223
    20
\<^isac_test>\<open>
walther@59977
    21
  val appl_add': ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
walther@59977
    22
    string * TermC.as_string -> I_Model.single
walther@59977
    23
  val appl_adds: ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
walther@59977
    24
    (string * TermC.as_string) list -> I_Model.T
walther@59977
    25
  val eq7: ''a * ''b -> ''a * (''b * 'c) -> bool
walther@59977
    26
  val filter_sep: ('a -> bool) -> 'a list -> 'a list * 'a list
walther@59977
    27
  val fstr2itm_: theory -> (''a * (term * term)) list -> ''a * string ->
walther@59977
    28
    int list * bool * ''a * I_Model.feedback (*I_Model.single'*)
walther@59977
    29
  val imodel2fstr: iitem list -> (string * TermC.as_string) list
walther@59977
    30
  val is_Par: 'a * 'b * 'c * 'd * I_Model.feedback -> bool (*I_Model.T?*)
walther@59977
    31
  val is_e_ts: term list -> bool
walther@59977
    32
  val itms2fstr: I_Model.single -> string * string
walther@59977
    33
  val par2fstr: I_Model.single -> string * TermC.as_string
walther@59977
    34
  val parsitm: theory -> I_Model.single -> I_Model.single
walther@59977
    35
  val unknown_expl: ThyC.id -> (''a * (term * term)) list -> (*Model_Pattern.T?*)
walther@59977
    36
    (''a * string) list ->
walther@59977
    37
      (int * int list * bool * ''a * Model_Def.i_model_feedback) list (*I_Model.T?*)
wenzelm@60223
    38
\<close>
walther@59995
    39
(*\------- rename -------/*)
walther@59822
    40
end
walther@59822
    41
walther@59977
    42
(**)
walther@59984
    43
structure P_Spec(**): PRESENTATION_SPECIFICATION(**) =
walther@59822
    44
struct
walther@59977
    45
(**)
walther@59822
    46
walther@60132
    47
  type record = {thy_id: ThyC.id, pbl_id: Problem.id,           (* headline of Problem *)
walther@60132
    48
    givens: TermC.as_string list, wheres: TermC.as_string list, (* Model.T             *)
walther@60132
    49
      find: TermC.as_string, relates: TermC.as_string list,
walther@60154
    50
    rthy_id: ThyC.id, rpbl_id: Problem.id, rmet_id: MethodC.id}  (* References.T        *)
walther@60132
    51
walther@59822
    52
fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
walther@59822
    53
walther@59982
    54
(** handle an input P_Specific'action **)
walther@59822
    55
walther@59822
    56
datatype iitem = 
walther@59865
    57
  Given of TermC.as_string list
walther@59977
    58
(*Where is still not input*) 
walther@59865
    59
| Find  of TermC.as_string list
walther@59865
    60
| Relate  of TermC.as_string list
walther@59822
    61
walther@59822
    62
type imodel = iitem list
walther@59822
    63
walther@59822
    64
type icalhd =
walther@59977
    65
  Pos.pos' *         (* the position in Ctree              *) 
walther@59977
    66
  TermC.as_string *  (* the headline shown on Calc.T       *)
walther@59977
    67
  imodel *           (* the model                          *)
walther@60154
    68
  Pos.pos_ *         (* model belongs to Problem or MethodC *)
walther@59977
    69
  References.T;      (* into Know_Store                    *)
walther@59995
    70
val empty = (Pos.e_pos', "", [Given [""]], Pos.Pbl, References.empty)
walther@59822
    71
walther@59822
    72
(* re-parse itms with a new thy and prepare for checking with ori list *)
walther@59943
    73
fun parsitm dI (itm as (i, v, _, f, I_Model.Cor ((d, ts), _))) =
walther@60265
    74
    (case \<^try>\<open>
walther@60265
    75
        let val t = Input_Descript.join (d, ts)
walther@60265
    76
          val _ = (UnparseC.term_in_thy dI t)
walther@60265
    77
          (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
walther@60265
    78
        in itm end\<close> of
walther@60265
    79
      SOME x => x
walther@60265
    80
    | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
    81
  | parsitm dI (i, v, b, f, I_Model.Syn str) =
walther@60265
    82
    (case \<^try>\<open>
walther@60417
    83
        let val _ = TermC.parseNEW'' dI str
walther@60265
    84
        in (i, v, b ,f, I_Model.Par str) end\<close> of
walther@60265
    85
      SOME x => x
walther@60265
    86
    | NONE => (i, v, b, f, I_Model.Syn str))
walther@59943
    87
  | parsitm dI (i, v, b, f, I_Model.Typ str) =
walther@60265
    88
    (case \<^try>\<open>
walther@60417
    89
        let val _ = TermC.parseNEW'' dI str
walther@60265
    90
         in (i, v, b, f, I_Model.Par str) end\<close> of
walther@60265
    91
      SOME x => x
walther@60265
    92
    | NONE => (i, v, b, f, I_Model.Syn str))
walther@59943
    93
  | parsitm dI (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
walther@60265
    94
    (case \<^try>\<open>
walther@60265
    95
        let val t = Input_Descript.join (d,ts);
walther@60265
    96
	        val _ = UnparseC.term_in_thy dI t;
walther@60265
    97
        (*this    ^ should raise the exn on unability of re-parsing dts*)
walther@60265
    98
        in itm end\<close> of
walther@60265
    99
      SOME x => x
walther@60265
   100
    | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
   101
  | parsitm dI (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
walther@60265
   102
    (case \<^try>\<open>
walther@60265
   103
        let val t = Input_Descript.join (d,ts);
walther@60265
   104
	        val _ = UnparseC.term_in_thy dI t;
walther@60265
   105
        (*this    ^ should raise the exn on unability of re-parsing dts*)
walther@60265
   106
        in itm end\<close> of
walther@60265
   107
      SOME x => x
walther@60265
   108
    | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
   109
  | parsitm dI (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
walther@60265
   110
    (case \<^try>\<open>
walther@60265
   111
        let val t = d $ t';
walther@60265
   112
	        val _ = UnparseC.term_in_thy dI t;
walther@60265
   113
        (*this    ^ should raise the exn on unability of re-parsing dts*)
walther@60265
   114
        in itm end\<close> of
walther@60265
   115
      SOME x => x
walther@60265
   116
    | NONE => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
   117
  | parsitm dI (itm as (_, _, _, _, I_Model.Par _)) = 
walther@60360
   118
    raise ERROR ("parsitm (" ^ I_Model.single_to_string (Proof_Context.init_global dI) itm ^
walther@60360
   119
      "): Par should be internal");
walther@59822
   120
walther@59822
   121
(*separate a list to a pair of elements that do NOT satisfy the predicate,
walther@59822
   122
 and of elements that satisfy the predicate, i.e. (false, true)*)
walther@59822
   123
fun filter_sep pred xs =
walther@59822
   124
  let
walther@59822
   125
    fun filt ab [] = ab
walther@59822
   126
      | filt (a, b) (x :: xs) =
walther@59822
   127
        if pred x 
walther@59822
   128
			  then filt (a, b @ [x]) xs 
walther@59822
   129
			  else filt (a @ [x], b) xs
walther@59822
   130
  in filt ([], []) xs end;
walther@59943
   131
fun is_Par (_, _, _, _, I_Model.Par _) = true
walther@59822
   132
  | is_Par _ = false;
walther@59822
   133
walther@59822
   134
fun is_e_ts [] = true
wenzelm@60309
   135
  | is_e_ts [Const (\<^const_name>\<open>Nil\<close>, _)] = true
walther@59822
   136
  | is_e_ts _ = false;
walther@59822
   137
walther@59822
   138
(* WN.9.11.03 copied from fun appl_add *)
Walther@60586
   139
fun appl_add' dI oris model pbt (sel, ct) = 
walther@59822
   140
  let 
walther@60360
   141
     val ctxt = ThyC.get_theory dI |> Proof_Context.init_global;
walther@59822
   142
  in
walther@59822
   143
    case TermC.parseNEW ctxt ct of
walther@59943
   144
	    NONE => (0, [], false, sel, I_Model.Syn ct)
walther@59822
   145
	  | SOME t =>
Walther@60471
   146
	    (case O_Model.contains ctxt sel oris t of
walther@59822
   147
        ("", ori', all) =>
Walther@60586
   148
          (case I_Model.is_notyet_input ctxt model all ori' pbt of
walther@59822
   149
            ("",itm)  => itm
walther@59962
   150
          | (msg,_) => raise ERROR ("appl_add': " ^ msg))
walther@59822
   151
      | (_, (i, v, _, d, ts), _) =>
walther@59822
   152
        if is_e_ts ts
walther@59943
   153
        then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
walther@59943
   154
        else (i, v, false, sel, I_Model.Sup (d, ts)))
walther@59822
   155
   end
walther@59822
   156
Walther@60422
   157
(* generate preliminary itm_ from a string (with field "#Given" etc.) *)
walther@59822
   158
fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
walther@59822
   159
fun fstr2itm_ thy pbt (f, str) =
walther@59822
   160
  let
Walther@60422
   161
    val topt = TermC.parseNEW (Proof_Context.init_global thy) str
walther@59822
   162
  in
walther@59822
   163
    case topt of
walther@59943
   164
      NONE => ([], false, f, I_Model.Syn str)
walther@59822
   165
    | SOME ct => 
walther@59822
   166
	    let
Walther@60422
   167
	      val (d, ts) = Input_Descript.split ct
walther@59822
   168
	      val popt = find_first (eq7 (f, d)) pbt
walther@59822
   169
	    in
walther@59822
   170
	      case popt of
walther@59943
   171
	        NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
walther@59943
   172
	      | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts))) 
walther@59822
   173
	    end
walther@59822
   174
  end
walther@59822
   175
walther@59822
   176
(*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
walther@59822
   177
fun unknown_expl dI pbt selcts =
walther@59822
   178
  let
Walther@60649
   179
    val thy = Know_Store.get_via_last_thy dI
walther@59822
   180
    val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
Walther@60472
   181
    val its = O_Model.add_enumerate its_ 
walther@59822
   182
  in map flattup2 its end
walther@59822
   183
walther@59822
   184
(* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
walther@59822
   185
   appl_add': generate 1 item 
Walther@60471
   186
   appl_add' . contains: parse, get data from oris (vats, all (elems if list)..)
walther@59822
   187
   appl_add' . is_notyet_input: compare with items in model already input
walther@59822
   188
   insert_ppc': insert this 1 item*)
walther@59822
   189
fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
walther@59822
   190
    (*already present itms in model are being overwritten*)
Walther@60586
   191
  | appl_adds _ _ model _ [] = model
Walther@60586
   192
  | appl_adds dI oris model pbt (selct :: ss) =
Walther@60586
   193
    let val itm = appl_add' dI oris model pbt selct;
Walther@60586
   194
    in appl_adds dI oris (I_Model.add itm model) pbt ss end
walther@59822
   195
walther@59943
   196
fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
walther@59962
   197
  | par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
walther@59953
   198
fun itms2fstr (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
walther@59943
   199
  | itms2fstr (_, _, _, f, I_Model.Syn str) = (f, str)
walther@59943
   200
  | itms2fstr (_, _, _, f, I_Model.Typ str) = (f, str)
walther@59953
   201
  | itms2fstr (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
walther@59953
   202
  | itms2fstr (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
walther@59943
   203
  | itms2fstr (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term (d $ t))
walther@59943
   204
  | itms2fstr (itm as (_, _, _, _, I_Model.Par _)) = 
walther@59962
   205
    raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm ^ "): Par should be internal");
walther@59822
   206
walther@59822
   207
fun imodel2fstr iitems = 
walther@59822
   208
  let 
walther@59822
   209
    fun xxx is [] = is
walther@59822
   210
	    | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
walther@59822
   211
	    | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
walther@59822
   212
	    | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
walther@59822
   213
  in xxx [] iitems end;
walther@59822
   214
walther@59822
   215
(* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
walther@59822
   216
fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
walther@59822
   217
    let
walther@59822
   218
		  val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
walther@59822
   219
		    Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
walther@59822
   220
		      spec = sspec as (sdI, spI, smI), probl, meth, ...}
walther@59822
   221
        => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
walther@59962
   222
      | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
Walther@60556
   223
      val ctxt = Proof_Context.init_global (ThyC.get_theory sdI)
Walther@60567
   224
    in if CAS_Cmd.is_from hdf fmz then the (CAS_Cmd.input (TermC.parse ctxt hdf)) 
walther@59822
   225
       else        (*hacked WN0602 ~~~            ~~~~~~~~~,   ..dropped !*)
walther@59822
   226
         let val (pos_, pits, mits) = 
walther@59822
   227
	         if dI <> sdI
walther@59881
   228
	         then let val its = map (parsitm (ThyC.get_theory dI)) probl;
walther@59822
   229
			            val (its, trms) = filter_sep is_Par its;
Walther@60556
   230
			            val pbt =
Walther@60585
   231
			              (#model o Problem.from_store ctxt) (#2 (References.select_input ospec sspec))
walther@59822
   232
		            in (Pos.Pbl, appl_adds dI oris its pbt  (map par2fstr trms), meth) end 
walther@59822
   233
           else
walther@59822
   234
             if pI <> spI 
walther@59822
   235
	           then if pI = snd3 ospec then (Pos.Pbl, probl, meth) 
walther@59822
   236
                  else
Walther@60585
   237
		                let val pbt = (#model o Problem.from_store ctxt) pI
Walther@60494
   238
			                val dI' = #1 (References.select_input ospec spec)
Walther@60470
   239
			                val oris =
Walther@60470
   240
			                  if pI = #2 ospec then oris 
Walther@60470
   241
				                else O_Model.init (ThyC.get_theory "Isac_Knowledge") fmz_ pbt(* |> #1*);
walther@59822
   242
		                in (Pos.Pbl, appl_adds dI' oris probl pbt 
walther@59822
   243
				              (map itms2fstr probl), meth) end 
walther@59822
   244
             else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
Walther@60586
   245
	                then let val met = (#model o MethodC.from_store ctxt) mI
walther@59988
   246
		                     val mits = I_Model.complete oris probl meth met
walther@59822
   247
		                   in if foldl and_ (true, map #3 mits)
walther@59822
   248
		                      then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits) 
walther@59822
   249
		                   end 
Walther@60494
   250
                  else (Pos.Pbl, appl_adds (#1 (References.select_input ospec spec)) oris [(*!!!*)]
Walther@60585
   251
			                  ((#model o Problem.from_store ctxt)
Walther@60557
   252
			                    (#2 (References.select_input ospec spec)))
Walther@60557
   253
			                      (imodel2fstr imodel), meth)
walther@59822
   254
	         val pt = Ctree.update_spec pt p spec;
walther@59822
   255
         in if pos_ = Pos.Pbl
Walther@60557
   256
	          then 
Walther@60557
   257
	            let 
Walther@60585
   258
	              val {where_rls, where_,...} = Problem.from_store ctxt 
Walther@60557
   259
	                (#2 (References.select_input ospec spec))
Walther@60590
   260
		            val (_, where_) = Pre_Conds.check ctxt where_rls where_ pits 0
Walther@60557
   261
	            in (Ctree.update_pbl pt p pits,
Walther@60586
   262
		                 (SpecificationC.is_complete' pits where_ spec, Pos.Pbl, hdf', pits, where_, spec)) 
Walther@60557
   263
              end
Walther@60557
   264
	          else 
Walther@60557
   265
	            let 
Walther@60586
   266
	              val {where_rls,where_,...} = MethodC.from_store ctxt 
Walther@60557
   267
	                (#3 (References.select_input ospec spec))
Walther@60590
   268
		            val (_, where_) = Pre_Conds.check ctxt where_rls where_ mits 0
Walther@60557
   269
	            in (Ctree.update_met pt p mits,
Walther@60586
   270
		                  (SpecificationC.is_complete' mits where_ spec, Pos.Met, hdf', mits, where_, spec))
Walther@60557
   271
              end
walther@59822
   272
         end 
walther@59822
   273
    end
walther@59962
   274
  | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."
walther@59822
   275
walther@60132
   276
(**)end (**)