src/Tools/isac/Interpret/error-pattern.sml
author wneuper <Walther.Neuper@jku.at>
Sat, 06 Aug 2022 17:36:59 +0200
changeset 60523 8e4fe2fb6590
parent 60509 2e0b7ca391dc
child 60524 1fef82aa491d
permissions -rw-r--r--
push Proof.context through Error_Pattern.check_for
walther@59909
     1
(* title: Interpret/error-pattern.sml
neuper@37906
     2
   author: Walther Neuper
neuper@37906
     3
   0603
neuper@37906
     4
   (c) due to copyright terms
walther@59858
     5
walther@59906
     6
In case a term is input, which contains an "error pattern" (provided by a mathauthor),
walther@59906
     7
several "fill patterns" can be presented as help for the next step.)
neuper@37906
     8
*)
neuper@37906
     9
walther@59909
    10
signature ERROR_PATTERN =
wneuper@59262
    11
sig
walther@59909
    12
  type id = Error_Pattern_Def.id
walther@59909
    13
  type T = Error_Pattern_Def.T
walther@59909
    14
  val s_to_string : T list -> string
walther@59971
    15
  val empty: T
walther@59858
    16
walther@59909
    17
  type fill_in_id = Error_Pattern_Def.fill_in_id
walther@59909
    18
  type fill_in = Error_Pattern_Def.fill_in
walther@59971
    19
  val fill_in_empty: fill_in
walther@59971
    20
walther@59909
    21
  type record
walther@59909
    22
walther@59909
    23
  val from_store: Tactic.input -> Error_Pattern_Def.id list
walther@59909
    24
  val filled_exactly: Calc.T -> string -> string * Tactic.input
Walther@60523
    25
  val check_for: Proof.context -> term * term -> term * Env.T -> T list * Rule_Set.T -> id option
walther@59906
    26
walther@59909
    27
  val find_fill_patterns: Calc.T -> id -> record list
wenzelm@60223
    28
wenzelm@60223
    29
\<^isac_test>\<open>
Walther@60523
    30
  val check_for': Proof.context -> term * term -> subst -> id * term -> Rule_Set.T -> id option
Walther@60477
    31
  val fill_from_store: Subst.input option * Subst.T -> term -> id -> thm ->
walther@59909
    32
    record list
Walther@60477
    33
  val fill_form: Subst.input option * Subst.T -> thm * term -> id -> fill_in ->
walther@59909
    34
    record option
wenzelm@60223
    35
\<close>
walther@59909
    36
end
wneuper@59310
    37
walther@59822
    38
(**)
walther@59909
    39
structure Error_Pattern(**): ERROR_PATTERN(**) =
walther@59822
    40
struct
walther@59822
    41
(**)
neuper@37906
    42
walther@59909
    43
type id = Error_Pattern_Def.id;
walther@59909
    44
type T = Error_Pattern_Def.T;
walther@59909
    45
val s_to_string = Error_Pattern_Def.s_to_string;
walther@59971
    46
val empty = ("e_errpatID", [TermC.parse_patt @{theory} "?a = ?b"], [@{thm refl}])
walther@59858
    47
walther@59909
    48
type fill_in_id = Error_Pattern_Def.fill_in_id;
walther@59909
    49
type fill_in = Error_Pattern_Def.fill_in;
walther@59971
    50
val fill_in_empty = ("e_fillpatID", TermC.parse_patt @{theory} "?a = _", "e_errpatID")
walther@59971
    51
walther@59911
    52
type record = (fill_in_id * term * thm * Subst.input option);
walther@59822
    53
walther@59909
    54
(*
walther@59909
    55
  check if (agreed result, input formula) matches the error pattern "pat" modulo simplifier rls
walther@59909
    56
*)
Walther@60523
    57
fun check_for' ctxt (res, inf) subst (id, pat) rls =
Walther@60500
    58
  let
