Module Cduce_types.Types
ℂDuce type algebra
include Cduce_types__Types
Algebra
ℂDuce types can be built using to class of operation : type constructors (products, arrows, records, …) and type connectives (union, intersection, difference). Types must have two properties :
- regularity: an infinite (co-inductive) type must have finite number of distinct subtrees.
- contractivity: an infinite (co-inductive) type must crosse inifinitely many type constructors.
These invariants are enforced by the operations in this module.
Basic operations on t. Note that equality here denotes the operation on the structural equality on the internal representation of t, not the semantic notion of equivalence between ℂDuce types.
val make : unit -> Node.tmake ()creates a fresh type reference, pointing to the empty type.
val define : Node.t -> t -> unitdefine n tupdates a reference to set its content to the given type. This can be used to define recursive types :let nil = Types.atom (AtomSet.atom (AtomSet.V.mk_ascii "nil")) in let x = Types.make () in let prod = Types.times (Types.cons Types.Int.any) ilist let ilist = Types.cup nil prod in define x ilist; (* use ilist or x, they represent the same recursive type [ [Int*] ] *)
val id : Node.t -> intid nreturns the internal identifier of the reference.
Boolean connectives
val empty : tThe empty type, 𝟘.
val any : tThe top type, 𝟙
Subtyping
Subtyping operations should only be called on fully defined types (that is, not on a recursive type whose cycles haven't yet been closed with define).
val is_empty : t -> boolis_empty treturnstrueif and only iftis empty.
val non_empty : t -> boolnon_empty treturnstrueif and only iftis non empty empty. This is just a convenience function fornot(is_empty t).
val subtype : t -> t -> boolsubtype t1 t2returnstrueif and only ift1is a subtype oft2. This is an alias foris_empty (diff t2 t1).
Components of a type
A type in ℂDuce is a finite union of disjoint kinds :
- Integers
- Atoms
- Characters
- Abstract types
- Products
- XML products
- Arrows
- Records
- Absent type (representing optional record fields.)
The following modules allow one to project a particular component of a type, retrieve its dijunctive normal form (DNF) and conversely create a ℂDuce type from such a DNF. These modules also define the top type of each kind.
module type Kind = sig ... endThe components of a type
Integers
module Int : Kind with module Dnf = Cduce_types__.Bdd.VarIntervalsaccess the integer component of a type
val interval : Intervals.t -> tinterval icreates a type from an intervali.
Atoms
module Atom : Kind with module Dnf = Cduce_types__.Bdd.VarAtomSetAccess the atom component of a type
Characters
module Char : Kind with module Dnf = Cduce_types__.Bdd.VarCharSetAccess the char component of a type.
Abstract types
module Abstract : Kind with module Dnf = Cduce_types__.Bdd.VarAbstractSetAccess the abstract component of a type.
val abstract : AbstractSet.t -> tabstract acreates a type from anAbstractSet.t.
val get_abstract : t -> AbstractSet.tget_abstract ais an alias forAbstract.get a.
Products
XML products
module Xml : Kind with type Dnf.atom = Node.t * Node.t and type Dnf.t = Times.Dnf.t and type Dnf.line = Times.Dnf.line and type Dnf.dnf = Times.Dnf.dnfAccess the XML component of a type. Note that
Xml.anyis different from theAnyXmltype of ℂDuce, which is a recursive typeX where X = <_ ..>[(X|Char)*]
Records
module Rec : Kind with type Dnf.atom = bool * Node.t Ident.label_map and type Dnf.line = (bool * Node.t Ident.label_map) Cduce_types__.Bdd.line and type Dnf.dnf = (bool * Node.t Ident.label_map) Cduce_types__.Bdd.dnfval record_fields : (bool * Node.t Ident.label_map) -> trecord_fiels (b, lm)is an alias forRec.mk (Rec.Dnf.atom (b, lm)). Ifbistruethe resulting record type is open, otherwise it is closed. SeeIdent.LabelMapto build thelmargument.Example :
open Types let int = Int.any in let int_opt = cup int Absent.any in let lab = Ident.Label.mk_ascii "mylabel" in let lm = Ident.LabelMap.singleton lab int_opt in let r = record_fields (true, lm) in (* r is the record { mylabel=?Int ..} *)
val record : Ident.label -> Node.t -> trecord l ncreates an open record type with a single explicit labell.
val rec_of_list : bool -> (bool * Ident.label * t) list -> trec_of_list op fieldscreates a record type. Ifopistruethe record is open otherwise it is closed. Thefieldsargument is a list of triples(opt, lab, t). The booleanoptindicate whether the field is optionnal (whenopt = true) (thus the typetgiven for a field is expected to not be absent already).- raises [Failure]
if
fieldscontains duplicate labels.
val empty_closed_record : tThe type
{}, that is a closed record with no fields.
val empty_open_record : tThe type
{ ..}, that is an opened record with no explicit fields. This is an alias forRec.any.
Arrows
module Function : Kind with type Dnf.atom = Node.t * Node.t and type Dnf.t = Times.Dnf.t and type Dnf.line = Times.Dnf.line and type Dnf.dnf = Times.Dnf.dnfAccess the arrow component of a type.
Absent types
Absent types represent the type of optional fields. They are represented by a kind that only has two values empty ≡ false and any ≡ true.
It is the programmer's responsibility to prevent optional absent types from appearing outside of record fields.
module Absent : sig ... endAccess the absent component of a type.
type const=|Integer of Intervals.V.t|Atom of AtomSet.V.t|Char of CharSet.V.t|Pair of const * const|Xml of const * const|Record of const Ident.label_map|String of Encodings.Utf8.uindex * Encodings.Utf8.uindex * Encodings.Utf8.t * constAn auxiliary type to denote syntactic constants and build singleton types from its values.
Normalization
Type constructors may yield equivalent types that are not structurally equal. For instance, the type t = (A, B) | (A, C) may also be represented as t = (A, B|C). In general, given a union of products, its decomposition is not unique.
The following modules allow one to compute various normalized forms for the product, XML, record and arrow type constructor.
These modules also provide the typing for high level operations such as first and second projection for products, field projection and concatenation for records and function application.
module Product : sig ... endProduct normalization.
module Record : sig ... endRecord normalization.
module Arrow : sig ... endArrow normalization.
val non_constructed : tUtilities
A type that is a union of all basic types, that is, neither a product, arrow, XML or record type.
val print_witness : Stdlib.Format.formatter -> t -> unitprint_witness fmt tprints a witness for the non empty type t to the given formatter. A witness is a in most case a constant representing an inhabitant of the type, except for arrows and abstract types for which a generic type name is used.- raises Not_found
if
tis the empty type.
module Cache : sig ... endA semantic cache indexed by types. Semantic equivalence is used to check whether a type is present in the cache.
module Positive : sig ... endPositive systems and least solutions.
module Print : sig ... endPretty-printing of types.
module Sample : sig ... endGenerate a sample from a type.
module Sequence : sig ... endConvenience module to build regular expression types.
module Iter : sig ... endmodule Subst : sig ... endmodule Tallying : sig ... end