<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>A Filter Model for the State Monad (short paper)</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Dipartimento di Informatica</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Universita degli Studi di Torino</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Corso Svizzera</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Torino</string-name>
        </contrib>
      </contrib-group>
      <abstract>
        <p>We consider an untyped computational lambda calculus equipped with primitives to read and write from a global store. The calculus is modeled into a solution of a domain equation involving the state monad. We introduce an intersection type theory with subtyping such that the induced lter model is a solution of such an equation. Finally we de ne a type assignment system which is sound and complete w.r.t. any model of the calculus.</p>
      </abstract>
      <kwd-group>
        <kwd>Intersection types</kwd>
        <kwd>Computational -calculi</kwd>
        <kwd>State Monad</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>An untyped imperative</p>
      <p>-calculus and its semantics
We conceive the calculus imp as an untyped call-by-value -calculus extended
with two operators to read and write from a store. To model such side-e ects,
we choose a monadic setting.</p>
      <p>
        In Wadler's formulation [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ], a monad is a triple (T; unit; ?) where T is a type
constructor, and for all types D; E, unitD : D ! TD and ?D;E : TD (D !
TE) ! TE are such that (omitting subscripts and writing ? as an in x
operator):
(unit d) ? f = f d;
a ? unit = a;
(a ? f ) ? g = a ?
d:(f d ? g):
Instances of monads are partiality, exceptions, input/output, store, non
determinism, continuations.
      </p>
      <p>
        Following [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ], an untyped computational calculus has a model that is the solution
of the equation D = D ! TD in the category Dom of domains, namely the
call-by-value re exive object.
      </p>
      <p>Such domain equation implies that we have just two types: the type of values
D, and the type of computations TD.</p>
      <p>Since now D = D ! TD, we have:
? : TD
(D ! TD) ! TD = TD</p>
      <p>
        D ! TD
Therefore, as in [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] the syntax of the monadic calculus is
      </p>
      <p>Val : V; W ::= x j x:M</p>
      <p>
        Com : M; N ::= [V ] j M ? V
To model imp we instantiate T to a variant of the state monad de ned as
SX = S ! (X S)?, where S is a suitable space of states, and ( )? is the lifting
monad. A natural choice for S is (a subspace of) L ! D (with the order induced
by that one of D), where D is the intended domain of values. However, D is the
solution of the domain equation D = D ! SD, which is clearly circular.
To break the circularity, we de ne the mixed-variant bi-functor G : Domop
Dom ! Dom by G(X; Y ) = F X ! (Y F Y )?, where F X = L ! X,
F h = h and G(f; g)(x) = (g F g)? F f where f : X0 ! X, g : Y ! Y 0
and 2 G(X; Y ). Now it is routine to prove that G is locally continuous (see
e.g. [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ]) so that, by the inverse limit technique, we can nd the initial solution
to the domain equation D = D ! G(D; D). Now let us de ne SDX = F D !
(X F X)?, that is a functor and a monad, and we conclude:
Theorem 1. There exists a domain D such that the state monad SD is a
solution in Dom to the domain equation: D = D ! SD D. Moreover, it is initial
among all solutions to such equation.
      </p>
      <p>It remains to de ne the operators to read and write from the store, for which we
