src/Tools/isac/Specify/p-spec.sml
author Walther Neuper <walther.neuper@jku.at>
Wed, 03 Jun 2020 13:57:22 +0200
changeset 60018 70a98f2b5754
parent 60000 0d8dd183cf3d
child 60097 0aa54181d7c9
permissions -rw-r--r--
unify Pre_Conds.check, partially
     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 (*/------- rename -------\*)
    11   datatype iitem =
    12       Find of TermC.as_string list
    13     | Given of TermC.as_string list
    14     | Relate of TermC.as_string list
    15   type imodel = iitem list
    16   type icalhd = Pos.pos' * TermC.as_string * imodel * Pos.pos_ * References.T
    17   val empty: icalhd
    18   val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * Specification.T
    19 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    20   (*  NONE *)
    21 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    22   val appl_add': ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
    23     string * TermC.as_string -> I_Model.single
    24   val appl_adds: ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
    25     (string * TermC.as_string) list -> I_Model.T
    26   val eq7: ''a * ''b -> ''a * (''b * 'c) -> bool
    27   val filter_sep: ('a -> bool) -> 'a list -> 'a list * 'a list
    28   val fstr2itm_: theory -> (''a * (term * term)) list -> ''a * string ->
    29     int list * bool * ''a * I_Model.feedback (*I_Model.single'*)
    30   val imodel2fstr: iitem list -> (string * TermC.as_string) list
    31   val is_Par: 'a * 'b * 'c * 'd * I_Model.feedback -> bool (*I_Model.T?*)
    32   val is_e_ts: term list -> bool
    33   val itms2fstr: I_Model.single -> string * string
    34   val par2fstr: I_Model.single -> string * TermC.as_string
    35   val parsitm: theory -> I_Model.single -> I_Model.single
    36   val unknown_expl: ThyC.id -> (''a * (term * term)) list -> (*Model_Pattern.T?*)
    37     (''a * string) list ->
    38       (int * int list * bool * ''a * Model_Def.i_model_feedback) list (*I_Model.T?*)
    39 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    40 (*\------- rename -------/*)
    41 end
    42 
    43 (**)
    44 structure P_Spec(**): PRESENTATION_SPECIFICATION(**) =
    45 struct
    46 (**)
    47 
    48 fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
    49 
    50 (** handle an input P_Specific'action **)
    51 
    52 datatype iitem = 
    53   Given of TermC.as_string list
    54 (*Where is still not input*) 
    55 | Find  of TermC.as_string list
    56 | Relate  of TermC.as_string list
    57 
    58 type imodel = iitem list
    59 
    60 type icalhd =
    61   Pos.pos' *         (* the position in Ctree              *) 
    62   TermC.as_string *  (* the headline shown on Calc.T       *)
    63   imodel *           (* the model                          *)
    64   Pos.pos_ *         (* model belongs to Problem or Method *)
    65   References.T;      (* into Know_Store                    *)
    66 val empty = (Pos.e_pos', "", [Given [""]], Pos.Pbl, References.empty)
    67 
    68 (* re-parse itms with a new thy and prepare for checking with ori list *)
    69 fun parsitm dI (itm as (i, v, _, f, I_Model.Cor ((d, ts), _))) =
    70     (let val t = Input_Descript.join (d, ts)
    71      val _ = (UnparseC.term_in_thy dI t)
    72      (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
    73     in itm end
    74     handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
    75   | parsitm dI (i, v, b, f, I_Model.Syn str) =
    76     (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
    77     in (i, v, b ,f, I_Model.Par str) end
    78     handle _ => (i, v, b, f, I_Model.Syn str))
    79   | parsitm dI (i, v, b, f, I_Model.Typ str) =
    80     (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
    81      in (i, v, b, f, I_Model.Par str) end
    82      handle _ => (i, v, b, f, I_Model.Syn str))
    83   | parsitm dI (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
    84     (let val t = Input_Descript.join (d,ts);
    85 	       val _ = UnparseC.term_in_thy dI t;
    86      (*this    ^ should raise the exn on unability of re-parsing dts*)
    87      in itm end
    88      handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
    89   | parsitm dI (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
    90     (let val t = Input_Descript.join (d,ts);
    91 	       val _ = UnparseC.term_in_thy dI t;
    92      (*this    ^ should raise the exn on unability of re-parsing dts*)
    93     in itm end
    94     handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
    95   | parsitm dI (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
    96     (let val t = d $ t';
    97 	       val _ = UnparseC.term_in_thy dI t;
    98      (*this    ^ should raise the exn on unability of re-parsing dts*)
    99     in itm end
   100     handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
   101   | parsitm dI (itm as (_, _, _, _, I_Model.Par _)) = 
   102     raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.to_ctxt dI) itm ^ "): Par should be internal");
   103 
   104 (*separate a list to a pair of elements that do NOT satisfy the predicate,
   105  and of elements that satisfy the predicate, i.e. (false, true)*)
   106 fun filter_sep pred xs =
   107   let
   108     fun filt ab [] = ab
   109       | filt (a, b) (x :: xs) =
   110         if pred x 
   111 			  then filt (a, b @ [x]) xs 
   112 			  else filt (a @ [x], b) xs
   113   in filt ([], []) xs end;
   114 fun is_Par (_, _, _, _, I_Model.Par _) = true
   115   | is_Par _ = false;
   116 
   117 fun is_e_ts [] = true
   118   | is_e_ts [Const ("List.list.Nil", _)] = true
   119   | is_e_ts _ = false;
   120 
   121 (* WN.9.11.03 copied from fun appl_add *)
   122 fun appl_add' dI oris ppc pbt (sel, ct) = 
   123   let 
   124      val ctxt = ThyC.get_theory dI |> ThyC.to_ctxt;
   125   in
   126     case TermC.parseNEW ctxt ct of
   127 	    NONE => (0, [], false, sel, I_Model.Syn ct)
   128 	  | SOME t =>
   129 	    (case O_Model.is_known ctxt sel oris t of
   130         ("", ori', all) =>
   131           (case I_Model.is_notyet_input ctxt ppc all ori' pbt of
   132             ("",itm)  => itm
   133           | (msg,_) => raise ERROR ("appl_add': " ^ msg))
   134       | (_, (i, v, _, d, ts), _) =>
   135         if is_e_ts ts
   136         then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
   137         else (i, v, false, sel, I_Model.Sup (d, ts)))
   138    end
   139 
   140 (* generate preliminary itm_ from a strin (with field "#Given" etc.) *)
   141 fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
   142 fun fstr2itm_ thy pbt (f, str) =
   143   let
   144     val topt = TermC.parse thy str
   145   in
   146     case topt of
   147       NONE => ([], false, f, I_Model.Syn str)
   148     | SOME ct => 
   149 	    let
   150 	      val (d, ts) = (Input_Descript.split o Thm.term_of) ct
   151 	      val popt = find_first (eq7 (f, d)) pbt
   152 	    in
   153 	      case popt of
   154 	        NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
   155 	      | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts))) 
   156 	    end
   157   end
   158 
   159 (*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
   160 fun unknown_expl dI pbt selcts =
   161   let
   162     val thy = ThyC.get_theory dI
   163     val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
   164     val its = O_Model.add_id its_ 
   165   in map flattup2 its end
   166 
   167 (* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
   168    appl_add': generate 1 item 
   169    appl_add' . is_known: parse, get data from oris (vats, all (elems if list)..)
   170    appl_add' . is_notyet_input: compare with items in model already input
   171    insert_ppc': insert this 1 item*)
   172 fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
   173     (*already present itms in model are being overwritten*)
   174   | appl_adds _ _ ppc _ [] = ppc
   175   | appl_adds dI oris ppc pbt (selct :: ss) =
   176     let val itm = appl_add' dI oris ppc pbt selct;
   177     in appl_adds dI oris (I_Model.add itm ppc) pbt ss end
   178 
   179 fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
   180   | par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
   181 fun itms2fstr (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
   182   | itms2fstr (_, _, _, f, I_Model.Syn str) = (f, str)
   183   | itms2fstr (_, _, _, f, I_Model.Typ str) = (f, str)
   184   | itms2fstr (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
   185   | itms2fstr (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
   186   | itms2fstr (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term (d $ t))
   187   | itms2fstr (itm as (_, _, _, _, I_Model.Par _)) = 
   188     raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm ^ "): Par should be internal");
   189 
   190 fun imodel2fstr iitems = 
   191   let 
   192     fun xxx is [] = is
   193 	    | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
   194 	    | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
   195 	    | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
   196   in xxx [] iitems end;
   197 
   198 (* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
   199 fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
   200     let
   201 		  val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
   202 		    Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
   203 		      spec = sspec as (sdI, spI, smI), probl, meth, ...}
   204         => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
   205       | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
   206     in if CAS_Cmd.is_from hdf fmz then the (CAS_Cmd.input (TermC.str2term hdf)) 
   207        else        (*hacked WN0602 ~~~            ~~~~~~~~~,   ..dropped !*)
   208          let val (pos_, pits, mits) = 
   209 	         if dI <> sdI
   210 	         then let val its = map (parsitm (ThyC.get_theory dI)) probl;
   211 			            val (its, trms) = filter_sep is_Par its;
   212 			            val pbt = (#ppc o Problem.from_store) (#2 (References.select ospec sspec))
   213 		            in (Pos.Pbl, appl_adds dI oris its pbt  (map par2fstr trms), meth) end 
   214            else
   215              if pI <> spI 
   216 	           then if pI = snd3 ospec then (Pos.Pbl, probl, meth) 
   217                   else
   218 		                let val pbt = (#ppc o Problem.from_store) pI
   219 			                val dI' = #1 (References.select ospec spec)
   220 			                val oris = if pI = #2 ospec then oris 
   221 				                         else O_Model.init fmz_(ThyC.get_theory"Isac_Knowledge") pbt(* |> #1*);
   222 		                in (Pos.Pbl, appl_adds dI' oris probl pbt 
   223 				              (map itms2fstr probl), meth) end 
   224              else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
   225 	                then let val met = (#ppc o Method.from_store) mI
   226 		                     val mits = I_Model.complete oris probl meth met
   227 		                   in if foldl and_ (true, map #3 mits)
   228 		                      then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits) 
   229 		                   end 
   230                   else (Pos.Pbl, appl_adds (#1 (References.select ospec spec)) oris [(*!!!*)]
   231 			                  ((#ppc o Problem.from_store) (#2 (References.select ospec spec)))
   232 			                  (imodel2fstr imodel), meth)
   233 	         val pt = Ctree.update_spec pt p spec;
   234          in if pos_ = Pos.Pbl
   235 	          then let val {prls,where_,...} = Problem.from_store (#2 (References.select ospec spec))
   236 		               val (_, pre) = Pre_Conds.check prls where_ pits 0
   237 	               in (Ctree.update_pbl pt p pits,
   238 		                 (Specification.complete pits pre spec, Pos.Pbl, hdf', pits, pre, spec): Specification.T) 
   239                  end
   240 	           else let val {prls,pre,...} = Method.from_store (#3 (References.select ospec spec))
   241 		                val (_, pre) = Pre_Conds.check prls pre mits 0
   242 	                in (Ctree.update_met pt p mits,
   243 		                  (Specification.complete mits pre spec, Pos.Met, hdf', mits, pre, spec) : Specification.T)
   244                   end
   245          end 
   246     end
   247   | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."
   248 
   249 (**)end (**)