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