src/Tools/isac/Interpret/error-pattern.sml
author Walther Neuper <walther.neuper@jku.at>
Wed, 29 Apr 2020 12:30:51 +0200
changeset 59920 33913fe24685
parent 59917 e98d714cca1a
child 59921 0766dade4a78
permissions -rw-r--r--
prep. separation of check Applicable between specify-phase and solve-phase
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@59858
    15
walther@59909
    16
  type fill_in_id = Error_Pattern_Def.fill_in_id
walther@59909
    17
  type fill_in = Error_Pattern_Def.fill_in
walther@59909
    18
  type record
walther@59909
    19
walther@59909
    20
  val from_store: Tactic.input -> Error_Pattern_Def.id list
walther@59909
    21
  val filled_exactly: Calc.T -> string -> string * Tactic.input
walther@59909
    22
  val check_for: term * term -> term * Env.T -> T list * Rule_Set.T -> id option
walther@59906
    23
wneuper@59310
    24
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59909
    25
  val find_fill_patterns: Calc.T -> id -> record list
walther@59917
    26
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59910
    27
  val check_for': term * term -> subst -> id * term -> Rule_Set.T -> id option
walther@59911
    28
  val fill_from_store: Subst.input option * (term * term) list -> term -> id -> thm ->
walther@59909
    29
    record list
walther@59911
    30
  val fill_form: Subst.input option * (term * term) list -> thm * term -> id -> fill_in ->
walther@59909
    31
    record option
walther@59917
    32
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59909
    33
end
wneuper@59310
    34
walther@59822
    35
(**)
walther@59909
    36
structure Error_Pattern(**): ERROR_PATTERN(**) =
walther@59822
    37
struct
walther@59822
    38
(**)
neuper@37906
    39
walther@59909
    40
type id = Error_Pattern_Def.id;
walther@59909
    41
type T = Error_Pattern_Def.T;
walther@59909
    42
val s_to_string = Error_Pattern_Def.s_to_string;
walther@59858
    43
walther@59909
    44
type fill_in_id = Error_Pattern_Def.fill_in_id;
walther@59909
    45
type fill_in = Error_Pattern_Def.fill_in;
walther@59911
    46
type record = (fill_in_id * term * thm * Subst.input option);
walther@59822
    47
walther@59909
    48
(*
walther@59909
    49
  check if (agreed result, input formula) matches the error pattern "pat" modulo simplifier rls
walther@59909
    50
*)
walther@59909
    51
fun check_for' (res, inf) subst (id, pat) rls =
neuper@42423
    52
  let 