Walther@60500
    59
    val (res', _, _, rewritten) =
Walther@60509
    60
      Rewrite.rew_sub ctxt 1 subst Rewrite_Ord.function_empty
Walther@60500
    61
        Rule_Set.empty false [] (HOLogic.Trueprop $ pat) res;
neuper@42423
    62
  in
walther@59848
    63
    if rewritten then 
neuper@42423
    64
      let
Walther@60500
    65
        val norm_res = case Rewrite.rewrite_set_inst_ ctxt false subst rls  res' of
walther@59909
    66
            NONE => res'
walther@59909
    67
          | SOME (norm_res, _) => norm_res
Walther@60500
    68
        val norm_inf = case Rewrite.rewrite_set_inst_ ctxt false subst rls inf of
walther@59909
    69
            NONE => inf
walther@59909
    70
          | SOME (norm_inf, _) => norm_inf
walther@59909
    71
      in
walther@59909
    72
        if norm_res = norm_inf then SOME id else NONE
neuper@42423
    73
      end
neuper@42423
    74
    else NONE
neuper@42423
    75
  end;
neuper@42423
    76
walther@59908
    77
(*
walther@59908
    78
  check if (agreed result, input formula) matches SOME error pattern modulo simplifier rls;
walther@59908
    79
  (prog, env) for retrieval of casual substitution for bdv in the pattern.
walther@59908
    80
*)
Walther@60523
    81
fun check_for ctxt (res, inf) (prog, env) (errpats, rls) =
neuper@42428
    82
  let
walther@59911
    83
    val (_, subst) = Subst.for_bdv prog env
neuper@42428
    84
    fun scan (_, [], _) = NONE
walther@59909
    85
      | scan (id, T :: errpats, _) =
Walther@60523
    86
          case check_for' ctxt (res, inf) subst (id, T) rls of
walther@59909
    87
            NONE => scan (id, errpats, [])
walther@59909
    88
          | SOME id => SOME id
neuper@42428
    89
    fun scans [] = NONE
neuper@42428
    90
      | scans (group :: groups) =
neuper@42428
    91
          case scan group of
neuper@42428
    92
            NONE => scans groups
walther@59909
    93
          | SOME id => SOME id
neuper@42428
    94
  in scans errpats end;
neuper@42428
    95
neuper@42433
    96
(* fill-in patterns an forms.
neuper@42433
    97
  returns thm required by "fun in_fillform *)
walther@59909
    98
fun fill_form (subs_opt, subst) (thm, form) id (fillpatID, pat, erpaID) =
neuper@42430
    99
  let
Walther@60500
   100
    val ctxt = Proof_Context.init_global ((ThyC.Isac()))
neuper@42430
   101
    val (form', _, _, rewritten) =
Walther@60509
   102
      Rewrite.rew_sub ctxt 1 subst Rewrite_Ord.function_empty Rule_Set.empty false [] (HOLogic.Trueprop $ pat) form;
walther@59909
   103
  in (*the fillpat of the thm must be dedicated to id*)
walther@59909
   104
    if id = erpaID andalso rewritten then
walther@59848
   105
      SOME (fillpatID, HOLogic.mk_eq (form, form'), thm, subs_opt) 
wneuper@59264
   106
    else NONE
wneuper@59264
   107
  end
neuper@42430
   108
walther@59909
   109
fun fill_from_store subst form id thm =
wneuper@59264
   110
  let
wneuper@59264
   111
    val thmDeriv = Thm.get_name_hint thm
walther@59916
   112
    val (part, thyID) = Thy_Read.thy_containing_thm thmDeriv
walther@59876
   113
    val theID = [part, thyID, "Theorems", ThmC.cut_id thmDeriv]
walther@59970
   114
    val fillpats = case Thy_Read.from_store theID of
walther@59917
   115
      Thy_Write.Hthm {fillpats, ...} => fillpats
walther@59909
   116
    | _ => raise ERROR "fill_from_store: uncovered case of get_the"
walther@59909
   117
    val some = map (fill_form subst (thm, form) id) fillpats
wneuper@59264
   118
  in some |> filter is_some |> map the end
neuper@42430
   119
walther@59909
   120
fun find_fill_patterns (pt, pos as (p, _): Pos.pos') id =
neuper@42430
   121
  let 
wneuper@59276
   122
    val f_curr = Ctree.get_curr_formula (pt, pos);
wneuper@59276
   123
    val pp = Ctree.par_pblobj pt p
walther@60154
   124
    val (errpats, prog) = case MethodC.from_store (Ctree.get_obj Ctree.g_metID pt pp) of
wneuper@59416
   125
      {errpats, scr = Rule.Prog prog, ...} => (errpats, prog)
walther@59909
   126
    | _ => raise ERROR "find_fill_patterns: uncovered case of get_met"
walther@59807
   127
    val {env, ...} = Ctree.get_istate_LI pt pos |> Istate.the_pstate
walther@59911
   128
    val subst = Subst.for_bdv prog env
neuper@42430
   129
    val errpatthms = errpats
walther@59909
   130
      |> filter ((curry op = id) o (#1: Error_Pattern_Def.T -> Error_Pattern_Def.id))
walther@59909
   131
      |> map (#3: Error_Pattern_Def.T -> thm list)
neuper@42430
   132
      |> flat
walther@59909
   133
  in map (fill_from_store subst f_curr id) errpatthms |> flat end
neuper@37906
   134
walther@59909
   135
(*
walther@59909
   136
  Check input on a fill-pattern:
walther@59909
   137
  check if input is exactly equal to the rewrite from a rule
walther@59909
   138
  which is stored at the pos where the input is stored of "ok".
walther@59909
   139
*)
walther@59909
   140
fun filled_exactly (pt, pos as (p, _)) istr =
walther@60360
   141
  case TermC.parseNEW (ThyC.get_theory "Isac_Knowledge" |> Proof_Context.init_global) istr of
wneuper@59571
   142
    NONE => ("syntax error in '" ^ istr ^ "'", Tactic.Tac "")
neuper@42437
   143
  | SOME ifo => 
neuper@42437
   144
      let
walther@59757
   145
        val p' = Pos.lev_on p;
wneuper@59276
   146
        val tac = Ctree.get_obj Ctree.g_tac pt p';
neuper@42437
   147
      in 
walther@59921
   148
        case Solve_Step.check tac (pt, pos) of
walther@59920
   149
          Applicable.No msg => (msg, Tactic.Tac "")
walther@59920
   150
        | Applicable.Yes rew =>
neuper@42437
   151
            let
neuper@42437
   152
              val res = case rew of
wneuper@59571
   153
               Tactic.Rewrite_Inst' (_, _, _, _, _, _, _, (res, _)) => res
wneuper@59571
   154
              |Tactic.Rewrite' (_, _, _, _, _, _, (res, _)) => res
walther@59909
   155
              | t => raise ERROR ("filled_exactly: uncovered case for " ^ Tactic.string_of t)
neuper@42437
   156
            in 
walther@59848
   157
              if not (ifo = res) then
walther@59848
   158
                ("input does not exactly fill the gaps", Tactic.Tac "")
neuper@42437
   159
              else ("ok", tac)
neuper@42437
   160
            end
neuper@42437
   161
      end
neuper@42437
   162
neuper@42458
   163
(* fetch errpatIDs from an arbitrary tactic *)
walther@59909
   164
fun from_store tac =
neuper@42458
   165
  let
neuper@42458
   166
    val rlsID =
neuper@42458
   167
      case tac of
wneuper@59571
   168
       Tactic.Rewrite_Set rlsID => rlsID
wneuper@59571
   169
      |Tactic.Rewrite_Set_Inst (_, rlsID) => rlsID
walther@59852
   170
      | _ => "empty"
walther@59916
   171
    val (part, thyID) = Thy_Read.thy_containing_rls "Isac_Knowledge" rlsID;
walther@59970
   172
    val rls = case Thy_Read.from_store [part, thyID, "Rulesets", rlsID] of
walther@59917
   173
      Thy_Write.Hrls {thy_rls = (_, rls), ...} => rls
walther@59909
   174
    | _ => raise ERROR "from_store: uncovered case of get_the"
neuper@42458
   175
  in case rls of
walther@59851
   176
    Rule_Def.Repeat {errpatts, ...} => errpatts
walther@59878
   177
  | Rule_Set.Sequence {errpatts, ...} => errpatts
walther@59850
   178
  | Rule_Set.Rrls {errpatts, ...} => errpatts
walther@59851
   179
  | Rule_Set.Empty => []
neuper@42458
   180
  end
wneuper@59264
   181
walther@59858
   182
(**)end(**)