src/Tools/isac/Interpret/ptyps.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Wed, 24 Aug 2016 17:27:54 +0200
changeset 59230 57593b2a9d41
parent 59186 d9c3e373f8f5
child 59269 1da53d1540fe
permissions -rw-r--r--
tuned
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
neuper@37906
     6
(*-----------------------------------------vvv-(1) aus modspec.sml 23.3.02*)
wneuper@59186
     7
val dsc_unknown = (Thm.term_of o the o (parseold @{theory Script})) 
neuper@37906
     8
  "unknown::'a => unknow";
neuper@37906
     9
(*-----------------------------------------^^^-(1) aus modspec.sml 23.3.02*)
neuper@37906
    10
neuper@37906
    11
neuper@37906
    12
(*-----------------------------------------vvv-(2) aus modspec.sml 23.3.02*)
neuper@37906
    13
neuper@37928
    14
fun itm_2item thy (Cor ((d,ts),_)) = 
bonzai@41949
    15
    Correct (term2str (comp_dts (d,ts)))
neuper@37928
    16
  | itm_2item _ (Syn c)            = SyntaxE c
neuper@37928
    17
  | itm_2item _ (Typ c)            = TypeE c
neuper@37928
    18
  | itm_2item thy (Inc ((d,ts),_)) = 
bonzai@41949
    19
    Incompl (term2str (comp_dts (d,ts)))
neuper@37928
    20
  | itm_2item thy (Sup (d,ts))     = 
bonzai@41949
    21
    Superfl (term2str (comp_dts (d,ts)))
neuper@37928
    22
  | itm_2item _ (Mis (d,pid))   =
neuper@38053
    23
    Missing (term2str d ^ " " ^ term2str pid);
neuper@37906
    24
neuper@37906
    25
neuper@37906
    26
(* --- 8.3.00
neuper@37906
    27
fun get_dsc_in dscppc sel = ((the (assoc (dscppc, sel))):term list)
neuper@37906
    28
  handle _ => error ("get_dsc_in not for "^sel);
neuper@37906
    29
neuper@37906
    30
fun dscs_in dscppc = 
neuper@37906
    31
  ((get_dsc_in dscppc "#Given") @
neuper@37906
    32
   (get_dsc_in dscppc "#Find") @
neuper@37906
    33
   (get_dsc_in dscppc "#Relate")):term list;
neuper@37906
    34
neuper@37906
    35
   --- 26.1.88
neuper@37906
    36
fun get_dsc_of pblID sel = (the (assoc((snd o get_pbt) pblID, sel)));
neuper@37906
    37
fun get_dsc pblID = 
neuper@37906
    38
  (get_dsc_of pblID "#Given") @
neuper@37906
    39
  (get_dsc_of pblID "#Find") @
neuper@37906
    40
  (get_dsc_of pblID "#Relate");
neuper@37906
    41
 --- *)
neuper@37906
    42
neuper@37906
    43
