src/Pure/IsaPlanner/term_lib.ML
author haftmann
Mon, 07 Nov 2005 12:06:11 +0100
changeset 18103 7a524bfa8d65
parent 18036 d5d5ad4b78c5
child 18184 43c4589a9a78
permissions -rw-r--r--
added proper fillin_mixfix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
     1
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
16179
fa7e70be26b0 header;
wenzelm
parents: 15928
diff changeset
     2
(*  Title:      Pure/IsaPlanner/term_lib.ML
fa7e70be26b0 header;
wenzelm
parents: 15928
diff changeset
     3
    ID:		$Id$
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     4
    Author:     Lucas Dixon, University of Edinburgh
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     5
                lucasd@dai.ed.ac.uk
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     6
    Created:    17 Aug 2002
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     7
*)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
     8
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
     9
(*  DESCRIPTION:
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    10
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    11
    Additional code to work with terms.
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    12
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    13
*)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    14
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    15
signature TERM_LIB =
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    16
sig
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    17
    val fo_term_height : term -> int
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    18
    val ho_term_height : term -> int
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    19
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    20
    (* Matching/unification with exceptions handled *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    21
    val clean_match : theory -> term * term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    22
                      -> ((indexname * (sort * typ)) list
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    23
                         * (indexname * (typ * term)) list) option
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    24
    val clean_unify : theory -> int -> term * term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    25
                      -> ((indexname * (sort * typ)) list
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    26
                         * (indexname * (typ * term)) list) Seq.seq
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    27
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    28
    (* managing variables in terms, can doing conversions *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    29
    val bounds_to_frees : term -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    30
    val bounds_to_frees_with_vars :
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    31
       (string * typ) list -> term -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    32
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    33
    val change_bounds_to_frees : term -> term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    34
    val change_frees_to_vars : term -> term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    35
    val change_vars_to_frees : term -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    36
    val loose_bnds_to_frees :
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    37
       (string * typ) list -> term -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    38
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    39
    (* make all variables named uniquely *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    40
    val unique_namify : term -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    41
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    42
		(* breasking up a term and putting it into a normal form
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    43
       independent of internal term context *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    44
    val cleaned_term_conc : term -> term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    45
    val cleaned_term_parts : term -> term list * term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    46
    val cterm_of_term : term -> cterm
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    47
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    48
    (* terms of theorems and subgoals *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    49
    val term_of_thm : thm -> term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    50
    val get_prems_of_sg_term : term -> term list
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    51
    val triv_thm_from_string : string -> thm
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    52
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    53
    (* some common term manipulations *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    54
    val try_dest_Trueprop : term -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    55
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    56
    val bot_left_leaf_of : term -> term
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    57
    val bot_left_leaf_noabs_of : term -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    58
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    59
    (* term containing another term - an embedding check *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    60
    val term_contains : term -> term -> bool
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    61
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    62
    (* name-convertable checking - like ae-convertable, but allows for
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    63
       var's and free's to be mixed - and doesn't used buggy code. :-) *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    64
		val get_name_eq_member : term -> term list -> term option
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    65
    val name_eq_member : term -> term list -> bool
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    66
    val term_name_eq :
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    67
       term ->
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    68
       term ->
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    69
       (term * term) list ->
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    70
       (term * term) list option
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    71
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    72
     (* is the typ a function or is it atomic *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    73
     val is_fun_typ : typ -> bool
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    74
     val is_atomic_typ : typ -> bool
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    75
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    76
    (* variable analysis *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    77
    val is_some_kind_of_var : term -> bool
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    78
    val same_var_check :
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
    79
       ''a -> ''b -> (''a * ''b) list -> (''a * ''b) list option
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    80
		val has_new_vars : term * term -> bool
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    81
    val has_new_typ_vars : term * term -> bool
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    82
   (* checks to see if the lhs -> rhs is a invalid rewrite rule *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    83
    val is_not_valid_rwrule : theory -> (term * term) -> bool
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    84
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    85
    (* get the frees in a term that are of atomic type, ie non-functions *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    86
    val atomic_frees_of_term : term -> (string * typ) list
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    87
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
    88
    (* get used names in a theorem to avoid upon instantiation. *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    89
    val usednames_of_term : term -> string list
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    90
    val usednames_of_thm : thm -> string list
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
    91
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    92
    (* Isar term skolemisationm and unsolemisation *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    93
    (* I think this is written also in IsarRTechn and also in somewhere else *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    94
    (* val skolemise_term : (string,typ) list -> term -> term *)
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    95
    val unskolemise_all_term :
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    96
        term ->
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    97
        (((string * typ) * string) list * term)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
    98
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
    99
    (* given a string describing term then a string for its type, returns
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   100
       read term *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   101
    val readwty : string -> string -> term
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   102
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   103
    (* pretty stuff *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   104
    val print_term : term -> unit
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   105
    val pretty_print_sort : string list -> string
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   106
    val pretty_print_term : term -> string
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   107
    val pretty_print_type : typ -> string
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   108
    val pretty_print_typelist :
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   109
       typ list -> (typ -> string) -> string
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   110
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   111
    (* for debugging: quickly get a string of a term w.r.t the_context() *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   112
    val string_of_term : term -> string
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   113
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   114
    (* Pretty printing in a way that allows them to be parsed by ML.
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   115
       these are perhaps redundent, check the standard basis
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   116
       lib for generic versions for any datatype? *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   117
    val writesort : string list -> unit
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   118
    val writeterm : term -> unit
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   119
    val writetype : typ -> unit
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   120
  end
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   121
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   122
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   123
structure TermLib : TERM_LIB =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   124
struct
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   125
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   126
(* Two kinds of depth measure for HOAS terms, a first order (flat) and a
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   127
   higher order one.
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   128
   Note: not stable of eta-contraction: embedding eta-expands term,
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   129
   thus we assume eta-expanded *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   130
fun fo_term_height (a $ b) =
17797
9996f3a0ffdf Int.max;
wenzelm
parents: 17412
diff changeset
   131
    Int.max (1 + fo_term_height b, (fo_term_height a))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   132
  | fo_term_height (Abs(_,_,t)) = fo_term_height t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   133
  | fo_term_height _ = 0;
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   134
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   135
fun ho_term_height  (a $ b) =
17797
9996f3a0ffdf Int.max;
wenzelm
parents: 17412
diff changeset
   136
    1 + (Int.max (ho_term_height b, ho_term_height a))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   137
  | ho_term_height (Abs(_,_,t)) = ho_term_height t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   138
  | ho_term_height _ = 0;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   139
15928
66b165ee016c lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
dixon
parents: 15915
diff changeset
   140
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   141
(* Higher order matching with exception handled *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   142
(* returns optional instantiation *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   143
fun clean_match thy (a as (pat, t)) =
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 16934
diff changeset
   144
  let val (tyenv, tenv) = Pattern.match thy a
15915
b0e8b37642a4 lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
dixon
parents: 15814
diff changeset
   145
  in SOME (Vartab.dest tyenv, Vartab.dest tenv)
15798
016f3be5a5ec Adapted to new interface of instantiation and unification / matching functions.
berghofe
parents: 15570
diff changeset
   146
  end handle Pattern.MATCH => NONE;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   147
(* Higher order unification with exception handled, return the instantiations *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   148
(* given Signature, max var index, pat, tgt *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   149
(* returns Seq of instantiations *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   150
fun clean_unify sgn ix (a as (pat, tgt)) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   151
    let
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   152
      (* type info will be re-derived, maybe this can be cached
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   153
         for efficiency? *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   154
      val pat_ty = Term.type_of pat;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   155
      val tgt_ty = Term.type_of tgt;
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   156
      (* is it OK to ignore the type instantiation info?
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   157
         or should I be using it? *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   158
      val typs_unify =
16934
9ef19e3c7fdd Sign.typ_unify;
wenzelm
parents: 16857
diff changeset
   159
          SOME (Sign.typ_unify sgn (pat_ty, tgt_ty) (Term.Vartab.empty, ix))
9ef19e3c7fdd Sign.typ_unify;
wenzelm
parents: 16857
diff changeset
   160
            handle Type.TUNIFY => NONE;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   161
    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   162
      case typs_unify of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   163
        SOME (typinsttab, ix2) =>
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   164
        let
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   165
      (* is it right to throw away the flexes?
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   166
         or should I be using them somehow? *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   167
          fun mk_insts env =
15915
b0e8b37642a4 lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
dixon
parents: 15814
diff changeset
   168
            (Vartab.dest (Envir.type_env env),
b0e8b37642a4 lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
dixon
parents: 15814
diff changeset
   169
             Envir.alist_of env);
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   170
          val initenv = Envir.Envir {asol = Vartab.empty,
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   171
                                     iTs = typinsttab, maxidx = ix2};
15928
66b165ee016c lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
dixon
parents: 15915
diff changeset
   172
          val useq = (Unify.smash_unifiers (sgn,initenv,[a]))
66b165ee016c lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
dixon
parents: 15915
diff changeset
   173
	            handle UnequalLengths => Seq.empty
66b165ee016c lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
dixon
parents: 15915
diff changeset
   174
		               | Term.TERM _ => Seq.empty;
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   175
          fun clean_unify' useq () =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   176
              (case (Seq.pull useq) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   177
                 NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   178
               | SOME (h,t) => SOME (mk_insts h, Seq.make (clean_unify' t)))
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   179
	      handle UnequalLengths => NONE
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   180
                   | Term.TERM _ => NONE;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   181
        in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   182
          (Seq.make (clean_unify' useq))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   183
        end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   184
      | NONE => Seq.empty
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   185
    end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   186
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   187
fun asm_mk t = assume (cterm_of (the_context()) t);
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   188
fun asm_read s = (Thm.assume (read_cterm (the_context()) (s,propT)));
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   189
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   190
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   191
(* more pretty printing code for Isabelle terms etc *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   192
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   193
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   194
(* pretty_print_typelist l f = print a typelist.
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   195
   l = list of types to print : typ list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   196
   f = function used to print a single type : typ -> string
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   197
*)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   198
fun pretty_print_typelist [] f = ""
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   199
  | pretty_print_typelist [(h: typ)] (f : typ -> string) = (f h)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   200
  | pretty_print_typelist ((h: typ) :: t) (f : typ -> string) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   201
      (f h) ^ ", " ^ (pretty_print_typelist t f);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   202
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   203
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   204
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   205
(* pretty_print_sort s = print a sort
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   206
   s = sort to print : string list
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   207
*)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   208
fun pretty_print_sort [] = ""
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   209
  | pretty_print_sort ([h])  = "\"" ^ h ^ "\""
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   210
  | pretty_print_sort (h :: t)  = "\"" ^ h ^ "\"," ^ (pretty_print_sort t);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   211
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   212
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   213
(* pretty_print_type t = print a type
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   214
   t = type to print : type
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   215
*)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   216
fun pretty_print_type (Type (n, l)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   217
      "Type(\"" ^ n ^ "\", [" ^ (pretty_print_typelist l pretty_print_type) ^ "])"
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   218
  | pretty_print_type (TFree (n, s)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   219
      "TFree(\"" ^ n ^ "\", [" ^ (pretty_print_sort s) ^ "])"
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   220
  | pretty_print_type (TVar ((n, i), s)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   221
      "TVar( (\"" ^ n ^ "\", " ^ (string_of_int i) ^ "), [" ^ (pretty_print_sort s) ^ "])";
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   222
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   223
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   224
(* pretty_print_term t = print a term prints types and sorts too.
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   225
   t = term to print : term
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   226
*)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   227
fun pretty_print_term (Const (s, t)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   228
      "Const(\"" ^ s ^ "\", " ^ (pretty_print_type t) ^ ")"
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   229
  | pretty_print_term (Free (s, t)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   230
      "Free(\"" ^ s ^ "\", " ^ (pretty_print_type t) ^ ")"
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   231
  | pretty_print_term (Var ((n, i), t)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   232
      "Var( (\"" ^ n ^ "\"," ^ (string_of_int i) ^ "), " ^ (pretty_print_type t) ^ ")"
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   233
  | pretty_print_term (Bound i) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   234
      "Bound(" ^ (string_of_int i) ^ ")"
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   235
  | pretty_print_term (Abs (s, t, r)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   236
      "Abs(\"" ^ s ^ "\"," ^ (pretty_print_type t) ^ ", \n  " ^ (pretty_print_term r) ^ ")"
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   237
  | pretty_print_term (op $ (t1, t2)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   238
      "(" ^ (pretty_print_term t1) ^ ") $\n (" ^ (pretty_print_term t2) ^ ")";
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   239
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   240
(* Write the term out nicly instead of just creating a string for it *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   241
fun writeterm t = writeln (pretty_print_term t);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   242
fun writetype t = writeln (pretty_print_type t);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   243
fun writesort s = writeln (pretty_print_sort s);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   244
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   245
fun cterm_of_term (t : term) = Thm.cterm_of (the_context()) t;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   246
fun term_of_thm t = (Thm.prop_of t);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   247
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   248
fun string_of_term t = Sign.string_of_term (the_context()) t;
16857
6389511d4609 lucas - slightly cleaned up. Removed redudent copy of Symtab structure.
dixon
parents: 16179
diff changeset
   249
fun print_term t = writeln (string_of_term t);
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   250
17931
881274f283cf avoid lagacy read function;
wenzelm
parents: 17797
diff changeset
   251
(* create a trivial thm from anything... *)
881274f283cf avoid lagacy read function;
wenzelm
parents: 17797
diff changeset
   252
fun triv_thm_from_string s =
881274f283cf avoid lagacy read function;
wenzelm
parents: 17797
diff changeset
   253
  let val thy = the_context()
881274f283cf avoid lagacy read function;
wenzelm
parents: 17797
diff changeset
   254
  in Thm.trivial (cterm_of thy (Sign.read_prop thy s)) end;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   255
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   256
  (* Checks if vars could be the same - alpha convertable
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   257
  w.r.t. previous vars, a and b are assumed to be vars,
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   258
  free vars, but not bound vars,
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   259
  Note frees and vars must all have unique names. *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   260
  fun same_var_check a b L =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   261
  let
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   262
    fun bterm_from t [] = NONE
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   263
      | bterm_from t ((a,b)::m) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   264
          if t = a then SOME b else bterm_from t m
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   265
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   266
    val bl_opt = bterm_from a L
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   267
  in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   268
		case bterm_from a L of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   269
			NONE => SOME ((a,b)::L)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   270
		| SOME b2 => if b2 = b then SOME L else NONE
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   271
  end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   272
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   273
  (* FIXME: make more efficient, only require a single new var! *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   274
  (* check if the new term has any meta variables not in the old term *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   275
  fun has_new_vars (old, new) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   276
			(case IsaPLib.lrem (op =) (Term.term_vars old) (Term.term_vars new) of
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   277
				 [] => false
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   278
			 | (_::_) => true);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   279
  (* check if the new term has any meta variables not in the old term *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   280
  fun has_new_typ_vars (old, new) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   281
			(case IsaPLib.lrem (op =) (Term.term_tvars old) (Term.term_tvars new) of
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   282
				 [] => false
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   283
			 | (_::_) => true);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   284
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   285
(* This version avoids name conflicts that might be introduced by
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   286
unskolemisation, and returns a list of (string * Term.typ) * string,
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   287
where the outer string is the original name, and the inner string is
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   288
the new name, and the type is the type of the free variable that was
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   289
renamed. *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   290
local
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   291
  fun myadd (n,ty) (L as (renames, names)) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   292
      let val n' = Syntax.dest_skolem n in
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   293
        case (Library.find_first (fn (_,n2) => (n2 = n)) renames) of
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   294
          NONE =>
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   295
          let val renamedn = Term.variant names n' in
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   296
            (renamedn, (((renamedn, ty), n) :: renames,
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   297
                        renamedn :: names)) end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   298
        | (SOME ((renamedn, _), _)) => (renamedn, L)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   299
      end
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   300
      handle Fail _ => (n, L);
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   301
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   302
  fun unsk (L,f) (t1 $ t2) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   303
      let val (L', t1') = unsk (L, I) t1
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   304
      in unsk (L', (fn x => f (t1' $ x))) t2 end
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   305
    | unsk (L,f) (Abs(n,ty,t)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   306
      unsk (L, (fn x => Abs(n,ty,x))) t
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   307
    | unsk (L, f) (Free (n,ty)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   308
      let val (renamed_n, L') = myadd (n ,ty) L
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   309
       in (L', f (Free (renamed_n, ty))) end
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   310
    | unsk (L, f) l = (L, f l);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   311
in
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   312
fun unskolemise_all_term t =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   313
    let
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   314
      val names = Term.add_term_names (t,[])
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   315
      val ((renames,names'),t') = unsk (([], names),I) t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   316
    in (renames,t') end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   317
end
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   318
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   319
(* true if the type t is a function *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   320
fun is_fun_typ (Type(s, l)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   321
    if s = "fun" then true else false
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   322
  | is_fun_typ _ = false;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   323
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   324
val is_atomic_typ = not o is_fun_typ;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   325
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   326
(* get the frees in a term that are of atomic type, ie non-functions *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   327
val atomic_frees_of_term =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   328
     List.filter (is_atomic_typ o snd)
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   329
     o map Term.dest_Free
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   330
     o Term.term_frees;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   331
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   332
fun usednames_of_term t = Term.add_term_names (t,[]);
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   333
fun usednames_of_thm th =
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   334
    let val rep = Thm.rep_thm th
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   335
      val hyps = #hyps rep
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   336
      val (tpairl,tpairr) = Library.split_list (#tpairs rep)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   337
      val prop = #prop rep
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   338
    in
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   339
      List.foldr Term.add_term_names [] (prop :: (tpairl @ (tpairr @ hyps)))
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   340
    end;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   341
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   342
(* read in a string and a top-level type and this will give back a term *)
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   343
fun readwty tstr tystr =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   344
  let val thy = the_context()
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   345
  in Sign.simple_read_term thy (Sign.read_typ (thy, K NONE) tystr) tstr end;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   346
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   347
  (* first term is equal to the second in some name convertable
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   348
  way... Note: This differs from the aeconv in the Term.ML file of
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   349
  Isabelle in that it allows a var to be alpha-equiv to a free var.
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   350
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   351
  Also, the isabelle term.ML version of aeconv uses a
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   352
  function that it claims doesn't work! *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   353
  fun term_name_eq (Abs(_,ty1,t1)) (Abs(_,ty2,t2)) l =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   354
    if ty1 = ty2 then term_name_eq t1 t2 l
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   355
    else NONE
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   356
  | term_name_eq (ah $ at) (bh $ bt) l =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   357
    let
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   358
      val lopt = (term_name_eq ah bh l)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   359
    in
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   360
      if isSome lopt then
16857
6389511d4609 lucas - slightly cleaned up. Removed redudent copy of Symtab structure.
dixon
parents: 16179
diff changeset
   361
	      term_name_eq at bt (valOf lopt)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   362
      else
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   363
        NONE
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   364
    end
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   365
  | term_name_eq (Const(a,T)) (Const(b,U)) l =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   366
    if a=b andalso T=U then SOME l
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   367
    else NONE
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   368
  | term_name_eq (a as Free(s1,ty1)) (b as Free(s2,ty2)) l =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   369
    same_var_check a b l
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   370
  | term_name_eq (a as Free(s1,ty1)) (b as Var(n2,ty2)) l =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   371
    same_var_check a b l
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   372
  | term_name_eq (a as Var(n1,ty1)) (b as Free(s2,ty2)) l =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   373
    same_var_check a b l
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   374
  | term_name_eq (a as Var(n1,ty1)) (b as Var(n2,ty2)) l =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   375
    same_var_check a b l
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   376
  | term_name_eq (Bound i) (Bound j) l =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   377
    if i = j then SOME l else NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   378
  | term_name_eq a b l = ((*writeln ("unchecked case:\n" ^ "a:\n" ^ (pretty_print_term a) ^ "\nb:\n" ^ (pretty_print_term b));*) NONE);
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   379
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   380
 (* checks to see if the term in a name-equivalent member of the list of terms. *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   381
  fun get_name_eq_member a [] = NONE
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   382
    | get_name_eq_member a (h :: t) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   383
        if isSome (term_name_eq a h []) then SOME h
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   384
				else get_name_eq_member a t;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   385
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   386
  fun name_eq_member a [] = false
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   387
    | name_eq_member a (h :: t) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   388
        if isSome (term_name_eq a h []) then true
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   389
				else name_eq_member a t;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   390
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   391
  (* true if term is a variable *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   392
  fun is_some_kind_of_var (Free(s, ty)) = true
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   393
    | is_some_kind_of_var (Var(i, ty)) = true
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   394
    | is_some_kind_of_var (Bound(i)) = true
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   395
    | is_some_kind_of_var _ = false;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   396
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   397
    (* checks to see if the lhs -> rhs is a invalid rewrite rule *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   398
(* FIXME: we should really check that there is a subterm on the lhs
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   399
which embeds into the rhs, this would be much closer to the normal
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   400
notion of valid wave rule - ie there exists at least one case where it
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   401
is a valid wave rule... *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   402
	fun is_not_valid_rwrule thy (lhs, rhs) =
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   403
      Term.is_Var (Term.head_of lhs) (* if lhs is essentially just a var *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   404
      orelse has_new_vars (lhs,rhs)
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   405
      orelse has_new_typ_vars (lhs,rhs)
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 16934
diff changeset
   406
      orelse Pattern.matches_subterm thy (lhs, rhs);
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   407
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   408
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   409
  (* first term contains the second in some name convertable way... *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   410
  (* note: this is equivalent to an alpha-convertable emedding *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   411
  (* takes as args: term containing a, term contained b,
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   412
     (bound vars of a, bound vars of b),
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   413
     current alpha-conversion-pairs,
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   414
     returns: bool:can convert, new alpha-conversion table *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   415
  (* in bellow: we *don't* use: a loose notion that only requires
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   416
  variables to match variables, and doesn't worry about the actual
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   417
  pattern in the variables. *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   418
  fun term_contains1 (Bs, FVs) (Abs(s,ty,t)) (Abs(s2,ty2,t2)) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   419
			if ty = ty2 then
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   420
				term_contains1 ((SOME(s,s2,ty)::Bs), FVs) t t2
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   421
			else []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   422
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   423
  | term_contains1 T t1 (Abs(s2,ty2,t2)) = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   424
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   425
  | term_contains1 (Bs, FVs) (Abs(s,ty,t)) t2 =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   426
    term_contains1 (NONE::Bs, FVs) t t2
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   427
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   428
  | term_contains1 T (ah $ at) (bh $ bt) =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   429
    (term_contains1 T ah (bh $ bt)) @
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   430
    (term_contains1 T at (bh $ bt)) @
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   431
    (List.concat (map (fn inT => (term_contains1 inT at bt))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   432
               (term_contains1 T ah bh)))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   433
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   434
  | term_contains1 T a (bh $ bt) = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   435
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   436
  | term_contains1 T (ah $ at) b =
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   437
		(term_contains1 T ah b) @ (term_contains1 T at b)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   438
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   439
  | term_contains1 T a b =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   440
  (* simple list table lookup to check if a named variable has been
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   441
  mapped to a variable, if not adds the mapping and return some new
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   442
  list mapping, if it is then it checks that the pair are mapped to
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   443
  each other, if so returns the current mapping list, else none. *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   444
		let
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   445
			fun bterm_from t [] = NONE
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   446
				| bterm_from t ((a,b)::m) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   447
					if t = a then SOME b else bterm_from t m
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   448
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   449
  (* check to see if, w.r.t. the variable mapping, two terms are leaf
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   450
  terms and are mapped to each other. Note constants are only mapped
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   451
  to the same constant. *)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   452
			fun same_leaf_check (T as (Bs,FVs)) (Bound i) (Bound j) =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   453
					let
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   454
						fun aux_chk (i1,i2) [] = false
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   455
							| aux_chk (0,0) ((SOME _) :: bnds) = true
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   456
							| aux_chk (i1,0) (NONE :: bnds) = false
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   457
							| aux_chk (i1,i2) ((SOME _) :: bnds) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   458
								aux_chk (i1 - 1,i2 - 1) bnds
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   459
							| aux_chk (i1,i2) (NONE :: bnds) =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   460
								aux_chk (i1,i2 - 1) bnds
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   461
					in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   462
						if (aux_chk (i,j) Bs) then [T]
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   463
						else []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   464
					end
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   465
				| same_leaf_check (T as (Bs,(Fs,Vs)))
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   466
                          (a as (Free (an,aty)))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   467
                          (b as (Free (bn,bty))) =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   468
					(case bterm_from an Fs of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   469
						 SOME b2n => if bn = b2n then [T]
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   470
												 else [] (* conflict of var name *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   471
					 | NONE => [(Bs,((an,bn)::Fs,Vs))])
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   472
				| same_leaf_check (T as (Bs,(Fs,Vs)))
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   473
                          (a as (Var (an,aty)))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   474
                          (b as (Var (bn,bty))) =
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   475
					(case bterm_from an Vs of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   476
						 SOME b2n => if bn = b2n then [T]
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   477
												 else [] (* conflict of var name *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   478
					 | NONE => [(Bs,(Fs,(an,bn)::Vs))])
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   479
				| same_leaf_check T (a as (Const _)) (b as (Const _)) =
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   480
					if a = b then [T] else []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   481
				| same_leaf_check T _ _ = []
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   482
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   483
		in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   484
			same_leaf_check T a b
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   485
		end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   486
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   487
  (* wrapper for term_contains1: checks if the term "a" contains in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   488
  some embedded way, (w.r.t. name -convertable) the term "b" *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   489
  fun term_contains a b =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   490
			case term_contains1 ([],([],[])) a b of
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   491
			  (_ :: _) => true
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   492
			| [] => false;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   493
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   494
  (* change all bound variables to see ones with appropriate name and
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   495
  type *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   496
  (* naming convention is OK as we use 'variant' from term.ML *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   497
  (* Note "bounds_to_frees" defined below, its better and quicker, but
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   498
  keeps the quantifiers handing about, and changes all bounds, not
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   499
  just universally quantified ones. *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   500
  fun change_bounds_to_frees t =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   501
    let
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   502
      val vars = strip_all_vars t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   503
      val frees_names = map (fn Free(s,n) => s | _ => "") (term_frees t)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   504
      val body = strip_all_body t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   505
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   506
      fun bnds_to_frees [] _ acc = acc
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   507
        | bnds_to_frees ((name,ty)::more) names acc =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   508
            let
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   509
	      val new_name = variant names name
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   510
	    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   511
	      bnds_to_frees more (new_name::names) (Free(new_name,ty)::acc)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   512
	    end
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   513
    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   514
      (subst_bounds ((bnds_to_frees vars frees_names []), body))
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   515
    end;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   516
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   517
(* a runtime-quick function which makes sure that every variable has a
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   518
unique name *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   519
fun unique_namify_aux (ntab,(Abs(s,ty,t))) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   520
    (case Symtab.lookup ntab s of
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   521
       NONE =>
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   522
       let
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   523
	 val (ntab2,t2) = unique_namify_aux (Symtab.update (s, s) ntab, t)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   524
       in
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   525
	 (ntab2,Abs(s,ty,t2))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   526
       end
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   527
     | SOME s2 =>
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   528
       let
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   529
	 val s_new = (Term.variant (Symtab.keys ntab) s)
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   530
	 val (ntab2,t2) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   531
	     unique_namify_aux (Symtab.update (s_new, s_new) ntab, t)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   532
       in
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   533
	 (ntab2,Abs(s_new,ty,t2))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   534
       end)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   535
  | unique_namify_aux (ntab,(a $ b)) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   536
    let
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   537
      val (ntab1,t1) = unique_namify_aux (ntab,a)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   538
      val (ntab2,t2) = unique_namify_aux (ntab1,b)		
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   539
    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   540
      (ntab2, t1$t2)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   541
    end
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   542
  | unique_namify_aux (nt as (ntab,Const x)) = nt
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   543
  | unique_namify_aux (nt as (ntab,f as Free (s,ty))) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   544
    (case Symtab.lookup ntab s of
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   545
       NONE => (Symtab.update (s, s) ntab, f)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   546
     | SOME _ => nt)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   547
  | unique_namify_aux (nt as (ntab,v as Var ((s,i),ty))) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   548
    (case Symtab.lookup ntab s of
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   549
       NONE => (Symtab.update (s, s) ntab, v)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   550
     | SOME _ => nt)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   551
  | unique_namify_aux (nt as (ntab, Bound i)) = nt;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   552
		
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   553
fun unique_namify t =
16857
6389511d4609 lucas - slightly cleaned up. Removed redudent copy of Symtab structure.
dixon
parents: 16179
diff changeset
   554
    #2 (unique_namify_aux (Symtab.empty, t));
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   555
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   556
(* a runtime-quick function which makes sure that every variable has a
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   557
unique name and also changes bound variables to free variables, used
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   558
for embedding checks, Note that this is a pretty naughty term
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   559
manipulation, which doesn't have necessary relation to the original
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   560
sematics of the term. This is really a trick for our embedding code. *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   561
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   562
fun bounds_to_frees_aux T (ntab,(Abs(s,ty,t))) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   563
    (case Symtab.lookup ntab s of
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   564
      NONE =>
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   565
      let
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   566
	val (ntab2,t2) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   567
          bounds_to_frees_aux ((s,ty)::T) (Symtab.update (s, s) ntab, t)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   568
      in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   569
	(ntab2,Abs(s,ty,t2))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   570
      end
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   571
    | SOME s2 =>
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   572
      let
16857
6389511d4609 lucas - slightly cleaned up. Removed redudent copy of Symtab structure.
dixon
parents: 16179
diff changeset
   573
	val s_new = (Term.variant (Symtab.keys ntab) s)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   574
	val (ntab2,t2) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   575
	  bounds_to_frees_aux ((s_new,ty)::T)
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   576
            (Symtab.update (s_new, s_new) ntab, t)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   577
      in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   578
	(ntab2,Abs(s_new,ty,t2))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   579
      end)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   580
  | bounds_to_frees_aux T (ntab,(a $ b)) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   581
    let
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   582
      val (ntab1,t1) = bounds_to_frees_aux T (ntab,a)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   583
      val (ntab2,t2) = bounds_to_frees_aux T (ntab1,b)		
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   584
    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   585
      (ntab2, t1$t2)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   586
    end
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   587
  | bounds_to_frees_aux T (nt as (ntab,Const x)) = nt
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   588
  | bounds_to_frees_aux T (nt as (ntab,f as Free (s,ty))) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   589
    (case Symtab.lookup ntab s of
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   590
      NONE => (Symtab.update (s, s) ntab, f)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   591
    | SOME _ => nt)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   592
  | bounds_to_frees_aux T (nt as (ntab,v as Var ((s,i),ty))) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   593
     (case Symtab.lookup ntab s of
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17225
diff changeset
   594
      NONE => (Symtab.update (s, s) ntab, v)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15481
diff changeset
   595
    | SOME _ => nt)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   596
  | bounds_to_frees_aux T (nt as (ntab, Bound i)) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   597
    let
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   598
      val (s,ty) = List.nth (T,i)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   599
    in
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   600
      (ntab, Free (s,ty))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   601
    end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   602
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   603
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   604
fun bounds_to_frees t =
16857
6389511d4609 lucas - slightly cleaned up. Removed redudent copy of Symtab structure.
dixon
parents: 16179
diff changeset
   605
    #2 (bounds_to_frees_aux [] (Symtab.empty,t));
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   606
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   607
fun bounds_to_frees_with_vars vars t =
16857
6389511d4609 lucas - slightly cleaned up. Removed redudent copy of Symtab structure.
dixon
parents: 16179
diff changeset
   608
    #2 (bounds_to_frees_aux vars (Symtab.empty,t));
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   609
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   610
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   611
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   612
(* loose bounds to frees *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   613
fun loose_bnds_to_frees_aux (bnds,vars) (Abs(s,ty,t)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   614
    Abs(s,ty,loose_bnds_to_frees_aux (bnds + 1,vars) t)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   615
  | loose_bnds_to_frees_aux (bnds,vars) (a $ b) =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   616
    (loose_bnds_to_frees_aux (bnds,vars) a)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   617
      $ (loose_bnds_to_frees_aux (bnds,vars) b)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   618
  | loose_bnds_to_frees_aux (bnds,vars) (t as (Bound i)) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   619
    if (bnds <= i) then Free (List.nth (vars,i - bnds))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   620
    else t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   621
  | loose_bnds_to_frees_aux (bnds,vars) t = t;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   622
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   623
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   624
fun loose_bnds_to_frees vars t =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   625
    loose_bnds_to_frees_aux (0,vars) t;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   626
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   627
18036
d5d5ad4b78c5 removed try_dest_Goal, use Logic.unprotect;
wenzelm
parents: 17931
diff changeset
   628
  (* FIXME ObjectLogic.drop_judgment *)
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   629
  fun try_dest_Trueprop (Const("Trueprop", _) $ T) = T
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   630
    | try_dest_Trueprop T = T;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   631
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   632
  fun bot_left_leaf_of (l $ r) = bot_left_leaf_of l
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   633
    | bot_left_leaf_of (Abs(s,ty,t)) = bot_left_leaf_of t
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   634
    | bot_left_leaf_of x = x;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   635
15814
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   636
  fun bot_left_leaf_noabs_of (l $ r) = bot_left_leaf_noabs_of l
d65f461c8672 lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents: 15798
diff changeset
   637
    | bot_left_leaf_noabs_of x = x;
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   638
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   639
(* cleaned up normal form version of the subgoal terms conclusion *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   640
fun cleaned_term_conc t =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   641
    let
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   642
      val concl = Logic.strip_imp_concl (change_bounds_to_frees t)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   643
    in
18036
d5d5ad4b78c5 removed try_dest_Goal, use Logic.unprotect;
wenzelm
parents: 17931
diff changeset
   644
      (try_dest_Trueprop (Logic.unprotect concl handle TERM _ => concl))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   645
    end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   646
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   647
(*   fun get_prems_of_sg_term t =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   648
			map opt_dest_Trueprop (Logic.strip_imp_prems t); *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   649
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   650
fun get_prems_of_sg_term t =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   651
		map try_dest_Trueprop (Logic.strip_assums_hyp t);
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   652
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   653
18036
d5d5ad4b78c5 removed try_dest_Goal, use Logic.unprotect;
wenzelm
parents: 17931
diff changeset
   654
(* drop premises, clean bound var stuff, and make a trueprop... *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   655
  fun cleaned_term_parts t =
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   656
      let
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   657
				val t2 = (change_bounds_to_frees t)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   658
        val concl = Logic.strip_imp_concl t2
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   659
				val prems = map try_dest_Trueprop (Logic.strip_imp_prems t2)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   660
      in
18036
d5d5ad4b78c5 removed try_dest_Goal, use Logic.unprotect;
wenzelm
parents: 17931
diff changeset
   661
	(prems, (try_dest_Trueprop ((Logic.unprotect concl handle TERM _ => concl))))
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   662
      end;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   663
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   664
  (* change free variables to vars and visa versa *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   665
  (* *** check naming is OK, can we just use the vasr old name? *)
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   666
  (* *** Check: Logic.varify and Logic.unvarify *)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   667
  fun change_vars_to_frees (a$b) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   668
        (change_vars_to_frees a) $ (change_vars_to_frees b)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   669
    | change_vars_to_frees (Abs(s,ty,t)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   670
        (Abs(s,Type.varifyT ty,change_vars_to_frees t))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   671
    | change_vars_to_frees (Var((s,i),ty)) = (Free(s,Type.unvarifyT ty))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   672
    | change_vars_to_frees l = l;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   673
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   674
  fun change_frees_to_vars (a$b) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   675
        (change_frees_to_vars a) $ (change_frees_to_vars b)
17225
e2998d50f51a curried_lookup/update;
wenzelm
parents: 17203
diff changeset
   676
    | change_frees_to_vars (Abs(s,ty,t)) =
15481
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   677
        (Abs(s,Type.varifyT ty,change_frees_to_vars t))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   678
    | change_frees_to_vars (Free(s,ty)) = (Var((s,0),Type.varifyT ty))
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   679
    | change_frees_to_vars l = l;
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   680
fc075ae929e4 the new subst tactic, by Lucas Dixon
paulson
parents:
diff changeset
   681
16934
9ef19e3c7fdd Sign.typ_unify;
wenzelm
parents: 16857
diff changeset
   682
end;