src/Tools/isac/Specify/refine.sml
author wneuper <Walther.Neuper@jku.at>
Sat, 26 Aug 2023 10:51:35 +0200
changeset 60736 7297c166991e
parent 60729 43d11e7742e1
child 60738 74b4c2abdb84
permissions -rw-r--r--
prepare 17: repair Biegelinie formals and accordingly data for Formlise.model
walther@59960
     1
(* Title:  Specify/refine.sml
walther@59960
     2
   Author: Walther Neuper 110226
walther@59960
     3
   (c) due to copyright terms
walther@59960
     4
Walther@60555
     5
Refine a problem by a search for a \<open>ML_structure Model_Pattern\<close> 
Walther@60586
     6
better fitting the respective where_-condition.
Walther@60555
     7
Walther@60658
     8
The search on the tree given by @{term Know_Store.get_pbls} is costly such that 
Walther@60658
     9
\<open>ML_structure Know_Store\<close> holds terms pre-parsed with a most generally type. 
Walther@60658
    10
Walther@60658
    11
On transfer to a calculation these terms are strongly typed by Model_Pattern.adapt_to_type
Walther@60658
    12
(and users of this function in \<open>ML_structure Error_Pattern, MethodC, Problem\<close>)
Walther@60658
    13
according to the current context.
Walther@60555
    14
Walther@60555
    15
Note: From the children of eb89f586b0b2 onwards the old functions (\<open>term TermC.typ_a2real\<close> etc)
Walther@60555
    16
are adapted for "adapt_to_type on the fly" until further clarification.
Walther@60556
    17
Walther@60556
    18
timing (from evaluating imports to use-cases.sml) for Test_Isac_Short
Walther@60556
    19
  before adapt_to_type: 01:05 sec:sec
Walther@60556
    20
  after  adapt_to_type: 
Walther@60556
    21
timing for test/../refine.sml --- equation with CalcTree [ = 6 / 5] for timing: ...
Walther@60556
    22
  before adapt_to_type: 1.5 sec
Walther@60556
    23
  after  adapt_to_type: 
Walther@60556
    24
timing for test/../refine.sml --- refine ad-hoc equation for timing: ...
Walther@60556
    25
  before adapt_to_type: 0.05 sec
Walther@60556
    26
  after  adapt_to_type: 
Walther@60556
    27
*)                          
walther@59960
    28
walther@59960
    29
signature REFINE_PROBLEM =
walther@59960
    30
sig
Walther@60556
    31
(**)
walther@59968
    32
  val problem: theory -> Problem.id -> I_Model.T -> (Problem.id * (I_Model.T * Pre_Conds.T)) option
walther@59968
    33
Walther@60556
    34
(**)
Walther@60559
    35
  val refine_ori : Proof.context -> O_Model.T -> Problem.id -> Problem.id option
Walther@60556
    36
(**)
Walther@60559
    37
  val refine_ori' : Proof.context -> O_Model.T -> Problem.id -> Problem.id
Walther@60705
    38
(*from isac_test for Minisubpbl*)
Walther@60705
    39
(**)
Walther@60706
    40
  datatype match_ = Match_ of Problem.id * (( I_Model.T) * (Pre_Conds.T)) | NoMatch_;
Walther@60705
    41
(**)
Walther@60705
    42
  val refin'': theory -> Problem.id -> I_Model.T -> match_ list -> Problem.T Store.node -> match_ list
walther@59960
    43
Walther@60729
    44
