src/Tools/isac/Specify/p-spec.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 29 Nov 2023 07:51:28 +0100
changeset 60768 14da2230d5c3
parent 60767 466f0a5bfb73
child 60771 1b072aab8f4e
permissions -rw-r--r--
prepare 15: adapt M_Match.by_o_model to varaints
Walther@60767
     1
(* Title:  Specify/p-spec.sml
walther@59822
     2
   Author: Walther Neuper
walther@59822
     3
   (c) due to copyright terms
walther@59960
     4
walther@60111
     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@60132
    10
  type record
walther@59995
    11
(*/------- rename -------\*)
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@59976
    17
  type icalhd = Pos.pos' * TermC.as_string * imodel * Pos.pos_ * References.T
walther@59995
    18
  val empty: icalhd
walther@60097
    19
  val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * SpecificationC.T
wenzelm@60223
    20
\<^isac_test>\<open>
walther@59977
    21
  val appl_add': ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
walther@59977
    22
    string * TermC.as_string -> I_Model.single
walther@59977
    23
  val appl_adds: ThyC.id -> O_Model.T -> I_Model.T -> Model_Pattern.T ->
walther@59977
    24
    (string * TermC.as_string) list -> I_Model.T
walther@59977
    25
  val eq7: ''a * ''b -> ''a * (''b * 'c) -> bool
walther@59977
    26
  val fstr2itm_: theory -> (''a * (term * term)) list -> ''a * string ->
walther@59977
    27
    int list * bool * ''a * I_Model.feedback (*I_Model.single'*)
walther@59977
    28
  val imodel2fstr: iitem list -> (string * TermC.as_string) list
walther@59977
    29
  val is_e_ts: term list -> bool
Walther@60673
    30
  val itms2fstr: Proof.context -> I_Model.single -> string * string
walther@59977
    31
  val par2fstr: I_Model.single -> string * TermC.as_string
Walther@60747
    32
  val unknown_expl: ThyC.id -> (''a * (term * term)) list -> (*Model_Pattern.T*)
walther@59977
    33
    (''a * string) list ->
Walther@60747
    34
      (int * int list * bool * ''a * Model_Def.i_model_feedback) list (*I_Model.T*)
wenzelm@60223
    35
\<close>
walther@59995
    36
(*\------- rename -------/*)
walther@59822
    37
end
walther@59822
    38
walther@59977
    39
(**)
walther@59984
    40
structure P_Spec(**): PRESENTATION_SPECIFICATION(**) =
walther@59822
    41
struct
walther@59977
    42
(**)
walther@59822
    43
walther@60132
    44
  type record = {thy_id: ThyC.id, pbl_id: Problem.id,           (* headline of Problem *)
walther@60132
    45
    givens: TermC.as_string list, wheres: TermC.as_string list, (* Model.T             *)
walther@60132
    46
      find: TermC.as_string, relates: TermC.as_string list,
walther@60154
    47
    rthy_id: ThyC.id, rpbl_id: Problem.id, rmet_id: MethodC.id}  (* References.T        *)
walther@60132
    48
walther@59822
    49
fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
walther@59822
    50
walther@59982
    51
(** handle an input P_Specific'action **)
walther@59822
    52
walther@59822
    53
datatype iitem = 
walther@59865
    54
  Given of TermC.as_string list
walther@59977
    55
(*Where is still not input*) 
walther@59865
    56
| Find  of TermC.as_string list
walther@59865
    57
| Relate  of TermC.as_string list
walther@59822
    58
walther@59822
    59
type imodel = iitem list
walther@59822
    60
walther@59822
    61
type icalhd =
walther@59977
    62
  Pos.pos' *         (* the position in Ctree              *) 
walther@59977
    63
  TermC.as_string *  (* the headline shown on Calc.T       *)
walther@59977
    64
  imodel *           (* the model                          *)
walther@60154
    65
  Pos.pos_ *         (* model belongs to Problem or MethodC *)
walther@59977
    66
  References.T;      (* into Know_Store                    *)
walther@59995
    67
val empty = (Pos.e_pos', "", [Given [""]], Pos.Pbl, References.empty)
walther@59822
    68
walther@59822
    69
fun is_e_ts [] = true
wenzelm@60309
    70
  | is_e_ts [Const (\<^const_name>\<open>Nil\<close>, _)] = true
walther@59822
    71
  | is_e_ts _ = false;
walther@59822
    72
walther@59822
    73
(* WN.9.11.03 copied from fun appl_add *)
Walther@60586
    74
fun appl_add' dI oris model pbt (sel, ct) = 
walther@59822
    75
  let 
Walther@60676
    76
     val ctxt = Know_Store.get_via_last_thy dI |> Proof_Context.init_global;
walther@59822
    77
  in
Walther@60663
    78
    (*/------------ replace by ParseC.term_position ------------------\*)
Walther@60663
    79
    case ParseC.term_opt ctxt ct of
Walther@60663
    80
    (*\------------ replace by ParseC.term_position ------------------/*)
walther@59943
    81
	    NONE => (0, [], false, sel, I_Model.Syn ct)
walther@59822
    82
	  | SOME t =>
Walther@60471
    83
	    (case O_Model.contains ctxt sel oris t of
walther@59822
    84
        ("", ori', all) =>
Walther@60586
    85
          (case I_Model.is_notyet_input ctxt model all ori' pbt of
walther@59822
    86
            ("",itm)  => itm
walther@59962
    87
          | (msg,_) => raise ERROR ("appl_add': " ^ msg))
walther@59822
    88
      | (_, (i, v, _, d, ts), _) =>
walther@59822
    89
        if is_e_ts ts
walther@59943
    90
        then (i, v, false, sel, I_Model.Inc ((d, ts), (TermC.empty, [])))
walther@59943
    91
        else (i, v, false, sel, I_Model.Sup (d, ts)))
walther@59822
    92
   end
walther@59822
    93
Walther@60422
    94
(* generate preliminary itm_ from a string (with field "#Given" etc.) *)
walther@59822
    95
fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
walther@59822
    96
fun fstr2itm_ thy pbt (f, str) =
walther@59822
    97
  let
Walther@60663
    98
    (*/------------ replace by ParseC.term_position ? ------------\*)
Walther@60663
    99
    val topt = ParseC.term_opt (Proof_Context.init_global thy) str
Walther@60663
   100
    (*\------------ replace by ParseC.term_position ? ------------/*)
walther@59822
   101
  in
walther@59822
   102
    case topt of
walther@59943
   103
      NONE => ([], false, f, I_Model.Syn str)
walther@59822
   104
    | SOME ct => 
walther@59822
   105
	    let
Walther@60422
   106
	      val (d, ts) = Input_Descript.split ct
walther@59822
   107
	      val popt = find_first (eq7 (f, d)) pbt
walther@59822
   108
	    in
walther@59822
   109
	      case popt of
walther@59943
   110
	        NONE => ([1](*??*), true(*??*), f, I_Model.Sup (d, ts))
walther@59943
   111
	      | SOME (f, (d, id)) => ([1], true, f, I_Model.Cor ((d, ts), (id, ts))) 
walther@59822
   112
	    end
walther@59822
   113
  end
walther@59822
   114
walther@59822
   115
(*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
walther@59822
   116
fun unknown_expl dI pbt selcts =
walther@59822
   117
  let
Walther@60649
   118
    val thy = Know_Store.get_via_last_thy dI
walther@59822
   119
    val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
Walther@60472
   120
    val its = O_Model.add_enumerate its_ 
walther@59822
   121
  in map flattup2 its end
walther@59822
   122
Walther@60766
   123
fun eq_dsc ((_, _, _, _, itm_), (_, _, _, _, iitm_)) =
Walther@60766
   124
  (I_Model.descriptor itm_ = I_Model.descriptor iitm_)
Walther@60768
   125
(* handles superfluous items carelessly *)
Walther@60766
   126
fun add itm itms = if member eq_dsc itms itm then itms else itms @ [itm] (* @ new itm *)
walther@59822
   127
fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
walther@59822
   128
    (*already present itms in model are being overwritten*)
Walther@60586
   129
  | appl_adds _ _ model _ [] = model
Walther@60586
   130
  | appl_adds dI oris model pbt (selct :: ss) =
Walther@60586
   131
    let val itm = appl_add' dI oris model pbt selct;
Walther@60766
   132
    in appl_adds dI oris (add itm model) pbt ss end
walther@59822
   133
walther@59943
   134
fun par2fstr (_, _, _, f, I_Model.Par s) = (f, s)
Walther@60676
   135
  | par2fstr itm = raise ERROR ("par2fstr: called with " ^
Walther@60676
   136
    I_Model.single_to_string (ContextC.for_ERROR ()) itm)
Walther@60673
   137
fun itms2fstr _ (_, _, _, f, I_Model.Cor ((d, ts), _)) = (f, Input_Descript.join''' (d, ts))
Walther@60673
   138
  | itms2fstr _ (_, _, _, f, I_Model.Syn str) = (f, str)
Walther@60673
   139
  | itms2fstr _ (_, _, _, f, I_Model.Typ str) = (f, str)
Walther@60673
   140
  | itms2fstr _ (_, _, _, f, I_Model.Inc ((d, ts), _)) = (f, Input_Descript.join''' (d,ts))
Walther@60673
   141
  | itms2fstr _ (_, _, _, f, I_Model.Sup (d, ts)) = (f, Input_Descript.join''' (d, ts))
Walther@60675
   142
  | itms2fstr ctxt (_, _, _, f, I_Model.Mis (d, t)) = (f, UnparseC.term ctxt (d $ t))
Walther@60673
   143
  | itms2fstr ctxt (itm as (_, _, _, _, I_Model.Par _)) = 
Walther@60708
   144
    raise ERROR ("itms2fstr (" ^ I_Model.single_to_string ctxt itm ^ "): Par should be internal");
walther@59822
   145
walther@59822
   146
fun imodel2fstr iitems = 
walther@59822
   147
  let 
walther@59822
   148
    fun xxx is [] = is
walther@59822
   149
	    | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
walther@59822
   150
	    | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
walther@59822
   151
	    | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
walther@59822
   152
  in xxx [] iitems end;
walther@59822
   153
walther@59822
   154
(* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
walther@59822
   155
fun input_icalhd pt (((p, _), hdf, imodel, Pos.Pbl, spec as (dI, pI, mI)) : icalhd) =
walther@59822
   156
    let
walther@59822
   157
		  val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
walther@59822
   158
		    Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
Walther@60676
   159
		      spec = sspec as (sdI, spI, smI), probl, meth, ...}
walther@59822
   160
        => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
walther@59962
   161
      | _ => raise ERROR "input_icalhd: uncovered case of get_obj I pt p"
Walther@60653
   162
      val thy = Know_Store.get_via_last_thy dI
Walther@60673
   163
      val ctxt = Proof_Context.init_global thy
Walther@60653
   164
    in if CAS_Cmd.is_from hdf fmz
Walther@60673
   165
       then the (CAS_Cmd.input (ParseC.term_opt ctxt hdf |> the)) 
Walther@60653
   166
       else
walther@59822
   167
         let val (pos_, pits, mits) = 
walther@59822
   168
	         if dI <> sdI
Walther@60709
   169
	         then let (* eliminated for PIDE turn 11: take Position into I_Model
Walther@60709
   170
                  val its = map (parsitm thy) probl;
Walther@60709
   171
			            val (its, trms) = filter_sep is_Par its;*)
Walther@60673
   172
			            val pbt = (#model o Problem.from_store ctxt)
Walther@60653
   173
			              (#2 (References.select_input ospec sspec))
Walther@60709
   174
		            in (Pos.Pbl, appl_adds dI oris [](*its*) pbt  (map par2fstr [](*trms*)), meth) end 
walther@59822
   175
           else
walther@59822
   176
             if pI <> spI 
walther@59822
   177
	           then if pI = snd3 ospec then (Pos.Pbl, probl, meth) 
Walther@60757
   178
                  else let
Walther@60757
   179
                      val pbt = (#model o Problem.from_store ctxt) pI
Walther@60494
   180
			                val dI' = #1 (References.select_input ospec spec)
Walther@60470
   181
			                val oris =
Walther@60470
   182
			                  if pI = #2 ospec then oris 
Walther@60653
   183
				                else O_Model.init thy fmz_ pbt |> #1;
walther@59822
   184
		                in (Pos.Pbl, appl_adds dI' oris probl pbt 
Walther@60673
   185
				              (map (itms2fstr ctxt) probl), meth) end 
walther@59822
   186
             else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
Walther@60754
   187
	                then let
Walther@60757
   188
		                  val (_, mits) = I_Model.s_make_complete ctxt oris
Walther@60757
   189
		                    (I_Model.OLD_to_TEST probl, I_Model.OLD_to_TEST meth) (pI, mI)
Walther@60757
   190
		                in if foldl and_ (true, map #3 mits)
Walther@60757
   191
		                   then (Pos.Pbl, probl, I_Model.TEST_to_OLD mits)
Walther@60757
   192
		                   else (Pos.Met, probl, I_Model.TEST_to_OLD mits)
Walther@60757
   193
		                end 
Walther@60494
   194
                  else (Pos.Pbl, appl_adds (#1 (References.select_input ospec spec)) oris [(*!!!*)]
Walther@60673
   195
			                  ((#model o Problem.from_store ctxt)
Walther@60557
   196
			                    (#2 (References.select_input ospec spec)))
Walther@60557
   197
			                      (imodel2fstr imodel), meth)
walther@59822
   198
	         val pt = Ctree.update_spec pt p spec;
walther@59822
   199
         in if pos_ = Pos.Pbl
Walther@60557
   200
	          then 
Walther@60557
   201
	            let 
Walther@60729
   202
	              val {where_rls, where_, model, ...} = Problem.from_store ctxt
Walther@60729
   203
	                (#2 (References.select_input ospec spec))
Walther@60741
   204
		            val (_, where_) = Pre_Conds.check ctxt where_rls where_
Walther@60729
   205
		              (model, I_Model.OLD_to_TEST pits)
Walther@60557
   206
	            in (Ctree.update_pbl pt p pits,
Walther@60586
   207
		                 (SpecificationC.is_complete' pits where_ spec, Pos.Pbl, hdf', pits, where_, spec)) 
Walther@60557
   208
              end
Walther@60557
   209
	          else 
Walther@60557
   210
	            let 
Walther@60729
   211
	              val {where_rls,where_, model, ...} = MethodC.from_store ctxt 
Walther@60729
   212
	                (#3 (References.select_input ospec spec))
Walther@60741
   213
		            val (_, where_) = Pre_Conds.check ctxt where_rls where_
Walther@60729
   214
		              (model, I_Model.OLD_to_TEST mits)
Walther@60557
   215
	            in (Ctree.update_met pt p mits,
Walther@60586
   216
		                  (SpecificationC.is_complete' mits where_ spec, Pos.Met, hdf', mits, where_, spec))
Walther@60557
   217
              end
walther@59822
   218
         end 
walther@59822
   219
    end
walther@59962
   220
  | input_icalhd _ (_, _, _, _(*Met*), _) = raise ERROR "input_icalhd Met not impl."
walther@59822
   221
walther@60132
   222
(**)end (**)