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