src/Tools/isac/Specify/input-calchead.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 11 May 2020 20:49:27 +0200
changeset 59965 0763aec4c5b6
parent 59962 6a59d252345d
child 59970 ab1c25c0339a
permissions -rw-r--r--
prep. remove Specify/mstools.sml
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@59822
     8
signature INPUT_CALCHEAD =
walther@59822
     9
sig
walther@59918
    10
  type pbt_ = string * (term * term)
walther@59918
    11
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@59903
    17
  type icalhd = Pos.pos' * TermC.as_string * imodel * Pos.pos_ * Spec.T
walther@59822
    18
  val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * Ctree.ocalhd
walther@59822
    19
  val cas_input : term -> (Ctree.ctree * Ctree.ocalhd) option
walther@59822
    20
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59822
    21
  (*  NONE *)
walther@59886
    22
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59822
    23
  (*  NONE *)
walther@59886
    24
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59822
    25
walther@59822
    26
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
walther@59822
    27
  val e_icalhd : icalhd
walther@59822
    28
  val oris2itms : 'a -> ''b list -> ('c * ''b * 'd * term * term list) list ->
walther@59943
    29
    ('c * ''b * bool * 'd * I_Model.feedback) list
walther@59822
    30
end
walther@59822
    31
walther@59822
    32
structure In_Chead(**): INPUT_CALCHEAD(**) =
walther@59822
    33
struct
walther@59822
    34
walther@59918
    35
(* types for problems models (TODO rename to specification models) *)
walther@59918
    36
type pbt_ =
walther@59918
    37
  (string *   (* field "#Given",..*)(*deprecated due to 'type pat'*)
walther@59918
    38
    (term *   (* description      *)
walther@59918
    39
      term)); (* id | struct-var  *)
walther@59918
    40
walther@59822
    41
(*** handle an input by CAS-command ***)
walther@59822
    42
walther@59822
    43
fun dtss2itm_ ppc (d, ts) =
walther@59822
    44
  let
