src/HOL/Tools/function_package/fundef_lib.ML
changeset 19922 984ae977f7aa
parent 19841 f2fa72c13186
child 20154 c709a29f1363
equal deleted inserted replaced
19921:2a48a5550045 19922:984ae977f7aa
     7 *)
     7 *)
     8 
     8 
     9 
     9 
    10 fun mk_forall (var as Free (v,T)) t =
    10 fun mk_forall (var as Free (v,T)) t =
    11     all T $ Abs (v,T, abstract_over (var,t))
    11     all T $ Abs (v,T, abstract_over (var,t))
    12   | mk_forall _ _ = raise Match
    12   | mk_forall v _ = let val _ = print v in sys_error "mk_forall" end
    13 
    13 
    14 (* Builds a quantification with a new name for the variable. *)
    14 (* Builds a quantification with a new name for the variable. *)
    15 fun mk_forall_rename ((v,T),newname) t =
    15 fun mk_forall_rename (v as Free (_,T),newname) t =
    16     all T $ Abs (newname,T, abstract_over (Free (v,T),t))
    16     all T $ Abs (newname, T, abstract_over (v, t))
       
    17   | mk_forall_rename (v, _) _ = let val _ = print v in sys_error "mk_forall_rename" end
    17 
    18 
    18 (* Constructs a tupled abstraction from an arbitrarily nested tuple of variables and a term. *)
    19 (* Constructs a tupled abstraction from an arbitrarily nested tuple of variables and a term. *)
    19 fun tupled_lambda vars t =
    20 fun tupled_lambda vars t =
    20     case vars of
    21     case vars of
    21 	(Free v) => lambda (Free v) t
    22 	(Free v) => lambda (Free v) t
    42     in
    43     in
    43 	(v :: vs, b')
    44 	(v :: vs, b')
    44     end
    45     end
    45   | dest_all_all t = ([],t)
    46   | dest_all_all t = ([],t)
    46 
    47 
       
    48 
       
    49 fun dest_all_all_ctx ctx (Const ("all", _) $ Abs (a as (n,T,b))) =
       
    50     let
       
    51       val [(n', _)] = Variable.rename_wrt ctx [] [(n,T)]
       
    52       val (_, ctx') = ProofContext.add_fixes_i [(n', SOME T, NoSyn)] ctx
       
    53 
       
    54       val (n'', body) = Term.dest_abs (n', T, b) 
       
    55       val _ = assert (n' = n'') "dest_all_ctx" (* Note: We assume that n' does not occur in the body. Otherwise it would be fixed. *)
       
    56 
       
    57       val (ctx'', vs, bd) = dest_all_all_ctx ctx' body
       
    58     in
       
    59 	(ctx'', (n', T) :: vs, bd)
       
    60     end
       
    61   | dest_all_all_ctx ctx t = 
       
    62     (ctx, [], t)
       
    63 
       
    64 
       
    65 
    47 (* unfold *)
    66 (* unfold *)
    48 fun unfold P f g b x = if (P x) then ((f x)::(unfold P f g b (g x))) else (b x)
    67 fun unfold P f g b x = if (P x) then ((f x)::(unfold P f g b (g x))) else (b x)
    49 
    68 
    50 val dest_implies_list = 
    69 val dest_implies_list = 
    51     split_last o (unfold Logic.is_implies (fst o Logic.dest_implies) (snd o Logic.dest_implies) single)
    70     split_last o (unfold Logic.is_implies (fst o Logic.dest_implies) (snd o Logic.dest_implies) single)
    54 
    73 
    55 fun map3 _ [] [] [] = []
    74 fun map3 _ [] [] [] = []
    56   | map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs
    75   | map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs
    57   | map3 _ _ _ _ = raise Library.UnequalLengths;
    76   | map3 _ _ _ _ = raise Library.UnequalLengths;
    58 
    77 
       
    78 fun map6 _ [] [] [] [] [] [] = []
       
    79   | 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
       
    80   | map6 _ _ _ _ _ _ _ = raise Library.UnequalLengths;
       
    81 
    59 
    82 
    60 
    83 
    61 (* forms all "unordered pairs": [1, 2, 3] ==> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)] *)
    84 (* forms all "unordered pairs": [1, 2, 3] ==> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)] *)
    62 fun upairs [] = []
    85 fun unordered_pairs [] = []
    63   | upairs (x::xs) = map (pair x) (x::xs) @ upairs xs
    86   | unordered_pairs (x::xs) = map (pair x) (x::xs) @ unordered_pairs xs
    64 
    87 
    65 
    88 
    66 fun the_single [x] = x
    89 fun the_single [x] = x
    67   | the_single _ = sys_error "the_single"
    90   | the_single _ = sys_error "the_single"