src/Tools/isac/Specify/p-spec.sml
author wenzelm
Sun, 18 Apr 2021 23:37:59 +0200
changeset 60223 740ebee5948b
parent 60154 2ab0d1523731
child 60265 9c9d61fed195
permissions -rw-r--r--
conditional compilation via system option "isac_test" and antiquotation \<^isac_test>CARTOUCHE:
option is provided in session ROOT, or interactively via $ISABELLE_HOME_USER/etc/preferences (i.e. Isabelle/jEdit plugin preferences);
     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: 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 dI (itm as (i, v, _, f, I_Model.Cor ((d, ts), _))) =
    74     (let val t = Input_Descript.join (d, ts)
    75      val _ = (UnparseC.term_in_thy dI t)
    76      (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
    77     in itm end
    78     handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
    79   | parsitm dI (i, v, b, f, I_Model.Syn 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 (i, v, b, f, I_Model.Typ str) =
    84     (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
    85      in (i, v, b, f, I_Model.Par str) end
    86      handle _ => (i, v, b, f, I_Model.Syn str))
    87   | parsitm dI (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
    88     (let val t = Input_Descript.join (d,ts);
    89 	       val _ = UnparseC.term_in_thy dI t;
    90      (*this    ^ should raise the exn on unability of re-parsing dts*)
    91      in itm end
    92      handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
    93   | parsitm dI (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
    94     (let val t = Input_Descript.join (d,ts);
    95 	       val _ = UnparseC.term_in_thy dI t;
    96      (*this    ^ should raise the exn on unability of re-parsing dts*)
    97     in itm end
    98     handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
    99   | parsitm dI (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
   100     (let val t = d $ t';
   101 	       val _ = UnparseC.term_in_thy dI t;
   102      (*this    ^ should raise the exn on unability of re-parsing dts*)
   103     in itm end
   104     handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
   105   | parsitm dI (itm as (_, _, _, _, I_Model.Par _)) = 
   106     raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.to_ctxt dI) itm ^ "): Par should be internal");
   107 
   108 (*separate a list to a pair of elements that do NOT satisfy the predicate,
   109  and of elements that satisfy the predicate, i.e. (false, true)*)
   110 fun filter_sep pred xs =
   111   let
   112     fun filt ab [] = ab
   113       | filt (a, b) (x :: xs) =
   114         if pred x 
   115 			  then filt (a, b @ [x]) xs 
   116 			  else filt (a @ [x], b) xs
   117   in filt ([], []) xs end;
   118 fun is_Par (_, _, _, _, I_Model.Par _) = true
   119   | is_Par _ = false;
   120 
   121 fun is_e_ts [] = true
   122   | is_e_ts [Const ("List.list.Nil", _)] = true
   123   | is_e_ts _ = false;
   124 
   125 (* WN.9.11.03 copied from fun appl_add *)
   126 fun appl_add' dI oris ppc pbt (sel, ct) = 
   127   let 
   128      val ctxt = ThyC.get_theory dI |> ThyC.to_ctxt;
   129   in
   130     case TermC.parseNEW ctxt ct of
   131 	    NONE => (0, [], false, sel, I_Model.Syn ct)
   132 	  | SOME t =>
   133 	    (case O_Model.is_known ctxt sel oris t of
   134         ("", ori', all) =>
   135           (case I_Model.is_notyet_input ctxt ppc all ori' pbt of
   136             ("",itm)  => itm
   137           | (msg,_) => raise ERROR ("appl_add': " ^ msg))
   138       | (_, (i, v, _, d, ts), _) =>
   139         if is_e_ts ts
   140         then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
   141         else (i, v, false, sel, I_Model.Sup (d, ts)))
   142    end
   143 
   144 (* generate preliminary itm_ from a strin (with field "#Given" etc.) *)
   145 fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
   146 fun fstr2itm_ thy pbt (f, str) =
   147   let
   148     val topt = TermC.parse thy str
   149   in
   150     case topt of
   151       NONE => ([], false, f, I_Model.Syn str)
   152     | SOME ct => 
   153 	    let
   154 	      val (d, ts) = (Input_Descript.split o Thm.term_of) ct
   155 	      val popt = find_first (eq7 (f, d)) pbt
   156 	    in
   157 	      case popt of
   158 	        NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
   159 	      | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts))) 
   160 	    end
   161   end
   162 
   163 (*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
   164 fun unknown_expl dI pbt selcts =
   165   let
   166     val thy = ThyC.get_theory dI
   167     val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
   168     val its = O_Model.add_id its_ 
   169   in map flattup2 its end
   170 
   171 (* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
   172    appl_add': generate 1 item 
   173    appl_add' . is_known: parse, get data from oris (vats, all (elems if list)..)
   174    appl_add' . is_notyet_input: compare with items in model already input
   175    insert_ppc': insert this 1 item*)
   176 fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
   177     (*already present itms in model are being overwritten*)
   178   | appl_adds _ _ ppc _ [] = ppc
   179   | appl_adds dI oris ppc pbt (selct :: ss) =
   180     let val itm = appl_add' dI oris ppc pbt selct;
   181     in appl_adds dI oris (I_Model.add itm ppc) pbt ss end
   182 
   183 fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
   184   | par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
   185 fun itms2fstr (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
   186   | itms2fstr (_, _, _, f, I_Model.Syn str) = (f, str)
   187   | itms2fstr (_, _, _, f, I_Model.Typ str) = (f, str)
   188   | itms2fstr (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
   189   | itms2fstr (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
   190   | itms2fstr (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term (d $ t))
   191   | itms2fstr (itm as (_, _, _, _, I_Model.Par _)) = 
   192     raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm ^ "): Par should be internal");
   193 
   194 fun imodel2fstr iitems = 
   195   let 
   196     fun xxx is [] = is
   197 	    | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
   198 	    | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
   199 	    | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
   200   in xxx [] iitems end;
   201 
   202 (* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
   203 fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
   204     let
   205 		  val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
   206 		    Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
   207 		      spec = sspec as (sdI, spI, smI), probl, meth, ...}
   208         => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
   209       | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
   210     in if CAS_Cmd.is_from hdf fmz then the (CAS_Cmd.input (TermC.str2term hdf)) 
   211        else        (*hacked WN0602 ~~~            ~~~~~~~~~,   ..dropped !*)
   212          let val (pos_, pits, mits) = 
   213 	         if dI <> sdI
   214 	         then let val its = map (parsitm (ThyC.get_theory dI)) probl;
   215 			            val (its, trms) = filter_sep is_Par its;
   216 			            val pbt = (#ppc o Problem.from_store) (#2 (References.select ospec sspec))
   217 		            in (Pos.Pbl, appl_adds dI oris its pbt  (map par2fstr trms), meth) end 
   218            else
   219              if pI <> spI 
   220 	           then if pI = snd3 ospec then (Pos.Pbl, probl, meth) 
   221                   else
   222 		                let val pbt = (#ppc o Problem.from_store) pI
   223 			                val dI' = #1 (References.select ospec spec)
   224 			                val oris = if pI = #2 ospec then oris 
   225 				                         else O_Model.init fmz_(ThyC.get_theory"Isac_Knowledge") pbt(* |> #1*);
   226 		                in (Pos.Pbl, appl_adds dI' oris probl pbt 
   227 				              (map itms2fstr probl), meth) end 
   228              else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
   229 	                then let val met = (#ppc o MethodC.from_store) mI
   230 		                     val mits = I_Model.complete oris probl meth met
   231 		                   in if foldl and_ (true, map #3 mits)
   232 		                      then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits) 
   233 		                   end 
   234                   else (Pos.Pbl, appl_adds (#1 (References.select ospec spec)) oris [(*!!!*)]
   235 			                  ((#ppc o Problem.from_store) (#2 (References.select ospec spec)))
   236 			                  (imodel2fstr imodel), meth)
   237 	         val pt = Ctree.update_spec pt p spec;
   238          in if pos_ = Pos.Pbl
   239 	          then let val {prls,where_,...} = Problem.from_store (#2 (References.select ospec spec))
   240 		               val (_, pre) = Pre_Conds.check prls where_ pits 0
   241 	               in (Ctree.update_pbl pt p pits,
   242 		                 (SpecificationC.complete pits pre spec, Pos.Pbl, hdf', pits, pre, spec): SpecificationC.T) 
   243                  end
   244 	           else let val {prls,pre,...} = MethodC.from_store (#3 (References.select ospec spec))
   245 		                val (_, pre) = Pre_Conds.check prls pre mits 0
   246 	                in (Ctree.update_met pt p mits,
   247 		                  (SpecificationC.complete mits pre spec, Pos.Met, hdf', mits, pre, spec) : SpecificationC.T)
   248                   end
   249          end 
   250     end
   251   | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."
   252 
   253 (**)end (**)