src/HOL/Eisbach/Examples.thy
changeset 60209 022ca2799c73
parent 60119 54bea620e54f
child 60248 f7e4294216d2
equal deleted inserted replaced
60208:d72795f401c3 60209:022ca2799c73
     9 begin
     9 begin
    10 
    10 
    11 
    11 
    12 subsection \<open>Basic methods\<close>
    12 subsection \<open>Basic methods\<close>
    13 
    13 
    14 method my_intros = \<open>rule conjI | rule impI\<close>
    14 method my_intros = (rule conjI | rule impI)
    15 
    15 
    16 lemma "P \<and> Q \<longrightarrow> Z \<and> X"
    16 lemma "P \<and> Q \<longrightarrow> Z \<and> X"
    17   apply my_intros+
    17   apply my_intros+
    18   oops
    18   oops
    19 
    19 
    20 method my_intros' uses intros = \<open>rule conjI | rule impI | rule intros\<close>
    20 method my_intros' uses intros = (rule conjI | rule impI | rule intros)
    21 
    21 
    22 lemma "P \<and> Q \<longrightarrow> Z \<or> X"
    22 lemma "P \<and> Q \<longrightarrow> Z \<or> X"
    23   apply (my_intros' intros: disjI1)+
    23   apply (my_intros' intros: disjI1)+
    24   oops
    24   oops
    25 
    25 
    26 method my_spec for x :: 'a = \<open>drule spec[where x="x"]\<close>
    26 method my_spec for x :: 'a = (drule spec[where x="x"])
    27 
    27 
    28 lemma "\<forall>x. P x \<Longrightarrow> P x"
    28 lemma "\<forall>x. P x \<Longrightarrow> P x"
    29   apply (my_spec x)
    29   apply (my_spec x)
    30   apply assumption
    30   apply assumption
    31   done
    31   done
    32 
    32 
    33 
    33 
    34 subsection \<open>Focusing and matching\<close>
    34 subsection \<open>Focusing and matching\<close>
    35 
    35 
    36 method match_test =
    36 method match_test =
    37   \<open>match prems in U: "P x \<and> Q x" for P Q x \<Rightarrow>
    37   (match premises in U: "P x \<and> Q x" for P Q x \<Rightarrow>
    38     \<open>print_term P,
    38     \<open>print_term P,
    39      print_term Q,
    39      print_term Q,
    40      print_term x,
    40      print_term x,
    41      print_fact U\<close>\<close>
    41      print_fact U\<close>)
    42 
    42 
    43 lemma "\<And>x. P x \<and> Q x \<Longrightarrow> A x \<and> B x \<Longrightarrow> R x y \<Longrightarrow> True"
    43 lemma "\<And>x. P x \<and> Q x \<Longrightarrow> A x \<and> B x \<Longrightarrow> R x y \<Longrightarrow> True"
    44   apply match_test  -- \<open>Valid match, but not quite what we were expecting..\<close>
    44   apply match_test  -- \<open>Valid match, but not quite what we were expecting..\<close>
    45   back  -- \<open>Can backtrack over matches, exploring all bindings\<close>
    45   back  -- \<open>Can backtrack over matches, exploring all bindings\<close>
    46   back
    46   back
    49   back
    49   back
    50   back  -- \<open>Found the other conjunction\<close>
    50   back  -- \<open>Found the other conjunction\<close>
    51   back
    51   back
    52   back
    52   back
    53   back
    53   back
    54   back
       
    55   back
       
    56   oops
    54   oops
    57 
    55 
    58 text \<open>Use matching to avoid "improper" methods\<close>
    56 text \<open>Use matching to avoid "improper" methods\<close>
    59 
    57 
    60 lemma focus_test:
    58 lemma focus_test:
    61   shows "\<And>x. \<forall>x. P x \<Longrightarrow> P x"
    59   shows "\<And>x. \<forall>x. P x \<Longrightarrow> P x"
    62   apply (my_spec "x :: 'a", assumption)?  -- \<open>Wrong x\<close>
    60   apply (my_spec "x :: 'a", assumption)?  -- \<open>Wrong x\<close>
    63   apply (match concl in "P x" for x \<Rightarrow> \<open>my_spec x, assumption\<close>)
    61   apply (match conclusion in "P x" for x \<Rightarrow> \<open>my_spec x, assumption\<close>)
    64   done
    62   done
    65 
    63 
    66 
    64 
    67 text \<open>Matches are exclusive. Backtracking will not occur past a match\<close>
    65 text \<open>Matches are exclusive. Backtracking will not occur past a match\<close>
    68 
    66 
    69 method match_test' =
    67 method match_test' =
    70   \<open>match concl in
    68   (match conclusion in
    71     "P \<and> Q" for P Q \<Rightarrow>
    69     "P \<and> Q" for P Q \<Rightarrow>
    72       \<open>print_term P, print_term Q, rule conjI[where P="P" and Q="Q"]; assumption\<close>
    70       \<open>print_term P, print_term Q, rule conjI[where P="P" and Q="Q"]; assumption\<close>
    73     \<bar> "H" for H \<Rightarrow> \<open>print_term H\<close>
    71     \<bar> "H" for H \<Rightarrow> \<open>print_term H\<close>
    74   \<close>
    72   )
    75 
    73 
    76 text \<open>Solves goal\<close>
    74 text \<open>Solves goal\<close>
    77 lemma "P \<Longrightarrow> Q \<Longrightarrow> P \<and> Q"
    75 lemma "P \<Longrightarrow> Q \<Longrightarrow> P \<and> Q"
    78   apply match_test'
    76   apply match_test'
    79   done
    77   done
    87   apply match_test'
    85   apply match_test'
    88   oops
    86   oops
    89 
    87 
    90 
    88 
    91 method my_spec_guess =
    89 method my_spec_guess =
    92   \<open>match concl in "P (x :: 'a)" for P x \<Rightarrow>
    90   (match conclusion in "P (x :: 'a)" for P x \<Rightarrow>
    93     \<open>drule spec[where x=x],
    91     \<open>drule spec[where x=x],
    94      print_term P,
    92      print_term P,
    95      print_term x\<close>\<close>
    93      print_term x\<close>)
    96 
    94 
    97 lemma "\<forall>x. P (x :: nat) \<Longrightarrow> Q (x :: nat)"
    95 lemma "\<forall>x. P (x :: nat) \<Longrightarrow> Q (x :: nat)"
    98   apply my_spec_guess
    96   apply my_spec_guess
    99   oops
    97   oops
   100 
    98 
   101 method my_spec_guess2 =
    99 method my_spec_guess2 =
   102   \<open>match prems in U[thin]:"\<forall>x. P x \<longrightarrow> Q x" and U':"P x" for P Q x \<Rightarrow>
   100   (match premises in U[thin]:"\<forall>x. P x \<longrightarrow> Q x" and U':"P x" for P Q x \<Rightarrow>
   103     \<open>insert spec[where x=x, OF U],
   101     \<open>insert spec[where x=x, OF U],
   104      print_term P,
   102      print_term P,
   105      print_term Q\<close>\<close>
   103      print_term Q\<close>)
   106 
   104 
   107 lemma "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> Q x \<Longrightarrow> Q x"
   105 lemma "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> Q x \<Longrightarrow> Q x"
   108   apply my_spec_guess2?  -- \<open>Fails. Note that both "P"s must match\<close>
   106   apply my_spec_guess2?  -- \<open>Fails. Note that both "P"s must match\<close>
   109   oops
   107   oops
   110 
   108 
   116 
   114 
   117 
   115 
   118 subsection \<open>Higher-order methods\<close>
   116 subsection \<open>Higher-order methods\<close>
   119 
   117 
   120 method higher_order_example for x methods meth =
   118 method higher_order_example for x methods meth =
   121   \<open>cases x, meth, meth\<close>
   119   (cases x, meth, meth)
   122 
   120 
   123 lemma
   121 lemma
   124   assumes A: "x = Some a"
   122   assumes A: "x = Some a"
   125   shows "the x = a"
   123   shows "the x = a"
   126   by (higher_order_example x \<open>simp add: A\<close>)
   124   by (higher_order_example x \<open>simp add: A\<close>)
   127 
   125 
   128 
   126 
   129 subsection \<open>Recursion\<close>
   127 subsection \<open>Recursion\<close>
   130 
   128 
   131 method recursion_example for x :: bool =
   129 method recursion_example for x :: bool =
   132   \<open>print_term x,
   130   (print_term x,
   133      match (x) in "A \<and> B" for A B \<Rightarrow>
   131      match (x) in "A \<and> B" for A B \<Rightarrow>
   134     \<open>(print_term A,
   132     \<open>print_term A,
   135      print_term B,
   133      print_term B,
   136      recursion_example A,
   134      recursion_example A,
   137      recursion_example B) | -\<close>\<close>
   135      recursion_example B | -\<close>)
   138 
   136 
   139 lemma "P"
   137 lemma "P"
   140   apply (recursion_example "(A \<and> D) \<and> (B \<and> C)")
   138   apply (recursion_example "(A \<and> D) \<and> (B \<and> C)")
   141   oops
   139   oops
   142 
   140 
   149   done
   147   done
   150 
   148 
   151 
   149 
   152 subsection \<open>Demo\<close>
   150 subsection \<open>Demo\<close>
   153 
   151 
   154 method solve methods m = \<open>m;fail\<close>
   152 method solve methods m = (m; fail)
   155 
   153 
   156 named_theorems intros and elims and subst
   154 named_theorems intros and elims and subst
   157 
   155 
   158 method prop_solver declares intros elims subst =
   156 method prop_solver declares intros elims subst =
   159   \<open>(assumption |
   157   (assumption |
   160     rule intros | erule elims |
   158     rule intros | erule elims |
   161     subst subst | subst (asm) subst |
   159     subst subst | subst (asm) subst |
   162     (erule notE; solve \<open>prop_solver\<close>))+\<close>
   160     (erule notE; solve \<open>prop_solver\<close>))+
   163 
   161 
   164 lemmas [intros] =
   162 lemmas [intros] =
   165   conjI
   163   conjI
   166   impI
   164   impI
   167   disjCI
   165   disjCI
   175 lemma "((A \<or> B) \<and> (A \<longrightarrow> C) \<and> (B \<longrightarrow> C)) \<longrightarrow> C"
   173 lemma "((A \<or> B) \<and> (A \<longrightarrow> C) \<and> (B \<longrightarrow> C)) \<longrightarrow> C"
   176   apply prop_solver
   174   apply prop_solver
   177   done
   175   done
   178 
   176 
   179 method guess_all =
   177 method guess_all =
   180   \<open>match prems in U[thin]:"\<forall>x. P (x :: 'a)" for P \<Rightarrow>
   178   (match premises in U[thin]:"\<forall>x. P (x :: 'a)" for P \<Rightarrow>
   181     \<open>match prems in "?H (y :: 'a)" for y \<Rightarrow>
   179     \<open>match premises in "?H (y :: 'a)" for y \<Rightarrow>
   182        \<open>rule allE[where P = P and x = y, OF U]\<close>
   180        \<open>rule allE[where P = P and x = y, OF U]\<close>
   183    | match concl in "?H (y :: 'a)" for y \<Rightarrow>
   181    | match conclusion in "?H (y :: 'a)" for y \<Rightarrow>
   184        \<open>rule allE[where P = P and x = y, OF U]\<close>\<close>\<close>
   182        \<open>rule allE[where P = P and x = y, OF U]\<close>\<close>)
   185 
   183 
   186 lemma "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> P y \<Longrightarrow> Q y"
   184 lemma "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> P y \<Longrightarrow> Q y"
   187   apply guess_all
   185   apply guess_all
   188   apply prop_solver
   186   apply prop_solver
   189   done
   187   done
   191 lemma "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow>  P z \<Longrightarrow> P y \<Longrightarrow> Q y"
   189 lemma "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow>  P z \<Longrightarrow> P y \<Longrightarrow> Q y"
   192   apply (solve \<open>guess_all, prop_solver\<close>)  -- \<open>Try it without solve\<close>
   190   apply (solve \<open>guess_all, prop_solver\<close>)  -- \<open>Try it without solve\<close>
   193   done
   191   done
   194 
   192 
   195 method guess_ex =
   193 method guess_ex =
   196   \<open>match concl in
   194   (match conclusion in
   197     "\<exists>x. P (x :: 'a)" for P \<Rightarrow>
   195     "\<exists>x. P (x :: 'a)" for P \<Rightarrow>
   198       \<open>match prems in "?H (x :: 'a)" for x \<Rightarrow>
   196       \<open>match premises in "?H (x :: 'a)" for x \<Rightarrow>
   199               \<open>rule exI[where x=x]\<close>\<close>\<close>
   197               \<open>rule exI[where x=x]\<close>\<close>)
   200 
   198 
   201 lemma "P x \<Longrightarrow> \<exists>x. P x"
   199 lemma "P x \<Longrightarrow> \<exists>x. P x"
   202   apply guess_ex
   200   apply guess_ex
   203   apply prop_solver
   201   apply prop_solver
   204   done
   202   done
   205 
   203 
   206 method fol_solver =
   204 method fol_solver =
   207   \<open>(guess_ex | guess_all | prop_solver) ; solve \<open>fol_solver\<close>\<close>
   205   ((guess_ex | guess_all | prop_solver) ; solve \<open>fol_solver\<close>)
   208 
   206 
   209 declare
   207 declare
   210   allI [intros]
   208   allI [intros]
   211   exE [elims]
   209   exE [elims]
   212   ex_simps [subst]
   210   ex_simps [subst]