src/HOL/Tools/function_package/fundef_lib.ML
author krauss
Mon, 06 Nov 2006 12:04:44 +0100
changeset 21188 2aa15b663cd4
parent 21051 c49467a9c1e1
child 21237 b803f9870e97
permissions -rw-r--r--
minor cleanup
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
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20876
diff changeset
    10
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20876
diff changeset
    11
structure FundefLib = struct
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20876
diff changeset
    12
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    13
fun eq_str (s : string, t) = (s = t) (* monomorphic equality on strings *)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    14
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    15
fun mk_forall v t = all (fastype_of v) $ lambda v t
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    16
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    17
(* 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
    18
fun tupled_lambda vars t =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    19
    case vars of
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    20
	    (Free v) => lambda (Free v) t
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    21
    | (Var v) => lambda (Var v) t
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    22
    | (Const ("Pair", Type ("fun", [Ta, Type ("fun", [Tb, _])]))) $ us $ vs =>  
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    23
	    (HOLogic.split_const (Ta,Tb, fastype_of t)) $ (tupled_lambda us (tupled_lambda vs t))
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    24
    | _ => raise Match
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    25
                 
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    26
                 
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    27
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
    28
    let
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    29
	    val (n, body) = Term.dest_abs a
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    30
    in
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    31
	    (Free (n, T), body)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    32
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    33
  | dest_all _ = raise Match
21188
2aa15b663cd4 minor cleanup
krauss
parents: 21051
diff changeset
    34
                         
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    35
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    36
(* 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
    37
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
    38
    let
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    39
	val (v,b) = dest_all t
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    40
	val (vs, b') = dest_all_all b
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    41
    in
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    42
	(v :: vs, b')
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    43
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    44
  | dest_all_all t = ([],t)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    45
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    46
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    47
fun dest_all_all_ctx ctx (Const ("all", _) $ Abs (a as (n,T,b))) =
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    48
    let
20154
c709a29f1363 renamed Variable.rename_wrt to Variable.variant_frees;
wenzelm
parents: 19922
diff changeset
    49
      val [(n', _)] = Variable.variant_frees ctx [] [(n,T)]
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    50
      val (_, ctx') = ProofContext.add_fixes_i [(n', SOME T, NoSyn)] ctx
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    51
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    52
      val (n'', body) = Term.dest_abs (n', T, b) 
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    53
      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
    54
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    55
      val (ctx'', vs, bd) = dest_all_all_ctx ctx' body
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    56
    in
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    57
	(ctx'', (n', T) :: vs, bd)
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    58
    end
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    59
  | dest_all_all_ctx ctx t = 
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    60
    (ctx, [], t)
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    61
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    62
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    63
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
    64
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    65
fun map3 _ [] [] [] = []
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    66
  | 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
    67
  | map3 _ _ _ _ = raise Library.UnequalLengths;
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    68
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    69
fun map4 _ [] [] [] [] = []
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    70
  | map4 f (x :: xs) (y :: ys) (z :: zs) (u :: us) = f x y z u :: map4 f xs ys zs us
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    71
  | map4 _ _ _ _ _ = raise Library.UnequalLengths;
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    72
19922
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    73
fun map6 _ [] [] [] [] [] [] = []
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    74
  | 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
    75
  | map6 _ _ _ _ _ _ _ = raise Library.UnequalLengths;
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    76
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    77
fun map7 _ [] [] [] [] [] [] [] = []
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    78
  | 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
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    79
  | map7 _ _ _ _ _ _ _ _ = raise Library.UnequalLengths;
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    80
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    81
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
(* 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
    84
fun unordered_pairs [] = []
984ae977f7aa Fixed name clash.
krauss
parents: 19841
diff changeset
    85
  | 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
    86
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19564
diff changeset
    87
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    88
(* Replaces Frees by name. Probably quicker than Pattern.rewrite_terms, and works with loose Bounds. *)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    89
fun replace_frees assoc =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    90
    map_aterms (fn c as Free (n, _) => (case AList.lookup (op =) assoc n of
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    91
                                          NONE => c
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    92
                                        | SOME t => t)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    93
                 | t => t)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    94
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    95
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    96
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    97
fun rename_bound n (Q $ Abs(_, T, b)) = (Q $ Abs(n, T, b))
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    98
  | rename_bound n _ = raise Match
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
    99
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   100
fun mk_forall_rename (n, v) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   101
    rename_bound n o mk_forall v 
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   102
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   103
fun forall_intr_rename (n, cv) thm =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   104
    let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   105
      val allthm = forall_intr cv thm
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   106
      val reflx = prop_of allthm
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   107
                   |> rename_bound n
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   108
                   |> cterm_of (theory_of_thm thm)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   109
                   |> reflexive
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   110
    in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   111
      equal_elim reflx allthm
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   112
    end
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   113
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   114
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   115
(* Returns the frees in a term in canonical order, excluding the fixes from the context *)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   116
fun frees_in_term ctxt t =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   117
    rev (fold_aterms (fn  Free (v as (x, _)) =>
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20154
diff changeset
   118
                          if Variable.is_fixed ctxt x then I else insert (fn ((x, _),(y, _)) => x = y) v | _ => I) t [])
20851
bf80cb83f8be Improved error reporting
krauss
parents: 20523
diff changeset
   119
bf80cb83f8be Improved error reporting
krauss
parents: 20523
diff changeset
   120
bf80cb83f8be Improved error reporting
krauss
parents: 20523
diff changeset
   121
fun plural sg pl [] = sys_error "plural"
bf80cb83f8be Improved error reporting
krauss
parents: 20523
diff changeset
   122
  | plural sg pl [x] = sg
bf80cb83f8be Improved error reporting
krauss
parents: 20523
diff changeset
   123
  | plural sg pl (x::y::ys) = pl
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20876
diff changeset
   124
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20876
diff changeset
   125
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20876
diff changeset
   126
end