src/Tools/isac/Interpret/inform.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 26 Mar 2018 07:28:39 +0200
changeset 59416 229e5c9cf78b
parent 59409 b832f1f20bce
child 59555 125a54fa7be0
permissions -rw-r--r--
Rule: structure pushed to code files
neuper@37906
     1
(* Handle user-input during the specify- and the solve-phase. 
neuper@37906
     2
   author: Walther Neuper
neuper@37906
     3
   0603
neuper@37906
     4
   (c) due to copyright terms
neuper@37906
     5
*)
neuper@37906
     6
wneuper@59262
     7
signature INPUT_FORMULAS =
wneuper@59262
     8
sig
wneuper@59416
     9
  datatype iitem = Find of Rule.cterm' list | Given of Rule.cterm' list | Relate of Rule.cterm' list
wneuper@59262
    10
  type imodel = iitem list
wneuper@59416
    11
  type icalhd = Ctree.pos' * Rule.cterm' * imodel * Ctree.pos_ * Celem.spec
wneuper@59416
    12
  val fetchErrorpatterns : Tac.tac -> Rule.errpatID list
wneuper@59279
    13
  val input_icalhd : Ctree.ctree -> icalhd -> Ctree.ctree * Ctree.ocalhd
wneuper@59265
    14
  val inform : Chead.calcstate' -> string -> string * Chead.calcstate'
wneuper@59416
    15
  val find_fillpatterns : Ctree.state -> Rule.errpatID -> (Celem.fillpatID * term * thm * Selem.subs option) list
wneuper@59302
    16
  val is_exactly_equal : Ctree.state -> string -> string * Tac.tac
wneuper@59310
    17
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59310
    18
  (*  NONE *)
wneuper@59299
    19
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59262
    20
  val dropwhile' : ('a -> 'b -> bool) -> 'a list -> 'b list -> 'a list * 'b list
wneuper@59279
    21
  val cas_input : term -> (Ctree.ctree * Ctree.ocalhd) option
wneuper@59416
    22
  val rev_deriv' : 'a * Rule.rule * ('b * 'c) -> 'b * Rule.rule * ('a * 'c)
wneuper@59281
    23
  val compare_step : Generate.taci list * Ctree.pos' list * (Ctree.state) -> term -> string * Chead.calcstate'
wneuper@59416
    24
  val check_err_patt : term * term -> Rule.subst -> Rule.errpatID * term -> Rule.rls -> Rule.errpatID option
wneuper@59262
    25
  val concat_deriv : 'a * ((term * term) list -> term * term -> bool) ->
wneuper@59416
    26
    Rule.rls -> Rule.rule list -> term -> term -> bool * (term * Rule.rule * (term * term list)) list
wneuper@59262
    27
  val check_error_patterns :
wneuper@59262
    28
    term * term ->
wneuper@59416
    29
    term * (term * term) list -> (Rule.errpatID * term list * 'a list) list * Rule.rls -> Rule.errpatID option
wneuper@59262
    30
  val get_fillform :
wneuper@59416
    31
     'a * (term * term) list -> 'b * term -> Rule.errpatID -> Celem.fillpat -> (Celem.fillpatID * term * 'b * 'a) option
wneuper@59262
    32
  val get_fillpats :
wneuper@59416
    33
     'a * (term * term) list -> term -> Rule.errpatID -> thm -> (Celem.fillpatID * term * thm * 'a) list
wneuper@59299
    34
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
    35
wneuper@59310
    36
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59313
    37
  val e_icalhd : icalhd
wneuper@59313
    38
  val oris2itms : 'a -> ''b list -> ('c * ''b * 'd * term * term list) list ->
wneuper@59316
    39
    ('c * ''b * bool * 'd * Model.itm_) list
wneuper@59262
    40
end
neuper@37906
    41
wneuper@59262
    42
structure Inform(**): INPUT_FORMULAS(**) =
wneuper@59262
    43
struct
neuper@37906
    44
wneuper@59262
    45
(*** handle an input calc-head ***)
neuper@37906
    46
neuper@37906
    47
datatype iitem = 
wneuper@59416
    48
  Given of Rule.cterm' list
neuper@37906
    49
(*Where is never input*) 
wneuper@59416
    50
| Find  of Rule.cterm' list
wneuper@59416
    51
| Relate  of Rule.cterm' list
neuper@37906
    52
wneuper@59262
    53
type imodel = iitem list
neuper@37906
    54
neuper@37906
    55
(*calc-head as input*)
neuper@37906
    56
type icalhd =
wneuper@59276
    57
  Ctree.pos' *     (*the position of the calc-head in the calc-tree*) 
wneuper@59416
    58
  Rule.cterm' *   (*the headline*)
wneuper@59405
    59
  imodel *         (*the model (without Find) of the calc-head*)
wneuper@59276
    60
  Ctree.pos_ *     (*model belongs to Pbl or Met*)
wneuper@59405
    61
  Celem.spec;      (*specification: domID, pblID, metID*)
wneuper@59405
    62
