src/Tools/isac/Interpret/error-pattern.sml
author wneuper <walther.neuper@jku.at>
Tue, 10 Aug 2021 11:01:18 +0200
changeset 60360 49680d595342
parent 60223 740ebee5948b
child 60477 4ac966aaa785
permissions -rw-r--r--
eliminate ThyC.to_ctxt, use Proof_Context.init_global inline
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@59909
    25
  val check_for: 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@59910
    30
  val check_for': term * term -> subst -> id * term -> Rule_Set.T -> id option
walther@59911
    31
  val fill_from_store: Subst.input option * (term * term) list -> term -> id -> thm ->
walther@59909
    32
    record list
walther@59911
    33
  val fill_form: Subst.input option * (term * term) list -> 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@59909
    57
fun check_for' (res, inf) subst (id, pat) rls =
neuper@42423
    58
  let 
walther@59909
    59
    val (res', _, _, rewritten) = Rewrite.rew_sub (ThyC.Isac()) 1 subst Rewrite_Ord.e_rew_ord
walther@59909
    60
      Rule_Set.empty false [] (HOLogic.Trueprop $ pat) res;
neuper@42423
    61
  in
walther@59848
    62
    if rewritten then 
neuper@42423
    63
      let
walther@59854
    64
        val norm_res = case Rewrite.rewrite_set_inst_ (ThyC.Isac()) false subst rls  res' of
walther@59909
    65
            NONE => res'
walther@59909
    66
          | SOME (norm_res, _) => norm_res
walther@59854
    67
        val norm_inf = case Rewrite.rewrite_set_inst_ (ThyC.Isac()) false subst rls inf of
walther@59909
    68
            NONE => inf
walther@59909
    69
          | SOME (norm_inf, _) => norm_inf
walther@59909
    70
      in
walther@59909
    71
        if norm_res = norm_inf then SOME id else NONE
neuper@42423
    72
      end
neuper@42423
    73
    else NONE
neuper@42423
    74
  end;
neuper@42423
    75
walther@59908
    76
(*
walther@59908
    77
  check if (agreed result, input formula) matches SOME error pattern modulo simplifier rls;
walther@59908
    78
  (prog, env) for retrieval of casual substitution for bdv in the pattern.
walther@59908
    79
*)
walther@59844
    80
fun check_for (res, inf) (prog, env) (errpats, rls) =
neuper@42428
    81
  let
walther@59911
    82
    val (_, subst) = Subst.for_bdv prog env
neuper@42428
    83
    fun scan (_, [], _) = NONE
walther@59909
    84
      | scan (id, T :: errpats, _) =
walther@59909
    85
          case check_for' (res, inf) subst (id, T) rls of
walther@59909
    86
            NONE => scan (id, errpats, [])
walther@59909
    87
          | SOME id => SOME id
neuper@42428
    88
    fun scans [] = NONE
neuper@42428
    89
      | scans (group :: groups) =
neuper@42428
    90
          case scan group of
neuper@42428
    91
            NONE => scans groups
walther@59909
    92
          | SOME id => SOME id
neuper@42428
    93
  in scans errpats end;
neuper@42428
    94
neuper@42433
    95
(* fill-in patterns an forms.
neuper@42433
    96
  returns thm required by "fun in_fillform *)
walther@59909
    97
fun fill_form (subs_opt, subst) (thm, form) id (fillpatID, pat, erpaID) =
neuper@42430
    98
  let
neuper@42430
    99
    val (form', _, _, rewritten) =
walther@59857
   100
      Rewrite.rew_sub (ThyC.Isac()) 1 subst Rewrite_Ord.e_rew_ord Rule_Set.empty false [] (HOLogic.Trueprop $ pat) form;
walther@59909
   101
  in (*the fillpat of the thm must be dedicated to id*)
walther@59909
   102
    if id = erpaID andalso rewritten then
walther@59848
   103
      SOME (fillpatID, HOLogic.mk_eq (form, form'), thm, subs_opt) 
wneuper@59264
   104
    else NONE
wneuper@59264
   105
  end
neuper@42430
   106
walther@59909
   107
fun fill_from_store subst form id thm =
wneuper@59264
   108
  let
wneuper@59264
   109
    val thmDeriv = Thm.get_name_hint thm
walther@59916
   110
    val (part, thyID) = Thy_Read.thy_containing_thm thmDeriv
walther@59876
   111
    val theID = [part, thyID, "Theorems", ThmC.cut_id thmDeriv]
walther@59970
   112
    val fillpats = case Thy_Read.from_store theID of
walther@59917
   113
      Thy_Write.Hthm {fillpats, ...} => fillpats
walther@59909
   114
    | _ => raise ERROR "fill_from_store: uncovered case of get_the"
walther@59909
   115
    val some = map (fill_form subst (thm, form) id) fillpats
wneuper@59264
   116
  in some |> filter is_some |> map the end
neuper@42430
   117
walther@59909
   118
fun find_fill_patterns (pt, pos as (p, _): Pos.pos') id =
neuper@42430
   119
  let 
wneuper@59276
   120
    val f_curr = Ctree.get_curr_formula (pt, pos);
wneuper@59276
   121
    val pp = Ctree.par_pblobj pt p
walther@60154
   122
    val (errpats, prog) = case MethodC.from_store (Ctree.get_obj Ctree.g_metID pt pp) of
wneuper@59416
   123
      {errpats, scr = Rule.Prog prog, ...} => (errpats, prog)
walther@59909
   124
    | _ => raise ERROR "find_fill_patterns: uncovered case of get_met"
walther@59807
   125
    val {env, ...} = Ctree.get_istate_LI pt pos |> Istate.the_pstate
walther@59911
   126
    val subst = Subst.for_bdv prog env
neuper@42430
   127
    val errpatthms = errpats
walther@59909
   128
      |> filter ((curry op = id) o (#1: Error_Pattern_Def.T -> Error_Pattern_Def.id))
walther@59909
   129
      |> map (#3: Error_Pattern_Def.T -> thm list)
neuper@42430
   130
      |> flat
walther@59909
   131
  in map (fill_from_store subst f_curr id) errpatthms |> flat end
neuper@37906
   132
walther@59909
   133
(*
walther@59909
   134
  Check input on a fill-pattern:
walther@59909
   135
  check if input is exactly equal to the rewrite from a rule
walther@59909
   136
  which is stored at the pos where the input is stored of "ok".
walther@59909
   137
*)
walther@59909
   138
fun filled_exactly (pt, pos as (p, _)) istr =
walther@60360
   139
  case TermC.parseNEW (ThyC.get_theory "Isac_Knowledge" |> Proof_Context.init_global) istr of
wneuper@59571
   140
    NONE => ("syntax error in '" ^ istr ^ "'", Tactic.Tac "")
neuper@42437
   141
  | SOME ifo => 
neuper@42437
   142
      let
walther@59757
   143
        val p' = Pos.lev_on p;
wneuper@59276
   144
        val tac = Ctree.get_obj Ctree.g_tac pt p';
neuper@42437
   145
      in 
walther@59921
   146
        case Solve_Step.check tac (pt, pos) of
walther@59920
   147
          Applicable.No msg => (msg, Tactic.Tac "")
walther@59920
   148
        | Applicable.Yes rew =>
neuper@42437
   149
            let
neuper@42437
   150
              val res = case rew of
wneuper@59571
   151
               Tactic.Rewrite_Inst' (_, _, _, _, _, _, _, (res, _)) => res
wneuper@59571
   152
              |Tactic.Rewrite' (_, _, _, _, _, _, (res, _)) => res
walther@59909
   153
              | t => raise ERROR ("filled_exactly: uncovered case for " ^ Tactic.string_of t)
neuper@42437
   154
            in 
walther@59848
   155
              if not (ifo = res) then
walther@59848
   156
                ("input does not exactly fill the gaps", Tactic.Tac "")
neuper@42437
   157
              else ("ok", tac)
neuper@42437
   158
            end
neuper@42437
   159
      end
neuper@42437
   160
neuper@42458
   161
(* fetch errpatIDs from an arbitrary tactic *)
walther@59909
   162
fun from_store tac =
neuper@42458
   163
  let
neuper@42458
   164
    val rlsID =
neuper@42458
   165
      case tac of
wneuper@59571
   166
       Tactic.Rewrite_Set rlsID => rlsID
wneuper@59571
   167
      |Tactic.Rewrite_Set_Inst (_, rlsID) => rlsID
walther@59852
   168
      | _ => "empty"
walther@59916
   169
    val (part, thyID) = Thy_Read.thy_containing_rls "Isac_Knowledge" rlsID;
walther@59970
   170
    val rls = case Thy_Read.from_store [part, thyID, "Rulesets", rlsID] of
walther@59917
   171
      Thy_Write.Hrls {thy_rls = (_, rls), ...} => rls
walther@59909
   172
    | _ => raise ERROR "from_store: uncovered case of get_the"
neuper@42458
   173
  in case rls of
walther@59851
   174
    Rule_Def.Repeat {errpatts, ...} => errpatts
walther@59878
   175
  | Rule_Set.Sequence {errpatts, ...} => errpatts
walther@59850
   176
  | Rule_Set.Rrls {errpatts, ...} => errpatts
walther@59851
   177
  | Rule_Set.Empty => []
neuper@42458
   178
  end
wneuper@59264
   179
walther@59858
   180
(**)end(**)