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