use ideas from [9{11]: get` : (D ! SDD) ! SDD and set` : D SDD ! SDD;
so the syntax of imp is completed by</p>
      <p>
        Com : M; N ::= : : : j get`( x:M ) j set`(V; M ) (` 2 L)
The additional operators are actually two families of operators, indexed over
the denumerable set of locations: get`( x:M ) reading the value V associated to
the location ` in the current state, and binding x to V in M ; set`(V; M ) which
modi es the state assigning V to `, and then proceeds as M . The operational
semantics is given in [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>Denotational Semantics. The triple (SD; unit ; ?) is a monad, where (unit d) &amp;
= (d; &amp;) and (m ? d) &amp; : let (d0; &amp;0) = m &amp; in (d d0)&amp; which is ? if m &amp; = ?, and
d 2 D = D ! SDD.</p>
      <p>The semantics a la Plotkin and Power [9{11] of get` and set`, is given by Jget`K :
(SDD)D ! SDD where Jget`K d &amp; = d (&amp; `) &amp;, and Jset`K : D SDD ! SDD where
Jset`K(d; c) &amp; = c(&amp;[` 7! d]) and c 2 SDD = S ! (D S)? and &amp;[` 7! d] is the
store sending ` to d and it is equal to &amp;, otherwise.</p>
      <p>Then we interpret values from Val in D and computations from Com in SDD
via the maps J KD : Val ! Env ! D and J KSD : Com ! Env ! SDD, where
Env = Var ! D is the set of environments interpreting term variables.
De nition 1. A</p>
      <p>imp-model is a structure D = (D; SD; J KD; J KSD) such that:
1. D is a domain s.t. D = D ! SDD, where SD is the state monad;
2. for all e 2 Env, V 2 Val and M 2 Com:</p>
      <p>JxKDe = e(x) J x:M KDe = d 2 D: JM KSDe[x 7! d]
J [V ] KSDe = unit (JV KDe) JM ? V KSDe = (JM KSDe) ? (JV KDe)
Jget`( x:M )KSDe = Jget`K(J x:M KDe) Jset`(V; M )KSDe = Jset`K(JV KDe; JM KSDe)
By unravelling de nitions and applying to an arbitrary store &amp; 2 S, the last two
clauses can be written:</p>
      <p>Jget`( x:M )KSDe &amp; = JM KSD(e[x 7! &amp;(`)]) &amp;</p>
      <p>Jset`(V; M )KSDe &amp; = JM KSDe (&amp;[` 7! JV KDe])
For M; N 2 Com, we say that the equation M = N is true in D, notation
j=D M = N , if JM KSDe = JN KSDe for all e 2 Env.</p>
      <p>Proposition 1. The following equations are true in any imp-model D:
1. [V ] ? ( x:M ) = M [V =x]
2. M ? x:[x] = M
3. (L ? x:M ) ? y:N = L ? x:(M ? y:N )
4. get`( x:M ) ? W = get`( x:(M ? W ))
5. set`(V; M ) ? W = set`(V; M ? W )
The above proposition states that the three monadic equations (parts (1) to (3))
are true in any model, while parts (4) and (5) imply that the operators set and
get are algebraic.
2</p>
      <p>The</p>
      <p>
        lter model construction
Following Abramsky [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ], domains in a suitable category can be described via
an intersection type theory. A type theory is a structure ThA = (LA; ^; A; !A)
where LA is the language of ThA, inducing the algebraic domain A (see below),
namely a set of type expressions closed under ^; !A 2 LA is a special constant,
and A is a pre-order over LA such that A !A for all 2 LA and ^ 0 is
the meet of ; 0 w.r.t. A.
      </p>
      <p>A non empty F LA is a lter of ThA if it is upward closed and closed under
intersection; let FA be the set of lters of ThA; then (FA; ) is a domain. We
seek theories ThD and ThS such that FD = [FD ! FS ! (FD FS )?].
Actually, we consider four theories, whose de nitions are mutually inductive,
derived by applying operators in the above equation. Indeed these are speci c
functors that can be represented as type theoretical constructors. We recall that
if ThA, ThB are type theories, then we may de ne for 2 LA and 2 LB the
following languages:</p>
      <p>LA?
LA B
LA!B
LS;A
::= j ^ 0 j !A?
::= j ^ 0 j !A B
::= ! j ^ 0 j !A!B
::= h` : i j ^ 0 j !S
bottom theory
product theory
arrow theory
store theory</p>
    </sec>
    <sec id="sec-2">
      <title>Then the respective theories are:</title>
      <p>ThA?
:</p>
      <p>A 0 =)</p>
      <p>A? 0
ThA B : !A B A B !A</p>
      <p>!B (
A 0</p>
      <p>B 0 =)
) ^ ( 0
A B 0</p>
      <p>0
0) A B ( ^ 0) ( ^ 0)
ThA!B : !A!B A!B !A ! !B ( ! ) ^ ( ! 0) A!B
! ( ^ 0)
0 A</p>
      <p>B 0 =)
!</p>
      <p>A!B 0 ! 0
ThS;A : h` : i ^ h` : 0i S;A h` : ^ 0i
A 0 =) h` : i S;A h` : 0i
We assume that ^ and take precedence over !, and that ! associates to
the right, so that ! ^ 0 reads as ! ( ^ 0) and 0 ! 0 ! 00 00 reads
as 0 ! ( 0 ! ( 00 00)).</p>
      <p>We obtain a solution of the domain equation in terms of domains of lters.
Theorem 2. If ThD = ThD!SD, ThS = ThS;D, ThC = Th(D SD)? , ThSD =
ThS!C are de ned by mutual induction, then FD = [FD ! FSD].
A imp-model. To show that FD is a imp-model we have to de ne the following
operators:
unit : FD ! FSD
get` : FD ! FSD</p>
      <p>? : FSD
set` : FD</p>
      <p>FD ! FSD
FSD ! FSD</p>
    </sec>
    <sec id="sec-3">
      <title>This can be done as follows:</title>
      <p>unit F F = Filtf ! 2 LSD j 2 F g
G ?F F = Filtf ! 00 00 2 LSD j 9 0; 0: ! 0 2 G &amp; 0 ! 0 ! 00
get`F (F ) = Filtf(h` : i ^ ) ! 2 LSD j ! ( ! ) 2 F g
set`F (F; G) = Filtf 0 ! 2 LSD j 9 2 F: h` : i ^ 0 ! 2 G &amp; ` 62 dom( 0)g
00 2 F g
where FiltX is the least lter including the set X.</p>
      <p>Theorem 3. (FD; SFD ; J KFD ; J KFSD ) is a imp-model.</p>
      <p>Type assignment system. A typing context is a nite set = fx1 : 1; : : : ; xn :
ng with pairwise distinct xi's and with i 2 LD for all i = 1; : : : ; n; with as
before, we set dom( ) = fx1; : : : ; xng; nally, by ; x : we mean [ fx : g for
x 62 dom( ). Type judgments are of either forms ` V : or ` M : . De ne
` 2 dom( ) if 9 6= !D: S h` : i; then the rules of the type assignment
system are listed below.</p>
      <p>; x : ` x :
` [V ] : !
; x : ` M : !
(var)</p>
      <p>(unit)
` get`( x:M) : (h` : i ^ ) !
` M : ! 0
0</p>
      <p>` V : 0 ! 0 ! 00
(get)
` T : '
` T : '0</p>
      <p>
        (^)
` T : !
See [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] for an explanation of the rules of this system.
      </p>
      <p>` T : ' ^ '0
` T : ' '</p>
      <p>'0
` T : '0
( )
De nition 2 (Type interpretation). Let TypeEnv = TypeVar ! }(D) be
the set of the interpretations of type variables , ranged over by ; then we de ne
the sets: J K D; J K S = [L ! D]; J K C = (D S)?; J K SDD by
the following inductive clauses:
635421...... JJJJJJh!'`DK^!:K='iK0=KKK (=D===),JfJfJas'Kg!nK2Sd2KS\J[SJ=jJ!'!sKS(0K`,,()KD,J2!fo=CJrKSKf'f)=;g?',2]C0j[oDg=f(Jt!(hDeKSs)DamDS )e]J?js,Kfoar(gJtn,,dK J)!SDK
J K g,
= SDD.</p>
      <p>
        The following de nition is standard in literature, see [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] Def. 17.1.3.
De nition 3 (Semantic Satis ability). Let e be a term environment and
a type environment:
1. e; j=D (x)KD for all x 2 dom ( );
      </p>
      <p>if e(x=)D2 JM : ) if
2. j=D V : ( j
3. e; j=j=VD: i(mpj=lieMsJ V: KD) ief 2 Jj=KD (VJ M: KS(D ej=2DJ MK ):; ) for all models D.
Theorem 4 (Soundness and Completeness).</p>
      <p>` V :
,
j= V :
and
` M :
The proof of soundness is by induction on the type derivation; the completeness
is proved using Theorem 3 and the fact that the interpretation of a term in the
lter model coincides with the set of types that can be assigned to it.
Final remarks. In the present work we gave a sketch of some of the issues
that can be encountered in investigating the semantics of an untyped imperative
-calculus and, albeit not in full details, outlined how a lter model for the state
monad can be derived once the domain equation de ning the speci c
call-byvalue re exive object is understood and dissected.</p>
      <p>
        Although lter models have been extensively discussed in the literature, see
e.g. [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ], to our knowledge this is the rst construction of such a model for an
imperative lambda calculus.
      </p>
      <p>
        In [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] we constructed a lter model for the pure computational -calculus, namely
without operations nor constants, where the underlying monad is generic. In
that work we highlighted that this construction requires some conditions on
interpretation of intersection types. Here such conditions are naturally satis ed
by the type theory and assignment system tailored for the state monad. An open
issue is the investigation whether there is a uniform construction of lter models
for calculi with algebraic operators over generic monads.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Abramsky</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          :
          <article-title>Domain theory in logical form</article-title>
          .
          <source>Ann. Pure Appl. Log</source>
          .
          <volume>51</volume>
          (
          <issue>1-2</issue>
          ),
          <volume>1</volume>
          {
          <fpage>77</fpage>
          (
          <year>1991</year>
          ). https://doi.org/10.1016/
          <fpage>0168</fpage>
          -
          <lpage>0072</lpage>
          (
          <issue>91</issue>
          )
          <fpage>90065</fpage>
          -T
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Barendregt</surname>
            ,
            <given-names>H.P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Dekkers</surname>
            ,
            <given-names>W.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Statman</surname>
          </string-name>
          , R.:
          <article-title>Lambda Calculus with Types. Perspectives in logic</article-title>
          , Cambridge University Press (
          <year>2013</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>de'Liguoro</surname>
            ,
            <given-names>U.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Treglia</surname>
          </string-name>
          , R.:
          <article-title>Intersection types for the computational lambdacalculus</article-title>
          . CoRR abs/
          <year>1907</year>
          .05706 (
          <year>2019</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>de'Liguoro</surname>
            ,
            <given-names>U.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Treglia</surname>
            ,
            <given-names>R.:</given-names>
          </string-name>
          <article-title>The untyped computational -calculus and its intersection type discipline</article-title>
          .
          <source>Theor. Comput. Sci</source>
          .
          <volume>846</volume>
          ,
          <issue>141</issue>
          {
          <fpage>159</fpage>
          (
          <year>2020</year>
          ). https://doi.org/10.1016/j.tcs.
          <year>2020</year>
          .
          <volume>09</volume>
          .029, https://doi.org/10.1016/j.tcs.
          <year>2020</year>
          .
          <volume>09</volume>
          .029
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>de'Liguoro</surname>
            ,
            <given-names>U.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Treglia</surname>
          </string-name>
          , R.:
          <article-title>Intersection types for a computational lambda-calculus with global state (</article-title>
          <year>2021</year>
          ), https://arxiv.org/abs/2104.01358
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Dezani-Ciancaglini</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Honsell</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Alessi</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          :
          <article-title>A complete characterization of complete intersection-type preorders</article-title>
          .
          <source>ACM Trans. Comput. Log</source>
          .
          <volume>4</volume>
          (
          <issue>1</issue>
          ),
          <volume>120</volume>
          {
          <fpage>147</fpage>
          (
          <year>2003</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Moggi</surname>
          </string-name>
          , E.:
          <article-title>Computational Lambda-calculus and Monads</article-title>
          .
          <source>Report ECS-LFCS-88- 66</source>
          , University of Edinburgh, Edinburgh, Scotland (
          <year>1988</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <surname>Pierce</surname>
            ,
            <given-names>B.C.</given-names>
          </string-name>
          :
          <article-title>Basic category theory for computer scientists</article-title>
          . Foundations of computing, MIT Press (
          <year>1991</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Plotkin</surname>
            ,
            <given-names>G.D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Power</surname>
          </string-name>
          , J.:
          <article-title>Notions of computation determine monads</article-title>
          .
          <source>In: FOSSACS 2002. Lecture Notes in Computer Science</source>
          , vol.
          <volume>2303</volume>
          , pp.
          <volume>342</volume>
          {
          <fpage>356</fpage>
          . Springer (
          <year>2002</year>
          ). https://doi.org/10.1007/3-540-45931-6 24, https://doi.org/10. 1007/3-540-45931-6\_
          <fpage>24</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>Plotkin</surname>
            ,
            <given-names>G.D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Power</surname>
          </string-name>
          , J.:
          <article-title>Algebraic operations and generic e ects</article-title>
          .
          <source>Appl. Categorical Struct</source>
          .
          <volume>11</volume>
          (
          <issue>1</issue>
          ),
          <volume>69</volume>
          {
          <fpage>94</fpage>
          (
          <year>2003</year>
          ). https://doi.org/10.1023/A:1023064908962
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <surname>Power</surname>
          </string-name>
          , J.:
          <article-title>Generic models for computational e ects</article-title>
          .
          <source>Theor. Comput. Sci</source>
          .
          <volume>364</volume>
          (
          <issue>2</issue>
          ),
          <volume>254</volume>
          {
          <fpage>269</fpage>
          (
          <year>2006</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          12.
          <string-name>
            <surname>Wadler</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          :
          <article-title>Monads for functional programming</article-title>
          .
          <source>In: Advanced Functional Programming</source>
          ,
          <source>First International Spring School on Advanced Functional Programming Techniques. Lecture Notes in Computer Science</source>
          , vol.
          <volume>925</volume>
          , pp.
          <volume>24</volume>
          {
          <fpage>52</fpage>
          . Springer (
          <year>1995</year>
          ). https://doi.org/10.1007/3-540
          <source>-59451-5 2</source>
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>