src/Tools/isac/Interpret/ptyps.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 15 Mar 2018 10:17:44 +0100
changeset 59405 49d7d410b83c
parent 59393 4274a44ec183
child 59411 3e241a6938ce
permissions -rw-r--r--
separate structure Celem: CALC_ELEMENT, all but Knowledge/
neuper@37906
     1
(* the problems and methods as stored in hierarchies
e0726734@41962
     2
   author Walther Neuper 1998, Mathias Lehnfeld
neuper@37906
     3
   (c) due to copyright terms
neuper@37906
     4
*)
neuper@37906
     5
wneuper@59269
     6
signature MODEL_SPECIFY =
wneuper@59269
     7
sig
wneuper@59270
     8
  type field
wneuper@59270
     9
  (* for calchead.sml, if below at left margin *)
wneuper@59316
    10
  val prep_ori : Selem.fmz_ -> theory -> field list -> Model.ori list * Proof.context
wneuper@59269
    11
  val add_id : 'a list -> (int * 'a) list
wneuper@59316
    12
  val add_field' : theory -> field list -> Model.ori list -> Model.ori list
wneuper@59405
    13
  val match_itms_oris : theory -> Model.itm list -> field list * term list * Celem.rls ->
wneuper@59316
    14
    Model.ori list -> bool * (Model.itm list * (bool * term) list)
wneuper@59405
    15
  val refine_ori : Model.ori list -> Celem.pblID -> Celem.pblID option
wneuper@59405
    16
  val refine_ori' : Model.ori list -> Celem.pblID -> Celem.pblID
wneuper@59405
    17
  val refine_pbl : theory -> Celem.pblID -> Model.itm list ->
wneuper@59405
    18
    (Celem.pblID * (Model.itm list * (bool * term) list)) option
wneuper@59270
    19
wneuper@59316
    20
  val appc : ('a list -> 'b list) -> 'a Model.ppc -> 'b Model.ppc
wneuper@59316
    21
  val mappc : ('a -> 'b) -> 'a Model.ppc -> 'b Model.ppc
wneuper@59316
    22
  val itms2itemppc : theory -> Model.itm list -> (bool * term) list -> Model.item Model.ppc   (* for generate.sml *)
wneuper@59270
    23
wneuper@59405
    24
  val get_pbt : Celem.pblID -> Celem.pbt
wneuper@59405
    25
  val get_met : Celem.metID -> Celem.met                                    (* for generate.sml *)
wneuper@59405
    26
  val get_met' : theory -> Celem.metID -> Celem.met                (* for pbl-met-hierarchy.sml *)
wneuper@59405
    27
  val get_the : Celem.theID -> Celem.thydata                                  (* for inform.sml *)
wneuper@59270
    28
  
wneuper@59270
    29
  type pblRD = string list                                         (* for pbl-met-hierarchy.sml *)
wneuper@59270
    30
  val format_pblIDl : string list list -> string                       (* for thy-hierarchy.sml *)
wneuper@59405
    31
  val scan : string list -> 'a Celem.ptyp list -> string list list     (* for thy-hierarchy.sml *)
wneuper@59316
    32
  val itm_out : theory -> Model.itm_ -> string
wneuper@59270
    33
  val dsc_unknown : term
wneuper@59270
    34
  
wneuper@59405
    35
  val pblID2guh : Celem.pblID -> Celem.guh                                 (* for datatypes.sml *)
wneuper@59405
    36
  val metID2guh : Celem.metID -> Celem.guh                                 (* for datatypes.sml *)
wneuper@59405
    37
  val kestoreID2guh : Celem.ketype -> Celem.kestoreID -> Celem.guh         (* for datatypes.sml *)
wneuper@59405
    38
  val guh2kestoreID : Celem.guh -> string list                             (* for interface.sml *)
wneuper@59270
    39
  (* for Knowledge/, if below at left margin *)
wneuper@59405
    40
  val prep_pbt : theory -> Celem.guh -> string list -> Celem.pblID ->
wneuper@59405
    41
    string list * (string * string list) list * Celem.rls * string option * Celem.metID list ->
wneuper@59405
    42
      Celem.pbt * Celem.pblID
wneuper@59405
    43
  val prep_met : theory -> string -> string list -> Celem.pblID ->
wneuper@59269
    44
     string list * (string * string list) list *