fun mappc f ({Given=gi,Where=wh,Find=fi,With=wi,Relate=re}:'a ppc) = 
neuper@37906
    44
  {Given=map f gi, Where=map f wh,
neuper@37906
    45
   Find=map f fi, With=map f wi, Relate=map f re}:'b ppc;
neuper@37906
    46
fun appc f ({Given=gi,Where=wh,Find=fi,With=wi,Relate=re}:'a ppc) = 
neuper@37906
    47
  {Given=f gi, Where=f wh,
neuper@37906
    48
   Find=f fi, With=f wi, Relate=f re}:'b ppc;
neuper@37906
    49
neuper@37906
    50
(*for ppc of changing type*)
neuper@37906
    51
fun sel_ppc sel ppc =
neuper@37906
    52
  case sel of
neuper@37906
    53
    "#Given" => #Given (ppc:'a ppc)
neuper@37906
    54
  | "#Where" => #Where (ppc:'a ppc)
neuper@37906
    55
  | "#Find" => #Find (ppc:'a ppc)
neuper@37906
    56
  | "#With" => #With (ppc:'a ppc)
neuper@37906
    57
  | "#Relate" => #Relate (ppc:'a ppc)
neuper@38031
    58
  | _  => error ("sel_ppc tried to select by '"^sel^"'");
neuper@37906
    59
neuper@37906
    60
fun repl_sel_ppc sel
neuper@37906
    61
  ({Given=gi,Where=wh,Find=fi,With=wi,Relate=re}:'a ppc) x =
neuper@37906
    62
  case sel of
neuper@37906
    63
    "#Given" => ({Given= x,Where=wh,Find=fi,With=wi,Relate=re}:'a ppc)
neuper@37906
    64
  | "#Where" => {Given=gi,Where= x,Find=fi,With=wi,Relate=re}
neuper@37906
    65
  | "#Find" => {Given=gi,Where=wh,Find= x,With=wi,Relate=re}
neuper@37906
    66
  | "#With" => {Given=gi,Where=wh,Find=fi,With= x,Relate=re}
neuper@37906
    67
  | "#Relate" => {Given=gi,Where=wh,Find=fi,With=wi,Relate= x}
neuper@38031
    68
  | _  => error ("repl_sel_ppc tried to select by '"^sel^"'");
neuper@37906
    69
neuper@37906
    70
fun add_sel_ppc thy sel
neuper@37906
    71
  ({Given=gi,Where=wh,Find=fi,With=wi,Relate=re}:'a ppc) x =
neuper@37906
    72
  case sel of
neuper@37906
    73
    "#Given" => ({Given=gi@[x],Where=wh,Find=fi,With=wi,Relate=re}:'a ppc)
neuper@37906
    74
  | "#Where" => {Given=gi,Where=wh@[x],Find=fi,With=wi,Relate=re}
neuper@37906
    75
  | "#Find"  => {Given=gi,Where=wh,Find=fi@[x],With=wi,Relate=re}
neuper@37906
    76
  | "#Relate"=> {Given=gi,Where=wh,Find=fi,With=wi,Relate=re@[x]}
neuper@37906
    77
  | "#undef" => {Given=gi@[x],Where=wh,Find=fi,With=wi,Relate=re}(*ori2itmSup*)
neuper@38031
    78
  | _  => error ("add_sel_ppc tried to select by '"^sel^"'");
neuper@37906
    79
fun add_where ({Given=gi,Find=fi,With=wi,Relate=re,...}:'a ppc) wh =
neuper@37906
    80
    ({Given=gi,Where=wh,Find=fi,With=wi,Relate=re}:'a ppc);
neuper@37906
    81
neuper@37906
    82
(*decompose a problem-type into description and identifier
neuper@37906
    83
  FIXME split_dsc: no term list !!! (just for quick redoing prep_ori) *)
neuper@37906
    84
fun split_dsc thy t =
neuper@37906
    85
  (let val (hd,args) = strip_comb t
neuper@41976
    86
   in if is_dsc hd
neuper@41976
    87
      then (hd, args)
neuper@41976
    88
       else (e_term, [t])    (*??? 9.01 just copied*)
neuper@41976
    89
   end)
neuper@38053
    90
  handle _ => error ("split_dsc: called with " ^ term2str t);
neuper@37906
    91
(*
wneuper@59186
    92
> val t1 = (Thm.term_of o the o (parse thy)) "errorBound err_";
neuper@37906
    93
> split_dsc t1;
neuper@37906
    94
(Const ("Descript.errorBound","bool => Tools.nam"),Free ("err_","bool"))
neuper@37906
    95
  : term * term
wneuper@59186
    96
> val t3 = (Thm.term_of o the o (parse thy)) "valuesFor vs_";
neuper@37906
    97
> split_dsc t3;
neuper@37906
    98
(Const ("Descript.valuesFor","bool List.list => Tools.toreall"),
neuper@37906
    99
   Free ("vs_","bool List.list")) : term * term*)
neuper@37906
   100
neuper@37906
   101
neuper@37906
   102
neuper@37906
   103
(*. take the first two return-values; for prep_ori .*)
neuper@37906
   104
(*WN.13.5.03fun split_dts' thy t =
neuper@37906
   105
    let val (d, ts, _) = split_dts thy t
neuper@37906
   106
    in (d, ts) end;*)
neuper@37906
   107
(*WN.8.12.03 quick for prep_ori'*)
neuper@37906
   108
fun split_dsc' t =
neuper@37906
   109
  (let val dsc $ var = t
neuper@37906
   110
  in var end)
neuper@38031
   111
  handle _ => error ("split_dsc': called with "^term2str t);
neuper@37906
   112
neuper@37906
   113
(*9.3.00*)
neuper@37906
   114
(* split a term into description and (id | structured variable)
neuper@37906
   115
   for pbt, met.ppc *)
neuper@37906
   116
fun split_did t =
neuper@37906
   117
  (let val (hd,[arg]) = strip_comb t
neuper@37906
   118
  in (hd,arg) end)
neuper@38053
   119
  handle _ => 
neuper@38053
   120
         error ("split_did: doesn't match (hd,[arg]) for t = " ^term2str t);
neuper@37906
   121
neuper@37906
   122
neuper@37906
   123
neuper@37906
   124
(*create output-string for itm_*)
bonzai@41949
   125
fun itm_out thy (Cor ((d,ts),_)) = term2str (comp_dts (d,ts))
neuper@37906
   126
  | itm_out thy (Syn c)      = c
neuper@37906
   127
  | itm_out thy (Typ c)      = c
bonzai@41949
   128
  | itm_out thy (Inc ((d,ts),_)) = term2str (comp_dts (d,ts))
bonzai@41949
   129
  | itm_out thy (Sup (d,ts)) = term2str (comp_dts (d,ts))
neuper@38053
   130
  | itm_out thy (Mis (d,pid)) = term2str d ^ " " ^ term2str pid;
neuper@37906
   131
neuper@37906
   132
fun boolterm2item (true, term) = Correct (term2str term)
neuper@37906
   133
  | boolterm2item (false, term) = False (term2str term);
neuper@37906
   134
neuper@37906
   135
(* use"ME/modspec.sml";
neuper@37906
   136
   *)
neuper@37906
   137
fun itms2itemppc thy (itms:itm list) (pre:(bool * term) list) =
neuper@37906
   138
  let
neuper@37906
   139
    fun coll ppc [] = ppc
neuper@37906
   140
      | coll ppc ((_,_,_,field,itm_)::itms) = 
neuper@37906
   141
      coll (add_sel_ppc thy field ppc (itm_2item thy itm_)) itms;
neuper@37906
   142
    val gfr = coll empty_ppc itms;
neuper@37906
   143
  in add_where gfr (map boolterm2item pre) end;
neuper@37906
   144
neuper@37906
   145
(* 9.3.00
neuper@37906
   146
   compare d and dsc in pbt and transfer field to pre-ori *)
neuper@37906
   147
fun add_field thy pbt (d,ts) = 
neuper@37906
   148
  let fun eq d pt = (d = (fst o snd) pt);
neuper@37906
   149
  in case filter (eq d) pbt of
neuper@37906
   150
       [(fi,(dsc,_))] => (fi,d,ts)
neuper@37906
   151
     | [] => ("#undef",d,ts)   (*may come with met.ppc*)
neuper@38053
   152
     | _ => error ("add_field: " ^ term2str d ^ " more than once in pbt")
neuper@37906
   153
  end;
neuper@37906
   154
neuper@37906
   155
(*. take over field from met.ppc at 'Specify_Method' into ori,
neuper@37906
   156
   i.e. also removes "#undef" fields                        .*)
neuper@37906
   157
(* val (mpc, ori) =  ((#ppc o get_met) mID, oris);
neuper@37906
   158
   *)
neuper@37906
   159
fun add_field' thy mpc (ori:ori list) =
neuper@37906
   160
  let fun eq d pt = (d = (fst o snd) pt);
neuper@37906
   161
    fun repl mpc (i,v,_,d,ts) = 
neuper@37906
   162
      case filter (eq d) mpc of
neuper@37906
   163
	[(fi,(dsc,_))] => [(i,v,fi,d,ts)]
neuper@37906
   164
      | [] => [] (*25.2.02: dsc in ori, but not in met -> superfluous*)    
neuper@38053
   165
      | _ => error ("add_field': " ^ term2str d ^ " more than once in met");
neuper@37906
   166
  in (flat ((map (repl mpc)) ori)):ori list end;
neuper@37906
   167
neuper@37906
   168
neuper@37906
   169
(*.mark an element with the position within a plateau;
neuper@37906
   170
   a plateau with length 1 is marked with 0        .*)
neuper@38031
   171
fun mark eq [] = error "mark []"
neuper@37906
   172
  | mark eq xs =
neuper@37906
   173
  let
neuper@37906
   174
    fun mar xx eq [x] n = xx @ [(if n=1 then 0 else n,x)]
neuper@37906
   175
      | mar xx eq (x::x'::xs) n = 
neuper@37906
   176
      if eq(x,x') then mar (xx @ [(n,x)]) eq (x'::xs) (n+1)
neuper@37906
   177
      else mar (xx @ [(if n=1 then 0 else n,x)]) eq (x'::xs) 1;
neuper@37906
   178
  in mar [] eq xs 1 end;
neuper@37906
   179
(*
neuper@37906
   180
> val xs = [1,1,1,2,4,4,5];
neuper@37906
   181
> mark (op=) xs;
neuper@37906
   182
val it = [(1,1),(2,1),(3,1),(0,2),(1,4),(2,4),(0,5)]
neuper@37906
   183
*)
neuper@37906
   184
neuper@37906
   185
(*.assumes equal descriptions to be in adjacent 'plateaus',
neuper@37906
   186
   items at a certain position within the plateaus form a variant;
neuper@37906
   187
   length = 1 ... marked with 0: covers all variants           .*)
neuper@37906
   188
fun add_variants fdts = 
neuper@37906
   189
  let 
neuper@37906
   190
    fun eq (a,b) = curry op= (snd3 a) (snd3 b);
neuper@37906
   191
  in mark eq fdts end;
neuper@37906
   192
neuper@37906
   193
(* collect equal elements: the model for coll_variants *)
neuper@37906
   194
fun coll eq xs =
neuper@37906
   195
  let
neuper@37906
   196
    fun col xs eq x [] = xs @ [x]
neuper@37906
   197
      | col xs eq x (y::ys) = 
neuper@37906
   198
      if eq(x,y) then col xs eq x ys
neuper@37906
   199
      else col (xs @ [x]) eq y ys;
neuper@37906
   200
  in col [] eq (hd xs) xs end;
neuper@37906
   201
(* 
neuper@37906
   202
> val xs = [1,1,1,2,4,4,4];
neuper@37906
   203
> coll (op=) xs;
neuper@37906
   204
val it = [1,2,4] : int list
neuper@37906
   205
*)
neuper@37906
   206
neuper@38031
   207
fun max [] = error "max of []"
neuper@37906
   208
  | max (y::ys) =
neuper@37906
   209
  let fun mx x [] = x
neuper@37906
   210
	| mx x (y::ys) = if x < y then mx y ys else mx x ys;
neuper@37906
   211
in mx y ys end;
neuper@38031
   212
fun gen_max _ [] = error "gen_max of []"
neuper@37906
   213
  | gen_max ord (y::ys) =
neuper@37906
   214
  let fun mx x [] = x
neuper@37906
   215
	| mx x (y::ys) = if ord (x, y) then mx y ys else mx x ys;
neuper@37906
   216
in mx y ys end;
neuper@37906
   217
neuper@37906
   218
neuper@37906
   219
neuper@37906
   220
(* assumes *)
neuper@37906
   221
fun coll_variants (((v,x)::vxs)) =
neuper@37906
   222
  let
neuper@37906
   223
    fun col xs (vs,x) [] = xs @ [(vs,x)]
neuper@37906
   224
      | col xs (vs,x) ((v',x')::vxs') = 
neuper@37906
   225
      if x=x' then col xs (vs @ [v'], x') vxs'
neuper@37906
   226
      else col (xs @ [(vs,x)]) ([v'], x') vxs';
neuper@37906
   227
  in col [] ([v],x) vxs end;
neuper@37906
   228
(* val xs = [(1,1),(2,1),(3,1),(0,2),(1,4),(2,4),(0,5)];
neuper@37906
   229
> col [] ([(fst o hd) xs],(snd o hd) xs) (tl xs);
neuper@37906
   230
val it = [([1,2,3],1),([0],2),([1,2],4),([0],5)]  *)
neuper@37906
   231
neuper@37906
   232
neuper@37906
   233
fun replace_0 vm [0] = intsto vm
neuper@37906
   234
  | replace_0 vm vs = vs;
neuper@37906
   235
neuper@38031
   236
fun add_id [] = error "add_id []"
neuper@37906
   237
  | add_id xs =
neuper@37906
   238
  let fun add n [] = []
neuper@37906
   239
	| add n (x::xs) = (n,x) :: add (n+1) xs;
neuper@37906
   240
in add 1 xs end;
neuper@37906
   241
(*
neuper@37906
   242
> val xs = [([1,2,3],1),([0],2),([1,2],4),([0],5)];
neuper@37906
   243
> add_id xs;
neuper@37906
   244
val it = [(1,([#,#,#],1)),(2,([#],2)),(3,([#,#],4)),(4,([#],5))]
neuper@37906
   245
 *)
neuper@37906
   246
neuper@37906
   247
fun flattup (a,(b,(c,d,e))) = (a,b,c,d,e);
neuper@37906
   248
fun flattup' (a,(b,((c,d),e))) = (a,b,c,d,e);
neuper@37906
   249
fun flat3 (a,(b,c)) = (a,b,c);
neuper@37906
   250
(*
neuper@37906
   251
 val pI = pI';
neuper@37906
   252
 !pbts;
neuper@37906
   253
*)
neuper@37906
   254
(* in root (only!) fmz may be empty: fill with ..,dsc,[]
neuper@37906
   255
fun init_ori fmz thy pI =
neuper@37906
   256
  if fmz <> [] then prep_ori fmz thy pI (*fmz assumed complete*)
neuper@37906
   257
  else
neuper@37906
   258
    let 
neuper@37906
   259
      val fds = map (cons2 (fst, fst o snd)) (get_pbt pI);
neuper@37906
   260
      val vfds = map ((pair [1]) o (rpair [])) fds;
neuper@37906
   261
      val ivfds = add_id vfds
neuper@37906
   262
    in (map flattup' ivfds):ori list end;   10.3.00---*)
neuper@37906
   263
(* val fmz = ctl; val pI=["sqroot-test","univariate","equation"];
neuper@37906
   264
   val (thy,pbt) = (assoc_thy dI',(#ppc o get_pbt) pI');
neuper@37906
   265
   val (fmz, thy, pbt) = (fmz, thy, ((#ppc o get_pbt) pI));
neuper@37906
   266
   *)
bonzai@41952
   267
bonzai@41949
   268
fun prep_ori [] _ _ = ([], e_ctxt)
neuper@41990
   269
  | prep_ori fmz thy pbt =
bonzai@41949
   270
      let
neuper@48761
   271
        val ctxt = Proof_Context.init_global thy |> fold declare_constraints fmz;
bonzai@41952
   272
        val ori = map (add_field thy pbt o split_dts o the o parseNEW ctxt) fmz
bonzai@41949
   273
          |> add_variants;
bonzai@41949
   274
        val maxv = map fst ori |> max;
bonzai@41949
   275
        val maxv = if maxv = 0 then 1 else maxv;
bonzai@41949
   276
        val oris = coll_variants ori
bonzai@41949
   277
          |> map (replace_0 maxv |> apfst)
bonzai@41949
   278
          |> add_id
bonzai@41949
   279
          |> map flattup;
bonzai@41949
   280
      in (oris, ctxt) end;
neuper@37906
   281
neuper@37906
   282
neuper@42424
   283
val e_errpat = ("e_errpatID", [parse_patt @{theory} "?a = ?b"], [@{thm refl}]): errpat
neuper@42429
   284
val e_fillpat = ("e_fillpatID", parse_patt @{theory} "?a = _", "e_errpatID")
neuper@42424
   285
neuper@37906
   286
neuper@37906
   287
(**+ breadth-first search on hierarchy of problem-types +**)
neuper@37906
   288
neuper@37906
   289
type pblRD = pblID;(*pblID are Reverted _on calling_ the retrieve-funs*)
neuper@37906
   290
     (* eg. ["equations","univariate","normalize"] while
neuper@37906
   291
	    ["normalize","univariate","equations"] is the related pblID
neuper@37906
   292
      WN.24.4.03: also used for metID*)
neuper@37906
   293
s1210629013@55321
   294
neuper@37906
   295
(*> ptyps:= 
neuper@37906
   296
[Ptyp ("1",[("ptyp 1",([],[]))],
neuper@37906
   297
	[Ptyp ("11",[("ptyp 11",([],[]))],
neuper@37906
   298
		[])
neuper@37906
   299
	 ]),
neuper@37906
   300
 Ptyp ("2",[("ptyp 2",([],[]))],
neuper@37906
   301
	[Ptyp ("21",[("ptyp 21",([],[]))],
neuper@37906
   302
		[])
neuper@37906
   303
	 ])
neuper@37906
   304
 ];
neuper@37906
   305
> get_py SqRoot.thy ["1"] ["1"] (!ptyps);
neuper@37906
   306
> get_py SqRoot.thy ["2","21"] ["2","21"] (!ptyps);
neuper@37906
   307
         _REVERSE_  .......... !!!!!!!!!!*)
neuper@37906
   308
s1210629013@55321
   309
(*. apply a fun to a ptyps node .*)
s1210629013@55338
   310
fun app_ptyp x = app_py (get_ptyps ()) x;
s1210629013@55321
   311
neuper@37906
   312
(*TODO: search generalized for subthy*)
s1210629013@55354
   313
fun get_pbt (pblID:pblID) = get_py (get_ptyps ()) pblID (rev pblID)
neuper@37906
   314
(* get_pbt thy ["1"];
neuper@37906
   315
   get_pbt thy ["21","2"];
neuper@37906
   316
   *)
neuper@37906
   317
neuper@37906
   318
(*TODO: throws exn 'get_pbt not found: ' ... confusing !!
neuper@37906
   319
  take 'ketype' as an argument !!!!!*)
neuper@55490
   320
fun get_met (metID : metID) = get_py (get_mets ()) metID metID;
neuper@55490
   321
fun get_met' thy (metID : metID) = get_py (KEStore_Elems.get_mets thy) metID metID;
neuper@55490
   322
fun get_the (theID : theID) = get_py (get_thes ()) theID theID;
neuper@37906
   323
neuper@37906
   324
neuper@37906
   325
fun del_eq k ptyps =
neuper@37906
   326
let fun del k ptyps [] = ptyps
neuper@37906
   327
      | del k ptyps ((Ptyp (k', [p], ps))::pys) =
neuper@37906
   328
	if k=k' then del k ptyps pys
neuper@37906
   329
	else del k (ptyps @ [Ptyp (k', [p], ps)]) pys;
neuper@37906
   330
in del k [] ptyps end;
neuper@37906
   331
neuper@37906
   332
(*.lookup a guh in hierarchy or methods depending on fst chars in guh.*)
neuper@37906
   333
fun guh2kestoreID (guh:guh) =
neuper@40836
   334
    case (implode o (take_fromto 1 4) o Symbol.explode) guh of
neuper@37906
   335
	"pbl_" =>
neuper@37906
   336
	let fun node ids gu (Ptyp (id,[n as {guh,...} : pbt], ns)) =
neuper@37906
   337
		if gu = guh 
neuper@37926
   338
		then SOME ((ids@[id]) : kestoreID)
neuper@37906
   339
		else nodes (ids@[id]) gu ns
neuper@37926
   340
	    and nodes _ _ [] = NONE 
neuper@37906
   341
	      | nodes ids gu (n::ns) = 
neuper@37926
   342
		case node ids gu n of SOME id => SOME id
neuper@37926
   343
				    | NONE =>  nodes ids gu ns
s1210629013@55338
   344
	in case nodes [] guh (get_ptyps ()) of
neuper@37926
   345
	       SOME id => rev id
neuper@37926
   346
	     | NONE => error ("guh2kestoreID: '" ^ guh ^ "' " ^
s1210629013@55354
   347
				    "not found in ptyps")
neuper@37906
   348
	end
neuper@37906
   349
      | "met_" =>
neuper@37906
   350
	let fun node ids gu (Ptyp (id,[n as {guh,...} : met], ns)) =
neuper@37906
   351
		if gu = guh 
neuper@37926
   352
		then SOME ((ids@[id]) : kestoreID)
neuper@37906
   353
		else nodes (ids@[id]) gu ns
neuper@37926
   354
	    and nodes _ _ [] = NONE 
neuper@37906
   355
	      | nodes ids gu (n::ns) = 
neuper@37926
   356
		case node ids gu n of SOME id => SOME id
neuper@37926
   357
				    | NONE =>  nodes ids gu ns
s1210629013@55372
   358
	in case nodes [] guh (get_mets ()) of
neuper@37926
   359
	       SOME id => id
neuper@37926
   360
	     | NONE => error ("guh2kestoreID: '" ^ guh ^ "' " ^
s1210629013@55377
   361
				    "not found in mets") end
neuper@37906
   362
      | _ => error ("guh2kestoreID called with '" ^ guh ^ "'");
neuper@37906
   363
(*> guh2kestoreID "pbl_equ_univ_lin";
neuper@37906
   364
val it = ["linear", "univariate", "equation"] : string list*)
neuper@37906
   365
neuper@37906
   366
neuper@37906
   367
(*. prepare problem-types before storing in pbltypes; 
neuper@37906
   368
    dont forget to 'check_guh_unique' before ins.*)
neuper@37906
   369
fun prep_pbt thy guh maa init
neuper@37906
   370
	     (pblID, dsc_dats: (string * (string list)) list, 
neuper@37906
   371
		  ev:rls, ca: string option, metIDs:metID list) =
neuper@37906
   372
(* val (thy, (pblID, dsc_dats: (string * (string list)) list, 
neuper@37906
   373
		  ev:rls, ca: string option, metIDs:metID list)) =
neuper@37906
   374
       ((EqSystem.thy, (["system"],
neuper@37906
   375
		       [("#Given" ,["equalities es_", "solveForVars vs_"]),
neuper@37906
   376
			("#Find"  ,["solution ss___"](*___ is copy-named*))
neuper@37906
   377
			],
neuper@37906
   378
		       append_rls "e_rls" e_rls [(*for preds in where_*)], 
neuper@37926
   379
		       SOME "solveSystem es_ vs_", 
neuper@37906
   380
		       [])));
neuper@37906
   381
   *)
neuper@37906
   382
    let fun eq f (f', _) = f = f';
neuper@37906
   383
	val gi = filter (eq "#Given") dsc_dats;
neuper@37906
   384
(*val gi = [("#Given",["equality e_","solveFor v_"])]
neuper@37906
   385
  : (string * string list) list*)
neuper@37906
   386
	val gi = (case gi of
neuper@37906
   387
		     [] => []
neuper@37906
   388
		   | ((_,gi')::[]) => 
wneuper@59186
   389
		     ((map (split_did o Thm.term_of o the o (parse thy)) gi')
neuper@37906
   390
		     handle _ => error 
neuper@37906
   391
			("prep_pbt: syntax error in '#Given' of "^
neuper@37906
   392
			 (strs2str pblID)))
neuper@37906
   393
		   | _ =>
neuper@37906
   394
		     (error ("prep_pbt: more than one '#Given' in "^
neuper@37906
   395
				  (strs2str pblID))));
neuper@37906
   396
(*val gi =
neuper@37906
   397
  [(Const ("Descript.equality","bool => Tools.una"),Free ("e_","bool")),
neuper@37906
   398
   (Const ("Descript.solveFor","RealDef.real => Tools.una"),
neuper@37906
   399
    Free ("v_","RealDef.real"))] : (term * term) list  *)
neuper@37906
   400
	val gi = map (pair "#Given") gi;
neuper@37906
   401
(*val gi =
neuper@37906
   402
  [("#Given",
neuper@37906
   403
    (Const ("Descript.equality","bool => Tools.una"),Free ("e_","bool"))),
neuper@37906
   404
   ("#Given",
neuper@37906
   405
    (Const ("Descript.solveFor","RealDef.real => Tools.una"),
neuper@37906
   406
     Free ("v_","RealDef.real")))] : (string * (term * term)) list*)
neuper@37906
   407
neuper@37906
   408
	val fi = filter (eq "#Find") dsc_dats;
neuper@37906
   409
	val fi = (case fi of
neuper@38031
   410
		     [] => [](*28.8.01: ["tool"] ...// error 
neuper@37906
   411
			("prep_pbt: no '#Find' in "^(strs2str pblID))*)
neuper@37906
   412
(* val ((_,fi')::[]) = fi;
neuper@37906
   413
   *)
neuper@37906
   414
		   | ((_,fi')::[]) => 
wneuper@59186
   415
		     ((map (split_did o Thm.term_of o the o (parse thy)) fi')
neuper@38031
   416
		     handle _ => error 
neuper@37906
   417
			("prep_pbt: syntax error in '#Find' of "^
neuper@37906
   418
			 (strs2str pblID)))
neuper@37906
   419
		   | _ =>
neuper@38031
   420
		     (error ("prep_pbt: more than one '#Find' in "^
neuper@37906
   421
				  (strs2str pblID))));
neuper@37906
   422
	val fi = map (pair "#Find") fi;
neuper@37906
   423
neuper@37906
   424
	val re = filter (eq "#Relate") dsc_dats;
neuper@37906
   425
	val re = (case re of
neuper@37906
   426
		     [] => []
neuper@37906
   427
		   | ((_,re')::[]) => 
wneuper@59186
   428
		     ((map (split_did o Thm.term_of o the o (parse thy)) re')
neuper@38031
   429
		     handle _ => error 
neuper@37906
   430
			("prep_pbt: syntax error in '#Relate' of "^
neuper@37906
   431
			 (strs2str pblID)))
neuper@37906
   432
		   | _ =>
neuper@38031
   433
		     (error ("prep_pbt: more than one '#Relate' in "^
neuper@37906
   434
				  (strs2str pblID))));
neuper@37906
   435
	val re = map (pair "#Relate") re;
neuper@37906
   436
neuper@37906
   437
	val wh = filter (eq "#Where") dsc_dats;
neuper@37906
   438
	val wh = (case wh of
neuper@37906
   439
		     [] => []
neuper@37906
   440
		   | ((_,wh')::[]) => 
neuper@37977
   441
		     ((map (parse_patt thy) wh')
neuper@38031
   442
		      handle _ => error 
neuper@37906
   443
			("prep_pbt: syntax error in '#Where' of "^
neuper@37906
   444
			 (strs2str pblID)))
neuper@37906
   445
		   | _ =>
neuper@38031
   446
		     (error ("prep_pbt: more than one '#Where' in "^
neuper@37906
   447
				  (strs2str pblID))));
neuper@37906
   448
    in ({guh=guh,mathauthors=maa,init=init,
neuper@37926
   449
	 thy=thy,cas= case ca of NONE => NONE
neuper@37926
   450
			       | SOME s => 
wneuper@59186
   451
				 SOME ((Thm.term_of o the o (parse thy)) s),
neuper@37906
   452
	 prls=ev,where_=wh,ppc= gi @ fi @ re,
neuper@37906
   453
	 met=metIDs}, pblID):pbt * pblID end;
neuper@37906
   454
(* prep_pbt thy (pblID, dsc_dats, metIDs);   
neuper@37906
   455
 val it =
neuper@37906
   456
  ({met=[],
neuper@37906
   457
    ppc=[("#Given",(Const (#,#),Free (#,#))),
neuper@37906
   458
         ("#Given",(Const (#,#),Free (#,#))),
neuper@37906
   459
         ("#Find",(Const (#,#),Free (#,#)))],
neuper@37906
   460
    thy={ProtoPure, ..., Atools, RatArith},
neuper@37906
   461
    where_=[Const ("Descript.solutions","bool List.list => Tools.toreall") $
neuper@37906
   462
            Free ("v_i_","bool List.list")]},["equation"]) : pbt * pblID    *)
neuper@37906
   463
neuper@37906
   464
neuper@37906
   465
neuper@37906
   466
neuper@37906
   467
(*. prepare met for storage analogous to pbt .*)
neuper@37906
   468
fun prep_met thy guh maa init
neuper@37906
   469
	     (metID, ppc: (string * string list) list (*'#Where' -> #pre*),
neuper@37906
   470
    {rew_ord'=ro, rls'=rls, srls=srls, prls=prls, 
neuper@37906
   471
     calc = scr_isa_fns(*FIXME.040207: del - auto-done*),
neuper@42425
   472
     crls=cr, errpats = ep, nrls= nr}, scr) =
neuper@37906
   473
    let fun eq f (f', _) = f = f';
neuper@37906
   474
	(*val thy = (assoc_thy o fst) metID*)
neuper@37906
   475
	val gi = filter (eq "#Given") ppc;
neuper@37906
   476
	val gi = (case gi of
neuper@42255
   477
		     [] => (writeln ("prep_met: in " ^ guh ^ " #Given is empty ?!?"); [])
neuper@37906
   478
		   | ((_,gi')::[]) => 
wneuper@59186
   479
		     ((map (split_did o Thm.term_of o the o (parse thy)) gi')
neuper@38031
   480
		     handle _ => error 
neuper@37906
   481
			("prep_pbt: syntax error in '#Given' of "^
neuper@37906
   482
			 (strs2str metID)))
neuper@37906
   483
		   | _ =>
neuper@38031
   484
		     (error ("prep_pbt: more than one '#Given' in "^
neuper@37906
   485
				  (strs2str metID))));
neuper@37906
   486
	val gi = map (pair "#Given") gi;
neuper@37906
   487
neuper@37906
   488
	val fi = filter (eq "#Find") ppc;
neuper@37906
   489
	val fi = (case fi of
neuper@42255
   490
		     [] => (writeln ("prep_met: in " ^ guh ^ " #Find is empty ?!?"); [])
neuper@37906
   491
		   | ((_,fi')::[]) => 
wneuper@59186
   492
		     ((map (split_did o Thm.term_of o the o (parse thy)) fi')
neuper@38031
   493
		     handle _ => error 
neuper@37906
   494
			("prep_pbt: syntax error in '#Find' of "^
neuper@37906
   495
			 (strs2str metID)))
neuper@37906
   496
		   | _ =>
neuper@38031
   497
		     (error ("prep_pbt: more than one '#Find' in "^
neuper@37906
   498
				  (strs2str metID))));
neuper@37906
   499
	val fi = map (pair "#Find") fi;
neuper@37906
   500
neuper@37906
   501
	val re = filter (eq "#Relate") ppc;
neuper@37906
   502
	val re = (case re of
neuper@42255
   503
		     [] => (writeln ("prep_met: in " ^ guh ^ " #Relate is empty ?!?"); [])
neuper@37906
   504
		   | ((_,re')::[]) => 
wneuper@59186
   505
		     ((map (split_did o Thm.term_of o the o (parse thy)) re')
neuper@38031
   506
		     handle _ => error 
neuper@37906
   507
			("prep_pbt: syntax error in '#Relate' of "^
neuper@37906
   508
			 (strs2str metID)))
neuper@37906
   509
		   | _ =>
neuper@38031
   510
		     (error ("prep_pbt: more than one '#Relate' in "^
neuper@37906
   511
				  (strs2str metID))));
neuper@37906
   512
	val re = map (pair "#Relate") re;
neuper@37906
   513
neuper@37906
   514
	val wh = filter (eq "#Where") ppc;
neuper@37906
   515
	val wh = (case wh of
neuper@42255
   516
		     [] => (writeln ("prep_met: in " ^ guh ^ " #Where is empty ?!?"); [])
neuper@37906
   517
		   | ((_,wh')::[]) => 
neuper@37977
   518
		     ((map (parse_patt thy) wh')
neuper@38031
   519
		     handle _ => error 
neuper@37906
   520
			("prep_pbt: syntax error in '#Where' of "^
neuper@37906
   521
			 (strs2str metID)))
neuper@37906
   522
		   | _ =>
neuper@38031
   523
		     (error ("prep_pbt: more than one '#Where' in "^
neuper@37906
   524
				  (strs2str metID))));
wneuper@59186
   525
	val sc = (((inst_abs thy) o Thm.term_of o the o (parse thy)) scr)
neuper@37906
   526
    in ({guh=guh,mathauthors=maa,init=init,
neuper@37906
   527
	 ppc=gi@fi@re, pre=wh, rew_ord'=ro, erls=rls, srls=srls, prls=prls,
neuper@37906
   528
	 calc = if scr = "empty_script" then []
s1210629013@52153
   529
		else ((assoc_calc' thy |> map) o (map op_of_calc) o 
neuper@37906
   530
		      (filter is_calc) o stacpbls) sc, 
neuper@48763
   531
	 crls=cr, errpats = ep, nrls= nr, scr = Prog sc}:met,
neuper@37906
   532
	metID:metID)
neuper@37906
   533
    end;
neuper@37906
   534
neuper@37906
   535
neuper@37906
   536
(**. get pblIDs of all entries in mat3D .**)
neuper@37906
   537
neuper@37906
   538
neuper@37906
   539
fun format_pblID strl = enclose " [" "]" (commas_quote strl);
neuper@37906
   540
fun format_pblIDl strll = enclose "[\n" "\n]\n" 
neuper@37906
   541
    (space_implode ",\n" (map format_pblID strll));
neuper@37906
   542
neuper@37906
   543
fun scan _  [] = [] (* no base case, for empty doms only *)
neuper@37906
   544
  | scan id ((Ptyp ((i,_,[])))::[]) =      [id@[i]]
neuper@37906
   545
  | scan id ((Ptyp ((i,_,pl)))::[]) = scan (id@[i]) pl
neuper@37906
   546
  | scan id ((Ptyp ((i,_,[])))::ps) =      [id@[i]]    @(scan id ps)
neuper@37906
   547
  | scan id ((Ptyp ((i,_,pl)))::ps) =(scan (id@[i]) pl)@(scan id ps);
neuper@37906
   548
s1210629013@55338
   549
fun show_ptyps () = (writeln o format_pblIDl o (scan [])) (get_ptyps ());
neuper@37906
   550
(* ptyps:=[];
neuper@37906
   551
   show_ptyps();
neuper@37906
   552
   *)
s1210629013@55372
   553
fun show_mets () = (writeln o format_pblIDl o (scan [])) (get_mets ());
neuper@37906
   554
neuper@37906
   555
neuper@37906
   556
neuper@37906
   557
(*vvvvv---------- preparational work 8.01. UNUSED *)
neuper@37906
   558
(**+ instantiate a problem-type +**)
neuper@37906
   559
neuper@37906
   560
(*+ transform oris +*)
neuper@37906
   561
neuper@37928
   562
fun coll_vats (vats, ((_,vs,_,_,_):ori)) = union op = vats vs;
neuper@37906
   563
(*> coll_vats [11,22] (hd oris);
neuper@37906
   564
val it = [22,11,1,2,3] : int list
neuper@37906
   565
neuper@37906
   566
> foldl coll_vats ([],oris);
neuper@37906
   567
val it = [1,2,3] : int list
neuper@37906
   568
neuper@37906
   569
> val i=1;
neuper@37906
   570
> filter ((curry (op mem) i) o #2) oris;
neuper@37906
   571
val it =
neuper@37906
   572
  [(1,[1,2,3],"#Given",Const (#,#),[# $ #]),
neuper@37906
   573
   (2,[1,2,3],"#Find",Const (#,#),[Free #]),
neuper@37906
   574
   (3,[1,2,3],"#Find",Const (#,#),[# $ #,# $ #]),
neuper@37906
   575
   (4,[1,2],"#Relate",Const (#,#),[# $ #,# $ #]),
neuper@37906
   576
   (6,[1],"#undef",Const (#,#),[Free #]),
neuper@37906
   577
   (9,[1,2],"#undef",Const (#,#),[# $ #]),
neuper@37906
   578
   (11,[1,2,3],"#undef",Const (#,#),[# $ #])] : ori list *)    
neuper@37906
   579
neuper@37936
   580
local infix mem; (*from Isabelle2002*)
neuper@37928
   581
fun x mem [] = false
neuper@37928
   582
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37928
   583
in
neuper@37928
   584
fun filter_vat oris i = 
neuper@37928
   585
    filter ((curry (op mem) i) o (#2 : ori -> int list)) oris;
neuper@37928
   586
end;
neuper@37906
   587
(*> map (filter_vat oris) [1,2,3];
neuper@37906
   588
val it =
neuper@37906
   589
  [[(1,[1,2,3],"#Given",Const (#,#),[# $ #]),
neuper@37906
   590
    (2,[1,2,3],"#Find",Const (#,#),[Free #]),
neuper@37906
   591
    (3,[1,2,3],"#Find",Const (#,#),[# $ #,# $ #]),
neuper@37906
   592
    (4,[1,2],"#Relate",Const (#,#),[# $ #,# $ #]),
neuper@37906
   593
    (6,[1],"#undef",Const (#,#),[Free #]),
neuper@37906
   594
    (9,[1,2],"#undef",Const (#,#),[# $ #]),
neuper@37906
   595
    (11,[1,2,3],"#undef",Const (#,#),[# $ #])],
neuper@37906
   596
   [(1,[1,2,3],"#Given",Const (#,#),[# $ #]),
neuper@37906
   597
    (2,[1,2,3],"#Find",Const (#,#),[Free #]),
neuper@37906
   598
    (3,[1,2,3],"#Find",Const (#,#),[# $ #,# $ #]),
neuper@37906
   599
    (4,[1,2],"#Relate",Const (#,#),[# $ #,# $ #]),
neuper@37906
   600
    (7,[2],"#undef",Const (#,#),[Free #]),
neuper@37906
   601
    (9,[1,2],"#undef",Const (#,#),[# $ #]),
neuper@37906
   602
    (11,[1,2,3],"#undef",Const (#,#),[# $ #])],
neuper@37906
   603
   [(1,[1,2,3],"#Given",Const (#,#),[# $ #]),
neuper@37906
   604
    (2,[1,2,3],"#Find",Const (#,#),[Free #]),
neuper@37906
   605
    (3,[1,2,3],"#Find",Const (#,#),[# $ #,# $ #]),
neuper@37906
   606
    (5,[3],"#Relate",Const (#,#),[# $ #,# $ #,# $ #]),
neuper@37906
   607
    (8,[3],"#undef",Const (#,#),[Free #]),
neuper@37906
   608
    (10,[3],"#undef",Const (#,#),[# $ #]),
neuper@37906
   609
    (11,[1,2,3],"#undef",Const (#,#),[# $ #])]] : ori list list*)
neuper@37906
   610
neuper@37906
   611
fun separate_vats oris =
neuper@37928
   612
    let val vats = foldl coll_vats ([] : int list, oris);
neuper@37906
   613
    in map (filter_vat oris) vats end;
neuper@37906
   614
(*^^^ end preparational work 8.01.*)
neuper@37906
   615
neuper@37906
   616
neuper@37906
   617
neuper@37906
   618
(**. check a problem (ie. itm list) for matching a problemtype .**)
neuper@37906
   619
neuper@37906
   620
fun eq1 d (_,(d',_)) = (d = d');
neuper@37906
   621
fun itm_id ((i,_,_,_,_):itm) = i;
neuper@37906
   622
fun ori_id ((i,_,_,_,_):ori) = i;
neuper@37906
   623
fun ori2itmSup ((i,v,_,d,ts):ori) = ((i,v,true,"#Given",Sup(d,ts)):itm);
neuper@37906
   624
(*see + add_sel_ppc                             ~~~~~~~*)
neuper@37906
   625
fun field_eq f ((_,_,f',_,_):ori) = f = f';
neuper@37906
   626
neuper@37906
   627
(*. check an item (with arbitrary itm_ from previous matchings) 
neuper@37906
   628
    for matching a problemtype; returns true only for itms found in pbt .*)
neuper@37906
   629
fun chk_ thy pbt ((i,vats,b,f,Cor ((d,vs),_)):itm) =
neuper@37906
   630
    (case find_first (eq1 d) pbt of 
neuper@37926
   631
	 SOME (_,(_,id)) => ((i,vats,b,f,Cor ((d,vs),
bonzai@41949
   632
					      (id, pbl_ids' d vs))):itm)
neuper@37926
   633
       | NONE => (i,vats,false,f,Sup (d,vs)))
neuper@37906
   634
  | chk_ thy pbt ((i,vats,b,f,Inc ((d,vs),_)):itm) =
neuper@37906
   635
    (case find_first (eq1 d) pbt of 
neuper@37926
   636
	SOME (_,(_,id)) => ((i,vats,b,f,Cor ((d,vs),
bonzai@41949
   637
					     (id, pbl_ids' d vs))):itm)
neuper@37926
   638
      | NONE => (i,vats,false,f,Sup (d,vs)))
neuper@37906
   639
neuper@37906
   640
  | chk_ thy pbt (itm as (i,vats,b,f,Syn ct):itm) = itm
neuper@37906
   641
  | chk_ thy pbt (itm as (i,vats,b,f,Typ ct):itm) = itm
neuper@37906
   642
neuper@37906
   643
  | chk_ thy pbt ((i,vats,b,f,Sup (d,vs)):itm) =
neuper@37906
   644
    (case find_first (eq1 d) pbt of 
neuper@37926
   645
	SOME (_,(_,id)) => ((i,vats,b,f,Cor ((d,vs),
bonzai@41949
   646
					     (id, pbl_ids' d vs))):itm)
neuper@37926
   647
      | NONE => (i,vats,false,f,Sup (d,vs)))
neuper@37906
   648
(* val (i,vats,b,f,Mis (d,vs)) = i4;
neuper@37906
   649
   *)
neuper@37906
   650
  | chk_ thy pbt ((i,vats,b,f,Mis (d,vs)):itm) =
neuper@37906
   651
    (case find_first (eq1 d) pbt of
neuper@37926
   652
(* val SOME (_,(_,id)) = find_first (eq1 d) pbt;
neuper@37906
   653
   *) 
neuper@38031
   654
	SOME (_,(_,id)) => error "chk_: ((i,vats,b,f,Cor ((d,vs),\
neuper@37906
   655
				   \(id, pbl_ids' d vs))):itm)"
neuper@37926
   656
      | NONE => (i,vats,false,f,Sup (d,[vs])));
neuper@37906
   657
neuper@37906
   658
(* chk_ thy pbt i
neuper@37906
   659
    *)
neuper@37906
   660
neuper@37906
   661
fun eq2 (_,(d,_)) ((_,_,_,_,itm_):itm) = d = d_in itm_;
neuper@37906
   662
fun eq2' (_,(d,_)) ((_,_,_,d',_):ori) = d = d';
neuper@37906
   663
fun eq0 ((0,_,_,_,_):itm) = true
neuper@37906
   664
  | eq0 _ = false;
neuper@37906
   665
fun max_i i [] = i
neuper@37906
   666
  | max_i i ((id,_,_,_,_)::is) = 
neuper@37906
   667
    if i > id then max_i i is else max_i id is;
neuper@37906
   668
fun max_id [] = 0
neuper@37906
   669
  | max_id ((id,_,_,_,_)::is) = max_i id is;
neuper@37906
   670
fun add_idvat itms _ _ [] = itms
neuper@37906
   671
  | add_idvat itms i mvat (((_,_,b,f,itm_):itm)::its) =
neuper@37906
   672
    add_idvat (itms @ [(i,[(*mvat ...meaningless with pbl-identifier *)
neuper@37906
   673
			     ],b,f,itm_):itm]) (i+1) mvat its;
neuper@37906
   674
neuper@37906
   675
neuper@37906
   676
(*. find elements of pbt not contained in itms;
neuper@37906
   677
    if such one is untouched, return this one, otherwise create new itm .*)
neuper@37906
   678
fun chk_m (itms:itm list) untouched (p as (f,(d,id))) = 
neuper@37906
   679
    case find_first (eq2 p) itms of
neuper@37926
   680
	SOME _ => []
neuper@37926
   681
      | NONE => (case find_first (eq2 p) untouched of
neuper@37926
   682
		     SOME itm => [itm]
neuper@37926
   683
		   | NONE => [(0,[],false,f,Mis (d,id)):itm]);
neuper@37906
   684
(* val itms = itms'';
neuper@37906
   685
   *) 
neuper@37906
   686
fun chk_mis mvat itms untouched pbt = 
neuper@37906
   687
    let val mis = (flat o (map (chk_m itms untouched))) pbt; 
neuper@37906
   688
        val mid = max_id itms;
neuper@37906
   689
    in add_idvat [] (mid + 1) mvat mis end;
neuper@37906
   690
neuper@37906
   691
(*. check a problem (ie. itm list) for matching a problemtype, 
neuper@37906
   692
    takes the max_vt for concluding completeness (could be another!) .*)
neuper@37906
   693
(* val itms = itms'; val (pbt,pre) = (ppc, pre);
neuper@37906
   694
   val itms = itms; val (pbt,pre) = (ppc, pre);
neuper@37906
   695
   *)
neuper@37906
   696
fun match_itms thy itms (pbt,pre,prls) = 
neuper@37928
   697
    (let fun okv mvat (_,vats,b,_,_) = member op = vats mvat
neuper@37928
   698
				       andalso b;
neuper@37906
   699
	val itms' = map (chk_ thy pbt) itms; (*all found are #3 true*)
neuper@37906
   700
        val mvat = max_vt itms';
neuper@37906
   701
	val itms'' = filter (okv mvat) itms';
neuper@37906
   702
	val untouched = filter eq0 itms;(*i.e. dsc only (from init)*)
neuper@37906
   703
	val mis = chk_mis mvat itms'' untouched pbt;
neuper@37906
   704
	val pre' = check_preconds' prls pre itms'' mvat
neuper@37906
   705
	val pb = foldl and_ (true, map fst pre')
neuper@37906
   706
    in (length mis = 0 andalso pb, (itms'@ mis, pre')) end);
neuper@37906
   707
neuper@37906
   708
(*. check a problem pbl (ie. itm list) for matching a problemtype pbt,
neuper@37906
   709
    for missing items get data from formalization (ie. ori list); 
neuper@37906
   710
    takes the max_vt for concluding completeness (could be another!) .*)
neuper@37906
   711
(*  (0) determine the most frequent variant mv in pbl
neuper@37906
   712
    ALL pbt. (1) dsc(pbt) notmem dsc(pbls) =>
neuper@37906
   713
             (2) filter (dsc(pbt) = dsc(oris)) oris; -> news;
neuper@37906
   714
             (3) newitms = filter (mv mem vat(news)) news 
neuper@37906
   715
    (4) pbt @ newitms                                           *)
neuper@37906
   716
(* val (pbl, pbt, pre) = (met, mtt, pre);
neuper@37906
   717
   val (pbl, pbt, pre) = (itms, #ppc pbt, #where_ pbt);
neuper@37906
   718
   val (pbl, pbt, pre) = (itms, ppc, where_);
neuper@37906
   719
   *)
neuper@37906
   720
fun match_itms_oris thy (pbl:itm list) (pbt, pre, prls) oris =
neuper@37906
   721
  let 
neuper@37906
   722
 (*0*)val mv = max_vt pbl;
neuper@37906
   723
neuper@37906
   724
      fun eqdsc_pbt_itm ((_,(d,_))) ((_,_,_,_,itm_):itm) = d = d_in itm_;
neuper@37906
   725
      fun notmem pbl pbt1 = case find_first (eqdsc_pbt_itm pbt1) pbl of
neuper@37926
   726
				SOME _ => false | NONE => true;
neuper@37906
   727
 (*1*)val mis = (*(map (cons2 (fst, fst o snd)))o*) (filter (notmem pbl)) pbt;
neuper@37906
   728
neuper@37906
   729
      fun eqdsc_ori (_,(d,_)) ((_,_,_,d',_):ori) = d = d';
neuper@37906
   730
      fun ori2itmMis (f,(d,pid)) ((i,v,_,_,ts):ori) = 
neuper@37906
   731
	  (i,v,false,f,Mis (d,pid)):itm;
neuper@37906
   732
 (*2*)fun oris2itms oris mis1 = 
neuper@37906
   733
	  ((map (ori2itmMis mis1)) o (filter (eqdsc_ori mis1))) oris;
neuper@37906
   734
      val news = (flat o (map (oris2itms oris))) mis;
neuper@37928
   735
 (*3*)fun mem_vat (_,vats,b,_,_) = member op = vats mv;
neuper@37906
   736
      val newitms = filter mem_vat news;
neuper@37906
   737
 (*4*)val itms' = pbl @ newitms;
neuper@37906
   738
      val pre' = check_preconds' prls pre itms' mv
neuper@37906
   739
      val pb = foldl and_ (true, map fst pre')
neuper@37906
   740
  in (length mis = 0 andalso pb, (itms', pre')) end;
neuper@37906
   741
    (*handle _ => (false,([],[]))*);
neuper@37906
   742
neuper@37906
   743
neuper@37906
   744
(*vvv--- doubled 20.9.01: ... 7.3.02 itms  -->  oris, because oris
neuper@37906
   745
  allow for faster access to descriptions and terms *)
neuper@37906
   746
(**. check a problem (ie. itm list) for matching a problemtype .**)
neuper@37906
   747
neuper@37906
   748
(*. check an ori for matching a problemtype by description; 
neuper@37906
   749
    returns true only for itms found in pbt .*)
neuper@37906
   750
fun chk1_ thy pbt ((i,vats,f,d,vs):ori) =
neuper@37906
   751
    case find_first (eq1 d) pbt of 
neuper@37926
   752
	SOME (_,(_,id)) => [(i,vats,true,f,
bonzai@41949
   753
			     Cor ((d,vs), (id, pbl_ids' d vs))):itm]
neuper@37926
   754
      | NONE => [];
neuper@37906
   755
neuper@37906
   756
(* elem 'p' of pbt contained in itms ? *)
neuper@37906
   757
fun chk1_m (itms:itm list) p = 
neuper@37906
   758
    case find_first (eq2 p) itms of
neuper@37926
   759
	SOME _ => true | NONE => false;
neuper@37906
   760
fun chk1_m' (oris: ori list) (p as (f,(d,t))) = 
neuper@37906
   761
    case find_first (eq2' p) oris of
neuper@37926
   762
	SOME _ => []
neuper@37926
   763
      | NONE => [(f, Mis (d, t))];
neuper@37906
   764
fun pair0vatsfalse (f,itm_) = (0,[],false,f,itm_):itm;
neuper@37906
   765
neuper@37906
   766
fun chk1_mis mvat itms ppc = foldl and_ (true, map (chk1_m itms) ppc);
neuper@37906
   767
fun chk1_mis' oris ppc = 
neuper@37906
   768
    map pair0vatsfalse ((flat o (map (chk1_m' oris))) ppc);
neuper@37906
   769
neuper@37906
   770
  
neuper@37906
   771
(*. check a problem (ie. ori list) for matching a problemtype, 
neuper@37906
   772
    takes the max_vt for concluding completeness (FIXME could be another!) .*)
neuper@37906
   773
(* val (prls,oris,pbt,pre)=(#prls py, ori, #ppc py, #where_ py);
neuper@37906
   774
   *)
neuper@37906
   775
fun match_oris thy prls oris (pbt,pre) = 
neuper@37906
   776
    let val itms = (flat o (map (chk1_ thy pbt))) oris;
neuper@37906
   777
        val mvat = max_vt itms;
neuper@37906
   778
	val complete = chk1_mis mvat itms pbt;
neuper@37906
   779
	val pre' = check_preconds' prls pre itms mvat
neuper@37906
   780
	val pb = foldl and_ (true, map fst pre')
neuper@37906
   781
    in if complete andalso pb then true else false end;
neuper@37906
   782
neuper@37906
   783
(*. check a problem (ie. ori list) for matching a problemtype, 
neuper@37906
   784
    returns items for output to math-experts .*)
neuper@37906
   785
fun match_oris' thy oris (ppc,pre,prls) =
neuper@37906
   786
(* val (thy, oris, (ppc,pre,prls)) = (thy, oris, (ppc, where_, prls));
neuper@37906
   787
   *)
neuper@37906
   788
    let val itms = (flat o (map (chk1_ thy ppc))) oris;
neuper@37906
   789
	val sups = ((map ori2itmSup) o (filter(field_eq "#undef")))oris;
neuper@37906
   790
        val mvat = max_vt itms;
neuper@37906
   791
	val miss = chk1_mis' oris ppc;
neuper@37906
   792
	val pre' = check_preconds' prls pre itms mvat
neuper@37906
   793
	val pb = foldl and_ (true, map fst pre')
neuper@37906
   794
    in (miss = [] andalso pb, (itms @ miss @ sups, pre')) end;
neuper@37906
   795
neuper@41980
   796
(* for the user *)
neuper@37906
   797
datatype match' = 
neuper@37906
   798
  Matches' of item ppc
neuper@37906
   799
| NoMatch' of item ppc;
neuper@37906
   800
neuper@41980
   801
(* match a formalization with a problem type *)
neuper@37906
   802
fun match_pbl (fmz:fmz_) ({thy=thy,where_=pre,ppc,prls=er,...}:pbt) =
bonzai@41949
   803
    let val oris = prep_ori fmz thy ppc |> #1;
neuper@37906
   804
	val (bool, (itms, pre')) = match_oris' thy oris (ppc,pre,er);
neuper@37906
   805
    in if bool then Matches' (itms2itemppc thy itms pre')
neuper@37906
   806
       else NoMatch' (itms2itemppc thy itms pre') end;
neuper@37906
   807
(* 
neuper@37906
   808
val fmz = ["equality (sqrt(9+4*x)=sqrt x + sqrt(5+x))",
neuper@37906
   809
	      "solveFor x","errorBound (eps=0)","solutions L"];
neuper@37906
   810
val pbt as {thy = thy, where_ = pre, ppc = ppc,...} =
neuper@37906
   811
    get_pbt ["univariate","equation"];
neuper@37906
   812
match_pbl fmz pbt;
neuper@37906
   813
*)
neuper@37906
   814
neuper@37906
   815
neuper@37906
   816
(*. refine a problem; construct pblRD while scanning .*)
neuper@37906
   817
(* val (pblRD,ori)=("xxx",oris);
neuper@37906
   818
 val py = get_pbt ["equation"];
neuper@37906
   819
 val py = get_pbt ["univariate","equation"];
neuper@37906
   820
 val py = get_pbt ["linear","univariate","equation"];
neuper@37986
   821
 val py = get_pbt ["root'","univariate","equation"];
neuper@37906
   822
 match_oris (#prls py) ori (#ppc py, #where_ py);
neuper@37906
   823
neuper@37906
   824
  *)
neuper@37906
   825
fun refin (pblRD:pblRD) ori 
neuper@37906
   826
((Ptyp (pI,[py],[])):pbt ptyp) =
neuper@37906
   827
    if match_oris (#thy py) (#prls py) ori (#ppc py, #where_ py) 
neuper@37926
   828
    then SOME ((pblRD @ [pI]):pblRD)
neuper@37926
   829
    else NONE
neuper@37906
   830
  | refin pblRD ori (Ptyp (pI,[py],pys)) =
neuper@37906
   831
    if match_oris (#thy py) (#prls py) ori (#ppc py, #where_ py) 
neuper@37906
   832
    then (case refins (pblRD @ [pI]) ori pys of
neuper@37926
   833
	      SOME pblRD' => SOME pblRD'
neuper@37926
   834
	    | NONE => SOME (pblRD @ [pI]))
neuper@37926
   835
    else NONE
neuper@37926
   836
and refins pblRD ori [] = NONE
neuper@37906
   837
  | refins pblRD ori ((p as Ptyp (pI,_,_))::pts) =
neuper@37906
   838
    (case refin pblRD ori p of
neuper@37926
   839
	 SOME pblRD' => SOME pblRD'
neuper@37926
   840
       | NONE => refins pblRD ori pts);
neuper@37906
   841
neuper@37906
   842
(*. refine a problem; version providing output for math-experts .*)
neuper@42391
   843
fun refin' (pblRD: pblRD) fmz pbls ((Ptyp (pI, [py], [])):pbt ptyp) =
neuper@42391
   844
    let
neuper@42391
   845
      val _ = (tracing o ((curry op ^)"*** pass ") o strs2str) (pblRD @ [pI])
neuper@42391
   846
      val {thy, ppc, where_, prls, ...} = py 
neuper@42391
   847
      val oris = prep_ori fmz thy ppc |> #1;
neuper@42391
   848
      (*8.3.02: itms!: oris ev. are _not_ complete here*)
neuper@42391
   849
      val (b, (itms, pre')) = match_oris' thy oris (ppc, where_, prls)
neuper@42391
   850
    in if b
neuper@42391
   851
      then pbls @ [Matches (rev (pblRD @ [pI]), itms2itemppc thy itms pre')]
neuper@42391
   852
      else pbls @ [NoMatch (rev (pblRD @ [pI]), itms2itemppc thy itms pre')]
neuper@37906
   853
    end
neuper@42391
   854
neuper@42391
   855
  | refin' pblRD fmz pbls (Ptyp (pI, [py], pys)) =
neuper@42391
   856
    let
neuper@42391
   857
      val _ = (tracing o ((curry op ^)"*** pass ") o strs2str) (pblRD @ [pI])
neuper@42391
   858
      val {thy, ppc, where_, prls, ...} = py 
neuper@42391
   859
      val oris = prep_ori fmz thy ppc |> #1;
neuper@42391
   860
      (*8.3.02: itms!: oris ev. are _not_ complete here*)
neuper@42391
   861
      val(b, (itms, pre')) = match_oris' thy oris (ppc,where_,prls);
neuper@37906
   862
    in if b 
neuper@42391
   863
      then let val pbl = Matches (rev (pblRD @ [pI]), itms2itemppc thy itms pre')
neuper@42391
   864
	      in refins' (pblRD @ [pI]) fmz (pbls @ [pbl]) pys end
neuper@42391
   865
      else (pbls @ [NoMatch (rev (pblRD @ [pI]), itms2itemppc thy itms pre')])
neuper@37906
   866
    end
neuper@42391
   867
neuper@37906
   868
and refins' pblRD fmz pbls [] = pbls
neuper@42391
   869
  | refins' pblRD fmz pbls ((p as Ptyp (pI, _, _))::pts) =
neuper@37906
   870
    let val pbls' = refin' pblRD fmz pbls p
neuper@37906
   871
    in case last_elem pbls' of
neuper@42391
   872
        Matches _ => pbls'
neuper@42391
   873
      | NoMatch _ => refins' pblRD fmz pbls' pts
neuper@42391
   874
    end;
neuper@37906
   875
neuper@37906
   876
(*. refine a problem; version for tactic Refine_Problem .*)
neuper@37906
   877
fun refin'' thy (pblRD:pblRD) itms pbls ((Ptyp (pI,[py],[])):pbt ptyp) =
neuper@38015
   878
    let (*val _ = tracing("### refin''1: pI="^pI);*)
neuper@37906
   879
	val {thy,ppc,where_,prls,...} = py 
neuper@37906
   880
	val (b, (itms', pre')) = match_itms thy itms (ppc,where_,prls);
neuper@37906
   881
    in if b then pbls @ [Match_ (rev (pblRD @ [pI]), (itms', pre'))]
neuper@37906
   882
       else pbls @ [NoMatch_] 
neuper@37906
   883
    end
neuper@37906
   884
(* val pblRD = (rev o tl) pblID; val pbls = []; 
neuper@37906
   885
   val Ptyp (pI,[py],pys) = app_ptyp I pblID (rev pblID) (!ptyps);
neuper@37906
   886
   *)
neuper@37906
   887
  | refin'' thy pblRD itms pbls (Ptyp (pI,[py],pys)) =
neuper@38015
   888
    let (*val _ = tracing("### refin''2: pI="^pI);*)
neuper@37906
   889
	val {thy,ppc,where_,prls,...} = py 
neuper@37906
   890
	val(b, (itms', pre')) = match_itms thy itms (ppc,where_,prls);
neuper@37906
   891
    in if b 
neuper@37906
   892
       then let val pbl = Match_ (rev (pblRD @ [pI]), (itms', pre'))
neuper@37906
   893
	    in refins'' thy (pblRD @ [pI]) itms (pbls @ [pbl]) pys end
neuper@37906
   894
       else (pbls @ [NoMatch_])
neuper@37906
   895
    end
neuper@37906
   896
and refins'' thy pblRD itms pbls [] = pbls
neuper@37906
   897
  | refins'' thy pblRD itms pbls ((p as Ptyp (pI,_,_))::pts) =
neuper@37906
   898
    let val pbls' = refin'' thy pblRD itms pbls p
neuper@37906
   899
    in case last_elem pbls' of
neuper@37906
   900
	 Match_ _ => pbls'
neuper@37906
   901
       | NoMatch_ => refins'' thy pblRD itms pbls' pts end;
neuper@37906
   902
neuper@37906
   903
(*. for tactic Refine_Tacitly .*)
neuper@37906
   904
(*!!! oris are already created wrt. some pbt; pbt contains thy for parsing*)
neuper@37906
   905
(* val (thy,pblID) = (assoc_thy dI',pI);
neuper@37906
   906
   *)
neuper@37906
   907
fun refine_ori oris (pblID:pblID) =
neuper@37906
   908
    let val opt = app_ptyp (refin ((rev o tl) pblID) oris) 
s1210629013@55321
   909
			   pblID (rev pblID);
neuper@37906
   910
    in case opt of 
neuper@37926
   911
	   SOME pblRD => let val (pblID':pblID) =(rev pblRD)
neuper@37926
   912
			 in if pblID' = pblID then NONE
neuper@37926
   913
			    else SOME pblID' end
neuper@37926
   914
	 | NONE => NONE end;
neuper@37906
   915
fun refine_ori' oris pI = (the (refine_ori oris pI)) handle _ => pI;
neuper@37906
   916
neuper@37906
   917
(*. for tactic Refine_Problem .*); 
neuper@37906
   918
(* 10.03: returnvalue -> (pIrefined, itm list) would be sufficient *)
neuper@37906
   919
(* val pblID = pI; app_ptyp I pblID (rev pblID) (!ptyps);
neuper@37906
   920
   *)
neuper@37906
   921
fun refine_pbl thy (pblID:pblID) itms =
neuper@37906
   922
    case refined_ (app_ptyp (refin'' thy ((rev o tl) pblID) itms []) 
s1210629013@55321
   923
			    pblID (rev pblID)) of
neuper@37926
   924
	NONE => NONE
neuper@37926
   925
      | SOME (Match_ (rfd as (pI',_))) => 
neuper@37926
   926
	if pblID = pI' then NONE else SOME rfd;
neuper@37906
   927
neuper@37906
   928
neuper@37906
   929
(*. for math-experts .*)
neuper@42390
   930
(*FIXME.WN021019: needs thy for parsing fmz*)
neuper@37906
   931
(* val fmz = fmz1; val pblID = ["pbla"]; val pblRD = (rev o tl) pblID; 
neuper@37906
   932
   val pbls = []; val ptys = !ptyps;
neuper@37906
   933
   *)
neuper@37906
   934
fun refine (fmz:fmz_) (pblID:pblID) =
s1210629013@55321
   935
  app_ptyp (refin' ((rev o tl) pblID) fmz []) pblID (rev pblID);
neuper@37906
   936
neuper@37906
   937
neuper@37906
   938
(*.make a guh from a reference to an element in the kestore;
neuper@37906
   939
   EXCEPT theory hierarchy ... compare 'fun keref2xml'.*)
neuper@37906
   940
fun pblID2guh (pblID:pblID) =
neuper@37906
   941
    (((#guh o get_pbt) pblID)
neuper@38031
   942
     handle _ => error ("pblID2guh: not for '"^strs2str' pblID ^ "'"));
neuper@37906
   943
fun metID2guh (metID:metID) =
neuper@37906
   944
    (((#guh o get_met) metID)
neuper@38031
   945
     handle _ => error ("metID2guh: no 'Met_' for '"^
neuper@37906
   946
			      strs2str' metID ^ "'"));
neuper@37906
   947
fun kestoreID2guh Pbl_ (kestoreID:kestoreID) = pblID2guh kestoreID
neuper@37906
   948
  | kestoreID2guh Met_ (kestoreID:kestoreID) = metID2guh kestoreID
neuper@37906
   949
  | kestoreID2guh ketype kestoreID =
neuper@38031
   950
    error ("kestoreID2guh: '" ^ ketype2str ketype ^ "' not for '" ^
neuper@37906
   951
		 strs2str' kestoreID ^ "'");
neuper@37906
   952
neuper@37906
   953
fun show_pblguhs () =
wneuper@59106
   954
    (default_print_depth 999; 
s1210629013@55338
   955
     (tracing o strs2str o (map linefeed)) (coll_pblguhs (get_ptyps ())); 
wneuper@59106
   956
     default_print_depth 3);
neuper@37906
   957
fun sort_pblguhs () =
wneuper@59106
   958
    (default_print_depth 999; 
neuper@38015
   959
     (tracing o strs2str o (map linefeed)) 
s1210629013@55338
   960
	 (((sort string_ord) o coll_pblguhs) (get_ptyps ())); 
wneuper@59106
   961
     default_print_depth 3);
neuper@37906
   962
neuper@37906
   963
fun show_metguhs () =
wneuper@59106
   964
    (default_print_depth 999; 
s1210629013@55372
   965
     (tracing o strs2str o (map linefeed)) (coll_metguhs (get_mets ())); 
wneuper@59106
   966
     default_print_depth 3);
neuper@37906
   967
fun sort_metguhs () =
wneuper@59106
   968
    (default_print_depth 999; 
neuper@38015
   969
     (tracing o strs2str o (map linefeed)) 
s1210629013@55372
   970
	 (((sort string_ord) o coll_metguhs) (get_mets ())); 
wneuper@59106
   971
     default_print_depth 3);