val e_icalhd = (Ctree.e_pos', "", [Given [""]], Ctree.Pbl, Celem.e_spec)
neuper@37906
    63
wneuper@59416
    64
fun is_casinput (hdf : Rule.cterm') ((fmz_, spec) : Selem.fmz) =
wneuper@59405
    65
  hdf <> "" andalso fmz_ = [] andalso spec = Celem.e_spec
neuper@37906
    66
neuper@37906
    67
(*.handle an input as into an algebra system.*)
neuper@37906
    68
fun dtss2itm_ ppc (d, ts) =
wneuper@59264
    69
  let
wneuper@59264
    70
    val (f, (d, id)) = the (find_first ((curry op= d) o 
wneuper@59264
    71
  		(#1: (term * term) -> term) o
wneuper@59405
    72
  		(#2: Celem.pbt_ -> (term * term))) ppc)
wneuper@59264
    73
  in
wneuper@59316
    74
    ([1], true, f, Model.Cor ((d, ts), (id, ts)))
wneuper@59264
    75
  end
neuper@37906
    76
wneuper@59262
    77
fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
neuper@37906
    78
wneuper@59405
    79
fun cas_input_ ((dI, pI, mI): Celem.spec) dtss = (*WN110515 reconsider thy..ctxt*)
neuper@41995
    80
  let
wneuper@59405
    81
    val thy = Celem.assoc_thy dI
wneuper@59269
    82
	  val {ppc, ...} = Specify.get_pbt pI
neuper@41995
    83
	  val its_ = map (dtss2itm_ ppc) dtss (*([1],true,"#Given",Cor (...))*)
wneuper@59269
    84
	  val its = Specify.add_id its_
neuper@41995
    85
	  val pits = map flattup2 its
neuper@41995
    86
	  val (pI, mI) =
neuper@41995
    87
      if mI <> ["no_met"]
neuper@41995
    88
      then (pI, mI)
neuper@41995
    89
		  else
wneuper@59269
    90
        case Specify.refine_pbl thy pI pits of
wneuper@59269
    91
			    SOME (pI,_) => (pI, (hd o #met o Specify.get_pbt) pI)
wneuper@59269
    92
			  | NONE => (pI, (hd o #met o Specify.get_pbt) pI)
wneuper@59269
    93
	  val {ppc, pre, prls, ...} = Specify.get_met mI
neuper@41995
    94
	  val its_ = map (dtss2itm_ ppc) dtss (*([1],true,"#Given",Cor (...))*)
wneuper@59269
    95
	  val its = Specify.add_id its_
neuper@41995
    96
	  val mits = map flattup2 its
wneuper@59308
    97
	  val pre = Stool.check_preconds thy prls pre mits
wneuper@59301
    98
    val ctxt = Selem.e_ctxt (*WN110515 cas_input_ DOESNT WORK*)
wneuper@59308
    99
  in (pI, pits, mI, mits, pre, ctxt) end;
neuper@37906
   100
neuper@37906
   101
wneuper@59279
   102
(* check if the input term is a CAScmd and return a ctree with a _complete_ calchead *)
neuper@37906
   103
fun cas_input hdt =
wneuper@59262
   104
  let
wneuper@59262
   105
    val (h, argl) = strip_comb hdt
neuper@41995
   106
  in
wneuper@59405
   107
    case assoc_cas (Celem.assoc_thy "Isac") h of
wneuper@59262
   108
      NONE => NONE
wneuper@59262
   109
    | SOME (spec as (dI,_,_), argl2dtss) =>
neuper@41995
   110
	      let
neuper@41995
   111
          val dtss = argl2dtss argl
neuper@41995
   112
	        val (pI, pits, mI, mits, pre, ctxt) = cas_input_ spec dtss
neuper@41995
   113
	        val spec = (dI, pI, mI)
neuper@41995
   114
	        val (pt,_) = 
wneuper@59405
   115
		        Ctree.cappend_problem Ctree.e_ctree [] (Selem.e_istate, Selem.e_ctxt) ([], Celem.e_spec) ([], Celem.e_spec, hdt)
wneuper@59276
   116
	        val pt = Ctree.update_spec pt [] spec
wneuper@59276
   117
	        val pt = Ctree.update_pbl pt [] pits
wneuper@59276
   118
	        val pt = Ctree.update_met pt [] mits
wneuper@59276
   119
	        val pt = Ctree.update_ctxt pt [] ctxt
wneuper@59262
   120
	      in
wneuper@59276
   121
	        SOME (pt, (true, Ctree.Met, hdt, mits, pre, spec) : Ctree.ocalhd)
wneuper@59262
   122
	      end
wneuper@59262
   123
  end
neuper@37906
   124
wneuper@59352
   125
(*lazy evaluation for (Thy_Info_get_theory "Isac")*)
wneuper@59405
   126
fun Isac _  = Celem.assoc_thy "Isac";
neuper@37906
   127
wneuper@59262
   128
(* re-parse itms with a new thy and prepare for checking with ori list *)
wneuper@59316
   129
fun parsitm dI (itm as (i, v, _, f, Model.Cor ((d, ts), _))) =
wneuper@59316
   130
    (let val t = Model.comp_dts (d, ts)
wneuper@59416
   131
     val _ = (Rule.term_to_string''' dI t)
wneuper@59262
   132
     (*t his ^^^^^^^^^^^^ should raise the exn on unability of re-parsing dts *)
neuper@52070
   133
    in itm end
wneuper@59416
   134
    handle _ => (i, v, false, f, Model.Syn (Rule.term2str Rule.e_term (*t  ..(t) has not been declared*))))
wneuper@59316
   135
  | parsitm dI (i, v, b, f, Model.Syn str) =
wneuper@59389
   136
    (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
wneuper@59316
   137
    in (i, v, b ,f, Model.Par str) end
wneuper@59316
   138
    handle _ => (i, v, b, f, Model.Syn str))
wneuper@59316
   139
  | parsitm dI (i, v, b, f, Model.Typ str) =
wneuper@59389
   140
    (let val _ = (Thm.term_of o the o (TermC.parse dI)) str
wneuper@59316
   141
     in (i, v, b, f, Model.Par str) end
wneuper@59316
   142
     handle _ => (i, v, b, f, Model.Syn str))
wneuper@59316
   143
  | parsitm dI (itm as (i, v, _, f, Model.Inc ((d, ts), _))) =
wneuper@59316
   144
    (let val t = Model.comp_dts (d,ts);
wneuper@59416
   145
	       val _ = Rule.term_to_string''' dI t;
neuper@37906
   146
     (*this    ^ should raise the exn on unability of re-parsing dts*)
neuper@52070
   147
     in itm end
wneuper@59416
   148
     handle _ => (i, v, false, f, Model.Syn (Rule.term2str Rule.e_term (*t  ..(t) has not been declared*))))
wneuper@59316
   149
  | parsitm dI (itm as (i, v, _, f, Model.Sup (d, ts))) =
wneuper@59316
   150
    (let val t = Model.comp_dts (d,ts);
wneuper@59416
   151
	       val _ = Rule.term_to_string''' dI t;
neuper@37906
   152
     (*this    ^ should raise the exn on unability of re-parsing dts*)
neuper@52070
   153
    in itm end
wneuper@59416
   154
    handle _ => (i, v, false, f, Model.Syn (Rule.term2str Rule.e_term (*t  ..(t) has not been declared*))))
wneuper@59316
   155
  | parsitm dI (itm as (i, v, _, f, Model.Mis (d, t'))) =
neuper@37906
   156
    (let val t = d $ t';
wneuper@59416
   157
	       val _ = Rule.term_to_string''' dI t;
neuper@37906
   158
     (*this    ^ should raise the exn on unability of re-parsing dts*)
neuper@52070
   159
    in itm end
wneuper@59416
   160
    handle _ => (i, v, false, f, Model.Syn (Rule.term2str Rule.e_term (*t  ..(t) has not been declared*))))
wneuper@59316
   161
  | parsitm dI (itm as (_, _, _, _, Model.Par _)) = 
wneuper@59416
   162
    error ("parsitm (" ^ Model.itm2str_ (Rule.thy2ctxt dI) itm ^ "): Par should be internal");
neuper@37906
   163
neuper@37906
   164
(*separate a list to a pair of elements that do NOT satisfy the predicate,
neuper@37906
   165
 and of elements that satisfy the predicate, i.e. (false, true)*)
neuper@37906
   166
fun filter_sep pred xs =
wneuper@59262
   167
  let
wneuper@59262
   168
    fun filt ab [] = ab
wneuper@59262
   169
      | filt (a, b) (x :: xs) =
wneuper@59262
   170
        if pred x 
wneuper@59262
   171
			  then filt (a, b @ [x]) xs 
wneuper@59262
   172
			  else filt (a @ [x], b) xs
wneuper@59262
   173
  in filt ([], []) xs end;
wneuper@59316
   174
fun is_Par (_, _, _, _, Model.Par _) = true
neuper@37906
   175
  | is_Par _ = false;
neuper@37906
   176
neuper@37906
   177
fun is_e_ts [] = true
neuper@37906
   178
  | is_e_ts [Const ("List.list.Nil", _)] = true
neuper@37906
   179
  | is_e_ts _ = false;
neuper@37906
   180
wneuper@59262
   181
(* WN.9.11.03 copied from fun appl_add *)
neuper@37906
   182
fun appl_add' dI oris ppc pbt (sel, ct) = 
wneuper@59262
   183
  let 
wneuper@59416
   184
     val ctxt = Celem.assoc_thy dI |> Rule.thy2ctxt;
wneuper@59262
   185
  in
wneuper@59389
   186
    case TermC.parseNEW ctxt ct of
wneuper@59316
   187
	    NONE => (0, [], false, sel, Model.Syn ct)
wneuper@59262
   188
	  | SOME t =>
wneuper@59265
   189
	    (case Chead.is_known ctxt sel oris t of
wneuper@59262
   190
        ("", ori', all) =>
wneuper@59265
   191
          (case Chead.is_notyet_input ctxt ppc all ori' pbt of
wneuper@59262
   192
            ("",itm)  => itm
wneuper@59262
   193
          | (msg,_) => error ("appl_add': " ^ msg))
wneuper@59262
   194
      | (_, (i, v, _, d, ts), _) =>
wneuper@59262
   195
        if is_e_ts ts
wneuper@59416
   196
        then (i, v, false, sel, Model.Inc ((d, ts), (Rule.e_term, [])))
wneuper@59316
   197
        else (i, v, false, sel, Model.Sup (d, ts)))
wneuper@59262
   198
   end
neuper@37906
   199
wneuper@59262
   200
(* generate preliminary itm_ from a strin (with field "#Given" etc.) *)
wneuper@59262
   201
fun eq7 (f, d) (f', (d', _)) = f = f' andalso d = d';
neuper@37906
   202
fun fstr2itm_ thy pbt (f, str) =
wneuper@59262
   203
  let
wneuper@59389
   204
    val topt = TermC.parse thy str
wneuper@59262
   205
  in
wneuper@59262
   206
    case topt of
wneuper@59316
   207
      NONE => ([], false, f, Model.Syn str)
wneuper@59262
   208
    | SOME ct => 
wneuper@59262
   209
	    let
wneuper@59316
   210
	      val (d, ts) = (Model.split_dts o Thm.term_of) ct
wneuper@59262
   211
	      val popt = find_first (eq7 (f, d)) pbt
wneuper@59262
   212
	    in
wneuper@59262
   213
	      case popt of
wneuper@59316
   214
	        NONE => ([1](*??*), true(*??*), f, Model.Sup (d, ts))
wneuper@59316
   215
	      | SOME (f, (d, id)) => ([1], true, f, Model.Cor ((d, ts), (id, ts))) 
wneuper@59262
   216
	    end
wneuper@59262
   217
  end
neuper@37906
   218
neuper@37906
   219
(*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
neuper@37906
   220
fun unknown_expl dI pbt selcts =
neuper@37906
   221
  let
wneuper@59405
   222
    val thy = Celem.assoc_thy dI
neuper@37906
   223
    val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
wneuper@59269
   224
    val its = Specify.add_id its_ 
wneuper@59308
   225
  in map flattup2 its end
neuper@37906
   226
wneuper@59262
   227
(* WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
wneuper@59262
   228
   appl_add': generate 1 item 
wneuper@59262
   229
   appl_add' . is_known: parse, get data from oris (vats, all (elems if list)..)
wneuper@59262
   230
   appl_add' . is_notyet_input: compare with items in model already input
wneuper@59262
   231
   insert_ppc': insert this 1 item*)
wneuper@59262
   232
fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
wneuper@59262
   233
    (*already present itms in model are being overwritten*)
wneuper@59262
   234
  | appl_adds _ _ ppc _ [] = ppc
wneuper@59262
   235
  | appl_adds dI oris ppc pbt (selct :: ss) =
wneuper@59262
   236
    let val itm = appl_add' dI oris ppc pbt selct;
wneuper@59265
   237
    in appl_adds dI oris (Chead.insert_ppc' itm ppc) pbt ss end
neuper@37906
   238
wneuper@59308
   239
fun oris2itms _  _ [] = [] (* WN161130: similar in ptyps ?!? *)
wneuper@59308
   240
  | oris2itms pbt vat ((i, v, f, d, ts) :: os) =
neuper@37930
   241
    if member op = vat v 
wneuper@59416
   242
    then (i, v, true, f, Model.Cor ((d, ts), (Rule.e_term, []))) :: (oris2itms pbt vat os)
wneuper@59262
   243
    else oris2itms pbt vat os
neuper@37906
   244
wneuper@59316
   245
fun par2fstr (_, _, _, f, Model.Par s) = (f, s)
wneuper@59416
   246
  | par2fstr itm = error ("par2fstr: called with " ^ Model.itm2str_ (Rule.thy2ctxt' "Isac") itm)
wneuper@59316
   247
fun itms2fstr (_, _, _, f, Model.Cor ((d, ts), _)) = (f, Model.comp_dts'' (d, ts))
wneuper@59316
   248
  | itms2fstr (_, _, _, f, Model.Syn str) = (f, str)
wneuper@59316
   249
  | itms2fstr (_, _, _, f, Model.Typ str) = (f, str)
wneuper@59316
   250
  | itms2fstr (_, _, _, f, Model.Inc ((d, ts), _)) = (f, Model.comp_dts'' (d,ts))
wneuper@59316
   251
  | itms2fstr (_, _, _, f, Model.Sup (d, ts)) = (f, Model.comp_dts'' (d, ts))
wneuper@59416
   252
  | itms2fstr (_, _, _, f, Model.Mis (d, t)) = (f, Rule.term2str (d $ t))
wneuper@59316
   253
  | itms2fstr (itm as (_, _, _, _, Model.Par _)) = 
wneuper@59416
   254
    error ("parsitm (" ^ Model.itm2str_ (Rule.thy2ctxt' "Isac") itm ^ "): Par should be internal");
neuper@37906
   255
neuper@37906
   256
fun imodel2fstr iitems = 
neuper@41976
   257
  let 
neuper@41976
   258
    fun xxx is [] = is
neuper@41976
   259
	    | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
neuper@41976
   260
	    | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
neuper@41976
   261
	    | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
neuper@41976
   262
  in xxx [] iitems end;
neuper@37906
   263
neuper@41976
   264
(* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
wneuper@59313
   265
fun input_icalhd pt (((p, _), hdf, imodel, Ctree.Pbl, spec as (dI, pI, mI)) : icalhd) =
wneuper@59264
   266
    let
wneuper@59276
   267
		  val (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth) = case Ctree.get_obj I pt p of
wneuper@59276
   268
		    Ctree.PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
wneuper@59264
   269
		      spec = sspec as (sdI, spI, smI), probl, meth, ...}
wneuper@59264
   270
        => (fmz, fmz_, oris, ospec, hdf', sspec, sdI, spI, smI, probl, meth)
wneuper@59264
   271
      | _ => error "input_icalhd: uncovered case of get_obj I pt p"
wneuper@59389
   272
    in if is_casinput hdf fmz then the (cas_input (TermC.str2term hdf)) 
neuper@37906
   273
       else        (*hacked WN0602 ~~~            ~~~~~~~~~,   ..dropped !*)
neuper@41976
   274
         let val (pos_, pits, mits) = 
neuper@41976
   275
	         if dI <> sdI
wneuper@59405
   276
	         then let val its = map (parsitm (Celem.assoc_thy dI)) probl;
neuper@41976
   277
			            val (its, trms) = filter_sep is_Par its;
wneuper@59269
   278
			            val pbt = (#ppc o Specify.get_pbt) (#2 (Chead.some_spec ospec sspec))
wneuper@59313
   279
		            in (Ctree.Pbl, appl_adds dI oris its pbt  (map par2fstr trms), meth) end 
wneuper@59264
   280
           else
wneuper@59264
   281
             if pI <> spI 
wneuper@59313
   282
	           then if pI = snd3 ospec then (Ctree.Pbl, probl, meth) 
wneuper@59264
   283
                  else
wneuper@59269
   284
		                let val pbt = (#ppc o Specify.get_pbt) pI
wneuper@59265
   285
			                val dI' = #1 (Chead.some_spec ospec spec)
wneuper@59264
   286
			                val oris = if pI = #2 ospec then oris 
wneuper@59405
   287
				                         else Specify.prep_ori fmz_(Celem.assoc_thy"Isac") pbt |> #1;
wneuper@59313
   288
		                in (Ctree.Pbl, appl_adds dI' oris probl pbt 
wneuper@59264
   289
				              (map itms2fstr probl), meth) end 
wneuper@59264
   290
             else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
wneuper@59269
   291
	                then let val met = (#ppc o Specify.get_met) mI
wneuper@59265
   292
		                     val mits = Chead.complete_metitms oris probl meth met
wneuper@59264
   293
		                   in if foldl and_ (true, map #3 mits)
wneuper@59313
   294
		                      then (Ctree.Pbl, probl, mits) else (Ctree.Met, probl, mits) 
wneuper@59264
   295
		                   end 
wneuper@59313
   296
                  else (Ctree.Pbl, appl_adds (#1 (Chead.some_spec ospec spec)) oris [(*!!!*)]
wneuper@59269
   297
			                  ((#ppc o Specify.get_pbt) (#2 (Chead.some_spec ospec spec)))
wneuper@59264
   298
			                  (imodel2fstr imodel), meth)
wneuper@59276
   299
	         val pt = Ctree.update_spec pt p spec;
wneuper@59313
   300
         in if pos_ = Ctree.Pbl
wneuper@59269
   301
	          then let val {prls,where_,...} = Specify.get_pbt (#2 (Chead.some_spec ospec spec))
wneuper@59405
   302
		               val pre = Stool.check_preconds (Celem.assoc_thy"Isac") prls where_ pits
wneuper@59276
   303
	               in (Ctree.update_pbl pt p pits,
wneuper@59313
   304
		                 (Chead.ocalhd_complete pits pre spec, Ctree.Pbl, hdf', pits, pre, spec): Ctree.ocalhd) 
neuper@41976
   305
                 end
wneuper@59269
   306
	           else let val {prls,pre,...} = Specify.get_met (#3 (Chead.some_spec ospec spec))
wneuper@59405
   307
		                val pre = Stool.check_preconds (Celem.assoc_thy"Isac") prls pre mits
wneuper@59276
   308
	                in (Ctree.update_met pt p mits,
wneuper@59276
   309
		                  (Chead.ocalhd_complete mits pre spec, Ctree.Met, hdf', mits, pre, spec) : Ctree.ocalhd)
neuper@41976
   310
                  end
neuper@41976
   311
         end 
neuper@41976
   312
    end
wneuper@59264
   313
  | input_icalhd _ (_, _, _, _(*Met*), _) = error "input_icalhd Met not impl."
neuper@37906
   314
neuper@37906
   315
neuper@37906
   316
(***. handle an input formula .***)
neuper@37906
   317
neuper@37906
   318
(*the lists contain eq-al elem-pairs at the beginning;
neuper@37906
   319
  return first list reverted (again) - ie. in order as required subsequently*)
neuper@37906
   320
fun dropwhile' equal (f1::f2::fs) (i1::i2::is) =
wneuper@59264
   321
    if equal f1 i1
wneuper@59264
   322
    then
wneuper@59264
   323
      if equal f2 i2 then dropwhile' equal (f2 :: fs) (i2 :: is)
wneuper@59264
   324
      else (rev (f1 :: f2 :: fs), i1 :: i2 :: is)
neuper@38031
   325
    else error "dropwhile': did not start with equal elements"
neuper@37906
   326
  | dropwhile' equal (f::fs) [i] =
wneuper@59264
   327
    if equal f i
wneuper@59264
   328
    then (rev (f::fs), [i])
neuper@38031
   329
    else error "dropwhile': did not start with equal elements"
neuper@37906
   330
  | dropwhile' equal [f] (i::is) =
wneuper@59264
   331
    if equal f i
wneuper@59264
   332
    then ([f], i::is)
wneuper@59264
   333
    else error "dropwhile': did not start with equal elements"
wneuper@59264
   334
  | dropwhile' _ _ _ = error "dropwhile': uncovered case"
neuper@37906
   335
wneuper@59264
   336
(* 040214: version for concat_deriv *)
wneuper@59263
   337
fun rev_deriv' (t, r, (t', a)) = (t', Rtools.sym_rule r, (t, a));
neuper@37906
   338
wneuper@59416
   339
fun mk_tacis ro erls (t, r as Rule.Thm (id, thm), (t', a)) = 
wneuper@59302
   340
      (Tac.Rewrite (id, thm), 
wneuper@59302
   341
        Tac.Rewrite' ("Isac", fst ro, erls, false, Lucin.rule2thm'' r, t, (t', a)),
wneuper@59301
   342
       (Ctree.e_pos'(*to be updated before generate tacis!!!*), (Selem.Uistate, Selem.e_ctxt)))
wneuper@59416
   343
  | mk_tacis _ _ (t, r as Rule.Rls_ rls, (t', a)) = 
wneuper@59302
   344
      (Tac.Rewrite_Set (Lucin.rule2rls' r), 
wneuper@59302
   345
        Tac.Rewrite_Set' ("Isac", false, rls, t, (t', a)),
wneuper@59301
   346
       (Ctree.e_pos'(*to be updated before generate tacis!!!*), (Selem.Uistate, Selem.e_ctxt)))
wneuper@59416
   347
  | mk_tacis _ _ (t, r, _) = error ("mk_tacis: not impl. for " ^ Rule.rule2str r ^ " at " ^ Rule.term2str t)
neuper@37906
   348
wneuper@59264
   349
(* fo = ifo excluded already in inform *)
neuper@37906
   350
fun concat_deriv rew_ord erls rules fo ifo =
neuper@55487
   351
  let 
wneuper@59416
   352
    fun derivat ([]:(term * Rule.rule * (term * term list)) list) = Rule.e_term
neuper@55487
   353
      | derivat dt = (#1 o #3 o last_elem) dt
neuper@55487
   354
    fun equal (_,_,(t1, _)) (_,_,(t2, _)) = t1=t2
wneuper@59263
   355
    val  fod = Rtools.make_deriv (Isac"") erls rules (snd rew_ord) NONE  fo
wneuper@59263
   356
    val ifod = Rtools.make_deriv (Isac"") erls rules (snd rew_ord) NONE ifo
neuper@55487
   357
  in 
neuper@55487
   358
    case (fod, ifod) of
neuper@55487
   359
      ([], []) => if fo = ifo then (true, []) else (false, [])
neuper@55487
   360
    | (fod, []) => if derivat fod = ifo then (true, fod) (*ifo is normal form*) else (false, [])
neuper@55487
   361
    | ([], ifod) => if fo = derivat ifod then (true, ((map rev_deriv') o rev) ifod) else (false, [])
neuper@55487
   362
    | (fod, ifod) =>
neuper@55487
   363
      if derivat fod = derivat ifod (*common normal form found*)
neuper@55487
   364
      then
neuper@55487
   365
        let 
neuper@55487
   366
          val (fod', rifod') = dropwhile' equal (rev fod) (rev ifod)
neuper@55487
   367
        in (true, fod' @ (map rev_deriv' rifod')) end
neuper@55487
   368
      else (false, [])
wneuper@59264
   369
  end
neuper@37906
   370
neuper@41995
   371
(* compare inform with ctree.form at current pos by nrls;
neuper@37906
   372
   if found, embed the derivation generated during comparison
neuper@41995
   373
   if not, let the mat-engine compute the next ctree.form *)
wneuper@59280
   374
(* code's structure is copied from complete_solve
wneuper@59264
   375
   CAUTION: tacis in returned calcstate' do NOT construct resulting ptp --
wneuper@59264
   376
            all_modspec etc. has to be inserted at Subproblem'*)
wneuper@59265
   377
fun compare_step ((tacis, c, ptp as (pt, pos as (p,p_))): Chead.calcstate') ifo =
neuper@41995
   378
  let
neuper@41995
   379
    val fo =
neuper@41995
   380
      case p_ of
wneuper@59313
   381
        Ctree.Frm => Ctree.get_obj Ctree.g_form pt p
wneuper@59313
   382
			| Ctree.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
wneuper@59416
   383
			| _ => Rule.e_term (*on PblObj is fo <> ifo*);
wneuper@59276
   384
	  val {nrls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt (Ctree.par_pblobj pt p))
wneuper@59416
   385
	  val {rew_ord, erls, rules, ...} = Rule.rep_rls nrls
neuper@42423
   386
	  val (found, der) = concat_deriv rew_ord erls rules fo ifo; (*<---------------*)
neuper@41995
   387
  in
neuper@41995
   388
    if found
neuper@41995
   389
    then
neuper@41995
   390
       let
neuper@41995
   391
         val tacis' = map (mk_tacis rew_ord erls) der;
wneuper@59271
   392
		     val (c', ptp) = Generate.embed_deriv tacis' ptp;
neuper@41995
   393
	     in ("ok", (tacis (*@ tacis'?WN050408*), c @ c', ptp)) end
neuper@41995
   394
     else 
wneuper@59276
   395
	     if pos = ([], Ctree.Res) (*TODO: we should stop earlier with trying subproblems *)
wneuper@59265
   396
	     then ("no derivation found", (tacis, c, ptp): Chead.calcstate') 
neuper@41995
   397
	     else
neuper@41995
   398
         let
wneuper@59273
   399
           val cs' as (tacis, c', ptp) = Solve.nxt_solve_ ptp; (*<---------------------*)
wneuper@59264
   400
		       val (tacis, c'', ptp) = case tacis of
wneuper@59302
   401
			       ((Tac.Subproblem _, _, _)::_) => 
wneuper@59264
   402
			         let
wneuper@59265
   403
                 val ptp as (pt, (p,_)) = Chead.all_modspec ptp (*<--------------------*)
wneuper@59276
   404
				         val mI = Ctree.get_obj Ctree.g_metID pt p
wneuper@59264
   405
			         in
wneuper@59302
   406
			           Solve.nxt_solv (Tac.Apply_Method' (mI, NONE, Selem.e_istate, Selem.e_ctxt)) (Selem.e_istate, Selem.e_ctxt) ptp
wneuper@59264
   407
               end
wneuper@59264
   408
			     | _ => cs';
neuper@41995
   409
		     in compare_step (tacis, c @ c' @ c'', ptp) ifo end
wneuper@59265
   410
  end
neuper@37906
   411
neuper@42428
   412
(* check if (agreed result, input formula) matches the error pattern "pat" modulo simplifier rls *)
wneuper@59405
   413
fun check_err_patt (res, inf) subst (errpatID, pat) rls =
neuper@42423
   414
  let 
neuper@42423
   415
    val (res', _, _, rewritten) =
wneuper@59416
   416
      Rewrite.rew_sub (Isac()) 1 subst Rule.e_rew_ord Rule.e_rls false [] (HOLogic.Trueprop $ pat) res;
neuper@42423
   417
  in
neuper@42423
   418
    if rewritten
neuper@42423
   419
    then 
neuper@42423
   420
      let
wneuper@59380
   421
        val norm_res = case Rewrite.rewrite_set_inst_ (Isac()) false subst rls  res' of
neuper@42423
   422
          NONE => res'
neuper@42423
   423
        | SOME (norm_res, _) => norm_res
wneuper@59380
   424
        val norm_inf = case Rewrite.rewrite_set_inst_ (Isac()) false subst rls inf of
neuper@42423
   425
          NONE => inf
neuper@42423
   426
        | SOME (norm_inf, _) => norm_inf
neuper@42423
   427
      in if norm_res = norm_inf then SOME errpatID else NONE
neuper@42423
   428
      end
neuper@42423
   429
    else NONE
neuper@42423
   430
  end;
neuper@42423
   431
neuper@42428
   432
(* check if (agreed result, input formula) matches SOME error pattern modulo simplifier rls;
neuper@42428
   433
   (prog, env) for retrieval of casual substitution for bdv in the pattern. *)
neuper@42428
   434
fun check_error_patterns (res, inf) (prog, env) (errpats, rls) =
neuper@42428
   435
  let
wneuper@59263
   436
    val (_, subst) = Rtools.get_bdv_subst prog env
neuper@42428
   437
    fun scan (_, [], _) = NONE
neuper@42428
   438
      | scan (errpatID, errpat :: errpats, _) =
wneuper@59405
   439
          case check_err_patt (res, inf) subst (errpatID, errpat) rls of
neuper@42428
   440
            NONE => scan (errpatID, errpats, [])
neuper@42428
   441
          | SOME errpatID => SOME errpatID
neuper@42428
   442
    fun scans [] = NONE
neuper@42428
   443
      | scans (group :: groups) =
neuper@42428
   444
          case scan group of
neuper@42428
   445
            NONE => scans groups
neuper@42428
   446
          | SOME errpatID => SOME errpatID
neuper@42428
   447
  in scans errpats end;
neuper@42428
   448
neuper@37906
   449
(*.handle a user-input formula, which may be a CAS-command, too.
neuper@37906
   450
CAS-command:
neuper@37906
   451
   create a calchead, and do 1 step
neuper@37906
   452
   TOOODO.WN0602 works only for the root-problem !!!
neuper@37906
   453
formula, which is no CAS-command:
neuper@37906
   454
   compare iform with calc-tree.form at pos by equ_nrls and all subsequent pos;
neuper@42423
   455
   collect all the tacs applied by the way;
neuper@42426
   456
   if "no derivation found" then check_error_patterns.
neuper@42427
   457
   ALTERNATIVE: check_error_patterns _within_ compare_step seems too expensive.*)
wneuper@59265
   458
fun inform (cs as (_, _, (pt, pos as (p, _))): Chead.calcstate') istr =
wneuper@59405
   459
  case TermC.parse (Celem.assoc_thy "Isac") istr of
neuper@42427
   460
	  SOME f_in =>
neuper@42423
   461
	    let
wneuper@59186
   462
	      val f_in = Thm.term_of f_in
wneuper@59276
   463
	      val f_succ = Ctree.get_curr_formula (pt, pos);
neuper@42423
   464
			in
neuper@42427
   465
			  if f_succ = f_in
neuper@42423
   466
			  then ("same-formula", cs) (* ctree not cut with replaceFormula *)
neuper@42423
   467
			  else
neuper@42427
   468
			    case cas_input f_in of
wneuper@59276
   469
			      SOME (pt, _) => ("ok",([], [], (pt, (p, Ctree.Met))))
neuper@42423
   470
			    | NONE =>
neuper@42428
   471
			        let
wneuper@59276
   472
			          val pos_pred = Ctree.lev_back' pos (*f_pred ---"step pos cs"---> f_succ in appendFormula*)
wneuper@59276
   473
			          val f_pred = Ctree.get_curr_formula (pt, pos_pred)
neuper@42430
   474
			          val msg_calcstate' = compare_step ([], [], (pt, pos_pred)) f_in (*<<=====*)
neuper@42426
   475
			          (*last step re-calc in compare_step TODO before WN09*)
neuper@42423
   476
			        in
neuper@42423
   477
			          case msg_calcstate' of
neuper@42426
   478
			            ("no derivation found", calcstate') => 
neuper@42426
   479
			               let
wneuper@59276
   480
			                 val pp = Ctree.par_pblobj pt p
wneuper@59276
   481
			                 val (errpats, nrls, prog) = case Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp) of
wneuper@59416
   482
			                   {errpats, nrls, scr = Rule.Prog prog, ...} => (errpats, nrls, prog)
wneuper@59264
   483
			                 | _ => error "inform: uncovered case of get_met"
wneuper@59276
   484
			                 val env = case Ctree.get_istate pt pos of
wneuper@59300
   485
			                   Selem.ScrState (env, _, _, _, _, _) => env
wneuper@59264
   486
			                 | _ => error "inform: uncovered case of get_istate"
neuper@42426
   487
			               in
neuper@42428
   488
			                 case check_error_patterns (f_pred, f_in) (prog, env) (errpats, nrls) of
neuper@42426
   489
			                   SOME errpatID => ("error pattern #" ^ errpatID ^ "#", calcstate')
neuper@42426
   490
			                 | NONE => msg_calcstate'
neuper@42426
   491
			               end
neuper@42423
   492
			          | _ => msg_calcstate'
neuper@42423
   493
			        end
neuper@42423
   494
			end
wneuper@59265
   495
    | NONE => ("syntax error in '" ^ istr ^ "'", Chead.e_calcstate')
neuper@37906
   496
neuper@42433
   497
(* fill-in patterns an forms.
neuper@42433
   498
  returns thm required by "fun in_fillform *)
wneuper@59405
   499
fun get_fillform (subs_opt, subst) (thm, form) errpatID (fillpatID, pat, erpaID) =
neuper@42430
   500
  let
neuper@42430
   501
    val (form', _, _, rewritten) =
wneuper@59416
   502
      Rewrite.rew_sub (Isac()) 1 subst Rule.e_rew_ord Rule.e_rls false [] (HOLogic.Trueprop $ pat) form;
neuper@42430
   503
  in (*the fillpat of the thm must be dedicated to errpatID*)
neuper@42430
   504
    if errpatID = erpaID andalso rewritten
wneuper@59264
   505
    then SOME (fillpatID, HOLogic.mk_eq (form, form'), thm, subs_opt) 
wneuper@59264
   506
    else NONE
wneuper@59264
   507
  end
neuper@42430
   508
neuper@42430
   509
fun get_fillpats subst form errpatID thm =
wneuper@59264
   510
  let
wneuper@59264
   511
    val thmDeriv = Thm.get_name_hint thm
wneuper@59264
   512
    val (part, thyID) = Rtools.thy_containing_thm thmDeriv
wneuper@59405
   513
    val theID = [part, thyID, "Theorems", Celem.thmID_of_derivation_name thmDeriv]
wneuper@59269
   514
    val fillpats = case Specify.get_the theID of
wneuper@59405
   515
      Celem.Hthm {fillpats, ...} => fillpats
wneuper@59264
   516
    | _ => error "get_fillpats: uncovered case of get_the"
wneuper@59264
   517
    val some = map (get_fillform subst (thm, form) errpatID) fillpats
wneuper@59264
   518
  in some |> filter is_some |> map the end
neuper@42430
   519
wneuper@59276
   520
fun find_fillpatterns (pt, pos as (p, _): Ctree.pos') errpatID =
neuper@42430
   521
  let 
wneuper@59276
   522
    val f_curr = Ctree.get_curr_formula (pt, pos);
wneuper@59276
   523
    val pp = Ctree.par_pblobj pt p
wneuper@59276
   524
    val (errpats, prog) = case Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp) of
wneuper@59416
   525
      {errpats, scr = Rule.Prog prog, ...} => (errpats, prog)
wneuper@59264
   526
    | _ => error "find_fillpatterns: uncovered case of get_met"
wneuper@59276
   527
    val env = case Ctree.get_istate pt pos of
wneuper@59300
   528
		  Selem.ScrState (env, _, _, _, _, _) => env
wneuper@59264
   529
		| _ => error "inform: uncovered case of get_istate"
wneuper@59263
   530
    val subst = Rtools.get_bdv_subst prog env
neuper@42430
   531
    val errpatthms = errpats
wneuper@59416
   532
      |> filter ((curry op = errpatID) o (#1: Rule.errpat -> Rule.errpatID))
wneuper@59416
   533
      |> map (#3: Rule.errpat -> thm list)
neuper@42430
   534
      |> flat
wneuper@59264
   535
  in map (get_fillpats subst f_curr errpatID) errpatthms |> flat end
neuper@37906
   536
neuper@42437
   537
(* check if an input formula is exactly equal the rewrite from a rule
neuper@42437
   538
   which is stored at the pos where the input is stored of "ok". *)
neuper@42437
   539
fun is_exactly_equal (pt, pos as (p, _)) istr =
wneuper@59416
   540
  case TermC.parseNEW (Celem.assoc_thy "Isac" |> Rule.thy2ctxt) istr of
wneuper@59302
   541
    NONE => ("syntax error in '" ^ istr ^ "'", Tac.Tac "")
neuper@42437
   542
  | SOME ifo => 
neuper@42437
   543
      let
wneuper@59276
   544
        val p' = Ctree.lev_on p;
wneuper@59276
   545
        val tac = Ctree.get_obj Ctree.g_tac pt p';
neuper@42437
   546
      in 
wneuper@59272
   547
        case Applicable.applicable_in pos pt tac of
wneuper@59302
   548
          Chead.Notappl msg => (msg, Tac.Tac "")
wneuper@59265
   549
        | Chead.Appl rew =>
neuper@42437
   550
            let
neuper@42437
   551
              val res = case rew of
wneuper@59302
   552
               Tac.Rewrite_Inst' (_, _, _, _, _, _, _, (res, _)) => res
wneuper@59302
   553
              |Tac.Rewrite' (_, _, _, _, _, _, (res, _)) => res
wneuper@59302
   554
              | t => error ("is_exactly_equal: uncovered case for " ^ Tac.tac_2str t)
neuper@42437
   555
            in 
neuper@42437
   556
              if not (ifo = res)
wneuper@59302
   557
              then ("input does not exactly fill the gaps", Tac.Tac "")
neuper@42437
   558
              else ("ok", tac)
neuper@42437
   559
            end
neuper@42437
   560
      end
neuper@42437
   561
neuper@42458
   562
(* fetch errpatIDs from an arbitrary tactic *)
neuper@42458
   563
fun fetchErrorpatterns tac =
neuper@42458
   564
  let
neuper@42458
   565
    val rlsID =
neuper@42458
   566
      case tac of
wneuper@59302
   567
       Tac.Rewrite_Set rlsID => rlsID
wneuper@59302
   568
      |Tac.Rewrite_Set_Inst (_, rlsID) => rlsID
neuper@55415
   569
      | _ => "e_rls"
wneuper@59263
   570
    val (part, thyID) = Rtools.thy_containing_rls "Isac" rlsID;
wneuper@59269
   571
    val rls = case Specify.get_the [part, thyID, "Rulesets", rlsID] of
wneuper@59405
   572
      Celem.Hrls {thy_rls = (_, rls), ...} => rls
wneuper@59264
   573
    | _ => error "fetchErrorpatterns: uncovered case of get_the"
neuper@42458
   574
  in case rls of
wneuper@59416
   575
    Rule.Rls {errpatts, ...} => errpatts
wneuper@59416
   576
  | Rule.Seq {errpatts, ...} => errpatts
wneuper@59416
   577
  | Rule.Rrls {errpatts, ...} => errpatts
wneuper@59416
   578
  | Rule.Erls => []
neuper@42458
   579
  end
wneuper@59264
   580
wneuper@59262
   581
(**)
neuper@37906
   582
end
wneuper@59262
   583
(**)