the Computational λ-Calculus
Ugo de’Liguoro, Riccardo Treglia
University of Turin, Italy
INRIA - Sophia Antipolis 16-12-2019
Why effects in functional languages
Semantic theories are an established field providing foundations to software analysis and verification
We have nice theories of the semantics for functional programming languages, but procedural languages commonly used for both sequential and concurrent programming have non-functional features, collectively called effects Since Algol’60 project various λ-calculi have been used as metalanguages providing semantics to programming languages in general, and a successful approach among others is based on the notion of computational monad [Moggi89, 91]
Monads have been studied and implemented mainly for typed languages; here we address the issue of modelling effects in the untyped case.
Why effects in functional languages
Semantic theories are an established field providing foundations to software analysis and verification
but procedural languages commonly used for both sequential and concurrent programming have non-functional features, collectively called effects Since Algol’60 project various λ-calculi have been used as metalanguages providing semantics to programming languages in general, and a successful approach among others is based on the notion of computational monad [Moggi89, 91]
Monads have been studied and implemented mainly for typed languages; here we address the issue of modelling effects in the untyped case.
Why effects in functional languages
Semantic theories are an established field providing foundations to software analysis and verification
We have nice theories of the semantics for functional programming languages, but procedural languages commonly used for both sequential and concurrent programming have non-functional features, collectively called effects
Since Algol’60 project various λ-calculi have been used as metalanguages providing semantics to programming languages in general, and a successful approach among others is based on the notion of computational monad [Moggi89, 91]
Monads have been studied and implemented mainly for typed languages; here we address the issue of modelling effects in the untyped case.
Why effects in functional languages
Semantic theories are an established field providing foundations to software analysis and verification
We have nice theories of the semantics for functional programming languages, but procedural languages commonly used for both sequential and concurrent programming have non-functional features, collectively called effects Since Algol’60 project various λ-calculi have been used as metalanguages providing semantics to programming languages in general, and a successful approach among others is based on the notion of computational monad [Moggi89, 91]
we address the issue of modelling effects in the untyped case.
Why effects in functional languages
Semantic theories are an established field providing foundations to software analysis and verification
We have nice theories of the semantics for functional programming languages, but procedural languages commonly used for both sequential and concurrent programming have non-functional features, collectively called effects Since Algol’60 project various λ-calculi have been used as metalanguages providing semantics to programming languages in general, and a successful approach among others is based on the notion of computational monad [Moggi89, 91]
Monads have been studied and implemented mainly for typed languages; here we address the issue of modelling effects in the untyped case.
Computational Monads (after Wadler)
A monad is a triple pT , unit, ‹q Types
D is the type ofvalues;
TD is the type ofcomputations(with effects) overD.
Operators
unitD :DÑTD (Haskell: return);
‹D,E :TD Ñ pDÑTEq ÑTE (Haskell: ąą“).
Axioms
punit d q ‹ f “ f d a ‹ unit “ a
pa ‹ f q ‹ g “ a ‹ λλ d.pf d ‹ g q
Computational Monads (after Wadler)
D
TD unitD
?
f˚“ λλ x. x ‹ f- TE f
-
pf˚˝ unit q d “ unit d ‹ f “ f d d P D
Computational Monads (after Wadler)
C D
TC unitC
?
g˚ - TD
unitD
?
f˚ -
g
-
TE f
-
TC idTC
? pf˚˝ g q˚“ λλ y . y ‹ pλλ x. g x ‹ f q
- TE idTE 6
pf˚˝ g q˚m “ m ‹ pλλ x. g x ‹ f q “ pm ‹ g q ‹ f m P TC
Moggi’s type-free λ
cMoggi considered a type-free version of his λc, obtained by type erasure:
Terms : e, e1 ::“ v | n Values : v ::“ x | λx.e
NonValues : n ::“ let x “ e in e1 | e e1
Problems:
a quite rich syntax with complex equational theory
a reduction e e1 is defined with seven rules (five about let) no clear model interpreting non values like: x x and x x x
Untyped computational λ-calculus: λ
ucWe consider Moggi’s call-by-value reflexive object:
D“DÑTD
hence there are two types,D andTD, and two kinds of terms:
Val : V , W ::“ x | λx.M (values)
Com: L, M, N ::“ unit V | M ‹ V (computations) having types:
x :D$ x :D x :D$ M :TD
$ λx .M :DÑTD “D
$ V :D
$ unit V :TD
$ M :TD $ V :D“D ÑTD
$ M ‹ V :TD
Untyped computational λ-calculus: λ
ucWe consider Moggi’s call-by-value reflexive object:
D“DÑTD
hence there are two types,D andTD, and two kinds of terms:
Val : V , W ::“ x | λx.M (values)
Com: L, M, N ::“ unit V | M ‹ V (computations) and a natural interpretation:
rr¨ssD : Val Ñ EnvDÑD rr¨ssTD: Com Ñ EnvD ÑTD where ρ P EnvD“ Var ÑD and
rrx ssDρ “ ρpxq rrunit V ssTDρ “ unitDrrV ssDρ
rrλx .MssDρ “ λλ d PD. rrMssTDρrxÞÑds rrM ‹ V ssTDρ “ rrMssTDρ ‹D,D rrV ssDρ
Reduction
Orienting monad axioms we get ÝÑ Ď Com ˆ Com as the compatible closure of:
pβcq unit V ‹ pλx.Mq ÝÑ MrV {xs (id) M ‹ λx.unit x ÝÑ M
(ass) pL ‹ λx .Mq ‹ λy .N ÝÑ L ‹ λx.pM ‹ λy .Nq for x R FV pNq where MrV {xs denotes the capture avoiding substitution of V for all free occurrences of x in M.
To have extensionality we add:
pηcq unit λx .punit x ‹ V q ÝÑ unit V if x R FV pV q
Confluence
Several critical pairs:
punit V ‹ λx Mq ‹ λy . N (ass)
- unit V ‹ λx pM ‹ λy . Nq
MrV {xs ‹ λy . N pβcq
?
”
pM ‹ λy . NqrV {x s pβcq
?
where x R FVpNq
Confluence
Several critical pairs:
pM ‹ λy Nq ‹ λx . unit x (ass)
- M ‹ λy . pN ‹ λx. unit xq
M ‹ λy . N
(id) (id)
-
Confluence
Several critical pairs:
pM ‹ λx . unit x q ‹ λy . N (ass)
- M ‹ λx. punit x ‹ λy Nq
M ‹ λy . N (id)
?
α M ‹ λx. Nrx{y s
pβcq
?
where x R FVpNq
Confluence
Theorem
If ÝÑ is either ÝÑβc or ÝÑβcηc then
M ÝÑ N˚ and M ÝÑ L˚ implies
NÝÑ M˚ 1 and LÝÑ M˚ 1 for some M1P Com.
Proof: by the parallel reduction technique.
Also the reduction relation is confluent; this has been shown by Hamana in 2018, by an implicit proof.
λ
ucversus type free λ
clet is definable:
let x “ M in N ” M ‹ λx.N No primitive functional application but
pλx .MqV ” punit V q ‹ pλx .Mq VM ” M ‹ V
MN ” M ‹ λz.pN ‹ zq for some fresh z By this we retrieve ordinary call-by-value reduction rule:
pβvq pλx .MqV ” punit V q ‹ pλx .Mq ÝÑβc MrV {xs
λ
ucversus type free λ
cThere exist the mappings
t¨uV: Values Ñ Val t¨uC: NonValues Ñ Com where e.g.
tλx .v uV“ λx .unit tv uV and tλx.nuV “ λx .tnuC tnv uC“ tnuC‹ pλz .unit tv uV‹ z q and
tnn1uC“ tnuC‹ pλz .tn1uC‹ z q for fresh z tlet x “ n in v uC“ tnuC‹ λx .unit tv uV and tlet x “ v in n1uC“ unit tv uV‹ λx .tn1uC Then putting
tnu “ tnuC tv u “ unit tv uV we have
e e1ñ teuÝÑ˚ βcηc te1u
Types and assignment system
Definition (Intersection types for values and computations)
ValType: δ ::“ α | δ Ñ τ | δ ^ δ | ωV refineD (value types) ComType: τ ::“ T δ | τ ^ τ | ωC refineTD (computation types)
Types and assignment system
Definition (Intersection types for values and computations)
ValType: δ ::“ α | δ Ñ τ | δ ^ δ | ωV refineD (value types) ComType: τ ::“ T δ | τ ^ τ | ωC refineTD (computation types)
x : D $ x : D x : D $ M : TD
$ λx .M : D Ñ TD “ D
Types and assignment system
Definition (Intersection types for values and computations)
ValType: δ ::“ α | δ Ñ τ | δ ^ δ | ωV refineD (value types) ComType: τ ::“ T δ | τ ^ τ | ωC refineTD (computation types)
x : δ P Γ (Ax) Γ $ x : δ
Γ, x : δ $ M : τ pÑ Iq Γ $ λx.M : δ Ñ τ
Types and assignment system
Definition (Intersection types for values and computations)
ValType: δ ::“ α | δ Ñ τ | δ ^ δ | ωV refineD (value types) ComType: τ ::“ T δ | τ ^ τ | ωC refineTD (computation types)
x : δ P Γ (Ax) Γ $ x : δ
Γ, x : δ $ M : τ pÑ Iq Γ $ λx.M : δ Ñ τ
$ V : D
$ unit V : TD
$ M : TD $ V : D “ D Ñ TD
$ M ‹ V : TD
Types and assignment system
Definition (Intersection types for values and computations)
ValType: δ ::“ α | δ Ñ τ | δ ^ δ | ωV refineD (value types) ComType: τ ::“ T δ | τ ^ τ | ωC refineTD (computation types)
x : δ P Γ (Ax) Γ $ x : δ
Γ, x : δ $ M : τ pÑ Iq Γ $ λx.M : δ Ñ τ
Γ $ V : δ
punit Iq Γ $ unit V : T δ
Γ $ M : T δ Γ $ V : δ Ñ τ pÑ Eq Γ $ M ‹ V : τ
where Γ, x : δ “ Γ Y tx : δu with x : δ R Γ.
Subtyping
Definition
A subtyping relation is defined as the smallest relation on types such that:
δ ďVωV ωVďVωVÑ ωC
pδ Ñ τ q ^ pδ Ñ τ1q ďVδ Ñ pτ ^ τ1q
δ1ďVδ τ ďCτ1 δ Ñ τ ďVδ1Ñ τ1 τ ďCωC T δ ^ T δ1ďCT pδ ^ δ1q
δ ďVδ1 T δ ďCT δ1 So we add pertinent rules:
Γ $ P : ω
pωq Γ $ P : σ Γ $ P : σ1 Γ $ P : σ ^ σ1
p^I q Γ $ P : σ σ ď σ1 Γ $ P : σ1
pSubq
where either P P Val , ω ” ωV, σ, σ1P ValType and ď “ ďV or P P Com, ω ” ωC, σ, σ1P ComType and ď “ ďC.
Subtyping
Definition
A subtyping relation is defined as the smallest relation on types such that:
δ ďVωV ωVďVωVÑ ωC
pδ Ñ τ q ^ pδ Ñ τ1q ďVδ Ñ pτ ^ τ1q
δ1ďVδ τ ďCτ1 δ Ñ τ ďVδ1Ñ τ1 τ ďCωC T δ ^ T δ1ďCT pδ ^ δ1q
δ ďVδ1 T δ ďCT δ1 So we add pertinent rules:
Γ $ P : ω
pωq Γ $ P : σ Γ $ P : σ1 Γ $ P : σ ^ σ1
p^I q Γ $ P : σ σ ď σ1 Γ $ P : σ1
pSubq
where either P P Val , ω ” ωV, σ, σ1P ValType and ď “ ďV or P P Com, ω ” ωC, σ, σ1P ComType and ď “ ďC.
Subject reduction and expansion
Theorem (Subject reduction)
If Γ $ M : τ and M ÝÑ N then Γ $ N : τ .
Theorem (Subject expansion)
If Γ $ M : τ and N ÝÑ M then Γ $ N : τ .
Type interpretation
Given ξ P TypeEnvD “ TypeVar Ñ 2D define
rr¨ssD: ValType ˆ TypeEnvDÑ 2D rr¨ssTD : ComType ˆ TypeEnvDÑ 2TD by
rrαssDξ “ ξpαq rrδ Ñ τ ssDξ “ td P D | @a P rrT δssTDξ . a ‹ d P rrτ ssTDξ u rrT δssTDξ “ tunit d | d P rrδssξu
rrωVssDξ “ D rrδ ^ δ1ssDξ “ rrδssDξ X rrδ1ssDξ
rrωCssTDξ “ TD rrτ ^ τ1ssTDξ “ rrτ ssTDξ X rrτ1ssTDξ
Lemma
δ ďVδ1 ñ @ξ P TypeEnvD. rrδssDξ Ď rrδ1ssDξ
τ ďCτ1 ñ @ξ P TypeEnvD. rrτ ssTDξ Ď rrτ1ssTDξ
Soundness and completeness
A model of λuc is a structure M “ pD, T , unit , ‹q such that pT , unit , ‹q is a monad and D » D Ñ TD ρ, ξ |ùMΓ if ρpxq P rrΓpxqssDξ for all x P dom pΓq
Γ |ùMV : δ (Γ |ùMM : τ ) if ρ, ξ |ùMΓ implies rrV ssDρ P rrδssDξ (rrMssTDρ P rrτ ssTDξ )
Γ |ù V : δ (Γ |ù M : τ ) if Γ |ùMV : δ (Γ |ùMM : τ ) for all M.
Theorem
Γ $ V : δ ô Γ |ù V : δ Γ $ M : τ ô Γ |ù M : τ
Convergence
Let ó Ď Com0ˆ Val0 be the smallest relation satisfying:
unit V ó V
M ó V NrV {xs ó W M ‹ λx.N ó W Define
M ó ô DV P Val0. M ó V Lemma
M ó ô DV P Val0. M ÝÑ unit V˚
Characterization by non trivial typing
Let I : TypeVar ÑPVal0be a map; then define
|δ|I Ď Val0 |τ |IĎ Com0 by induction as follows:
|α|I“ Ipαq
|δ Ñ τ |I“ tV P Val0| @M P |T δ|I. M ‹ V P |τ |Iu
|T δ|I“ tM P Com0| DV P |δ|I. M ó V u
|ωV|I“ Val0 and |ωC|I“ Com0
|δ ^ δ1|I“ |δ|IX |δ1|I and |τ ^ τ1|I “ |τ |IX |τ1|I. Lemma
Let Γ $ M : τ where Γ “ tx1: δ1, . . . , xk : δku and M P Com. For any V1, . . . , Vk P Val0 and I, if ViP |δi|I for all i “ 1, . . . , k then MrV1{x1s ¨ ¨ ¨ rVk{xks P |τ |I.
Characterization by non trivial typing
Theorem
For all M P Com0we have:
M ó ô D τ ‰CωC. $ M : τ.
Proof. If M ó then MÝÑ unit V for some V P Val˚ 0; since $ V : ωV we have
$ unit V : T pωVq ‰CωC, so $ M : τ by subject expansion.
If $ M : τ and M P Com0 then M P |τ |I and for all I; τ ‰CωCimplies τ ďCT pωVq and hence |τ |IĎ |T pωVq|I “ tN P Com0| N óu.
State monad
A statefull computation is a function:
initial state ÞÑ pcomputed value, initial stateq
State monad: pS, unitS, ‹Sq where
SD “ S Ñ D ˆ S, where say S Ď L Ñ D and L is a set of locations unitSd “ λλ s. pd, sq
a ‹Sf “ λλ s. let pd, s1q “ a s in f d s1 and suppose:
D » D Ñ pS Ñ D ˆ Sq Then
Com : M, N ::“ . . . |!`|` :“ M where
rr!`ssSDρ “ λλ s. psr`s, sq
rr` :“ MssSDρ “ rrMssSDρ ‹Sλλ d s. pd, sr` ÞÑ dsq
State monad
StateType : σ ::“ x` : δy | σ ^ σ1| ωS
ValType : δ ::“ α | δ Ñ τ | δ ^ δ1| ωV
ComType : τ ::“ σ Ñ δ ˆ σ1| τ ^ τ | ωC
Γ $ V : δ
Γ $ unit V : σ Ñ pδ ˆ σq
Γ $ M : σ Ñ pδ ˆ σ1q Γ $ V : δ Ñ σ1Ñ pδ1ˆ σ2q Γ $ M ‹ V : σ Ñ pδ1ˆ σ2q
and observe that (omitting ξ):
rrδ Ñ σ1Ñ pδ1ˆ σ2qssD “ rrpδ ˆ σ1q Ñ pδ1ˆ σ2qssD
State monad
Postulating
δ ďVδ1ñ x` : δy ďSx` : δ1y x` : δy ^ x` : δ1y ďS x` : δ ^ δ1y we can write the typing rules:
σ ďSx` : δy Γ $ !` : σ Ñ δ ˆ σ
Γ $ M : σ Ñ δ ˆ σ1 σ2“ σ1rx` : δy{x` : ys Γ $ ` :“ M : σ Ñ δ ˆ σ2
where σ1rx` : δy{x` : ys is the replacement of x` : y by x` : δy in σ1 Γ $ M : σ Ñ δ ˆ σ1 Γ $ N : σ1Ñ δ1ˆ σ2
Γ $ M; N ” M ‹ λ .N : σ Ñ δ1ˆ σ2
which is a derived rule, making it apparent that the value of M, that has type δ, is discarded, while the produced state of type σ1is passed to N.
State monad
Suppose δ ‰Vδ1:
$ V : δ
$ unit V : x` : δ1y Ñ δ ˆ x` : δ1y
$ ` :“ unit V : x` : δ1y Ñ δ ˆ x` : δy Now
rrV ssDP rrδssD and rr` :“ unit V ssSDP rrx` : δ1y Ñ δ ˆ x` : δyssSD but
rr` :“ unit V ssSDR tunitSd | d P rrδssDu
Monadic type interpretation
Definition
Type interpretations rr¨ssD and rr¨ssTD are monadic if for any d P D and a P TD:
(i) d P rrδssDξ ñ unit d P rrT δssTDξ
(ii) d P rrδ1Ñ T δssDξ & a P rrT δ1ssTDξ ñ a ‹ d P rrT δssTDξ
This definition is not inductive! in particular rrT δssTDξ depends on itself and also on rrT δ1ssTDξ for any δ1.
Monadic type interpretation
Suppose that D8“ limÐDnwhere D0is arbitrary and Dn`1“ rDnÑ TDns Then we can inductively define
rrδssDξnĎ Dn and rrτ ssTDξ nĎ TDn
so that we have Theorem
The mappings rrδssDξ8“ limÐrrδssDξn and rrτ ssTDξ 8“ limÐrrτ ssTDξ n are monadic type interpretations. In particular for any ξ P EnvD8:
rrδ Ñ τ ssDξ8“ td P D8| @d1P rrδssDξ8 d pd1q P rrτ ssTDξ 8u
rrT δssTDξ 8“ tunit d P TD8| d P rrδssDξ8u Y
ta ‹ d P TD8| Dδ1. d P rrδ1Ñ T δssDξ8 & a P rrT δ1ssTDξ 8u
Conclusion and open issues
Results
Moving from equation D “ D Ñ TD for a generic computational monad T we have defined a type assignment system that is sound and complete for a pure untyped computational λ-calculus.
The type assignment system enjoys basic properties of intersection type systems: invariance under conversion, soundness and completeness w.r.t.
standard domain semantics, computational adequacy.
Ongoing work
Is there a reasonable notion of type refinement w.r.t. generic T -types?
Is there a uniform way to instantiate the generic type system to concrete monads, preserving basic properties?
Can we extend this approach to algebras and coalgebras of a monad?
Reference
Full paper available on arXiv.org:
Ugo de’Liguoro, Riccardo Treglia
Intersection Types for the Computational lambda-Calculus, July 2019.
http://arxiv.org/abs/1907.05706