src/HOL/Tools/inductive_set_package.ML
changeset 23849 2a0e24c74593
parent 23764 15f81c5d5330
child 24039 273698405054
equal deleted inserted replaced
23848:ca73e86c22bb 23849:2a0e24c74593
    63 (* simplifies (%x y. (x, y) : S & P x y) to (%x y. (x, y) : S Int {(x, y). P x y}) *)
    63 (* simplifies (%x y. (x, y) : S & P x y) to (%x y. (x, y) : S Int {(x, y). P x y}) *)
    64 (* and        (%x y. (x, y) : S | P x y) to (%x y. (x, y) : S Un {(x, y). P x y})  *)
    64 (* and        (%x y. (x, y) : S | P x y) to (%x y. (x, y) : S Un {(x, y). P x y})  *)
    65 (* used for converting "strong" (co)induction rules                                *)
    65 (* used for converting "strong" (co)induction rules                                *)
    66 (***********************************************************************************)
    66 (***********************************************************************************)
    67 
    67 
    68 val strong_ind_simproc =
    68 val anyt = Free ("t", TFree ("'t", []));
    69   Simplifier.simproc HOL.thy "strong_ind" ["t"] (fn thy => fn ss => fn t =>
    69 
       
    70 fun strong_ind_simproc tab =
       
    71   Simplifier.simproc_i HOL.thy "strong_ind" [anyt] (fn thy => fn ss => fn t =>
    70     let
    72     let
    71       val xs = strip_abs_vars t;
    73       fun close p t f =
    72       fun close t = fold (fn x => fn u => all (fastype_of x) $ lambda x u)
    74         let val vs = Term.add_vars t []
    73         (term_vars t) t;
    75         in Drule.instantiate' [] (rev (map (SOME o cterm_of thy o Var) vs))
       
    76           (p (fold (fn x as (_, T) => fn u => all T $ lambda (Var x) u) vs t) f)
       
    77         end;
    74       fun mkop "op &" T x = SOME (Const ("op Int", T --> T --> T), x)
    78       fun mkop "op &" T x = SOME (Const ("op Int", T --> T --> T), x)
    75         | mkop "op |" T x = SOME (Const ("op Un", T --> T --> T), x)
    79         | mkop "op |" T x = SOME (Const ("op Un", T --> T --> T), x)
    76         | mkop _ _ _ = NONE;
    80         | mkop _ _ _ = NONE;
    77       fun mk_collect p T t =
    81       fun mk_collect p T t =
    78         let val U = HOLogic.dest_setT T
    82         let val U = HOLogic.dest_setT T
    86             Type (_, [_, Type (_, [T, _])]))) $ p $ S)) =
    90             Type (_, [_, Type (_, [T, _])]))) $ p $ S)) =
    87               mkop s T (m, p, mk_collect p T (head_of u), S)
    91               mkop s T (m, p, mk_collect p T (head_of u), S)
    88         | decomp _ = NONE;
    92         | decomp _ = NONE;
    89       val simp = full_simp_tac (Simplifier.inherit_context ss
    93       val simp = full_simp_tac (Simplifier.inherit_context ss
    90         (HOL_basic_ss addsimps [mem_Collect_eq, split_conv])) 1;
    94         (HOL_basic_ss addsimps [mem_Collect_eq, split_conv])) 1;
       
    95       fun mk_rew t = (case strip_abs_vars t of
       
    96           [] => NONE
       
    97         | xs => (case decomp (strip_abs_body t) of
       
    98             NONE => NONE
       
    99           | SOME (bop, (m, p, S, S')) =>
       
   100               SOME (close (Goal.prove (Simplifier.the_context ss) [] [])
       
   101                 (Logic.mk_equals (t, list_abs (xs, m $ p $ (bop $ S $ S'))))
       
   102                 (K (EVERY
       
   103                   [rtac eq_reflection 1, REPEAT (rtac ext 1), rtac iffI 1,
       
   104                    EVERY [etac conjE 1, rtac IntI 1, simp, simp,
       
   105                      etac IntE 1, rtac conjI 1, simp, simp] ORELSE
       
   106                    EVERY [etac disjE 1, rtac UnI1 1, simp, rtac UnI2 1, simp,
       
   107                      etac UnE 1, rtac disjI1 1, simp, rtac disjI2 1, simp]])))
       
   108                 handle ERROR _ => NONE))
    91     in
   109     in
    92       if null xs then NONE
   110       case strip_comb t of
    93       else case decomp (strip_abs_body t) of
   111         (h as Const (name, _), ts) => (case Symtab.lookup tab name of
    94         NONE => NONE
   112           SOME _ =>
    95       | SOME (bop, (m, p, S, S')) =>
   113             let val rews = map mk_rew ts
    96           SOME (mk_meta_eq (Goal.prove (Simplifier.the_context ss) [] []
   114             in
    97             (close (HOLogic.mk_Trueprop (HOLogic.mk_eq
   115               if forall is_none rews then NONE
    98               (t, list_abs (xs, m $ p $ (bop $ S $ S'))))))
   116               else SOME (fold (fn th1 => fn th2 => combination th2 th1)
    99             (K (EVERY
   117                 (map2 (fn SOME r => K r | NONE => reflexive o cterm_of thy)
   100               [REPEAT (rtac ext 1), rtac iffI 1,
   118                    rews ts) (reflexive (cterm_of thy h)))
   101                EVERY [etac conjE 1, rtac IntI 1, simp, simp,
   119             end
   102                  etac IntE 1, rtac conjI 1, simp, simp] ORELSE
   120         | NONE => NONE)
   103                EVERY [etac disjE 1, rtac UnI1 1, simp, rtac UnI2 1, simp,
   121       | _ => NONE
   104                  etac UnE 1, rtac disjI1 1, simp, rtac disjI2 1, simp]]))))
       
   105             handle ERROR _ => NONE
       
   106     end);
   122     end);
   107 
   123 
   108 (* only eta contract terms occurring as arguments of functions satisfying p *)
   124 (* only eta contract terms occurring as arguments of functions satisfying p *)
   109 fun eta_contract p =
   125 fun eta_contract p =
   110   let
   126   let
   302     SOME (SOME _) => true | _ => false;
   318     SOME (SOME _) => true | _ => false;
   303 
   319 
   304 fun to_pred_simproc rules =
   320 fun to_pred_simproc rules =
   305   let val rules' = map mk_meta_eq rules
   321   let val rules' = map mk_meta_eq rules
   306   in
   322   in
   307     Simplifier.simproc HOL.thy "to_pred" ["t"]
   323     Simplifier.simproc_i HOL.thy "to_pred" [anyt]
   308       (fn thy => K (lookup_rule thy (prop_of #> Logic.dest_equals) rules'))
   324       (fn thy => K (lookup_rule thy (prop_of #> Logic.dest_equals) rules'))
   309   end;
   325   end;
   310 
   326 
   311 fun to_pred_proc thy rules t = case lookup_rule thy I rules t of
   327 fun to_pred_proc thy rules t = case lookup_rule thy I rules t of
   312     NONE => NONE
   328     NONE => NONE
   354          cterm_of thy (list_abs (map (pair "x") Ts, HOLogic.mk_mem
   370          cterm_of thy (list_abs (map (pair "x") Ts, HOLogic.mk_mem
   355            (HOLogic.mk_tuple' ps T (map Bound (length ps downto 0)), x'))))
   371            (HOLogic.mk_tuple' ps T (map Bound (length ps downto 0)), x'))))
   356       end) fs
   372       end) fs
   357   in
   373   in
   358     Simplifier.full_simplify (HOL_basic_ss addsimps to_set_simps
   374     Simplifier.full_simplify (HOL_basic_ss addsimps to_set_simps
   359         addsimprocs [strong_ind_simproc])
   375         addsimprocs [strong_ind_simproc pred_arities])
   360       (Thm.instantiate ([], insts) thm)
   376       (Thm.instantiate ([], insts) thm)
   361   end;
   377   end;
   362 
   378 
   363 val to_set_att = Thm.rule_attribute o to_set;
   379 val to_set_att = Thm.rule_attribute o to_set;
   364 
   380