src/HOL/Tools/Function/function_lib.ML
changeset 33099 b8cdd3d73022
child 33611 168b928d5024
equal deleted inserted replaced
33098:3e9ae9032273 33099:b8cdd3d73022
       
     1 (*  Title:      HOL/Tools/Function/fundef_lib.ML
       
     2     Author:     Alexander Krauss, TU Muenchen
       
     3 
       
     4 A package for general recursive function definitions. 
       
     5 Some fairly general functions that should probably go somewhere else... 
       
     6 *)
       
     7 
       
     8 structure Function_Lib =
       
     9 struct
       
    10 
       
    11 fun map_option f NONE = NONE 
       
    12   | map_option f (SOME x) = SOME (f x);
       
    13 
       
    14 fun fold_option f NONE y = y
       
    15   | fold_option f (SOME x) y = f x y;
       
    16 
       
    17 fun fold_map_option f NONE y = (NONE, y)
       
    18   | fold_map_option f (SOME x) y = apfst SOME (f x y);
       
    19 
       
    20 (* Ex: "The variable" ^ plural " is" "s are" vs *)
       
    21 fun plural sg pl [x] = sg
       
    22   | plural sg pl _ = pl
       
    23 
       
    24 (* lambda-abstracts over an arbitrarily nested tuple
       
    25   ==> hologic.ML? *)
       
    26 fun tupled_lambda vars t =
       
    27     case vars of
       
    28       (Free v) => lambda (Free v) t
       
    29     | (Var v) => lambda (Var v) t
       
    30     | (Const ("Pair", Type ("fun", [Ta, Type ("fun", [Tb, _])]))) $ us $ vs =>  
       
    31       (HOLogic.split_const (Ta,Tb, fastype_of t)) $ (tupled_lambda us (tupled_lambda vs t))
       
    32     | _ => raise Match
       
    33                  
       
    34                  
       
    35 fun dest_all (Const ("all", _) $ Abs (a as (_,T,_))) =
       
    36     let
       
    37       val (n, body) = Term.dest_abs a
       
    38     in
       
    39       (Free (n, T), body)
       
    40     end
       
    41   | dest_all _ = raise Match
       
    42                          
       
    43 
       
    44 (* Removes all quantifiers from a term, replacing bound variables by frees. *)
       
    45 fun dest_all_all (t as (Const ("all",_) $ _)) = 
       
    46     let
       
    47       val (v,b) = dest_all t
       
    48       val (vs, b') = dest_all_all b
       
    49     in
       
    50       (v :: vs, b')
       
    51     end
       
    52   | dest_all_all t = ([],t)
       
    53                      
       
    54 
       
    55 (* FIXME: similar to Variable.focus *)
       
    56 fun dest_all_all_ctx ctx (Const ("all", _) $ Abs (a as (n,T,b))) =
       
    57     let
       
    58       val [(n', _)] = Variable.variant_frees ctx [] [(n,T)]
       
    59       val (_, ctx') = ProofContext.add_fixes [(Binding.name n', SOME T, NoSyn)] ctx
       
    60 
       
    61       val (n'', body) = Term.dest_abs (n', T, b) 
       
    62       val _ = (n' = n'') orelse error "dest_all_ctx"
       
    63       (* Note: We assume that n' does not occur in the body. Otherwise it would be fixed. *)
       
    64 
       
    65       val (ctx'', vs, bd) = dest_all_all_ctx ctx' body
       
    66     in
       
    67       (ctx'', (n', T) :: vs, bd)
       
    68     end
       
    69   | dest_all_all_ctx ctx t = 
       
    70     (ctx, [], t)
       
    71 
       
    72 
       
    73 fun map3 _ [] [] [] = []
       
    74   | map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs
       
    75   | map3 _ _ _ _ = raise Library.UnequalLengths;
       
    76 
       
    77 fun map4 _ [] [] [] [] = []
       
    78   | map4 f (x :: xs) (y :: ys) (z :: zs) (u :: us) = f x y z u :: map4 f xs ys zs us
       
    79   | map4 _ _ _ _ _ = raise Library.UnequalLengths;
       
    80 
       
    81 fun map6 _ [] [] [] [] [] [] = []
       
    82   | map6 f (x :: xs) (y :: ys) (z :: zs) (u :: us) (v :: vs) (w :: ws) = f x y z u v w :: map6 f xs ys zs us vs ws
       
    83   | map6 _ _ _ _ _ _ _ = raise Library.UnequalLengths;
       
    84 
       
    85 fun map7 _ [] [] [] [] [] [] [] = []
       
    86   | map7 f (x :: xs) (y :: ys) (z :: zs) (u :: us) (v :: vs) (w :: ws) (b :: bs) = f x y z u v w b :: map7 f xs ys zs us vs ws bs
       
    87   | map7 _ _ _ _ _ _ _ _ = raise Library.UnequalLengths;
       
    88 
       
    89 
       
    90 
       
    91 (* forms all "unordered pairs": [1, 2, 3] ==> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)] *)
       
    92 (* ==> library *)
       
    93 fun unordered_pairs [] = []
       
    94   | unordered_pairs (x::xs) = map (pair x) (x::xs) @ unordered_pairs xs
       
    95 
       
    96 
       
    97 (* Replaces Frees by name. Works with loose Bounds. *)
       
    98 fun replace_frees assoc =
       
    99     map_aterms (fn c as Free (n, _) => the_default c (AList.lookup (op =) assoc n)
       
   100                  | t => t)
       
   101 
       
   102 
       
   103 fun rename_bound n (Q $ Abs(_, T, b)) = (Q $ Abs(n, T, b))
       
   104   | rename_bound n _ = raise Match
       
   105 
       
   106 fun mk_forall_rename (n, v) =
       
   107     rename_bound n o Logic.all v 
       
   108 
       
   109 fun forall_intr_rename (n, cv) thm =
       
   110     let
       
   111       val allthm = forall_intr cv thm
       
   112       val (_ $ abs) = prop_of allthm
       
   113     in
       
   114       Thm.rename_boundvars abs (Abs (n, dummyT, Term.dummy_pattern dummyT)) allthm
       
   115     end
       
   116 
       
   117 
       
   118 (* Returns the frees in a term in canonical order, excluding the fixes from the context *)
       
   119 fun frees_in_term ctxt t =
       
   120     Term.add_frees t []
       
   121     |> filter_out (Variable.is_fixed ctxt o fst)
       
   122     |> rev
       
   123 
       
   124 
       
   125 datatype proof_attempt = Solved of thm | Stuck of thm | Fail
       
   126 
       
   127 fun try_proof cgoal tac = 
       
   128     case SINGLE tac (Goal.init cgoal) of
       
   129       NONE => Fail
       
   130     | SOME st =>
       
   131         if Thm.no_prems st
       
   132         then Solved (Goal.finish (Syntax.init_pretty_global (Thm.theory_of_cterm cgoal)) st)
       
   133         else Stuck st
       
   134 
       
   135 
       
   136 fun dest_binop_list cn (t as (Const (n, _) $ a $ b)) = 
       
   137     if cn = n then dest_binop_list cn a @ dest_binop_list cn b else [ t ]
       
   138   | dest_binop_list _ t = [ t ]
       
   139 
       
   140 
       
   141 (* separate two parts in a +-expression:
       
   142    "a + b + c + d + e" --> "(a + b + d) + (c + e)"
       
   143 
       
   144    Here, + can be any binary operation that is AC.
       
   145 
       
   146    cn - The name of the binop-constructor (e.g. @{const_name Un})
       
   147    ac - the AC rewrite rules for cn
       
   148    is - the list of indices of the expressions that should become the first part
       
   149         (e.g. [0,1,3] in the above example)
       
   150 *)
       
   151 
       
   152 fun regroup_conv neu cn ac is ct =
       
   153  let
       
   154    val mk = HOLogic.mk_binop cn
       
   155    val t = term_of ct
       
   156    val xs = dest_binop_list cn t
       
   157    val js = subtract (op =) is (0 upto (length xs) - 1)
       
   158    val ty = fastype_of t
       
   159    val thy = theory_of_cterm ct
       
   160  in
       
   161    Goal.prove_internal []
       
   162      (cterm_of thy
       
   163        (Logic.mk_equals (t,
       
   164           if is = []
       
   165           then mk (Const (neu, ty), foldr1 mk (map (nth xs) js))
       
   166           else if js = []
       
   167             then mk (foldr1 mk (map (nth xs) is), Const (neu, ty))
       
   168             else mk (foldr1 mk (map (nth xs) is), foldr1 mk (map (nth xs) js)))))
       
   169      (K (rewrite_goals_tac ac
       
   170          THEN rtac Drule.reflexive_thm 1))
       
   171  end
       
   172 
       
   173 (* instance for unions *)
       
   174 fun regroup_union_conv t = regroup_conv @{const_name Set.empty} @{const_name Lattices.sup}
       
   175   (map (fn t => t RS eq_reflection) (@{thms Un_ac} @
       
   176                                      @{thms Un_empty_right} @
       
   177                                      @{thms Un_empty_left})) t
       
   178 
       
   179 
       
   180 end