(*from isac_test for Minisubpbl*)
Walther@60729
    45
  val app_ptyp: (Probl_Def.T Store.node -> 'a) -> Store.key -> Store.key -> 'a
Walther@60729
    46
(*val refin: Problem.id -> O_Model.T -> Problem.T Store.node -> Problem.id option*)
Walther@60729
    47
  val refin: Proof.context -> Problem.id -> O_Model.T -> Problem.T Store.node -> Problem.id option
Walther@60575
    48
(*val test : Formalise.model -> Problem.id -> M_Match.T list*)
Walther@60556
    49
(*val refine : Formalise.model -> Problem.id -> M_Match.T list*)
Walther@60575
    50
  val xxxxx: Proof.context -> Formalise.model -> Problem.id -> M_Match.T list
Walther@60736
    51
Walther@60736
    52
\<^isac_test>\<open>
Walther@60556
    53
(**)
walther@60021
    54
  val refined_: match_ list -> match_ option
Walther@60556
    55
(**)
Walther@60556
    56
  val refins'': theory -> Problem.id -> I_Model.T -> match_ list -> Problem.T Store.node list -> match_ list
Walther@60556
    57
(**)
wenzelm@60223
    58
\<close>
walther@59960
    59
end
walther@59960
    60
walther@59965
    61
(**)
walther@59960
    62
structure Refine(**) : REFINE_PROBLEM(**) =
walther@59960
    63
struct
walther@59965
    64
(**)
walther@59960
    65
walther@59960
    66
datatype match_ = 
walther@59963
    67
  Match_ of Problem.id * (( I_Model.T) * (Pre_Conds.T))
walther@59960
    68
| NoMatch_;
walther@59960
    69
walther@59960
    70
fun is_matches_ (Match_ _) = true
walther@59960
    71
  | is_matches_ _ = false;
walther@59960
    72
walther@59960
    73
fun refined_ ms = ((find_first is_matches_) o rev) ms;
walther@59960
    74
walther@59960
    75
fun eq1 d (_, (d', _)) = (d = d');
walther@59960
    76
Walther@60478
    77
(*  chk_: theory -> ('a * (term * term)) list -> I_Model.single -> I_Model.single*)
walther@59960
    78
fun chk_ (_: theory) pbt (i, vats, b, f, I_Model.Cor ((d, vs), _)) =
walther@59960
    79
      (case find_first (eq1 d) pbt of 
Walther@60478
    80
        SOME (_, (_, id)) => (i, vats, b, f, I_Model.Cor ((d, vs), (id, [Input_Descript.join'''' (d, vs)])))
walther@59960
    81
      | NONE =>  (i, vats, false, f, I_Model.Sup (d, vs)))
walther@59960
    82
  | chk_ _ pbt (i, vats, b, f, I_Model.Inc ((d, vs), _)) =
walther@59960
    83
      (case find_first (eq1 d) pbt of 
Walther@60478
    84
        SOME (_, (_, id)) => (i, vats, b, f, I_Model.Cor ((d, vs), (id, [Input_Descript.join'''' (d, vs)])))
walther@59960
    85
      | NONE => (i, vats, false, f, I_Model.Sup (d, vs)))
walther@59960
    86
  | chk_ _ _ (itm as (_, _, _, _, I_Model.Syn _)) = itm
walther@59960
    87
  | chk_ _ _ (itm as (_, _, _, _, I_Model.Typ _)) = itm
walther@59960
    88
  | chk_ _ pbt (i, vats, b, f, I_Model.Sup (d, vs)) =
walther@59960
    89
      (case find_first (eq1 d) pbt of 
Walther@60478
    90
        SOME (_, (_, id)) => (i, vats, b, f, I_Model.Cor ((d,vs), (id, [Input_Descript.join'''' (d, vs)])))
walther@59960
    91
      | NONE => (i, vats, false, f, I_Model.Sup (d, vs)))
walther@59960
    92
  | chk_ _ pbt (i, vats, _, f, I_Model.Mis (d, vs)) =
walther@59960
    93
      (case find_first (eq1 d) pbt of
Walther@60478
    94
        SOME _ =>
Walther@60478
    95
          raise ERROR "chk_: ((i,vats,b,f,I_Model.Cor ((d,vs),(id, Input_Descript.join'''' d vs))):itm)"
walther@59960
    96
      | NONE => (i, vats, false, f, I_Model.Sup (d, [vs])))
walther@59960
    97
  | chk_ _ _ _ = raise ERROR "chk_: uncovered fun def.";
walther@59960
    98
Walther@60477
    99
fun eq2 (_, (d, _)) (_, _, _, _, itm_) = d = I_Model.descriptor itm_;
walther@59960
   100
fun eq0 (0, _, _, _, _) = true
walther@59960
   101
  | eq0 _ = false;
walther@59960
   102
fun max_i i [] = i
walther@59960
   103
  | max_i i ((id, _, _, _, _) :: is) = if i > id then max_i i is else max_i id is;
walther@59960
   104
fun max_id [] = 0
walther@59960
   105
  | max_id ((id, _, _, _, _) :: is) = max_i id is;
walther@59960
   106
fun add_idvat itms _ _ [] = itms
walther@59960
   107
  | add_idvat itms i mvat ((_, _, b, f, itm_) :: its) =
walther@59960
   108
    add_idvat (itms @ [(i, [], b, f, itm_)]) (i + 1) mvat its;
walther@59960
   109
walther@59960
   110
(* find elements of pbt not contained in itms;
walther@59960
   111
   if such one is untouched, return this one, otherwise create new itm *)
walther@59960
   112
fun chk_m itms untouched (p as (f, (d, id))) = 
walther@59960
   113
  case find_first (eq2 p) itms of
walther@59960
   114
	  SOME _ => []
walther@59960
   115
  | NONE =>
walther@59960
   116
      (case find_first (eq2 p) untouched of
walther@59960
   117
        SOME itm => [itm]
walther@59960
   118
      | NONE => [(0, [], false, f, I_Model.Mis (d, id))]);
walther@59960
   119
walther@59960
   120
fun chk_mis mvat itms untouched pbt = 
walther@59960
   121
    let val mis = (flat o (map (chk_m itms untouched))) pbt; 
walther@59960
   122
        val mid = max_id itms;
walther@59960
   123
    in add_idvat [] (mid + 1) mvat mis end;
walther@59960
   124
walther@59960
   125
(* check a problem (ie. itm list) for matching a problemtype, 
Walther@60705
   126
   takes the Pre_Conds.max_variant for concluding completeness (could be another!) *)
Walther@60729
   127
(*T_TESTold* )
Walther@60586
   128
fun match_itms thy itms (pbt, where_, where_rls) = 
walther@59960
   129
  let
walther@59960
   130
    fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b;
walther@59960
   131
    val itms' = map (chk_ thy pbt) itms; (* all found are #3 true *)
Walther@60705
   132
    val mvat = Pre_Conds.max_variant itms';
walther@59960
   133
	  val itms'' = filter (okv mvat) itms';
walther@59960
   134
	  val untouched = filter eq0 itms; (* i.e. dsc only (from init)*)
walther@59960
   135
	  val mis = chk_mis mvat itms'' untouched pbt;
Walther@60590
   136
	  val (pb, where_')  = Pre_Conds.check (Proof_Context.init_global thy) where_rls where_ itms'' mvat
Walther@60586
   137
  in (length mis = 0 andalso pb, (itms'@ mis, where_')) end;
Walther@60729
   138
( *T_TEST**)
Walther@60729
   139
fun match_itms thy itms (pbt, where_, where_rls) = 
Walther@60729
   140
  let
Walther@60729
   141
    fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b;
Walther@60729
   142
    val itms' = map (chk_ thy pbt) itms; (* all found are #3 true *)
Walther@60729
   143
    val mvat = Pre_Conds.max_variant itms';
Walther@60729
   144
	  val itms'' = filter (okv mvat) itms';
Walther@60729
   145
	  val untouched = filter eq0 itms; (* i.e. dsc only (from init)*)
Walther@60729
   146
	  val mis = chk_mis mvat itms'' untouched pbt;
Walther@60729
   147
	  val (pb, where_')  = Pre_Conds.check_OLD (Proof_Context.init_global thy) where_rls where_
Walther@60729
   148
	    (pbt, I_Model.OLD_to_TEST itms'')
Walther@60729
   149
  in (length mis = 0 andalso pb, (itms'@ mis, where_')) end;
Walther@60729
   150
(*T_TESTnew*)
walther@59960
   151
walther@59960
   152
(* refine a problem; version for tactic Refine_Problem *)
walther@59960
   153
fun refin'' _ (pblRD: Problem.id) itms pbls (Store.Node (pI, [py], [])) =
walther@59960
   154
    let
Walther@60585
   155
	    val {thy, model, where_, where_rls, ...} = py
Walther@60556
   156
	    (*TODO val where_ = map TermC.adapt_to_type where_ ...  adapt to current ctxt*)
Walther@60586
   157
	    val (b, (itms', where_')) = match_itms thy itms (model, where_, where_rls);
walther@59960
   158
    in
walther@59960
   159
      if b
Walther@60586
   160
      then pbls @ [Match_ (rev (pblRD @ [pI]), (itms', where_'))]
walther@59960
   161
      else pbls @ [NoMatch_] 
walther@59960
   162
    end
walther@59960
   163
  | refin'' _ pblRD itms pbls (Store.Node (pI, [py], pys)) =
walther@59960
   164
    let
Walther@60585
   165
      val {thy, model, where_, where_rls, ...} = py 
Walther@60586
   166
      val (b, (itms', where_')) = match_itms thy itms (model, where_, where_rls);
walther@59960
   167
    in if b 
Walther@60586
   168
       then let val pbl = Match_ (rev (pblRD @ [pI]), (itms', where_'))
walther@59960
   169
	    in refins'' thy (pblRD @ [pI]) itms (pbls @ [pbl]) pys end
walther@59960
   170
       else (pbls @ [NoMatch_])
Walther@60556
   171
    end              
walther@59965
   172
  | refin'' _ _ _ _ _ = raise ERROR "refin'': uncovered fun def."
walther@59960
   173
and refins'' _ _ _ pbls [] = pbls
walther@59960
   174
  | refins'' thy pblRD itms pbls ((p as Store.Node _) :: pts) =
walther@59960
   175
    let
walther@59960
   176
      val pbls' = refin'' thy pblRD itms pbls p
walther@59960
   177
    in case last_elem pbls' of
walther@59960
   178
      Match_ _ => pbls'
walther@59960
   179
    | NoMatch_ => refins'' thy pblRD itms pbls' pts
walther@59960
   180
  end;
walther@59960
   181
walther@59960
   182
fun problem thy pblID itms =
Walther@60495
   183
  case refined_ ((Store.apply (get_pbls ())) (refin'' thy ((rev o tl) pblID) itms [])
walther@60324
   184
      pblID (rev pblID)) of
walther@59960
   185
	  NONE => NONE
Walther@60556
   186
  | SOME (Match_ (rfd as (pI', _))) => if pblID = pI' then NONE else SOME rfd;
walther@59960
   187
Walther@60556
   188
(* 
Walther@60729
   189
  refine a problem; construct pblRD while scanning Problem.T Store.T
Walther@60729
   190
TODO: as \<open>refin: 'a -> .. -> 'b option\<close> could be ignorant of Store.T structure.
Walther@60556
   191
*)
Walther@60559
   192
fun refin ctxt pblRD ori (Store.Node (pI, [py], [])) =
Walther@60556
   193
    let
Walther@60729
   194
val _ = writeln ("refin 1: " ^ strs2str pblRD)
Walther@60585
   195
      val {where_rls, model, where_, ...} = py: Problem.T
Walther@60585
   196
      val model = map (Model_Pattern.adapt_to_type ctxt) model
Walther@60660
   197
      val where_ = map (ParseC.adapt_term_to_type ctxt) where_
Walther@60556
   198
    in
Walther@60590
   199
      if M_Match.match_oris ctxt where_rls ori (model, where_) 
Walther@60729
   200
      then SOME (pblRD(**) @ [pI](**))
Walther@60556
   201
      else NONE
Walther@60556
   202
    end
Walther@60559
   203
  | refin ctxt pblRD ori (Store.Node (pI, [py], pys)) =
Walther@60556
   204
    let
Walther@60729
   205
val _ = writeln ("refin 2: " ^ strs2str pblRD)
Walther@60585
   206
      val {where_rls, model, where_, ...} = py: Problem.T
Walther@60585
   207
      val model = map (Model_Pattern.adapt_to_type ctxt) model
Walther@60660
   208
      val where_ = map (ParseC.adapt_term_to_type ctxt) where_
Walther@60556
   209
    in
Walther@60590
   210
      if M_Match.match_oris ctxt where_rls ori (model, where_) 
Walther@60575
   211
      then (case refins ctxt (pblRD @ [pI]) ori pys of
Walther@60556
   212
	        SOME pblRD' => SOME pblRD'
Walther@60729
   213
	      | NONE => SOME (pblRD (**)@ [pI](**)))
Walther@60556
   214
      else NONE
Walther@60556
   215
    end
Walther@60559
   216
  | refin _ _ _ _ = raise ERROR "refin: uncovered fun def."
Walther@60575
   217
and refins _ _ _ [] = NONE
Walther@60575
   218
  | refins ctxt pblRD ori ((p as Store.Node _) :: pts) =
Walther@60729
   219
(writeln ("refins: " ^ strs2str pblRD);
Walther@60559
   220
    (case refin ctxt pblRD ori p of
Walther@60729
   221
      SOME pblRD' => SOME (pblRD')
Walther@60729
   222
    | NONE => refins ctxt pblRD ori pts)
Walther@60729
   223
);
Walther@60556
   224
Walther@60556
   225
\<^isac_test>\<open>
Walther@60729
   226
(* refine a problem; version providing output for math authors *)
Walther@60575
   227
(*val refin': Proof.context -> Problem.id -> Formalise.model -> M_Match.T list -> 
Walther@60556
   228
    Probl_Def.T Store.node -> M_Match.T list*)
Walther@60575
   229
fun refin' ctxt pblRD fmz pbls (Store.Node (pI, [py: Probl_Def.T], [])) =
Walther@60556
   230
    let
Walther@60556
   231
      val _ = (tracing o (curry op ^ "*** pass ") o strs2str) (pblRD @ [pI])
Walther@60585
   232
      val {thy, model, where_, where_rls, ...} = py 
Walther@60585
   233
      val model = map (Model_Pattern.adapt_to_type ctxt) model
Walther@60660
   234
      val where_ = map (ParseC.adapt_term_to_type ctxt) where_
Walther@60653
   235
      val (oris, _) = O_Model.init thy fmz model; (*WN020803: oris might NOT be complete here*)
Walther@60586
   236
      val (b, (itms, where_')) =
Walther@60585
   237
        M_Match.match_oris' (Proof_Context.theory_of ctxt) oris (model, where_, where_rls)
Walther@60653
   238
    in                                                  
Walther@60556
   239
      if b
Walther@60586
   240
      then pbls @ [M_Match.Matches (rev (pblRD @ [pI]), P_Model.from thy itms where_')]
Walther@60586
   241
      else pbls @ [M_Match.NoMatch (rev (pblRD @ [pI]), P_Model.from thy itms where_')]
Walther@60556
   242
    end
Walther@60575
   243
  | refin' ctxt pblRD fmz pbls (Store.Node (pI, [py], pys)) =
Walther@60556
   244
    let
Walther@60556
   245
      val _ = (tracing o ((curry op ^)"*** pass ") o strs2str) (pblRD @ [pI])
Walther@60585
   246
      val {thy, model, where_, where_rls, ...} = py 
Walther@60585
   247
      val model = map (Model_Pattern.adapt_to_type ctxt) model
Walther@60660
   248
      val where_ = map (ParseC.adapt_term_to_type ctxt) where_
Walther@60653
   249
      val (oris, _) = O_Model.init thy fmz model; (*WN020803: oris might NOT be complete here*)
Walther@60586
   250
      val (b, (itms, where_')) =
Walther@60585
   251
        M_Match.match_oris' (Proof_Context.theory_of ctxt) oris (model, where_, where_rls)
Walther@60556
   252
    in
Walther@60556
   253
      if b 
Walther@60556
   254
      then
Walther@60586
   255
        let val pbl = M_Match.Matches (rev (pblRD @ [pI]), P_Model.from thy itms where_')
Walther@60575
   256
	      in refins' ctxt (pblRD @ [pI]) fmz (pbls @ [pbl]) pys end
Walther@60586
   257
      else (pbls @ [M_Match.NoMatch (rev (pblRD @ [pI]), P_Model.from thy itms where_')])
Walther@60556
   258
    end
Walther@60575
   259
  | refin' _ _ _ _ _ = raise ERROR "refin': uncovered fun def."
Walther@60575
   260
and refins' _ _ _ pbls [] = pbls
Walther@60575
   261
  | refins' ctxt pblRD fmz pbls ((p as Store.Node _) :: pts) =
Walther@60556
   262
    let
Walther@60575
   263
      val pbls' = refin' ctxt pblRD fmz pbls p
Walther@60556
   264
    in
Walther@60556
   265
      case last_elem pbls' of
Walther@60556
   266
        M_Match.Matches _ => pbls'
Walther@60575
   267
      | M_Match.NoMatch _ => refins' ctxt pblRD fmz pbls' pts
Walther@60556
   268
    end;
walther@60268
   269
\<close>
walther@59968
   270
Walther@60556
   271
(*
Walther@60556
   272
  TODO: rename \<rightarrow> apply_to_node
Walther@60556
   273
  apply a fun to a ptyps node.
Walther@60556
   274
  val app_ptyp: (Probl_Def.T Store.node -> 'a) -> Store.key -> Store.key -> 'a
Walther@60729
   275
TODO: Store.apply scans Store.T only to the first hit; see Store.apply.
Walther@60556
   276
*)
Walther@60495
   277
fun app_ptyp x = Store.apply (get_pbls ()) x;
walther@59968
   278
Walther@60556
   279
(* TODO rename \<rightarrow> by_oris
Walther@60556
   280
   for tactic Refine_Tacitly
Walther@60556
   281
   oris are already created wrt. some pbt; ctxt overrides thy in pbt  *)
Walther@60559
   282
fun refine_ori ctxt oris pblID =
walther@59968
   283
  let
Walther@60559
   284
    val opt = app_ptyp (refin ctxt ((rev o tl) pblID) oris) pblID (rev pblID);
Walther@60556
   285
  in case opt of 
walther@59968
   286
      SOME pblRD =>
walther@59968
   287
        let val pblID': Problem.id = rev pblRD
walther@59968
   288
			  in if pblID' = pblID then NONE else SOME pblID' end
walther@59968
   289
	  | NONE => NONE
walther@59968
   290
	end;
Walther@60559
   291
fun refine_ori' ctxt oris pI = perhaps (refine_ori ctxt oris) pI;
walther@59968
   292
walther@60268
   293
\<^isac_test>\<open>
Walther@60575
   294
fun xxxxx ctxt fmz pblID =
Walther@60575
   295
  app_ptyp (refin' ctxt ((rev o tl) pblID) fmz []) pblID (rev pblID);
walther@60268
   296
\<close>
walther@59968
   297
walther@59965
   298
(**)end(**)