walther@59909
    53
    val (res', _, _, rewritten) = Rewrite.rew_sub (ThyC.Isac()) 1 subst Rewrite_Ord.e_rew_ord
walther@59909
    54
      Rule_Set.empty false [] (HOLogic.Trueprop $ pat) res;
neuper@42423
    55
  in
walther@59848
    56
    if rewritten then 
neuper@42423
    57
      let
walther@59854
    58
        val norm_res = case Rewrite.rewrite_set_inst_ (ThyC.Isac()) false subst rls  res' of
walther@59909
    59
            NONE => res'
walther@59909
    60
          | SOME (norm_res, _) => norm_res
walther@59854
    61
        val norm_inf = case Rewrite.rewrite_set_inst_ (ThyC.Isac()) false subst rls inf of
walther@59909
    62
            NONE => inf
walther@59909
    63
          | SOME (norm_inf, _) => norm_inf
walther@59909
    64
      in
walther@59909
    65
        if norm_res = norm_inf then SOME id else NONE
neuper@42423
    66
      end
neuper@42423
    67
    else NONE
neuper@42423
    68
  end;
neuper@42423
    69
walther@59908
    70
(*
walther@59908
    71
  check if (agreed result, input formula) matches SOME error pattern modulo simplifier rls;
walther@59908
    72
  (prog, env) for retrieval of casual substitution for bdv in the pattern.
walther@59908
    73
*)
walther@59844
    74
fun check_for (res, inf) (prog, env) (errpats, rls) =
neuper@42428
    75
  let
walther@59911
    76
    val (_, subst) = Subst.for_bdv prog env
neuper@42428
    77
    fun scan (_, [], _) = NONE
walther@59909
    78
      | scan (id, T :: errpats, _) =
walther@59909
    79
          case check_for' (res, inf) subst (id, T) rls of
walther@59909
    80
            NONE => scan (id, errpats, [])
walther@59909
    81
          | SOME id => SOME id
neuper@42428
    82
    fun scans [] = NONE
neuper@42428
    83
      | scans (group :: groups) =
neuper@42428
    84
          case scan group of
neuper@42428
    85
            NONE => scans groups
walther@59909
    86
          | SOME id => SOME id
neuper@42428
    87
  in scans errpats end;
neuper@42428
    88
neuper@42433
    89
(* fill-in patterns an forms.
neuper@42433
    90
  returns thm required by "fun in_fillform *)
walther@59909
    91
fun fill_form (subs_opt, subst) (thm, form) id (fillpatID, pat, erpaID) =
neuper@42430
    92
  let
neuper@42430
    93
    val (form', _, _, rewritten) =
walther@59857
    94
      Rewrite.rew_sub (ThyC.Isac()) 1 subst Rewrite_Ord.e_rew_ord Rule_Set.empty false [] (HOLogic.Trueprop $ pat) form;
walther@59909
    95
  in (*the fillpat of the thm must be dedicated to id*)
walther@59909
    96
    if id = erpaID andalso rewritten then
walther@59848
    97
      SOME (fillpatID, HOLogic.mk_eq (form, form'), thm, subs_opt) 
wneuper@59264
    98
    else NONE
wneuper@59264
    99
  end
neuper@42430
   100
walther@59909
   101
fun fill_from_store subst form id thm =
wneuper@59264
   102
  let
wneuper@59264
   103
    val thmDeriv = Thm.get_name_hint thm
walther@59916
   104
    val (part, thyID) = Thy_Read.thy_containing_thm thmDeriv
walther@59876
   105
    val theID = [part, thyID, "Theorems", ThmC.cut_id thmDeriv]
wneuper@59269
   106
    val fillpats = case Specify.get_the theID of
walther@59917
   107
      Thy_Write.Hthm {fillpats, ...} => fillpats
walther@59909
   108
    | _ => raise ERROR "fill_from_store: uncovered case of get_the"
walther@59909
   109
    val some = map (fill_form subst (thm, form) id) fillpats
wneuper@59264
   110
  in some |> filter is_some |> map the end
neuper@42430
   111
walther@59909
   112
fun find_fill_patterns (pt, pos as (p, _): Pos.pos') id =
neuper@42430
   113
  let 
wneuper@59276
   114
    val f_curr = Ctree.get_curr_formula (pt, pos);
wneuper@59276
   115
    val pp = Ctree.par_pblobj pt p
wneuper@59276
   116
    val (errpats, prog) = case Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp) of
wneuper@59416
   117
      {errpats, scr = Rule.Prog prog, ...} => (errpats, prog)
walther@59909
   118
    | _ => raise ERROR "find_fill_patterns: uncovered case of get_met"
walther@59807
   119
    val {env, ...} = Ctree.get_istate_LI pt pos |> Istate.the_pstate
walther@59911
   120
    val subst = Subst.for_bdv prog env
neuper@42430
   121
    val errpatthms = errpats
walther@59909
   122
      |> filter ((curry op = id) o (#1: Error_Pattern_Def.T -> Error_Pattern_Def.id))
walther@59909
   123
      |> map (#3: Error_Pattern_Def.T -> thm list)
neuper@42430
   124
      |> flat
walther@59909
   125
  in map (fill_from_store subst f_curr id) errpatthms |> flat end
neuper@37906
   126
walther@59909
   127
(*
walther@59909
   128
  Check input on a fill-pattern:
walther@59909
   129
  check if input is exactly equal to the rewrite from a rule
walther@59909
   130
  which is stored at the pos where the input is stored of "ok".
walther@59909
   131
*)
walther@59909
   132
fun filled_exactly (pt, pos as (p, _)) istr =
walther@59881
   133
  case TermC.parseNEW (ThyC.get_theory "Isac_Knowledge" |> ThyC.to_ctxt) istr of
wneuper@59571
   134
    NONE => ("syntax error in '" ^ istr ^ "'", Tactic.Tac "")
neuper@42437
   135
  | SOME ifo => 
neuper@42437
   136
      let
walther@59757
   137
        val p' = Pos.lev_on p;
wneuper@59276
   138
        val tac = Ctree.get_obj Ctree.g_tac pt p';
neuper@42437
   139
      in 
walther@59920
   140
        case ApplicableOLD.applicable_in pos pt tac of
walther@59920
   141
          Applicable.No msg => (msg, Tactic.Tac "")
walther@59920
   142
        | Applicable.Yes rew =>
neuper@42437
   143
            let
neuper@42437
   144
              val res = case rew of
wneuper@59571
   145
               Tactic.Rewrite_Inst' (_, _, _, _, _, _, _, (res, _)) => res
wneuper@59571
   146
              |Tactic.Rewrite' (_, _, _, _, _, _, (res, _)) => res
walther@59909
   147
              | t => raise ERROR ("filled_exactly: uncovered case for " ^ Tactic.string_of t)
neuper@42437
   148
            in 
walther@59848
   149
              if not (ifo = res) then
walther@59848
   150
                ("input does not exactly fill the gaps", Tactic.Tac "")
neuper@42437
   151
              else ("ok", tac)
neuper@42437
   152
            end
neuper@42437
   153
      end
neuper@42437
   154
neuper@42458
   155
(* fetch errpatIDs from an arbitrary tactic *)
walther@59909
   156
fun from_store tac =
neuper@42458
   157
  let
neuper@42458
   158
    val rlsID =
neuper@42458
   159
      case tac of
wneuper@59571
   160
       Tactic.Rewrite_Set rlsID => rlsID
wneuper@59571
   161
      |Tactic.Rewrite_Set_Inst (_, rlsID) => rlsID
walther@59852
   162
      | _ => "empty"
walther@59916
   163
    val (part, thyID) = Thy_Read.thy_containing_rls "Isac_Knowledge" rlsID;
wneuper@59269
   164
    val rls = case Specify.get_the [part, thyID, "Rulesets", rlsID] of
walther@59917
   165
      Thy_Write.Hrls {thy_rls = (_, rls), ...} => rls
walther@59909
   166
    | _ => raise ERROR "from_store: uncovered case of get_the"
neuper@42458
   167
  in case rls of
walther@59851
   168
    Rule_Def.Repeat {errpatts, ...} => errpatts
walther@59878
   169
  | Rule_Set.Sequence {errpatts, ...} => errpatts
walther@59850
   170
  | Rule_Set.Rrls {errpatts, ...} => errpatts
walther@59851
   171
  | Rule_Set.Empty => []
neuper@42458
   172
  end
wneuper@59264
   173
walther@59858
   174
(**)end(**)