1 (* ========================================================================= *)
2 (* Abstract type of HOL types and functions for manipulating them. *)
4 (* John Harrison, University of Cambridge Computer Laboratory *)
6 (* (c) Copyright, University of Cambridge 1998 *)
7 (* (c) Copyright, John Harrison 1998-2007 *)
8 (* ========================================================================= *)
10 module type Hol_type_primitives =
11 sig type hol_type = private
13 | Tyapp of string * hol_type list
14 val types: unit -> (string*int)list
15 val get_type_arity : string -> int
16 val new_type : (string * int) -> unit
17 val mk_type: (string * hol_type list) -> hol_type
18 val mk_vartype : string -> hol_type
19 val dest_type : hol_type -> (string * hol_type list)
20 val dest_vartype : hol_type -> string
21 val is_type : hol_type -> bool
22 val is_vartype : hol_type -> bool
23 val tyvars : hol_type -> hol_type list
24 val type_subst : (hol_type * hol_type)list -> hol_type -> hol_type
27 (* ------------------------------------------------------------------------- *)
28 (* This is the implementation of those primitives. *)
29 (* ------------------------------------------------------------------------- *)
31 module Type : Hol_type_primitives = struct
33 type hol_type = Tyvar of string
34 | Tyapp of string * hol_type list
36 (* ------------------------------------------------------------------------- *)
37 (* List of current type constants with their arities. *)
39 (* Initially we just have the boolean type and the function space *)
40 (* constructor. Later on we add as primitive the type of individuals. *)
41 (* All other new types result from definitional extension. *)
42 (* ------------------------------------------------------------------------- *)
44 let the_type_constants = ref ["bool",0; "fun",2]
46 (* ------------------------------------------------------------------------- *)
47 (* Return all the defined types. *)
48 (* ------------------------------------------------------------------------- *)
50 let types() = !the_type_constants
52 (* ------------------------------------------------------------------------- *)
53 (* Lookup function for type constants. Returns arity if it succeeds. *)
54 (* ------------------------------------------------------------------------- *)
56 let get_type_arity s = assoc s (!the_type_constants)
58 (* ------------------------------------------------------------------------- *)
59 (* Declare a new type. *)
60 (* ------------------------------------------------------------------------- *)
62 let new_type(name,arity) =
63 if can get_type_arity name then
64 failwith ("new_type: type "^name^" has already been declared")
65 else the_type_constants := (name,arity)::(!the_type_constants)
67 (* ------------------------------------------------------------------------- *)
68 (* Basic type constructors. *)
69 (* ------------------------------------------------------------------------- *)
71 let mk_type(tyop,args) =
72 let arity = try get_type_arity tyop with Failure _ ->
73 failwith ("mk_type: type "^tyop^" has not been defined") in
74 if arity = length args then
76 else failwith ("mk_type: wrong number of arguments to "^tyop)
78 let mk_vartype v = Tyvar(v)
80 (* ------------------------------------------------------------------------- *)
81 (* Basic type destructors. *)
82 (* ------------------------------------------------------------------------- *)
86 (Tyapp (s,ty)) -> s,ty
87 | (Tyvar _) -> failwith "dest_type: type variable not a constructor"
91 (Tyapp(_,_)) -> failwith "dest_vartype: type constructor not a variable"
94 (* ------------------------------------------------------------------------- *)
95 (* Basic type discriminators. *)
96 (* ------------------------------------------------------------------------- *)
98 let is_type = can dest_type
100 let is_vartype = can dest_vartype
102 (* ------------------------------------------------------------------------- *)
103 (* Return the type variables in a type and in a list of types. *)
104 (* ------------------------------------------------------------------------- *)
108 (Tyapp(_,args)) -> itlist (union o tyvars) args []
109 | (Tyvar v as tv) -> [tv]
111 (* ------------------------------------------------------------------------- *)
112 (* Substitute types for type variables. *)
114 (* NB: non-variables in subst list are just ignored (a check would be *)
115 (* repeated many times), as are repetitions (first possibility is taken). *)
116 (* ------------------------------------------------------------------------- *)
118 let rec type_subst i ty =
121 let args' = qmap (type_subst i) args in
122 if args' == args then ty else Tyapp(tycon,args')
123 | _ -> rev_assocd ty i ty
127 (* ------------------------------------------------------------------------- *)
128 (* Display all the externally visible functions. *)
129 (* ------------------------------------------------------------------------- *)
133 (* ------------------------------------------------------------------------- *)
134 (* The following are common enough to deserve their own bindings. *)
135 (* ------------------------------------------------------------------------- *)
137 let bool_ty = mk_type("bool",[]);;
139 let mk_fun_ty ty1 ty2 = mk_type("fun",[ty1; ty2]);;
141 let aty = mk_vartype "A";;
143 let bty = mk_vartype "B";;