The computational -calculus was introduced by Moggi [Mog89, Mog91] as a meta-language to describe non functional effects in programming languages via an incremental approach. The basic idea is to distinguish among values of some type and computations over such values, the latter having type . Semantically is a monad, endowing with a richer structure such that operations over computations can be seen as algebras of .
The monadic approach is not just useful when building compilers modularly with respect to various kinds of effects [Mog91], to interpret languages with effects like control operators via a CPS translation [Fil94], or to write effectful programs in a purely functional language such as Haskell [Wad95], but also to reason about such programs. In this respect, typed computational lambda-calculus has been related to static program analysis and type and effect systems [BHM02], PER based relational semantics [BKHB06], and more recently co-inductive methods for reasoning about effectful programs have been investigated, leading to principles that hold for arbitrary monads and their algebras [DGL17].
Aim of our work is to investigate the monadic approach to effectful functional languages in the untyped case. This is motivated by the fact that similar, if not more elusive questions arise for effectful untyped languages as well as for typed ones; but also because the untyped setting is the natural one where studying program analysis via type assignment systems in Curry style, like in the case of intersection types, which we advocate. Indeed in working out the approach in the untyped case lays the foundation for doing the same also for typed languages, either by seeing intersection types as refinement types, or by looking at them as to the formulas of the endogenous logic of domain theoretic interpretations of types [Abr91].
It might appear nonsense to speak of monads w.r.t. an untyped calculus, as the monad interprets a type constructor both in Moggi’s and in Wadler’s formulation of the computational -calculus [Mog91, Wad95]. However, much as the untyped -calculus can be seen as a calculus with a single type, which is interpreted by a retract of its own function space in a suitable category as formerly observed by Scott [Sco80], the untyped computational -calculus has two types: the type of values and the type of computations . The type is a retract of , written , that is an appropriate space of functions from values to computations [Mog89]. Consequently we have two sorts of terms, called values and computations denoting points in and respectively, and a reduction relation among computations that turns out to be Church-Rosser and such that, if then is a model of the conversion relation induced by the reduction, and we call it a -model.
Intersection types are naturally interpreted as predicates over a -model, and indeed intersection type systems have been originally conceived to characterize strongly normalizing, weakly normalizing and solvable terms namely having head normal form. Toward developing analogous systems for the computational -calculus, we introduce an intersection type assignment system with two sorts of intersection types, namely value types ranged over by , and computation types ranged over by , whose intended meanings are subsets of and , respectively. We then define the minimal type theories and axiomatizing the preorders over value and computation types respectively, and construct a type assignment system which is a generalization of the BCD type system for the ordinary -calculus in [BCD83]. Then, the subject reduction property smoothly follows, and can be established along the lines of the analogous property of system BCD.
We are looking at BCD type system because it defines a logical semantics of -terms, whose meaning are just the sets of types that can be assigned to them, which turn out to be filters of types. Such a model, named filter model, has the structure of an algebraic lattice with countable basis. This fact is at the hearth of the proof of completeness of the system, namely that the denotation of a term belongs to the interpretation of a type in any model if and only if the type can be assigned to the term in the type system.
However, the type interpretation over a -model is much more problematic than in case of intersection types and -models. The issue is ensuring that computation types are closed under the two basic operations of the monad , that is unit and bind, which we dub monadic type interpretations. As we shall see in the technical development, the natural clauses lead to an impredicative definition, hence not inductive. To solve the problem we cannot resort to the correspondence of intersection types to compact points in and , because there is no information about the compacts of , since the monad is a parameter.
The solution we propose is to restrict type interpretation to the case of -models that are (pre-)fixed points of the functor , existing as inverse limit constructions if and therefore F are -continuous functors. What one obtains in this way is an instance of Scott’s model, which is the co-limit of a chain of approximant domains . By interpreting types as admissible subsets of the by induction over , we obtain admissible subsets of and by the very same co-limit construction.
Coming to the filter model construction, we build over the fact that such models can be seen as inverse limit domains, whose structure is determined by the type preorder, that is the type theory one considers: see in particular [DHA03] and [BDS13] 16.3. To avoid the rather inelegant shape of domain equations arising from non extensional filter models, we show here how an extensional -model can be constructed as a filter model, that is itself a limit model satisfying the domain equation . This eventually leads to the completeness theorem, of which subject expansion is a corollary. We then define a natural convergence predicate, and characterize convergent terms via their non trivial typings, and conclude that the filter model is computationally adequate.
2 The untyped computational -calculus:
Proof. The syntax of the untyped computational -calculus differs from Moggi’s original definition of monadic metalanguages, and it is closer to Wadler’s in [Wad95]. As said in the Introduction the untyped computational calculus has two kinds of terms.
Definition 2.1 (Values and computations)
The untyped computational -calculus, shortly , is a calculus of two sorts of expressions:
where ranges over a denumerable set Var of variables. We set ; and are the sets of free variables occurring in and respectively, and are defined in the obvious way.
In the above definition the sorts correspond to the two types and . By considering and setting and , we see that all are of type , and all are of type .
The reduction rules in the next definition are inspired to [Wad95] and clearly reflect the equations in Definition 2.6 in the next subsection. However Wadler defines an equational theory instead of a reduction relation, and his definition has a much richer type system, since the calculus is simply typed and there are types like or etc.
Definition 2.3 (Reduction)
The reduction relation is defined as follows:
where denotes the capture avoiding substitution of for all free occurrences of in .
The syntax in Definition 2.1 has no functional application among computations, because it would have no type, as we do not assume that is isomorphic to . However we can introduce te abbreviations:
for some fresh . By this we have ordinary call-by-value rule:
as a derived rule w.r.t. , where however, to respect the sorts in syntax, the value must be replaced by its counterpart , so that corresponds to :
The reduction is not deterministic: let , then
by and ; but also
Nonetheless the reflexive and transitive closure of is Church-Rosser, namely confluent. This can be proved e.g. by adapting the method in [Ter03].
Theorem 2.5 (Confluence)
If and then there exists such that both and .
2.1 Models of
Let be a category of domains, namely a cartesian closed subcategory of the category of posets whose objects have directed sups and morphisms are Scott continuous functions. Below denotes the set of objects of .
Definition 2.6 (Monad)
A monad over is a triple where is a map over the objects of , and are families of morphisms
such that, writing functional application as , as infix operator and omitting subscripts:
This definition of a monad, akin to that of a Kleisli triple, is the type theoretic definition by Wadler in [Wad95], at the basis of Haskell implementation of monads. We use this instead of the category theoretic definition, originally used by Moggi in [Mog91] as it is more accessible to non categorist readers. If is a monad over , we say that a -model of is a call-by-value reflexive object in (see [Mog89]).
Definition 2.7 (-model)
A -model in the category is a tuple where , is a monad over , and and are morphisms in such that . A -model is extensional if also , namely in .
In the following we just say that some is a -model, when the monad and the injection-projection pair are understood.
The definition of -model is the call-by-value generalization of that of -model, where is a retract of . Also a call-by-name notion of model is possible by considering a retract of instead. We concentrate on call-by-value as it is a more natural model of effectful functional calculi.
In case of a -model we are interested to and , which are respectively the intended meanings of and operators in the computation syntax. We deliberately overload notations and avoid subscripts when unnecessary.
Let be a -model and be the set of variable interpretations into ranged over by , then the maps
are defined by mutual induction:
where if , it is equal to otherwise.
In any -model we have and .
If then for any -model and . Therefore, if is the convertibility relation of , that is the symmetric closure of , then implies .
3 Intersection type assignment system for
Definition 3.1 (Intersection types and Type theories)
A language of intersection types is a set of expressions including a constant and closed under the intersection operator: .
An intersection type theory (shortly a type theory) is a pair where is a language of intersection types and a pre-order over such that is the top, is monotonic, idempotent and commutative, and
Definition 3.2 (Intersection types for values and computations)
Let TypeVar be a countable set of type variables, ranged over by :
Intersection types are better understood as predicates of values and computations respectively, or as refinement types of the two types of , that is, using the notation in [MZ15], in case of values, and in case of computations.
In the definition of language ValType and consequently ComType the set of TypeVar (also called atoms) is left unspecified and it is a parameter
Definition 3.3 (Type theories and )
The intersection type theories and are the least type theories such that:
Writing and for the antisymmetric closure of and respectively, we see that all the axioms but and are actually equalities.
If is such that then for some we have ; hence .
Proof. By induction over . The only non trivial case is when . From it follows that at least one of them is different than : if say then os that by induction. Finally if both and are not equated to then by induction , for some .
Definition 3.6 (Type assignment)
A basis is a finite set of typings with pairwise distinct variables , whose domain is the set . A basis determines a function from variables to types such that if , otherwise.
A judgment is an expression of either shapes: or . It is derivable if it is the conclusion of a derivation according to the rules:
where with , and the rules:
where either , , and or , , and .
In the following we write and to mean that these judgments are derivable. The next Lemma is an extension of the analogous property of BCD type system, also called Inversion Lemma in [BDS13] 14.1.
Lemma 3.7 (Generation lemma)
Assume that and , then:
Lemma 3.8 (Substitution lemma)
If and then .
Theorem 3.9 (Subject reduction)
If and then .
4 Monadic type interpretations and limit -models
To interpret value and computation types we extend the usual interpretation of intersection types over -models to -models. Let be such a model; then for we abbreviate: . Also, if and then .
Let a type variable interpretation; the maps and are type interpretations if:
The type interpretation is closed under monadic operations, shortly monadic, if satisfies:
Because of the last clause, the definition of monadic type interpretation is not inductive; in particular depends on itself and also on for any . To turn this definition into an inductive one, we make essential use of the correspondence among intersection types and the category of -algebraic lattices.
Henceforth is the category of -algebraic lattices, which is a particular category of domains (see e.g. [AJ94] and [AC98], ch. 5). Objects of are lattices whose elements are directed sup of the compact points they dominate, where is compact if whenever is directed and there is some s.t. ; moreover is countable.
Suppose that the monad is an -continuous functor over , so that the functor is such. Let where is some fixed domain, and is such that for all , is an embedding. As a consequence we have and we call the isomorphism. Also, by continuity of , we have that . If , we write for its projection to ; if then we identify with its injection into . We fix the notation for the standard notions from inverse limit construction. For all the following are injection-projection pairs:
Let . ,
For definitions see any standard text on domain theory, e.g. [AC98]. The following lemma lists some well known facts. We set the following abbreviation .
To these we add:
Let and :
If we write , similarly for when . By means of this we define a notion of approximated type interpretation such that each type turns out to denote certain well behaved subset either of or according to its kind, called admissible subset ([AJ94] sec. 2.1.6).
A predicate on (i.e. a subset of) a domain is admissible if it contains and is closed under directed suprema. We write as the set of all admissible predicates on a domain . Fix a domain and a subset ; we define the following operator:
where are the elements of bounded above by the elements of .
Our goal is to show that the type interpretations are admissible subsets of either or provided that the are such. To enforce admissibility of the we have introduced the operator.
The operator is a closure, and is an object of for all , hence it is admissible and algebraic.
Next we define the notion of approximated type interpretation, that in the limit is a monadic type interpretation (Theorem 4.10).
Let ; then we define a family of approximated type interpretations and inductively over , and then over types:
The use of in the definition of can be avoided if is admissible, for which it suffices that is admissible. We say that is admissible if all are such. Clearly if is admissible.
Let . For all , every element of the families and is an admissible predicate on and , respectively. Moreover, they are algebraic domains. In addition, every such admissible predicate is an -algebraic sublattice of and , respectively.
Fix a . Define and .
Then, for all is injection-projection pair.
Define and . By functoriality of and Lemma 4.8 they are an injection-projection pair, too. This implies that the following subsets of and do exist.
Let be admissible. Then the mappings and are monadic type interpretations; in particular:
Proof. By Definition 4.6 . Let if and only if where for all .
Similarly, if and only if where for all .
Now, by Lemma 4.2, and as seen so far . Hence, .
Now we prove the monadicity of the type interpretation. Let . In particular, , thus where . Since by definition, and since is continuous, . As built so far, . So clause (i) is satisfied.
Let and . According to (iii) in Lemma 4.3, we have . By construction and . By definition , thus . So clause (ii) is satisfied.
5 The filter model construction
Let be an inf-semilattice; a non empty is a filter of if it is upward closed and closed under finite infs; is the set of filters of . The next proposition and theorem are known from the literature, e.g. [DP90, AC98]:
If is an inf-semilattice then is an algebraic lattice, whose compact elements are the filters . Hence is -algebraic if is denumerable.
Any arises by ideal completion of taken with the restriction of the order over ; dually it is isomorphic to the filter completion of , that is ordered by , the inverse of .
Theorem 5.2 (Representation theorem)
Let ; then is an inf-semilattice and is an isomorphism in .
Let be a type theory. Elements of are the non empty subsets which are upward closed w.r.t. the preorder and such that if then . This definition, coming from [BCD83], is not the same as that of because is just a preorder, and infs do exist only in the quotient , which is indeed an inf-semilattice. The resulting partial order , however, is isomorphic to .
Let be the quotient of the pre-order , whose elements are the equivalence classes , ordered by the relation . Then is an inf-semilattice; moreover is an isomorphism in .
Proof. Proving that is well defined and is an inf-semilattice is routine. The isomorphism is given by the map .
For , the functional type theory is the least type theory such that includes all expressions of the form for and and is such that
Also is continuous if
The theory is an extended abstract type systems, shortly eats (see e.g. [AC98] ch. 3), but for the sorts of type expressions. It is continuous if it is a continuous eats.
Let be a continuous functional type theory. Then the domain is isomorphic to , namely the domain of Scott continuous functions from to .
Proof. First if and then
Then the isomorphism is given by
To prove that and it is enough to show this for compact elements, which is obtained by simple calculations.
Let be a type theory and a unary symbol; then is the least type theory such that is defined by the grammar
Clearly that we abbreviate by ; also, by Theorem 5.2, if is the theory of some then