wneuper@59405
    45
       {calc: 'a, crls: Celem.rls, errpats: Celem.errpat list, nrls: Celem.rls, prls: Celem.rls,
wneuper@59405
    46
         rew_ord': Celem.rew_ord', rls': Celem.rls, srls: Celem.rls} * string ->
wneuper@59405
    47
     Celem.met * Celem.metID
wneuper@59310
    48
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59269
    49
  val show_ptyps : unit -> unit
wneuper@59269
    50
  val show_mets : unit -> unit
wneuper@59316
    51
  datatype match' = Matches' of Model.item Model.ppc | NoMatch' of Model.item Model.ppc
wneuper@59405
    52
  val match_pbl : Selem.fmz_ -> Celem.pbt -> match'
wneuper@59405
    53
  val refine : Selem.fmz_ -> Celem.pblID -> Stool.match list
wneuper@59405
    54
  val e_errpat : Celem.errpat
wneuper@59269
    55
  val show_pblguhs : unit -> unit
wneuper@59269
    56
  val sort_pblguhs : unit -> unit
wneuper@59311
    57
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59299
    58
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
    59
wneuper@59310
    60
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59311
    61
  val e_fillpat : string * term * string
wneuper@59311
    62
  val coll_vats : ''a list * ('b * ''a list * 'c * 'd * 'e) -> ''a list
wneuper@59316
    63
  val filter_vat : Model.ori list -> int -> Model.ori list
wneuper@59311
    64
  val show_metguhs : unit -> unit
wneuper@59311
    65
  val sort_metguhs : unit -> unit
wneuper@59269
    66
end
neuper@37906
    67
wneuper@59311
    68
structure Specify(**) : MODEL_SPECIFY(**) =
wneuper@59269
    69
struct
wneuper@59316
    70
open Model
neuper@37906
    71
wneuper@59270
    72
type field = string * (term * term)
wneuper@59389
    73
val dsc_unknown = (Thm.term_of o the o (TermC.parseold @{theory Script})) "unknown::'a => unknow";
neuper@37906
    74
wneuper@59405
    75
fun itm_2item (_: theory) (Model.Cor ((d, ts), _)) = Model.Correct (Celem.term2str (Model.comp_dts (d, ts)))
wneuper@59316
    76
  | itm_2item _ (Model.Syn c) = Model.SyntaxE c
wneuper@59316
    77
  | itm_2item _ (Model.Typ c) = Model.TypeE c
wneuper@59405
    78
  | itm_2item _ (Model.Inc ((d, ts), _)) = Model.Incompl (Celem.term2str (Model.comp_dts (d, ts)))
wneuper@59405
    79
  | itm_2item _ (Model.Sup (d, ts)) = Model.Superfl (Celem.term2str (Model.comp_dts (d, ts)))
wneuper@59405
    80
  | itm_2item _ (Model.Mis (d, pid)) = Model.Missing (Celem.term2str d ^ " " ^ Celem.term2str pid)
wneuper@59269
    81
  | itm_2item _ _ = error "itm_2item: uncovered definition"
neuper@37906
    82
wneuper@59308
    83
fun mappc f {Given = gi, Where = wh, Find = fi, With = wi, Relate = re} = 
wneuper@59308
    84
  {Given= map f gi, Where = map f wh, Find = map f fi, With = map f wi, Relate = map f re}
wneuper@59308
    85
fun appc f {Given = gi, Where = wh, Find = fi, With = wi, Relate = re} = 
wneuper@59308
    86
  {Given = f gi, Where = f wh, Find = f fi, With = f wi, Relate = f re}
neuper@37906
    87
wneuper@59308
    88
fun add_sel_ppc (_: theory) sel {Given = gi, Where = wh, Find = fi, With = wi, Relate = re} x =
neuper@37906
    89
  case sel of
wneuper@59308
    90
    "#Given" => {Given = gi @ [x], Where = wh, Find = fi, With = wi,Relate = re}
wneuper@59308
    91
  | "#Where" => {Given = gi, Where = wh @ [x], Find = fi, With = wi, Relate = re}
wneuper@59308
    92
  | "#Find"  => {Given = gi, Where = wh, Find = fi @ [x], With = wi, Relate = re}
wneuper@59308
    93
  | "#Relate"=> {Given = gi, Where = wh, Find = fi, With = wi, Relate = re @ [x]}
wneuper@59308
    94
  | "#undef" => {Given = gi @ [x], Where = wh, Find = fi, With = wi, Relate = re} (*ori2itmSup*)
wneuper@59269
    95
  | _  => error ("add_sel_ppc tried to select by \"" ^ sel ^ "\"")
wneuper@59308
    96
fun add_where {Given = gi, Where = _, Find = fi, With = wi, Relate = re} wh =
wneuper@59308
    97
  {Given = gi, Where = wh, Find= fi ,With = wi, Relate = re}
neuper@37906
    98
wneuper@59269
    99
(* split a term into description and (id | structured variable) for pbt, met.ppc *)
neuper@37906
   100
fun split_did t =
wneuper@59270
   101
  let
wneuper@59311
   102
    val (hd, arg) = case strip_comb t of
wneuper@59311
   103
      (hd, [arg]) => (hd, arg)
wneuper@59405
   104
    | _ => error ("split_did: doesn't match (hd,[arg]) for t = " ^ Celem.term2str t)
wneuper@59270
   105
  in (hd, arg) end
neuper@37906
   106
neuper@37906
   107
(*create output-string for itm_*)
wneuper@59405
   108
fun itm_out _ (Model.Cor ((d, ts), _)) = Celem.term2str (Model.comp_dts (d, ts))
wneuper@59316
   109
  | itm_out _ (Model.Syn c) = c
wneuper@59316
   110
  | itm_out _ (Model.Typ c) = c
wneuper@59405
   111
  | itm_out _ (Model.Inc ((d, ts), _)) = Celem.term2str (Model.comp_dts (d, ts))
wneuper@59405
   112
  | itm_out _ (Model.Sup (d, ts)) = Celem.term2str (Model.comp_dts (d, ts))
wneuper@59405
   113
  | itm_out _ (Model.Mis (d, pid)) = Celem.term2str d ^ " " ^ Celem.term2str pid
wneuper@59269
   114
  | itm_out _ _ = error "itm_out: uncovered definition"
neuper@37906
   115
wneuper@59405
   116
fun boolterm2item (true, term) = Model.Correct (Celem.term2str term)
wneuper@59405
   117
  | boolterm2item (false, term) = Model.False (Celem.term2str term);
neuper@37906
   118
wneuper@59308
   119
fun itms2itemppc thy itms pre =
neuper@37906
   120
  let
neuper@37906
   121
    fun coll ppc [] = ppc
wneuper@59269
   122
      | coll ppc ((_,_,_,field,itm_)::itms) =
wneuper@59269
   123
        coll (add_sel_ppc thy field ppc (itm_2item thy itm_)) itms;
wneuper@59316
   124
    val gfr = coll Model.empty_ppc itms;
neuper@37906
   125
  in add_where gfr (map boolterm2item pre) end;
neuper@37906
   126
wneuper@59269
   127
(* compare d and dsc in pbt and transfer field to pre-ori *)
wneuper@59269
   128
fun add_field (_: theory) pbt (d,ts) = 
neuper@37906
   129
  let fun eq d pt = (d = (fst o snd) pt);
neuper@37906
   130
  in case filter (eq d) pbt of
wneuper@59269
   131
       [(fi, (_, _))] => (fi, d, ts)
wneuper@59269
   132
     | [] => ("#undef", d, ts)   (*may come with met.ppc*)
wneuper@59405
   133
     | _ => error ("add_field: " ^ Celem.term2str d ^ " more than once in pbt")
neuper@37906
   134
  end;
neuper@37906
   135
wneuper@59269
   136
(* take over field from met.ppc at 'Specify_Method' into ori,
wneuper@59269
   137
   i.e. also removes "#undef" fields                        *)
wneuper@59308
   138
fun add_field' (_: theory) mpc ori =
neuper@37906
   139
  let fun eq d pt = (d = (fst o snd) pt);
wneuper@59269
   140
    fun repl mpc (i, v, _, d, ts) = 
neuper@37906
   141
      case filter (eq d) mpc of
wneuper@59269
   142
	[(fi, (_, _))] => [(i, v, fi, d, ts)]
neuper@37906
   143
      | [] => [] (*25.2.02: dsc in ori, but not in met -> superfluous*)    
wneuper@59405
   144
      | _ => error ("add_field': " ^ Celem.term2str d ^ " more than once in met");
wneuper@59308
   145
  in flat ((map (repl mpc)) ori) end;
neuper@37906
   146
wneuper@59269
   147
(* mark an element with the position within a plateau;
wneuper@59269
   148
   a plateau with length 1 is marked with 0         *)
wneuper@59269
   149
fun mark _ [] = error "mark []"
wneuper@59269
   150
  | mark eq xs =
wneuper@59269
   151
    let
wneuper@59269
   152
      fun mar xx _ [x] n = xx @ [(if n = 1 then 0 else n, x)]
wneuper@59269
   153
        | mar _ _ [] _ = error "mark []"
wneuper@59269
   154
        | mar xx eq (x:: x' :: xs) n = 
wneuper@59269
   155
        if eq(x, x') then mar (xx @ [(n, x)]) eq (x' :: xs) (n + 1)
wneuper@59269
   156
        else mar (xx @ [(if n = 1 then 0 else n, x)]) eq (x' :: xs) 1;
wneuper@59269
   157
    in mar [] eq xs 1 end;
neuper@37906
   158
wneuper@59269
   159
(* assumes equal descriptions to be in adjacent 'plateaus',
neuper@37906
   160
   items at a certain position within the plateaus form a variant;
wneuper@59269
   161
   length = 1 ... marked with 0: covers all variants            *)
neuper@37906
   162
fun add_variants fdts = 
neuper@37906
   163
  let 
wneuper@59269
   164
    fun eq (a, b) = curry op= (snd3 a) (snd3 b);
neuper@37906
   165
  in mark eq fdts end;
neuper@37906
   166
neuper@38031
   167
fun max [] = error "max of []"
wneuper@59269
   168
  | max (y :: ys) =
neuper@37906
   169
  let fun mx x [] = x
wneuper@59269
   170
	| mx x (y :: ys) = if x < y then mx y ys else mx x ys;
neuper@37906
   171
in mx y ys end;
neuper@37906
   172
wneuper@59269
   173
fun coll_variants (((v,x) :: vxs)) =
wneuper@59269
   174
    let
wneuper@59269
   175
      fun col xs (vs, x) [] = xs @ [(vs, x)]
wneuper@59269
   176
        | col xs (vs, x) ((v', x') :: vxs') = 
wneuper@59269
   177
        if x = x' then col xs (vs @ [v'], x') vxs'
wneuper@59269
   178
        else col (xs @ [(vs, x)]) ([v'], x') vxs';
wneuper@59269
   179
    in col [] ([v], x) vxs end
wneuper@59269
   180
  | coll_variants _ = error "coll_variants: called with []";
neuper@37906
   181
neuper@37906
   182
fun replace_0 vm [0] = intsto vm
wneuper@59269
   183
  | replace_0 _ vs = vs;
neuper@37906
   184
neuper@38031
   185
fun add_id [] = error "add_id []"
neuper@37906
   186
  | add_id xs =
wneuper@59269
   187
    let
wneuper@59269
   188
      fun add _ [] = []
wneuper@59269
   189
        | add n (x :: xs) = (n, x) :: add (n + 1) xs;
wneuper@59269
   190
    in add 1 xs end;
neuper@37906
   191
wneuper@59269
   192
fun flattup (a, (b, (c, d, e))) = (a, b, c, d, e);
bonzai@41952
   193
wneuper@59301
   194
fun prep_ori [] _ _ = ([], Selem.e_ctxt)
neuper@41990
   195
  | prep_ori fmz thy pbt =
wneuper@59269
   196
    let
wneuper@59308
   197
      val ctxt = Proof_Context.init_global thy |> fold Stool.declare_constraints fmz;
wneuper@59389
   198
      val ori = map (add_field thy pbt o Model.split_dts o the o TermC.parseNEW ctxt) fmz |> add_variants;
wneuper@59269
   199
      val maxv = map fst ori |> max;
wneuper@59269
   200
      val maxv = if maxv = 0 then 1 else maxv;
wneuper@59269
   201
      val oris = coll_variants ori
wneuper@59269
   202
        |> map (replace_0 maxv |> apfst)
wneuper@59269
   203
        |> add_id
wneuper@59269
   204
        |> map flattup;
wneuper@59269
   205
    in (oris, ctxt) end;
neuper@37906
   206
wneuper@59405
   207
val e_errpat = ("e_errpatID", [TermC.parse_patt @{theory} "?a = ?b"], [@{thm refl}]): Celem.errpat
wneuper@59389
   208
val e_fillpat = ("e_fillpatID", TermC.parse_patt @{theory} "?a = _", "e_errpatID")
neuper@42424
   209
wneuper@59269
   210
(** breadth-first search on hierarchy of problem-types **)
neuper@37906
   211
wneuper@59269
   212
(* pblID are reverted _on calling_ the retrieve-funs *)
wneuper@59405
   213
type pblRD =   (*e.g. ["equations","univariate","normalise"] for internal retrieval *)
wneuper@59405
   214
  Celem.pblID; (*e.g. ["normalise","univariate","equations"] presented to student   *)
neuper@37906
   215
wneuper@59269
   216
(* apply a fun to a ptyps node *)
wneuper@59405
   217
fun app_ptyp x = Celem.app_py (get_ptyps ()) x;
s1210629013@55321
   218
wneuper@59269
   219
(* TODO: generalize search for subthy *)
wneuper@59405
   220
fun get_pbt (pblID: Celem.pblID) = Celem.get_py (get_ptyps ()) pblID (rev pblID)
neuper@37906
   221
wneuper@59269
   222
(* TODO: throws exn 'get_pbt not found: ' ... confusing !! take 'ketype' as an argument *)
wneuper@59405
   223
fun get_met metID = Celem.get_py (get_mets ()) metID metID;
wneuper@59405
   224
fun get_met' thy metID = Celem.get_py (KEStore_Elems.get_mets thy) metID metID;
wneuper@59405
   225
fun get_the theID = Celem.get_py (get_thes ()) theID theID;
neuper@37906
   226
wneuper@59269
   227
(* lookup a guh in hierarchy of problems / methods depending on fst 4 chars in guh *)
wneuper@59405
   228
fun guh2kestoreID guh =
wneuper@59269
   229
  case (implode o (take_fromto 1 4) o Symbol.explode) guh of
wneuper@59269
   230
	  "pbl_" =>
wneuper@59269
   231
	    let
wneuper@59405
   232
	      fun node ids gu (Celem.Ptyp (id, [{guh, ...}], ns)) =
wneuper@59405
   233
	        if gu = guh then SOME (ids @ [id]) else nodes (ids @ [id]) gu ns
wneuper@59269
   234
	        | node _ _ _ = error "guh2kestoreID node: uncovered fun def."
wneuper@59269
   235
	      and nodes _ _ [] = NONE 
wneuper@59269
   236
	        | nodes ids gu (n :: ns) = case node ids gu n of
wneuper@59269
   237
	            SOME id => SOME id
wneuper@59269
   238
			      | NONE =>  nodes ids gu ns
wneuper@59269
   239
	    in case nodes [] guh (get_ptyps ()) of
wneuper@59269
   240
	      SOME id => rev id
wneuper@59269
   241
	    | NONE => error ("guh2kestoreID: \"" ^ guh ^ "\" " ^ "not found in ptyps")
wneuper@59269
   242
      end
wneuper@59269
   243
  | "met_" =>
wneuper@59269
   244
	    let
wneuper@59405
   245
	      fun node ids gu (Celem.Ptyp (id, [{guh, ...}], ns)) =
wneuper@59405
   246
	        if gu = guh then SOME (ids @ [id]) else nodes (ids @ [id]) gu ns
wneuper@59269
   247
	        | node _ _ _ = error "guh2kestoreID node: uncovered fun def."
wneuper@59269
   248
	      and nodes _ _ [] = NONE 
wneuper@59269
   249
	        | nodes ids gu (n :: ns) = case node ids gu n of
wneuper@59269
   250
	            SOME id => SOME id
wneuper@59269
   251
				    | NONE =>  nodes ids gu ns
wneuper@59269
   252
	    in case nodes [] guh (get_mets ()) of
wneuper@59269
   253
	      SOME id => id
wneuper@59269
   254
	    | NONE => error ("guh2kestoreID: \"" ^ guh ^ "\" " ^ "not found in mets")
wneuper@59269
   255
      end
wneuper@59269
   256
  | _ => error ("guh2kestoreID called with \"" ^ guh ^ "\":");
neuper@37906
   257
wneuper@59269
   258
(* prepare problem-types before storing in pbltypes; 
wneuper@59269
   259
   dont forget to "check_guh_unique" before ins   *)
wneuper@59405
   260
fun prep_pbt thy guh maa init (pblID, dsc_dats, ev, ca, metIDs) =
wneuper@59269
   261
    let
wneuper@59269
   262
      fun eq f (f', _) = f = f';
wneuper@59269
   263
      val gi = filter (eq "#Given") dsc_dats;
wneuper@59269
   264
      val gi = (case gi of
wneuper@59269
   265
		    [] => []
wneuper@59389
   266
		  | ((_, gi') :: []) => (map (split_did o Thm.term_of o the o (TermC.parse thy)) gi'
wneuper@59269
   267
		      handle _ => error ("prep_pbt: syntax error in '#Given' of " ^ strs2str pblID))
wneuper@59269
   268
		  | _ => error ("prep_pbt: more than one '#Given' in " ^ strs2str pblID));
wneuper@59269
   269
		  val gi = map (pair "#Given") gi;
neuper@37906
   270
wneuper@59269
   271
		  val fi = filter (eq "#Find") dsc_dats;
wneuper@59269
   272
		  val fi = (case fi of
wneuper@59269
   273
		    [] => []
wneuper@59389
   274
		  | ((_, fi') :: []) => (map (split_did o Thm.term_of o the o (TermC.parse thy)) fi'
wneuper@59269
   275
		      handle _ => error ("prep_pbt: syntax error in '#Find' of " ^ strs2str pblID))
wneuper@59269
   276
		  | _ => error ("prep_pbt: more than one '#Find' in " ^ strs2str pblID));
wneuper@59269
   277
		  val fi = map (pair "#Find") fi;
neuper@37906
   278
wneuper@59269
   279
		  val re = filter (eq "#Relate") dsc_dats;
wneuper@59269
   280
		  val re = (case re of
wneuper@59269
   281
		    [] => []
wneuper@59389
   282
		  | ((_, re') :: []) => (map (split_did o Thm.term_of o the o (TermC.parse thy)) re'
wneuper@59269
   283
		      handle _ => error ("prep_pbt: syntax error in '#Relate' of " ^ strs2str pblID))
wneuper@59269
   284
		  | _ => error ("prep_pbt: more than one '#Relate' in " ^ strs2str pblID));
wneuper@59269
   285
		  val re = map (pair "#Relate") re;
neuper@37906
   286
wneuper@59269
   287
		  val wh = filter (eq "#Where") dsc_dats;
wneuper@59269
   288
		  val wh = (case wh of
wneuper@59269
   289
		    [] => []
wneuper@59389
   290
		  | ((_, wh') :: []) => (map (TermC.parse_patt thy) wh'
wneuper@59269
   291
		      handle _ => error ("prep_pbt: syntax error in '#Where' of " ^ strs2str pblID))
wneuper@59269
   292
		  | _ => error ("prep_pbt: more than one '#Where' in " ^ strs2str pblID));
wneuper@59269
   293
    in
wneuper@59269
   294
      ({guh = guh, mathauthors = maa, init = init, thy = thy,
wneuper@59269
   295
        cas= case ca of
wneuper@59269
   296
          NONE => NONE
wneuper@59389
   297
			  | SOME s => SOME ((Thm.term_of o the o (TermC.parse thy)) s),
wneuper@59405
   298
			  prls = ev, where_ = wh, ppc = gi @ fi @ re, met = metIDs}, pblID)
neuper@37906
   299
    end;
neuper@37906
   300
wneuper@59269
   301
(* prepare met for storage analogous to pbt *)
wneuper@59269
   302
fun prep_met thy guh maa init
wneuper@59405
   303
	    (metID, ppc,
wneuper@59311
   304
        {rew_ord' = ro, rls' = rls, srls = srls, prls = prls, calc = _(*scr_isa_fns*), crls = cr,
wneuper@59269
   305
          errpats = ep, nrls = nr}, scr) =
wneuper@59269
   306
    let
wneuper@59269
   307
      fun eq f (f', _) = f = f';
wneuper@59269
   308
      val gi = filter (eq "#Given") ppc;
wneuper@59269
   309
      val gi = (case gi of
wneuper@59269
   310
		    [] => (writeln ("prep_met: in " ^ guh ^ " #Given is empty ?!?"); [])
wneuper@59389
   311
		  | ((_, gi') :: []) => (map (split_did o Thm.term_of o the o (TermC.parse thy)) gi'
wneuper@59269
   312
		      handle _ => error ("prep_pbt: syntax error in '#Given' of " ^ strs2str metID))
wneuper@59269
   313
		  | _ => error ("prep_pbt: more than one '#Given' in " ^ strs2str metID));
wneuper@59269
   314
		  val gi = map (pair "#Given") gi;
neuper@37906
   315
wneuper@59269
   316
		  val fi = filter (eq "#Find") ppc;
wneuper@59269
   317
		  val fi = (case fi of
wneuper@59269
   318
		    [] => (writeln ("prep_met: in " ^ guh ^ " #Find is empty ?!?"); [])
wneuper@59389
   319
		  | ((_, fi') :: []) =>  (map (split_did o Thm.term_of o the o (TermC.parse thy)) fi'
wneuper@59269
   320
		      handle _ => error ("prep_pbt: syntax error in '#Find' of " ^ strs2str metID))
wneuper@59269
   321
		  | _ => error ("prep_pbt: more than one '#Find' in " ^ strs2str metID));
wneuper@59269
   322
		  val fi = map (pair "#Find") fi;
neuper@37906
   323
wneuper@59269
   324
		  val re = filter (eq "#Relate") ppc;
wneuper@59269
   325
		  val re = (case re of
wneuper@59269
   326
		    [] => (writeln ("prep_met: in " ^ guh ^ " #Relate is empty ?!?"); [])
wneuper@59389
   327
		  | ((_,re') :: []) => (map (split_did o Thm.term_of o the o (TermC.parse thy)) re'
wneuper@59269
   328
		      handle _ => error ("prep_pbt: syntax error in '#Relate' of " ^ strs2str metID))
wneuper@59269
   329
		  | _ => error ("prep_pbt: more than one '#Relate' in " ^ strs2str metID));
wneuper@59269
   330
		  val re = map (pair "#Relate") re;
wneuper@59269
   331
wneuper@59269
   332
		  val wh = filter (eq "#Where") ppc;
wneuper@59269
   333
		  val wh = (case wh of
wneuper@59269
   334
		    [] => (writeln ("prep_met: in " ^ guh ^ " #Where is empty ?!?"); [])
wneuper@59389
   335
		  | ((_, wh') :: []) => (map (TermC.parse_patt thy) wh'
wneuper@59269
   336
		      handle _ => error ("prep_pbt: syntax error in '#Where' of " ^ strs2str metID))
wneuper@59269
   337
		  | _ => error ("prep_pbt: more than one '#Where' in " ^ strs2str metID));
wneuper@59393
   338
		  val sc = ((TermC.inst_abs o Thm.term_of o the o (TermC.parse thy)) scr)
wneuper@59377
   339
		  val calc = if scr = "empty_script" then [] else LTool.get_calcs thy sc
wneuper@59269
   340
    in
wneuper@59405
   341
       ({guh = guh, mathauthors = maa, init = init, ppc = gi @ fi @ re, pre = wh, rew_ord' = ro,
wneuper@59376
   342
         erls = rls, srls = srls, prls = prls, calc = calc,
wneuper@59405
   343
         crls = cr, errpats = ep, nrls = nr, scr = Celem.Prog sc}, metID)
wneuper@59269
   344
    end;
wneuper@59269
   345
wneuper@59269
   346
wneuper@59269
   347
(** get pblIDs of all entries in mat3D **)
neuper@37906
   348
neuper@37906
   349
fun format_pblID strl = enclose " [" "]" (commas_quote strl);
wneuper@59269
   350
fun format_pblIDl strll = enclose "[\n" "\n]\n" (space_implode ",\n" (map format_pblID strll));
neuper@37906
   351
neuper@37906
   352
fun scan _  [] = [] (* no base case, for empty doms only *)
wneuper@59405
   353
  | scan id ((Celem.Ptyp ((i, _, []))) :: []) = [id @ [i]]
wneuper@59405
   354
  | scan id ((Celem.Ptyp ((i, _, pl))) :: []) = scan (id @ [i]) pl
wneuper@59405
   355
  | scan id ((Celem.Ptyp ((i, _, []))) :: ps) = [id @ [i]] @ (scan id ps)
wneuper@59405
   356
  | scan id ((Celem.Ptyp ((i, _, pl))) :: ps) = (scan (id @ [i]) pl) @ (scan id ps);
neuper@37906
   357
wneuper@59270
   358
fun show_ptyps () = (writeln o format_pblIDl o (scan [])) (get_ptyps ()); (* for tests *)
wneuper@59270
   359
fun show_mets () = (writeln o format_pblIDl o (scan [])) (get_mets ()); (* for tests *)
neuper@37906
   360
wneuper@59269
   361
(* transform oris *)
neuper@37906
   362
wneuper@59308
   363
fun coll_vats (vats, (_, vs, _, _, _)) = union op = vats vs;
wneuper@59316
   364
fun filter_vat oris i = filter ((member_swap op = i) o (#2 : Model.ori -> int list)) oris;
neuper@37906
   365
wneuper@59269
   366
(** check a problem (ie. itm list) for matching a problemtype **)
neuper@37906
   367
wneuper@59269
   368
fun eq1 d (_, (d', _)) = (d = d');
wneuper@59316
   369
fun ori2itmSup (i, v, _, d, ts) = (i, v, true, "#Given", Model.Sup (d, ts));
wneuper@59269
   370
(*see + add_sel_ppc                                    ~~~~~~~*)
neuper@37906
   371
wneuper@59308
   372
fun field_eq f (_, _, f', _, _) = f = f';
neuper@37906
   373
wneuper@59269
   374
(* check an item (with arbitrary itm_ from previous matchings) 
wneuper@59269
   375
   for matching a problemtype; returns true only for itms found in pbt *)
wneuper@59316
   376
fun chk_ (_: theory) pbt (i, vats, b, f, Model.Cor ((d, vs), _)) =
wneuper@59269
   377
    (case find_first (eq1 d) pbt of 
wneuper@59316
   378
      SOME (_, (_, id)) => (i, vats, b, f, Model.Cor ((d, vs), (id, Model.pbl_ids' d vs)))
wneuper@59316
   379
    | NONE =>  (i, vats, false, f, Model.Sup (d, vs)))
wneuper@59316
   380
  | chk_ _ pbt (i, vats, b, f, Model.Inc ((d, vs), _)) =
wneuper@59269
   381
    (case find_first (eq1 d) pbt of 
wneuper@59316
   382
      SOME (_, (_, id)) => (i, vats, b, f, Model.Cor ((d, vs), (id, Model.pbl_ids' d vs)))
wneuper@59316
   383
    | NONE => (i, vats, false, f, Model.Sup (d, vs)))
wneuper@59316
   384
  | chk_ _ _ (itm as (_, _, _, _, Model.Syn _)) = itm
wneuper@59316
   385
  | chk_ _ _ (itm as (_, _, _, _, Model.Typ _)) = itm
wneuper@59316
   386
  | chk_ _ pbt (i, vats, b, f, Model.Sup (d, vs)) =
wneuper@59269
   387
    (case find_first (eq1 d) pbt of 
wneuper@59316
   388
      SOME (_, (_, id)) => (i, vats, b, f, Model.Cor ((d,vs), (id, Model.pbl_ids' d vs)))
wneuper@59316
   389
    | NONE => (i, vats, false, f, Model.Sup (d, vs)))
wneuper@59316
   390
  | chk_ _ pbt (i, vats, _, f, Model.Mis (d, vs)) =
wneuper@59269
   391
    (case find_first (eq1 d) pbt of
wneuper@59316
   392
      SOME (_, _) => error "chk_: ((i,vats,b,f,Model.Cor ((d,vs),(id, Model.pbl_ids' d vs))):itm)"
wneuper@59316
   393
    | NONE => (i, vats, false, f, Model.Sup (d, [vs])))
wneuper@59269
   394
  | chk_ _ _ _ = error "chk_: uncovered fun def.";
neuper@37906
   395
wneuper@59316
   396
fun eq2 (_, (d, _)) (_, _, _, _, itm_) = d = Model.d_in itm_;
wneuper@59308
   397
fun eq2' (_, (d, _)) (_, _, _, d', _) = d = d';
wneuper@59308
   398
fun eq0 (0, _, _, _, _) = true
neuper@37906
   399
  | eq0 _ = false;
neuper@37906
   400
fun max_i i [] = i
wneuper@59269
   401
  | max_i i ((id, _, _, _, _) :: is) = if i > id then max_i i is else max_i id is;
wneuper@59308
   402
fun max_id [] = 0
wneuper@59269
   403
  | max_id ((id, _, _, _, _) :: is) = max_i id is;
wneuper@59308
   404
fun add_idvat itms _ _ [] = itms
wneuper@59308
   405
  | add_idvat itms i mvat ((_, _, b, f, itm_) :: its) =
wneuper@59308
   406
    add_idvat (itms @ [(i,[],b,f,itm_)]) (i + 1) mvat its;
wneuper@59269
   407
                       (* ^^ mvat ...meaningless with pbl-identifier *)
neuper@37906
   408
wneuper@59269
   409
(* find elements of pbt not contained in itms;
wneuper@59269
   410
   if such one is untouched, return this one, otherwise create new itm *)
wneuper@59308
   411
fun chk_m itms untouched (p as (f, (d, id))) = 
wneuper@59269
   412
  case find_first (eq2 p) itms of
wneuper@59269
   413
	  SOME _ => []
wneuper@59269
   414
  | NONE =>
wneuper@59269
   415
      (case find_first (eq2 p) untouched of
wneuper@59269
   416
        SOME itm => [itm]
wneuper@59316
   417
      | NONE => [(0, [], false, f, Model.Mis (d, id))]);
neuper@37906
   418
neuper@37906
   419
fun chk_mis mvat itms untouched pbt = 
neuper@37906
   420
    let val mis = (flat o (map (chk_m itms untouched))) pbt; 
neuper@37906
   421
        val mid = max_id itms;
neuper@37906
   422
    in add_idvat [] (mid + 1) mvat mis end;
neuper@37906
   423
wneuper@59269
   424
(* check a problem (ie. itm list) for matching a problemtype, 
wneuper@59316
   425
   takes the Model.max_vt for concluding completeness (could be another!) *)
wneuper@59269
   426
fun match_itms thy itms (pbt, pre, prls) = 
wneuper@59269
   427
  let
wneuper@59269
   428
    fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b;
wneuper@59269
   429
    val itms' = map (chk_ thy pbt) itms; (* all found are #3 true *)
wneuper@59316
   430
    val mvat = Model.max_vt itms';
wneuper@59269
   431
	  val itms'' = filter (okv mvat) itms';
wneuper@59269
   432
	  val untouched = filter eq0 itms; (* i.e. dsc only (from init)*)
wneuper@59269
   433
	  val mis = chk_mis mvat itms'' untouched pbt;
wneuper@59308
   434
	  val pre' = Stool.check_preconds' prls pre itms'' mvat
wneuper@59269
   435
	  val pb = foldl and_ (true, map fst pre')
wneuper@59269
   436
  in (length mis = 0 andalso pb, (itms'@ mis, pre')) end;
neuper@37906
   437
wneuper@59269
   438
(* check a problem pbl (ie. itm list) for matching a problemtype pbt,
wneuper@59269
   439
   for missing items get data from formalization (ie. ori list); 
wneuper@59316
   440
   takes the Model.max_vt for concluding completeness (could be another!)
wneuper@59269
   441
 
wneuper@59269
   442
  (0) determine the most frequent variant mv in pbl
wneuper@59269
   443
   ALL pbt. (1) dsc(pbt) notmem dsc(pbls) =>
wneuper@59269
   444
            (2) filter (dsc(pbt) = dsc(oris)) oris; -> news;
wneuper@59269
   445
            (3) newitms = filter (mv mem vat(news)) news 
wneuper@59269
   446
   (4) pbt @ newitms                                           *)
wneuper@59308
   447
fun match_itms_oris (_: theory) pbl (pbt, pre, prls) oris =
neuper@37906
   448
  let 
wneuper@59316
   449
 (*0*)val mv = Model.max_vt pbl;
neuper@37906
   450
wneuper@59316
   451
      fun eqdsc_pbt_itm ((_,(d,_))) (_, _, _, _, itm_) = d = Model.d_in itm_;
neuper@37906
   452
      fun notmem pbl pbt1 = case find_first (eqdsc_pbt_itm pbt1) pbl of
neuper@37926
   453
				SOME _ => false | NONE => true;
wneuper@59269
   454
 (*1*)val mis = (filter (notmem pbl)) pbt;
neuper@37906
   455
wneuper@59308
   456
      fun eqdsc_ori (_,(d,_)) (_, _, _, d', _) = d = d';
wneuper@59316
   457
      fun ori2itmMis (f, (d, pid)) (i, v, _, _, _) = (i, v, false, f, Model.Mis (d, pid));
wneuper@59269
   458
 (*2*)fun oris2itms oris mis1 = ((map (ori2itmMis mis1)) o (filter (eqdsc_ori mis1))) oris;
neuper@37906
   459
      val news = (flat o (map (oris2itms oris))) mis;
wneuper@59269
   460
 (*3*)fun mem_vat (_, vats, _, _, _) = member op = vats mv;
neuper@37906
   461
      val newitms = filter mem_vat news;
neuper@37906
   462
 (*4*)val itms' = pbl @ newitms;
wneuper@59308
   463
      val pre' = Stool.check_preconds' prls pre itms' mv;
wneuper@59269
   464
      val pb = foldl and_ (true, map fst pre');
neuper@37906
   465
  in (length mis = 0 andalso pb, (itms', pre')) end;
neuper@37906
   466
neuper@37906
   467
wneuper@59269
   468
(** check a problem (ie. itm list) for matching a problemtype **)
neuper@37906
   469
wneuper@59269
   470
(* check an ori for matching a problemtype by description; 
wneuper@59269
   471
   returns true only for itms found in pbt              *)
wneuper@59308
   472
fun chk1_ (_: theory) pbt (i, vats, f, d, vs) =
wneuper@59269
   473
  case find_first (eq1 d) pbt of 
wneuper@59316
   474
	  SOME (_, (_, id)) => [(i, vats, true, f, Model.Cor ((d, vs), (id, Model.pbl_ids' d vs)))]
wneuper@59269
   475
  | NONE => [];
neuper@37906
   476
neuper@37906
   477
(* elem 'p' of pbt contained in itms ? *)
wneuper@59308
   478
fun chk1_m itms p = case find_first (eq2 p) itms of SOME _ => true | NONE => false;
wneuper@59308
   479
fun chk1_m' oris (p as (f, (d, t))) = 
wneuper@59269
   480
  case find_first (eq2' p) oris of
wneuper@59269
   481
	  SOME _ => []
wneuper@59316
   482
  | NONE => [(f, Model.Mis (d, t))];
wneuper@59308
   483
fun pair0vatsfalse (f, itm_) = (0, [], false, f, itm_);
neuper@37906
   484
wneuper@59269
   485
fun chk1_mis _(*mvat*) itms ppc = foldl and_ (true, map (chk1_m itms) ppc);
wneuper@59269
   486
fun chk1_mis' oris ppc = map pair0vatsfalse ((flat o (map (chk1_m' oris))) ppc);
wneuper@59269
   487
  
wneuper@59269
   488
(* check a problem (ie. ori list) for matching a problemtype, 
wneuper@59316
   489
   takes the Model.max_vt for concluding completeness (FIXME could be another!) *)
wneuper@59269
   490
fun match_oris thy prls oris (pbt,pre) = 
wneuper@59269
   491
  let
wneuper@59269
   492
    val itms = (flat o (map (chk1_ thy pbt))) oris;
wneuper@59316
   493
    val mvat = Model.max_vt itms;
wneuper@59269
   494
    val complete = chk1_mis mvat itms pbt;
wneuper@59308
   495
    val pre' = Stool.check_preconds' prls pre itms mvat;
wneuper@59269
   496
    val pb = foldl and_ (true, map fst pre');
wneuper@59269
   497
  in if complete andalso pb then true else false end;
neuper@37906
   498
wneuper@59269
   499
(* check a problem (ie. ori list) for matching a problemtype, 
wneuper@59269
   500
   returns items for output to math-experts *)
wneuper@59269
   501
fun match_oris' thy oris (ppc,pre,prls) =
wneuper@59269
   502
  let
wneuper@59269
   503
    val itms = (flat o (map (chk1_ thy ppc))) oris;
wneuper@59269
   504
    val sups = ((map ori2itmSup) o (filter (field_eq "#undef"))) oris;
wneuper@59316
   505
    val mvat = Model.max_vt itms;
wneuper@59269
   506
    val miss = chk1_mis' oris ppc;
wneuper@59308
   507
    val pre' = Stool.check_preconds' prls pre itms mvat;
wneuper@59269
   508
    val pb = foldl and_ (true, map fst pre');
wneuper@59269
   509
  in (miss = [] andalso pb, (itms @ miss @ sups, pre')) end;
neuper@37906
   510
wneuper@59269
   511
datatype match' = (* for the user *)
wneuper@59316
   512
  Matches' of Model.item Model.ppc
wneuper@59316
   513
| NoMatch' of Model.item Model.ppc;
neuper@37906
   514
wneuper@59270
   515
(* match a formalization with a problem type, for tests *)
wneuper@59405
   516
fun match_pbl fmz {thy = thy, where_ = pre, ppc, prls = er, ...} =
wneuper@59269
   517
  let
wneuper@59269
   518
    val oris = prep_ori fmz thy ppc |> #1;
wneuper@59269
   519
    val (bool, (itms, pre')) = match_oris' thy oris (ppc, pre, er);
wneuper@59269
   520
  in
wneuper@59269
   521
    if bool
wneuper@59269
   522
    then Matches' (itms2itemppc thy itms pre')
wneuper@59269
   523
    else NoMatch' (itms2itemppc thy itms pre')
wneuper@59269
   524
  end;
neuper@37906
   525
wneuper@59269
   526
(* refine a problem; construct pblRD while scanning *)
wneuper@59405
   527
fun refin (pblRD: pblRD) ori (Celem.Ptyp (pI, [py], [])) =
neuper@37906
   528
    if match_oris (#thy py) (#prls py) ori (#ppc py, #where_ py) 
wneuper@59269
   529
    then SOME ((pblRD @ [pI]): pblRD)
neuper@37926
   530
    else NONE
wneuper@59405
   531
  | refin pblRD ori (Celem.Ptyp (pI, [py], pys)) =
neuper@37906
   532
    if match_oris (#thy py) (#prls py) ori (#ppc py, #where_ py) 
neuper@37906
   533
    then (case refins (pblRD @ [pI]) ori pys of
neuper@37926
   534
	      SOME pblRD' => SOME pblRD'
neuper@37926
   535
	    | NONE => SOME (pblRD @ [pI]))
neuper@37926
   536
    else NONE
wneuper@59269
   537
  | refin _ _ _ = error "refin: uncovered fun def."
wneuper@59269
   538
and refins _ _ [] = NONE
wneuper@59405
   539
  | refins pblRD ori ((p as Celem.Ptyp _) :: pts) =
neuper@37906
   540
    (case refin pblRD ori p of
wneuper@59269
   541
      SOME pblRD' => SOME pblRD'
wneuper@59269
   542
    | NONE => refins pblRD ori pts);
neuper@37906
   543
wneuper@59269
   544
(* refine a problem; version providing output for math-experts *)
wneuper@59405
   545
fun refin' (pblRD: pblRD) fmz pbls (Celem.Ptyp (pI, [py], [])) =
neuper@42391
   546
    let
wneuper@59269
   547
      val _ = (tracing o ((curry op ^) "*** pass ") o strs2str) (pblRD @ [pI])
neuper@42391
   548
      val {thy, ppc, where_, prls, ...} = py 
neuper@42391
   549
      val oris = prep_ori fmz thy ppc |> #1;
wneuper@59269
   550
      (* WN020803: itms!: oris might _not_ be complete here *)
neuper@42391
   551
      val (b, (itms, pre')) = match_oris' thy oris (ppc, where_, prls)
wneuper@59269
   552
    in
wneuper@59269
   553
      if b
wneuper@59308
   554
      then pbls @ [Stool.Matches (rev (pblRD @ [pI]), itms2itemppc thy itms pre')]
wneuper@59308
   555
      else pbls @ [Stool.NoMatch (rev (pblRD @ [pI]), itms2itemppc thy itms pre')]
neuper@37906
   556
    end
wneuper@59405
   557
  | refin' pblRD fmz pbls (Celem.Ptyp (pI, [py], pys)) =
neuper@42391
   558
    let
neuper@42391
   559
      val _ = (tracing o ((curry op ^)"*** pass ") o strs2str) (pblRD @ [pI])
neuper@42391
   560
      val {thy, ppc, where_, prls, ...} = py 
neuper@42391
   561
      val oris = prep_ori fmz thy ppc |> #1;
wneuper@59269
   562
      (* WN020803: itms!: oris might _not_ be complete here *)
neuper@42391
   563
      val(b, (itms, pre')) = match_oris' thy oris (ppc,where_,prls);
wneuper@59269
   564
    in
wneuper@59269
   565
      if b 
wneuper@59269
   566
      then
wneuper@59308
   567
        let val pbl = Stool.Matches (rev (pblRD @ [pI]), itms2itemppc thy itms pre')
neuper@42391
   568
	      in refins' (pblRD @ [pI]) fmz (pbls @ [pbl]) pys end
wneuper@59308
   569
      else (pbls @ [Stool.NoMatch (rev (pblRD @ [pI]), itms2itemppc thy itms pre')])
neuper@37906
   570
    end
wneuper@59269
   571
  | refin' _ _ _ _ = error "refin': uncovered fun def."
wneuper@59269
   572
and refins' _ _ pbls [] = pbls
wneuper@59405
   573
  | refins' pblRD fmz pbls ((p as Celem.Ptyp _) :: pts) =
wneuper@59269
   574
    let
wneuper@59269
   575
      val pbls' = refin' pblRD fmz pbls p
wneuper@59269
   576
    in
wneuper@59269
   577
      case last_elem pbls' of
wneuper@59308
   578
        Stool.Matches _ => pbls'
wneuper@59308
   579
      | Stool.NoMatch _ => refins' pblRD fmz pbls' pts
neuper@42391
   580
    end;
neuper@37906
   581
wneuper@59269
   582
(* refine a problem; version for tactic Refine_Problem *)
wneuper@59405
   583
fun refin'' _ (pblRD: pblRD) itms pbls (Celem.Ptyp (pI, [py], [])) =
wneuper@59269
   584
    let
wneuper@59269
   585
	    val {thy, ppc, where_, prls, ...} = py 
wneuper@59269
   586
	    val (b, (itms', pre')) = match_itms thy itms (ppc, where_, prls);
wneuper@59269
   587
    in
wneuper@59269
   588
      if b
wneuper@59308
   589
      then pbls @ [Stool.Match_ (rev (pblRD @ [pI]), (itms', pre'))]
wneuper@59308
   590
      else pbls @ [Stool.NoMatch_] 
neuper@37906
   591
    end
wneuper@59405
   592
  | refin'' _ pblRD itms pbls (Celem.Ptyp (pI, [py], pys)) =
wneuper@59269
   593
    let
wneuper@59269
   594
      val {thy, ppc, where_, prls, ...} = py 
wneuper@59269
   595
      val (b, (itms', pre')) = match_itms thy itms (ppc, where_, prls);
neuper@37906
   596
    in if b 
wneuper@59308
   597
       then let val pbl = Stool.Match_ (rev (pblRD @ [pI]), (itms', pre'))
neuper@37906
   598
	    in refins'' thy (pblRD @ [pI]) itms (pbls @ [pbl]) pys end
wneuper@59308
   599
       else (pbls @ [Stool.NoMatch_])
neuper@37906
   600
    end
wneuper@59269
   601
  | refin'' _ _ _ _ _ = error "refin': uncovered fun def."
wneuper@59269
   602
and refins'' _ _ _ pbls [] = pbls
wneuper@59405
   603
  | refins'' thy pblRD itms pbls ((p as Celem.Ptyp _) :: pts) =
wneuper@59269
   604
    let
wneuper@59269
   605
      val pbls' = refin'' thy pblRD itms pbls p
neuper@37906
   606
    in case last_elem pbls' of
wneuper@59308
   607
      Stool.Match_ _ => pbls'
wneuper@59308
   608
    | Stool.NoMatch_ => refins'' thy pblRD itms pbls' pts
wneuper@59269
   609
  end;
neuper@37906
   610
wneuper@59269
   611
(* for tactic Refine_Tacitly
wneuper@59269
   612
   oris are already created wrt. some pbt; pbt contains thy for parsing *)
wneuper@59405
   613
fun refine_ori oris pblID =
wneuper@59269
   614
  let
wneuper@59269
   615
    val opt = app_ptyp (refin ((rev o tl) pblID) oris) pblID (rev pblID);
neuper@37906
   616
    in case opt of 
wneuper@59269
   617
      SOME pblRD =>
wneuper@59405
   618
        let val pblID': Celem.pblID = rev pblRD
wneuper@59269
   619
			  in if pblID' = pblID then NONE else SOME pblID' end
wneuper@59269
   620
	  | NONE => NONE
wneuper@59269
   621
	end;
neuper@37906
   622
fun refine_ori' oris pI = (the (refine_ori oris pI)) handle _ => pI;
neuper@37906
   623
wneuper@59269
   624
(* for tactic Refine_Problem
wneuper@59269
   625
   10.03: returnvalue -> (pIrefined, itm list) would be sufficient *)
wneuper@59405
   626
fun refine_pbl thy pblID itms =
wneuper@59308
   627
  case Stool.refined_ (app_ptyp (refin'' thy ((rev o tl) pblID) itms []) pblID (rev pblID)) of
wneuper@59269
   628
	  NONE => NONE
wneuper@59308
   629
  | SOME (Stool.Match_ (rfd as (pI', _))) => if pblID = pI' then NONE else SOME rfd
wneuper@59269
   630
  | _ => error "refine_pbl: uncovered case refined_";
neuper@37906
   631
neuper@37906
   632
wneuper@59270
   633
(* for math-experts and test;
wneuper@59269
   634
   FIXME.WN021019: needs thy for parsing fmz *)
wneuper@59298
   635
fun refine fmz pblID =
s1210629013@55321
   636
  app_ptyp (refin' ((rev o tl) pblID) fmz []) pblID (rev pblID);
neuper@37906
   637
wneuper@59269
   638
(* make a guh from a reference to an element in the kestore;
wneuper@59269
   639
   EXCEPT theory hierarchy ... compare 'fun keref2xml'    *)
wneuper@59405
   640
fun pblID2guh pblID = (((#guh o get_pbt) pblID)
wneuper@59269
   641
  handle _ => error ("pblID2guh: not for \"" ^ strs2str' pblID ^ "\""));
wneuper@59405
   642
fun metID2guh metID = (((#guh o get_met) metID)
wneuper@59269
   643
  handle _ => error ("metID2guh: no 'Met_' for \"" ^ strs2str' metID ^ "\""));
wneuper@59405
   644
fun kestoreID2guh Pbl_ kestoreID = pblID2guh kestoreID
wneuper@59405
   645
  | kestoreID2guh Met_ kestoreID = metID2guh kestoreID
neuper@37906
   646
  | kestoreID2guh ketype kestoreID =
wneuper@59405
   647
    error ("kestoreID2guh: \"" ^ Celem.ketype2str ketype ^ "\" not for \"" ^ strs2str' kestoreID ^ "\"");
neuper@37906
   648
wneuper@59270
   649
fun show_pblguhs () = (* for tests *)
wneuper@59405
   650
  (tracing o strs2str o (map Celem.linefeed)) (Celem.coll_pblguhs (get_ptyps ()))
wneuper@59270
   651
fun sort_pblguhs () = (* for tests *)
wneuper@59405
   652
  (tracing o strs2str o (map Celem.linefeed)) (((sort string_ord) o Celem.coll_pblguhs) (get_ptyps ()))
neuper@37906
   653
wneuper@59270
   654
fun show_metguhs () = (* for tests *)
wneuper@59405
   655
  (tracing o strs2str o (map Celem.linefeed)) (Celem.coll_metguhs (get_mets ()))
wneuper@59270
   656
fun sort_metguhs () = (* for tests *)
wneuper@59405
   657
  (tracing o strs2str o (map Celem.linefeed)) (((sort string_ord) o Celem.coll_metguhs) (get_mets ()))
wneuper@59269
   658
wneuper@59269
   659
end