src/HOL/Tools/function_package/fundef_lib.ML
author wenzelm
Wed, 19 Jul 2006 12:11:56 +0200
changeset 20154 c709a29f1363
parent 19922 984ae977f7aa
child 20523 36a59e5d0039
permissions -rw-r--r--
renamed Variable.rename_wrt to Variable.variant_frees;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/lib.ML
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     2
    ID:         $Id$
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     4
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
A package for general recursive function definitions. 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     6
Some fairly general functions that should probably go somewhere else... 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     7
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     8
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     9
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    10
fun mk_forall (var as Free (v,T)) t =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    11
    all T $ Abs (v,T, abstract_over (var,t))
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    12
  | mk_forall v _ = let val _ = print v in sys_error "mk_forall" end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    13
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    14
(* Builds a quantification with a new name for the variable. *)
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    15
fun mk_forall_rename (v as Free (_,T),newname) t =
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    16
    all T $ Abs (newname, T, abstract_over (v, t))
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    17
  | mk_forall_rename (v, _) _ = let val _ = print v in sys_error "mk_forall_rename" end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    18
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    19
(* Constructs a tupled abstraction from an arbitrarily nested tuple of variables and a term. *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    20
fun tupled_lambda vars t =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    21
    case vars of
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    22
	(Free v) => lambda (Free v) t
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    23
      | (Var v) => lambda (Var v) t
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    24
      | (Const ("Pair", Type ("fun", [Ta, Type ("fun", [Tb, _])]))) $ us $ vs =>  
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    25
	(HOLogic.split_const (Ta,Tb, fastype_of t)) $ (tupled_lambda us (tupled_lambda vs t))
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    26
      | _ => raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    27
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    28
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    29
fun dest_all (Const ("all", _) $ Abs (a as (_,T,_))) =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    30
    let
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    31
	val (n, body) = Term.dest_abs a
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    32
    in
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    33
	(Free (n, T), body)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    34
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    35
  | dest_all _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    36
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    37
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    38
(* Removes all quantifiers from a term, replacing bound variables by frees. *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    39
fun dest_all_all (t as (Const ("all",_) $ _)) = 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    40
    let
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    41
	val (v,b) = dest_all t
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    42
	val (vs, b') = dest_all_all b
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    43
    in
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    44
	(v :: vs, b')
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    45
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    46
  | dest_all_all t = ([],t)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    47
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    48
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    49
fun dest_all_all_ctx ctx (Const ("all", _) $ Abs (a as (n,T,b))) =
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    50
    let
20154
c709a29f1363 renamed Variable.rename_wrt to Variable.variant_frees;
wenzelm
parents: 19922
diff changeset
    51
      val [(n', _)] = Variable.variant_frees ctx [] [(n,T)]
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    52
      val (_, ctx') = ProofContext.add_fixes_i [(n', SOME T, NoSyn)] ctx
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    53
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    54
      val (n'', body) = Term.dest_abs (n', T, b) 
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    55
      val _ = assert (n' = n'') "dest_all_ctx" (* Note: We assume that n' does not occur in the body. Otherwise it would be fixed. *)
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    56
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    57
      val (ctx'', vs, bd) = dest_all_all_ctx ctx' body
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    58
    in
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    59
	(ctx'', (n', T) :: vs, bd)
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    60
    end
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    61
  | dest_all_all_ctx ctx t = 
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    62
    (ctx, [], t)
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    63
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    64
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    65
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    66
(* unfold *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    67
fun unfold P f g b x = if (P x) then ((f x)::(unfold P f g b (g x))) else (b x)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    68
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    69
val dest_implies_list = 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    70
    split_last o (unfold Logic.is_implies (fst o Logic.dest_implies) (snd o Logic.dest_implies) single)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    71
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    72
fun implies_elim_swp a b = implies_elim b a
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    73
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    74
fun map3 _ [] [] [] = []
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    75
  | map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs
19841
f2fa72c13186 avoid unqualified exception;
wenzelm
parents: 19770
diff changeset
    76
  | map3 _ _ _ _ = raise Library.UnequalLengths;
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    77
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    78
fun map6 _ [] [] [] [] [] [] = []
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    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
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    80
  | map6 _ _ _ _ _ _ _ = raise Library.UnequalLengths;
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    81
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    82
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    83
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    84
(* forms all "unordered pairs": [1, 2, 3] ==> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)] *)
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    85
fun unordered_pairs [] = []
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    86
  | unordered_pairs (x::xs) = map (pair x) (x::xs) @ unordered_pairs xs
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    87
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19564
diff changeset
    88
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19564
diff changeset
    89
fun the_single [x] = x
19841
f2fa72c13186 avoid unqualified exception;
wenzelm
parents: 19770
diff changeset
    90
  | the_single _ = sys_error "the_single"