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