diff --git a/lib/Makefile b/lib/Makefile index fba4c4f..214a79f 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,7 +1,7 @@ OCAMLMAKEFILE = ../OCamlMakefile ANNOTATE = yes -SOURCES = value.ml p4_value.ml pa_value.ml type.ml p4_type.ml pa_type.ml +SOURCES = value.ml{,i} p4_value.ml pa_value.ml type.ml{,i} p4_type.ml pa_type.ml PACKS = type-conv RESULT = pa_dyntype USE_CAMLP4 = yes diff --git a/lib/type.ml b/lib/type.ml index 4e781c3..7139b4d 100644 --- a/lib/type.ml +++ b/lib/type.ml @@ -1,5 +1,5 @@ (* - * Copyright (c) 2009 Thomas Gazagnaire + * Copyright (c) 2009-2010 Thomas Gazagnaire * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -177,8 +177,6 @@ let is_subtype_of (t1:t) (t2:t) = let (<:) = is_subtype_of -exception Subtype_error of string * string - let index_par c s = let res = ref None in let par = ref 0 in diff --git a/lib/type.mli b/lib/type.mli new file mode 100644 index 0000000..7ab28d8 --- /dev/null +++ b/lib/type.mli @@ -0,0 +1,77 @@ +(* + * Copyright (c) 2009-2010 Thomas Gazagnaire + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *) + +(** Dynamic types *) +type t = + | Unit (** unit *) + | Bool (** booleans *) + | Float (** floating-point numbers *) + | Char (** characters *) + | String (** strings *) + | Int of int option (** integer type of a given size (as 31-,32- or 64-bits); [Int None] is for bigints *) + | Enum of t (** collection of stuff of the same type (as lists or arrays) *) + | Tuple of t list (** Cartesian product *) + | Dict of (string * [ `RO | `RW ] * t) list (** record or object type; [`RW] stands for mutable fields *) + | Sum of (string * t list) list (** variant type *) + | Option of t (** option type *) + | Rec of string * t (** recursive type *) + | Var of string (** recursive fix-point *) + | Arrow of t * t (** arrow type *) + | Ext of string * t (** type variable *) + + +(** {2 Utility functions} *) + +(** [is_mutable t] checks whether [t] contains a mutable field *) +val is_mutable : t -> bool + +(** [free_vars t] returns all the free variables of type [t]. + If [t] is unfolded (as it should be when calling [type_of_t], + this call should return an empty list. *) +val free_vars : t -> string list + +(** [foreigns t] returns all the type variables appearing in [t]. *) +val foreigns : t -> string list + +(** [unroll env t] replaces every type appearing in [t] by its type value defined in [env]. *) +val unroll : (string * t) list -> t -> t + + +(** {2 Sub-typing} *) + +(** [is_subtype_of s t] checks whether [s] is a sub-type of [t]. Sub-typing relation is based on + naming. Basically, [s] is a sub-type of [t] if (i) named attributes have either compatible types + (ii) or some fields/methods defined in [t] do not appear in [s]. *) +val is_subtype_of : t -> t -> bool + +(** [s <: t] is a short-cut for [is_subtype_of s t] *) +val ( <: ) : t -> t -> bool + +(** Returns the more recent failing sub-type relation tested by [(<:)] or [is_subtype_of] *) +val string_of_last_type_error : unit -> string + +(** {2 Pretty-printing} *) + +(** [to_string t] pretty-prints the type [t] *) +val to_string : t -> string + + +(** Exception that may be raised by [!of_string] *) +exception Parse_error of string + +(** [of_string str] returns the type [t] corresponding to the pretty-printed string [str]. Raises [!Parse_error] + if is not a valid string *) +val of_string : string -> t diff --git a/lib/value.ml b/lib/value.ml index c169345..694fd14 100644 --- a/lib/value.ml +++ b/lib/value.ml @@ -1,5 +1,5 @@ (* - * Copyright (c) 2009 Thomas Gazagnaire + * Copyright (c) 2009-2010 Thomas Gazagnaire * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,9 +30,9 @@ type t = | Sum of string * t list | Null | Value of t - | Var of (string * int64) - | Rec of (string * int64) * t | Arrow of string + | Rec of (string * int64) * t + | Var of (string * int64) | Ext of (string * int64) * t (* If there are still some Var v, then the type is recursive for the type v *) diff --git a/lib/value.mli b/lib/value.mli new file mode 100644 index 0000000..a994fdc --- /dev/null +++ b/lib/value.mli @@ -0,0 +1,52 @@ +(* + * Copyright (c) 2009-2010 Thomas Gazagnaire + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *) + +(** Dynamic values *) +type t = + | Unit (** unit *) + | Int of int64 (** integers *) + | Bool of bool (** booleans *) + | Float of float (** floating-points numbers *) + | String of string (** strings *) + | Enum of t list (** collection of values of the same type (as lists or arrays) *) + | Tuple of t list (** Cartesian product *) + | Dict of (string * t) list (** records or objects *) + | Sum of string * t list (** variants *) + | Null (** empty value for option type *) + | Value of t (** values for option type *) + | Arrow of string (** arrows; value is marshaled using {!Marshal.to_string} *) + | Rec of (string * int64) * t (** Recursive value (i.e. cyclic values) *) + | Var of (string * int64) (** Fix-point variable for recursive value *) + | Ext of (string * int64) * t (** values for type variables *) + +(** {2 Utility functions} *) + +(** [free_vars v] returns the free variables inside [v]. If [v] is obtained using [value_of_t] then this + list should be empty *) +val free_vars : t -> (string * int64) list + +(** Checks whether two values are equal (this looks for equivalence modulo eta-conversion on variable indices) *) +val equal : t -> t -> bool + +(** [to_string v] pretty-prints the value [v] *) +val to_string : t -> string + +(** Exception raised by {!of_string} *) +exception Parse_error of string + +(** [of_string str] returns the value which had been pretty-printed to [str]. Raises {!Parse_error} if [str] has + not a valid format. *) +val of_string : string -> t