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