walther@59822
    45
    val (f, (d, id)) = the (find_first ((curry op= d) o 
walther@59822
    46
  		(#1: (term * term) -> term) o
walther@59918
    47
  		(#2: pbt_ -> (term * term))) ppc)
walther@59822
    48
  in
walther@59943
    49
    ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
walther@59822
    50
  end
walther@59822
    51
walther@59822
    52
fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
walther@59822
    53
walther@59903
    54
fun cas_input_ ((dI, pI, mI): Spec.T) dtss = (*WN110515 reconsider thy..ctxt*)
walther@59822
    55
  let
walther@59881
    56
    val thy = ThyC.get_theory dI
walther@59822
    57
	  val {ppc, ...} = Specify.get_pbt pI
walther@59822
    58
	  val its_ = map (dtss2itm_ ppc) dtss (*([1],true,"#Given",Cor (...))*)
walther@59947
    59
	  val its = O_Model.add_id its_
walther@59822
    60
	  val pits = map flattup2 its
walther@59822
    61
	  val (pI, mI) =
walther@59822
    62
      if mI <> ["no_met"]
walther@59822
    63
      then (pI, mI)
walther@59822
    64
		  else
walther@59960
    65
        case Refine.problem thy pI pits of
walther@59822
    66
			    SOME (pI,_) => (pI, (hd o #met o Specify.get_pbt) pI)
walther@59822
    67
			  | NONE => (pI, (hd o #met o Specify.get_pbt) pI)
walther@59822
    68
	  val {ppc, pre, prls, ...} = Specify.get_met mI
walther@59822
    69
	  val its_ = map (dtss2itm_ ppc) dtss (*([1],true,"#Given",Cor (...))*)
walther@59947
    70
	  val its = O_Model.add_id its_
walther@59822
    71
	  val mits = map flattup2 its
walther@59965
    72
	  val pre = Pre_Conds.check' thy prls pre mits
walther@59822
    73
    val ctxt = Proof_Context.init_global thy
walther@59822
    74
  in (pI, pits, mI, mits, pre, ctxt) end;
walther@59822
    75
walther@59822
    76
walther@59822
    77
(* check if the input term is a CAScmd and return a ctree with a _complete_ calchead *)
walther@59822
    78
fun cas_input hdt =
walther@59822
    79
  let
walther@59822
    80
    val (h, argl) = strip_comb hdt
walther@59822
    81
  in
walther@59881
    82
    case assoc_cas (ThyC.get_theory "Isac_Knowledge") h of
walther@59822
    83
      NONE => NONE
walther@59822
    84
    | SOME (spec as (dI,_,_), argl2dtss) =>
walther@59822
    85
	      let
walther@59822
    86
          val dtss = argl2dtss argl
walther@59822
    87
	        val (pI, pits, mI, mits, pre, ctxt) = cas_input_ spec dtss
walther@59822
    88
	        val spec = (dI, pI, mI)
walther@59846
    89
	        val (pt,_) = Ctree.cappend_problem Ctree.e_ctree [] (Istate_Def.empty, ContextC.empty)
walther@59902
    90
		        ([], Spec.empty) ([], Spec.empty, hdt, ctxt)
walther@59822
    91
	        val pt = Ctree.update_spec pt [] spec
walther@59822
    92
	        val pt = Ctree.update_pbl pt [] pits
walther@59822
    93
	        val pt = Ctree.update_met pt [] mits
walther@59822
    94
	      in
walther@59822
    95
	        SOME (pt, (true, Pos.Met, hdt, mits, pre, spec) : Ctree.ocalhd)
walther@59822
    96
	      end
walther@59822
    97
  end
walther@59822
    98
walther@59822
    99
walther@59822
   100
(*** handle an input calc-head ***)
walther@59822
   101
walther@59822
   102
datatype iitem = 
walther@59865
   103
  Given of TermC.as_string list
walther@59822
   104
(*Where is never input*) 
walther@59865
   105
| Find  of TermC.as_string list
walther@59865
   106
| Relate  of TermC.as_string list
walther@59822
   107
walther@59822
   108
type imodel = iitem list
walther@59822
   109
walther@59822
   110
(*calc-head as input*)
walther@59822
   111
type icalhd =
walther@59846
   112
  Pos.pos' *     (*the position of the calc-head in the calc-tree*) 
walther@59865
   113
  TermC.as_string *   (*the headline*)
walther@59822
   114
  imodel *         (*the model (without Find) of the calc-head*)
walther@59846
   115
  Pos.pos_ *     (*model belongs to Pbl or Met*)
walther@59903
   116
  Spec.T;      (*specification: domID, pblID, metID*)
walther@59902
   117
val e_icalhd = (Pos.e_pos', "", [Given [""]], Pos.Pbl, Spec.empty)
walther@59822
   118
walther@59941
   119
fun is_casinput (hdf : TermC.as_string) ((fmz_, spec) : Formalise.T) =
walther@59902
   120
  hdf <> "" andalso fmz_ = [] andalso spec = Spec.empty
walther@59822
   121
walther@59822
   122
(* re-parse itms with a new thy and prepare for checking with ori list *)
walther@59943
   123
fun parsitm dI (itm as (i, v, _, f, I_Model.Cor ((d, ts), _))) =
walther@59953
   124
    (let val t = Input_Descript.join (d, ts)
walther@59870
   125
     val _ = (UnparseC.term_in_thy dI t)
walther@59822
   126
     (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
walther@59822
   127
    in itm end
walther@59943
   128
    handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
   129
  | parsitm dI (i, v, b, f, I_Model.Syn str) =
walther@59822
   130
    (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
walther@59943
   131
    in (i, v, b ,f, I_Model.Par str) end
walther@59943
   132
    handle _ => (i, v, b, f, I_Model.Syn str))
walther@59943
   133
  | parsitm dI (i, v, b, f, I_Model.Typ str) =
walther@59822
   134
    (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
walther@59943
   135
     in (i, v, b, f, I_Model.Par str) end
walther@59943
   136
     handle _ => (i, v, b, f, I_Model.Syn str))
walther@59943
   137
  | parsitm dI (itm as (i, v, _, f, I_Model.Inc ((d, ts), _))) =
walther@59953
   138
    (let val t = Input_Descript.join (d,ts);
walther@59870
   139
	       val _ = UnparseC.term_in_thy dI t;
walther@59822
   140
     (*this    ^ should raise the exn on unability of re-parsing dts*)
walther@59822
   141
     in itm end
walther@59943
   142
     handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
   143
  | parsitm dI (itm as (i, v, _, f, I_Model.Sup (d, ts))) =
walther@59953
   144
    (let val t = Input_Descript.join (d,ts);
walther@59870
   145
	       val _ = UnparseC.term_in_thy dI t;
walther@59822
   146
     (*this    ^ should raise the exn on unability of re-parsing dts*)
walther@59822
   147
    in itm end
walther@59943
   148
    handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
   149
  | parsitm dI (itm as (i, v, _, f, I_Model.Mis (d, t'))) =
walther@59822
   150
    (let val t = d $ t';
walther@59870
   151
	       val _ = UnparseC.term_in_thy dI t;
walther@59822
   152
     (*this    ^ should raise the exn on unability of re-parsing dts*)
walther@59822
   153
    in itm end
walther@59943
   154
    handle _ => (i, v, false, f, I_Model.Syn (UnparseC.term TermC.empty (*t  ..(t) has not been declared*))))
walther@59943
   155
  | parsitm dI (itm as (_, _, _, _, I_Model.Par _)) = 
walther@59962
   156
    raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.to_ctxt dI) itm ^ "): Par should be internal");
walther@59822
   157
walther@59822
   158
(*separate a list to a pair of elements that do NOT satisfy the predicate,
walther@59822
   159
 and of elements that satisfy the predicate, i.e. (false, true)*)
walther@59822
   160
fun filter_sep pred xs =
walther@59822
   161
  let
walther@59822
   162
    fun filt ab [] = ab
walther@59822
   163
      | filt (a, b) (x :: xs) =
walther@59822
   164
        if pred x 
walther@59822
   165
			  then filt (a, b @ [x]) xs 
walther@59822
   166
			  else filt (a @ [x], b) xs
walther@59822
   167
  in filt ([], []) xs end;
walther@59943
   168
fun is_Par (_, _, _, _, I_Model.Par _) = true
walther@59822
   169
  | is_Par _ = false;
walther@59822
   170
walther@59822
   171
fun is_e_ts [] = true
walther@59822
   172
  | is_e_ts [Const ("List.list.Nil", _)] = true
walther@59822
   173
  | is_e_ts _ = false;
walther@59822
   174
walther@59822
   175
(* WN.9.11.03 copied from fun appl_add *)
walther@59822
   176
fun appl_add' dI oris ppc pbt (sel, ct) = 
walther@59822
   177
  let 
walther@59881
   178
     val ctxt = ThyC.get_theory dI |> ThyC.to_ctxt;
walther@59822
   179
  in
walther@59822
   180
    case TermC.parseNEW ctxt ct of
walther@59943
   181
	    NONE => (0, [], false, sel, I_Model.Syn ct)
walther@59822
   182
	  | SOME t =>
walther@59956
   183
	    (case I_Model.is_known ctxt sel oris t of
walther@59822
   184
        ("", ori', all) =>
walther@59956
   185
          (case I_Model.is_notyet_input ctxt ppc all ori' pbt of
walther@59822
   186
            ("",itm)  => itm
walther@59962
   187
          | (msg,_) => raise ERROR ("appl_add': " ^ msg))
walther@59822
   188
      | (_, (i, v, _, d, ts), _) =>
walther@59822
   189
        if is_e_ts ts
walther@59943
   190
        then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
walther@59943
   191
        else (i, v, false, sel, I_Model.Sup (d, ts)))
walther@59822
   192
   end
walther@59822
   193
walther@59822
   194
(* generate preliminary itm_ from a strin (with field "#Given" etc.) *)
walther@59822
   195
fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
walther@59822
   196
fun fstr2itm_ thy pbt (f, str) =
walther@59822
   197
  let
walther@59822
   198
    val topt = TermC.parse thy str
walther@59822
   199
  in
walther@59822
   200
    case topt of
walther@59943
   201
      NONE => ([], false, f, I_Model.Syn str)
walther@59822
   202
    | SOME ct => 
walther@59822
   203
	    let
walther@59953
   204
	      val (d, ts) = (Input_Descript.split o Thm.term_of) ct
walther@59822
   205
	      val popt = find_first (eq7 (f, d)) pbt
walther@59822
   206
	    in
walther@59822
   207
	      case popt of
walther@59943
   208
	        NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
walther@59943
   209
	      | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts))) 
walther@59822
   210
	    end
walther@59822
   211
  end
walther@59822
   212
walther@59822
   213
(*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
walther@59822
   214
fun unknown_expl dI pbt selcts =
walther@59822
   215
  let
walther@59881
   216
    val thy = ThyC.get_theory dI
walther@59822
   217
    val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
walther@59947
   218
    val its = O_Model.add_id its_ 
walther@59822
   219
  in map flattup2 its end
walther@59822
   220
walther@59822
   221
(* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
walther@59822
   222
   appl_add': generate 1 item 
walther@59822
   223
   appl_add' . is_known: parse, get data from oris (vats, all (elems if list)..)
walther@59822
   224
   appl_add' . is_notyet_input: compare with items in model already input
walther@59822
   225
   insert_ppc': insert this 1 item*)
walther@59822
   226
fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
walther@59822
   227
    (*already present itms in model are being overwritten*)
walther@59822
   228
  | appl_adds _ _ ppc _ [] = ppc
walther@59822
   229
  | appl_adds dI oris ppc pbt (selct :: ss) =
walther@59822
   230
    let val itm = appl_add' dI oris ppc pbt selct;
walther@59822
   231
    in appl_adds dI oris (Chead.insert_ppc' itm ppc) pbt ss end
walther@59822
   232
walther@59822
   233
fun oris2itms _  _ [] = [] (* WN161130: similar in ptyps ?!? *)
walther@59822
   234
  | oris2itms pbt vat ((i, v, f, d, ts) :: os) =
walther@59822
   235
    if member op = vat v 
walther@59943
   236
    then (i, v, true, f, I_Model.Cor ((d, ts), (TermC.empty, []))) :: (oris2itms pbt vat os)
walther@59822
   237
    else oris2itms pbt vat os
walther@59822
   238
walther@59943
   239
fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
walther@59962
   240
  | par2fstr itm = raise ERROR ("par2fstr: called with " ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm)
walther@59953
   241
fun itms2fstr (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
walther@59943
   242
  | itms2fstr (_, _, _, f, I_Model.Syn str) = (f, str)
walther@59943
   243
  | itms2fstr (_, _, _, f, I_Model.Typ str) = (f, str)
walther@59953
   244
  | itms2fstr (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
walther@59953
   245
  | itms2fstr (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
walther@59943
   246
  | itms2fstr (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term (d $ t))
walther@59943
   247
  | itms2fstr (itm as (_, _, _, _, I_Model.Par _)) = 
walther@59962
   248
    raise ERROR ("parsitm (" ^ I_Model.single_to_string (ThyC.id_to_ctxt "Isac_Knowledge") itm ^ "): Par should be internal");
walther@59822
   249
walther@59822
   250
fun imodel2fstr iitems = 
walther@59822
   251
  let 
walther@59822
   252
    fun xxx is [] = is
walther@59822
   253
	    | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
walther@59822
   254
	    | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
walther@59822
   255
	    | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
walther@59822
   256
  in xxx [] iitems end;
walther@59822
   257
walther@59822
   258
(* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
walther@59822
   259
fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
walther@59822
   260
    let
walther@59822
   261
		  val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
walther@59822
   262
		    Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
walther@59822
   263
		      spec = sspec as (sdI, spI, smI), probl, meth, ...}
walther@59822
   264
        => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
walther@59962
   265
      | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
walther@59822
   266
    in if is_casinput hdf fmz then the (cas_input (TermC.str2term hdf)) 
walther@59822
   267
       else        (*hacked WN0602 ~~~            ~~~~~~~~~,   ..dropped !*)
walther@59822
   268
         let val (pos_, pits, mits) = 
walther@59822
   269
	         if dI <> sdI
walther@59881
   270
	         then let val its = map (parsitm (ThyC.get_theory dI)) probl;
walther@59822
   271
			            val (its, trms) = filter_sep is_Par its;
walther@59822
   272
			            val pbt = (#ppc o Specify.get_pbt) (#2 (Chead.some_spec ospec sspec))
walther@59822
   273
		            in (Pos.Pbl, appl_adds dI oris its pbt  (map par2fstr trms), meth) end 
walther@59822
   274
           else
walther@59822
   275
             if pI <> spI 
walther@59822
   276
	           then if pI = snd3 ospec then (Pos.Pbl, probl, meth) 
walther@59822
   277
                  else
walther@59822
   278
		                let val pbt = (#ppc o Specify.get_pbt) pI
walther@59822
   279
			                val dI' = #1 (Chead.some_spec ospec spec)
walther@59822
   280
			                val oris = if pI = #2 ospec then oris 
walther@59952
   281
				                         else O_Model.init fmz_(ThyC.get_theory"Isac_Knowledge") pbt(* |> #1*);
walther@59822
   282
		                in (Pos.Pbl, appl_adds dI' oris probl pbt 
walther@59822
   283
				              (map itms2fstr probl), meth) end 
walther@59822
   284
             else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
walther@59822
   285
	                then let val met = (#ppc o Specify.get_met) mI
walther@59822
   286
		                     val mits = Chead.complete_metitms oris probl meth met
walther@59822
   287
		                   in if foldl and_ (true, map #3 mits)
walther@59822
   288
		                      then (Pos.Pbl, probl, mits) else (Pos.Met, probl, mits) 
walther@59822
   289
		                   end 
walther@59822
   290
                  else (Pos.Pbl, appl_adds (#1 (Chead.some_spec ospec spec)) oris [(*!!!*)]
walther@59822
   291
			                  ((#ppc o Specify.get_pbt) (#2 (Chead.some_spec ospec spec)))
walther@59822
   292
			                  (imodel2fstr imodel), meth)
walther@59822
   293
	         val pt = Ctree.update_spec pt p spec;
walther@59822
   294
         in if pos_ = Pos.Pbl
walther@59822
   295
	          then let val {prls,where_,...} = Specify.get_pbt (#2 (Chead.some_spec ospec spec))
walther@59965
   296
		               val pre = Pre_Conds.check' (ThyC.get_theory"Isac_Knowledge") prls where_ pits
walther@59822
   297
	               in (Ctree.update_pbl pt p pits,
walther@59822
   298
		                 (Chead.ocalhd_complete pits pre spec, Pos.Pbl, hdf', pits, pre, spec): Ctree.ocalhd) 
walther@59822
   299
                 end
walther@59822
   300
	           else let val {prls,pre,...} = Specify.get_met (#3 (Chead.some_spec ospec spec))
walther@59965
   301
		                val pre = Pre_Conds.check' (ThyC.get_theory"Isac_Knowledge") prls pre mits
walther@59822
   302
	                in (Ctree.update_met pt p mits,
walther@59822
   303
		                  (Chead.ocalhd_complete mits pre spec, Pos.Met, hdf', mits, pre, spec) : Ctree.ocalhd)
walther@59822
   304
                  end
walther@59822
   305
         end 
walther@59822
   306
    end
walther@59962
   307
  | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."
walther@59822
   308
walther@59822
   309
(**)end (**)