src/Pure/term_subst.ML
changeset 74266 612b7e0d6721
parent 74232 1091880266e5
child 74576 0b43d42cfde7
equal deleted inserted replaced
74265:633fe7390c97 74266:612b7e0d6721
     2     Author:     Makarius
     2     Author:     Makarius
     3 
     3 
     4 Efficient type/term substitution.
     4 Efficient type/term substitution.
     5 *)
     5 *)
     6 
     6 
     7 signature INST_TABLE =
       
     8 sig
       
     9   include TABLE
       
    10   val add: key * 'a -> 'a table -> 'a table
       
    11   val table: (key * 'a) list -> 'a table
       
    12 end;
       
    13 
       
    14 functor Inst_Table(Key: KEY): INST_TABLE =
       
    15 struct
       
    16 
       
    17 structure Tab = Table(Key);
       
    18 
       
    19 fun add entry = Tab.insert (K true) entry;
       
    20 fun table entries = Tab.build (fold add entries);
       
    21 
       
    22 open Tab;
       
    23 
       
    24 end;
       
    25 
       
    26 signature TERM_SUBST =
     7 signature TERM_SUBST =
    27 sig
     8 sig
    28   structure TFrees: INST_TABLE
       
    29   structure TVars: INST_TABLE
       
    30   structure Frees: INST_TABLE
       
    31   structure Vars: INST_TABLE
       
    32   val add_tfreesT: typ -> TFrees.set -> TFrees.set
       
    33   val add_tfrees: term -> TFrees.set -> TFrees.set
       
    34   val add_tvarsT: typ -> TVars.set -> TVars.set
       
    35   val add_tvars: term -> TVars.set -> TVars.set
       
    36   val add_frees: term -> Frees.set -> Frees.set
       
    37   val add_vars: term -> Vars.set -> Vars.set
       
    38   val add_tfree_namesT: typ -> Symtab.set -> Symtab.set
       
    39   val add_tfree_names: term -> Symtab.set -> Symtab.set
       
    40   val add_free_names: term -> Symtab.set -> Symtab.set
       
    41   val map_atypsT_same: typ Same.operation -> typ Same.operation
     9   val map_atypsT_same: typ Same.operation -> typ Same.operation
    42   val map_types_same: typ Same.operation -> term Same.operation
    10   val map_types_same: typ Same.operation -> term Same.operation
    43   val map_aterms_same: term Same.operation -> term Same.operation
    11   val map_aterms_same: term Same.operation -> term Same.operation
    44   val generalizeT_same: Symtab.set -> int -> typ Same.operation
    12   val generalizeT_same: Names.set -> int -> typ Same.operation
    45   val generalize_same: Symtab.set * Symtab.set -> int -> term Same.operation
    13   val generalize_same: Names.set * Names.set -> int -> term Same.operation
    46   val generalizeT: Symtab.set -> int -> typ -> typ
    14   val generalizeT: Names.set -> int -> typ -> typ
    47   val generalize: Symtab.set * Symtab.set -> int -> term -> term
    15   val generalize: Names.set * Names.set -> int -> term -> term
    48   val instantiateT_maxidx: (typ * int) TVars.table -> typ -> int -> typ * int
    16   val instantiateT_maxidx: (typ * int) TVars.table -> typ -> int -> typ * int
    49   val instantiate_maxidx: (typ * int) TVars.table * (term * int) Vars.table ->
    17   val instantiate_maxidx: (typ * int) TVars.table * (term * int) Vars.table ->
    50     term -> int -> term * int
    18     term -> int -> term * int
    51   val instantiateT_frees_same: typ TFrees.table -> typ Same.operation
    19   val instantiateT_frees_same: typ TFrees.table -> typ Same.operation
    52   val instantiate_frees_same: typ TFrees.table * term Frees.table -> term Same.operation
    20   val instantiate_frees_same: typ TFrees.table * term Frees.table -> term Same.operation
    62 end;
    30 end;
    63 
    31 
    64 structure Term_Subst: TERM_SUBST =
    32 structure Term_Subst: TERM_SUBST =
    65 struct
    33 struct
    66 
    34 
    67 (* instantiation as table *)
       
    68 
       
    69 structure TFrees = Inst_Table(
       
    70   type key = string * sort
       
    71   val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.sort_ord)
       
    72 );
       
    73 
       
    74 structure TVars = Inst_Table(
       
    75   type key = indexname * sort
       
    76   val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.sort_ord)
       
    77 );
       
    78 
       
    79 structure Frees = Inst_Table(
       
    80   type key = string * typ
       
    81   val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.typ_ord)
       
    82 );
       
    83 
       
    84 structure Vars = Inst_Table(
       
    85   type key = indexname * typ
       
    86   val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.typ_ord)
       
    87 );
       
    88 
       
    89 val add_tfreesT = fold_atyps (fn TFree v => TFrees.add (v, ()) | _ => I);
       
    90 val add_tfrees = fold_types add_tfreesT;
       
    91 val add_tvarsT = fold_atyps (fn TVar v => TVars.add (v, ()) | _ => I);
       
    92 val add_tvars = fold_types add_tvarsT;
       
    93 val add_frees = fold_aterms (fn Free v => Frees.add (v, ()) | _ => I);
       
    94 val add_vars = fold_aterms (fn Var v => Vars.add (v, ()) | _ => I);
       
    95 val add_tfree_namesT = fold_atyps (fn TFree (a, _) => Symtab.insert_set a | _ => I);
       
    96 val add_tfree_names = fold_types add_tfree_namesT;
       
    97 val add_free_names = fold_aterms (fn Free (x, _) => Symtab.insert_set x | _ => I);
       
    98 
       
    99 
       
   100 (* generic mapping *)
    35 (* generic mapping *)
   101 
    36 
   102 fun map_atypsT_same f =
    37 fun map_atypsT_same f =
   103   let
    38   let
   104     fun typ (Type (a, Ts)) = Type (a, Same.map typ Ts)
    39     fun typ (Type (a, Ts)) = Type (a, Same.map typ Ts)
   126 
    61 
   127 
    62 
   128 (* generalization of fixed variables *)
    63 (* generalization of fixed variables *)
   129 
    64 
   130 fun generalizeT_same tfrees idx ty =
    65 fun generalizeT_same tfrees idx ty =
   131   if Symtab.is_empty tfrees then raise Same.SAME
    66   if Names.is_empty tfrees then raise Same.SAME
   132   else
    67   else
   133     let
    68     let
   134       fun gen (Type (a, Ts)) = Type (a, Same.map gen Ts)
    69       fun gen (Type (a, Ts)) = Type (a, Same.map gen Ts)
   135         | gen (TFree (a, S)) =
    70         | gen (TFree (a, S)) =
   136             if Symtab.defined tfrees a then TVar ((a, idx), S)
    71             if Names.defined tfrees a then TVar ((a, idx), S)
   137             else raise Same.SAME
    72             else raise Same.SAME
   138         | gen _ = raise Same.SAME;
    73         | gen _ = raise Same.SAME;
   139     in gen ty end;
    74     in gen ty end;
   140 
    75 
   141 fun generalize_same (tfrees, frees) idx tm =
    76 fun generalize_same (tfrees, frees) idx tm =
   142   if Symtab.is_empty tfrees andalso Symtab.is_empty frees then raise Same.SAME
    77   if Names.is_empty tfrees andalso Names.is_empty frees then raise Same.SAME
   143   else
    78   else
   144     let
    79     let
   145       val genT = generalizeT_same tfrees idx;
    80       val genT = generalizeT_same tfrees idx;
   146       fun gen (Free (x, T)) =
    81       fun gen (Free (x, T)) =
   147             if Symtab.defined frees x then
    82             if Names.defined frees x then
   148               Var (Name.clean_index (x, idx), Same.commit genT T)
    83               Var (Name.clean_index (x, idx), Same.commit genT T)
   149             else Free (x, genT T)
    84             else Free (x, genT T)
   150         | gen (Var (xi, T)) = Var (xi, genT T)
    85         | gen (Var (xi, T)) = Var (xi, genT T)
   151         | gen (Const (c, T)) = Const (c, genT T)
    86         | gen (Const (c, T)) = Const (c, genT T)
   152         | gen (Bound _) = raise Same.SAME
    87         | gen (Bound _) = raise Same.SAME