A Metalanguage for Guarded Iteration Sergey Goncharov Christoph - - PowerPoint PPT Presentation

a metalanguage for guarded iteration
SMART_READER_LITE
LIVE PREVIEW

A Metalanguage for Guarded Iteration Sergey Goncharov Christoph - - PowerPoint PPT Presentation

A Metalanguage for Guarded Iteration Sergey Goncharov Christoph Rauch Lutz Schr oder Friedrich-Alexander-Universit at Erlangen-N urnberg ICTAC 2018, October 15-19, Stellenbosch Two Flavors of Computations Domain theory


slide-1
SLIDE 1

A Metalanguage for Guarded Iteration

Sergey Goncharov Christoph Rauch Lutz Schr¨

  • der

Friedrich-Alexander-Universit¨ at Erlangen-N¨ urnberg ICTAC 2018, October 15-19, Stellenbosch

slide-2
SLIDE 2

Two Flavors of Computations

Domain theory ❼ Computations are identified with final result (if any) ❼ Programs either terminate with a value, or they diverge ❼ Extensional paradigm: ❼ ❼ ❼

1/18

slide-3
SLIDE 3

Two Flavors of Computations

Domain theory ❼ Computations are identified with final result (if any) ❼ Programs either terminate with a value, or they diverge ❼ Extensional paradigm: Process algebra ❼ Computations are processes unfolding in time ❼ Behavioural semantics, potentially disregarding final result ❼ Intensional paradigm:

:Abramsky 2014, Intensionality, Definability and Computation

1/18

slide-4
SLIDE 4

Two Flavors of Computations

Domain theory ❼ Computations are identified with final result (if any) ❼ Programs either terminate with a value, or they diverge ❼ Extensional paradigm: Process algebra ❼ Computations are processes unfolding in time ❼ Behavioural semantics, potentially disregarding final result ❼ Intensional paradigm: Here Unified semantic framework for iterative computations

:Abramsky 2014, Intensionality, Definability and Computation

1/18

slide-5
SLIDE 5

Guardedness

General idea Ensure progress or productivity in (co)recursive definitions In process algebra ❼ Recursive process specification X “ t guarded if every occurrence of X in t is under an action ❼ E.g. in CCS under bisimulation semantics: guarded recursive specifications have unique solutions [Milner, 1989] ❼ For example, P “ a.P keeps performing the action a

2/18

slide-6
SLIDE 6

Guardedness

General idea Ensure progress or productivity in (co)recursive definitions More recently: in (co)programming ❼ Guardedness analysis in Coq for corecursive definitions, proofs by corecursion: Do corecursive calls occur under constructors? [Coquand, 1994] ❼ Guarded recursion by typing/functorial guardedness [Birkedal and Møgelberg, 2013], [Milius and Litak, 2017], [Clouston, Bizjak, Grathwohl, and Birkedal, 2015] and many others Abstract guardedness Unifying notion both for guarded recursion and for guarded iteration via guarded traced monoidal categories [Goncharov and Schr¨

  • der, 2018]

2/18

slide-7
SLIDE 7

(Abstractly) Guarded co-Cartesian Categories

Inference rules f : X Ñ Y in1 f : X Ñin2 Y ` Z f : X Ñσ Z g : Y Ñσ Z rf , gs : X ` Y Ñσ Z f : X Ñin2 Y ` Z g : Y Ñσ V h : Z Ñ V rg, hs f : X Ñσ V Definition (Guarded co-Cartesian category) A co-Cartesian category C equipped with distinguished subsets HomσpX, Y q Ď HompX, Y q of partially guarded morphisms for A, B P |C|, and any summand σ : Y1 Ñ Y1 ` Y2 » Y satisfying the rules above is called guarded (f : X Ñσ Y means f P HomσpX, Y q)

3/18

slide-8
SLIDE 8

(Abstractly) Guarded co-Cartesian Categories

Inference rules f : X Ñ Y in1 f : X Ñ2 Y ` Z f : X Ñσ Z g : Y Ñσ Z rf , gs : X ` Y Ñσ Z f : X Ñ2 Y ` Z g : Y Ñσ V h : Z Ñ V rg, hs f : X Ñσ V Definition (Guarded co-Cartesian category) A co-Cartesian category C equipped with distinguished subsets HomσpX, Y q Ď HompX, Y q of partially guarded morphisms for A, B P |C|, and any summand σ : Y1 Ñ Y1 ` Y2 » Y satisfying the rules above is called guarded (f : X Ñσ Y means f P HomσpX, Y q)

3/18

slide-9
SLIDE 9

(Abstractly) Guarded Symmetric Monoidal Categories

4/18

slide-10
SLIDE 10

Monads for Computations

❼ Monads formalize generalized functions f : X Ñ TY , like nondeterministic (with T “ PX) or partial (with TX “ X ` 1): ❼ ❼

:Moggi 1991, Notions of Computation and Monads ;Plotkin and Power 2002, Notions of Computation Determine Monads

5/18

slide-11
SLIDE 11

Monads for Computations

❼ Monads formalize generalized functions f : X Ñ TY , like nondeterministic (with T “ PX) or partial (with TX “ X ` 1): ❼ T is a type constructor, plus η : X Ñ TX (unit) and pf : X Ñ TY q ÞÑ pf ‹ : TX Ñ TY q (lifting), inducing the Klesili category of T: id “ η : X Ñ TX f ˛ g “ pf : Y Ñ TZq‹ pg : X Ñ TY q In Haskell’s point-full notation: do x Ð p; f pxq “ f ‹ppq ❼

:Moggi 1991, Notions of Computation and Monads ;Plotkin and Power 2002, Notions of Computation Determine Monads

5/18

slide-12
SLIDE 12

Monads for Computations

❼ Monads formalize generalized functions f : X Ñ TY , like nondeterministic (with T “ PX) or partial (with TX “ X ` 1): ❼ T is a type constructor, plus η : X Ñ TX (unit) and pf : X Ñ TY q ÞÑ pf ‹ : TX Ñ TY q (lifting), inducing the Klesili category of T: id “ η : X Ñ TX f ˛ g “ pf : Y Ñ TZq‹ pg : X Ñ TY q In Haskell’s point-full notation: do x Ð p; f pxq “ f ‹ppq ❼ Duality of operations and effects;: e.g. for T “ P, toss “ thead, tailu p ` q “ do x Ð toss; if px “ headq then p else q. In this sense TΣ extends T with Σ-operations, e.g. for Σ “ A ˆ -

  • :
  • a. p “ do pactiona : 1 Ñ TΣ1q; p

:Moggi 1991, Notions of Computation and Monads ;Plotkin and Power 2002, Notions of Computation Determine Monads

5/18

slide-13
SLIDE 13

Abstract Guardedness on Monads

Abstract guardedeness for a monad T is a relation between Kleisi morphisms f : X Ñ TY and summands σ : Y 1 Y satisfying (trv) f : X Ñ TY pT in1q f : X Ñin2 TpY ` Zq (sum) f : X Ñσ TZ g : Y Ñσ TZ rf , gs : X ` Y Ñσ TZ (cmp) f : X Ñin2 TpY ` Zq g : Y Ñσ TV h : Z Ñ TV rg, hs‹ f : X Ñσ TV where f : X Ñσ TY , equivalently f P HomσpX, TY q, means that f and σ are in the relation

6/18

slide-14
SLIDE 14

Abstract Guardedness on Monads

A monad is guarded Elgot if it supports partial iteration operator sending each f : X Ñ2 TpY ` Xq to f : : X Ñ TY satisfying the fixpoint law f : “ rη, f :s‹f and other laws of iteration: Roughly: Semantics of while-loops Example: TX “ pX ˆ Nat‹q Y Natω, equivalently, TX is a final pX ` Nat ˆ -

  • q-coalgebra

TX contains ❼ pairs px, τq of a result x P X and a finite trace τ P Nat‹, and ❼ infinite traces π P Natω

:Bloom and ´

Esik 1993, Iteration theories: The equational logic of iterative processes

7/18

slide-15
SLIDE 15

A Monad of (In)Finite Traces

❼ The unit of TX “ pX ˆ Nat‹q Y Natω sends x to px, x yq ❼ Given f : X Ñ TY , f ‹px, τq “ # py, τ `

` τ 1q

if f pxq “ py, τ 1q, τ `

` π

if f pxq “ π, f ‹pπq “ π. ❼ f : X Ñinr pY ` Zq ˆ Nat‹ Y Natω if for every x P X, f pxq P Z ˆ Nat‹ implies f pxq P Z ˆ Nat

`

8/18

slide-16
SLIDE 16

The Metalanguage

slide-17
SLIDE 17

Metalanguages for (Guarded) Iteration: Motivation

❼ Guardedness is a fundamental notion: Just like Moggi’s computational metalanguage is a metalanguage of abstract effects, the metalanguage for guarded iteration is a metalanguage of abstract guardedness ❼ The metalangauge for guarded iteration can be used as a ‘core programming language’ for effects associated with monads. The stock of examples is growing: various process semantic domains, hybrid monads, etc.

9/18

slide-18
SLIDE 18

The Main Idea

Geron and Levy: observed that ❼ modelling iteration directly would amount to syntax like return inr . . . inr inl . . . which is like using De Bruijn indexes instead of variables ❼ they also proposed to use labels to index coproduct summands in f : X Ñ Tpř

i Xiq, so as to be able to point the branch in which to

iterate Here, we assume labels = exceptions, for they can be uniformly used in three constructs exception raising exception handling iteration raisee v handle x in p with q handleit x “ v in p

:Geron and Levy 2016, Iteration and labelled iteration

10/18

slide-19
SLIDE 19

Quick Example

h a n d l e i t e = ‹ in handle u in ( print ("think of a number") & r a i s e u ‹ ) with ( do y Ð random ( ) ; z Ð read ( ) ; i f ( y = z ) then r e t ‹ e l s e r a i s e e ‹ )

11/18

slide-20
SLIDE 20

Syntax

Types: A, B, . . . ::“ C | 0 | 1 | A ` B | A ˆ B pC P Baseq Signatures: ❼ value signature Σv of f : A Ñ B (e.g. ` : Nat ˆ Nat Ñ Nat) ❼ effect signature Σc of f : A Ñ BrCs (e.g. put : Nat Ñ 0r1s) Value and Computation Term Judgements: Γ $v v : A and ∆ | Γ $c p : A In ∆ types are tagged over tg, uu to indicate (un-)guardedness

12/18

slide-21
SLIDE 21

Some Derivation Rules

e : E g in ∆ f : A Ñ 0r1s P Σc Γ $v p : A Γ $v q : E ∆ | Γ $c f ppq & raisee q : D ∆, e : E g | Γ $c p : A ∆1 | Γ, e : E $c q : A |∆| “ |∆1| ∆ | Γ $c handle e in p with q : A e : E u in ∆ Γ $v q : E ∆ | Γ $c raisee q : D Γ $v p : E ∆, e : E g | Γ, e : E $c q : A ∆ | Γ $c handleit e “ p in q : A

13/18

slide-22
SLIDE 22

Generic Denotational Semantics

slide-23
SLIDE 23

Typing the Semantics

Types: 0 “ ∅, 1 “ 1, A ` B “ A ` B, A ˆ B “ A ˆ B. Γ “ A1 ˆ . . . ˆ An for Γ “ px1 : A1, . . . , xn : Anq ∆ “ E1 ` . . . ` Em for ∆ “ pe1 : E α1

1 , . . . , em : E αm m q

Signatures: f P HompA, Bq for f : A Ñ B P Σv f P HominrpA, TpB ` Cqq for f : A Ñ BrCs P Σc Terms: Γ $v v : A P HompΓ, Aq ∆ | Γ $c p : A P Hom! `σ∆pΓ, TpA ` ∆qq

14/18

slide-24
SLIDE 24

Operational Semantics and Adequacy

slide-25
SLIDE 25

A Monad of (In)finite Traces

Geron and Levy: elaborated the maybe monad -

  • `1 on Set as the

simplest monad for unguarded iteration. Incidentally, it is an initial Elgot monad on Set; We elaborate TX “ pX ˆ Nat‹q Y Natω as the simplest monad for properly guarded iteration on Set. ❼ The only base type is Nat ❼ Value signature contains arithmetic operations ❼ Effect signature contains only put : Nat Ñ 0r1s

:Geron and Levy 2016, Iteration and labelled iteration ;Goncharov, Rauch, and Schr¨

  • der 2015, Unguarded recursion on coinductive

resumptions

15/18

slide-26
SLIDE 26

Big-Step Operational Semantics

Values, Computations, Terminals: v, w ::“ x | ‹ | 0 | succ v | inl v | inr v | xv, wy | . . . p, q ::“ ret v | pred v | put v | raisex v | put v & raisex w | . . . t ::“ ret v, τ | raisex v, τ | π pτ P Nat‹, π P Natωq Some Rules: put v & raisex w ó raisex w, xvy v0 “ v qrv0{xs ó raisex v1, τ1 . . . qrvn´1{xs ó t, τn handleit x “ v in q ó t, τ1 `

` ¨ ¨ ¨ ` ` τn

v0 “ v qrv0{xs ó raisex v1, τ1 . . . qrvn´1{xs ó π handleit x “ p in q ó τ1 `

` ¨ ¨ ¨ ` ` τn´1 ` ` π

v0 “ v qrv0{xs ó raisex v1, τ1 qrv1{xs ó raisex v2, τ2 . . . handleit x “ p in q ó τ1 `

` τ2 ` ` ¨ ¨ ¨

16/18

slide-27
SLIDE 27

The Adequacy Theorem

Theorem (Adequacy): Let ∆ | ´ $c p : B. Then,

  • 1. If p ó ret v, τ then ∆ | ´ $c p : B “ pin1 v, τq P pB ` ∆q ˆ Nat‹
  • 2. If p ó raisex v, τ and x : E g is in ∆ then

∆ | ´ $c p : B “ pin2 inx v, τq P pB ` ∆q ˆ Nat

`

  • 3. If p ó raisex v, τ and x : E u is in ∆ then

∆ | ´ $c p : B “ pin2 inx v, τq P pB ` ∆q ˆ Nat‹

  • 4. If p ó π, then ∆ | ´ $c p : B “ π P Natω

17/18

slide-28
SLIDE 28

Conclusions & Further Work

❼ The metalanguage for guarded iteration provides an extensible platform for programming with guarded iteration ❼ More concrete monads ñ more concrete operational semantics and more adequacy theorems ❼ Further case study: a monad for hybrid computation with guardedness as progressiveness: ❼ Hoare logic for guarded iteration: computation term t φ u x Ð p t ψ u what are these?

:Goncharov, Jakob, and Neves 2018, A Semantics for Hybrid Iteration

18/18

slide-29
SLIDE 29

References I

References

Samson Abramsky. Intensionality, definability and computation. In Alexandru Baltag and Sonja Smets, editors, Johan van Benthem on Logic and Information Dynamics, pages 121–142. 2014. Lars Birkedal and Rasmus E. Møgelberg. Intensional type theory with guarded recursive types qua fixed points on universes. In Proceedings

  • f LICS, pages 213–222, 2013.

Stephen L. Bloom and Zolt´ an ´

  • Esik. Iteration theories: the equational

logic of iterative processes. Springer-Verlag New York, Inc., New York, NY, USA, 1993.

14/18

slide-30
SLIDE 30

References II

Ranald Clouston, Ales Bizjak, Hans Bugge Grathwohl, and Lars Birkedal. Programming and reasoning with guarded recursion for coinductive

  • types. pages 407–421, 2015. URL

http://dx.doi.org/10.1007/978-3-662-46678-0_26. Thierry Coquand. Infinite objects in type theory. In Henk Barendregt and Tobias Nipkow, editors, Types for Proofs and Programs, pages 62–78, Berlin, Heidelberg, 1994. Springer Berlin Heidelberg. Bram Geron and Paul Blain Levy. Iteration and labelled iteration. In Mathematical Foundations of Programming Semantics, MFPS XXXII, volume 325, pages 127 – 146, 2016. Sergey Goncharov and Lutz Schr¨

  • der. Guarded traced categories. In

Christel Baier and Ugo Dal Lago, editors, Proc. 21th International Conference on Foundations of Software Science and Computation Structures (FoSSaCS 2018). Springer, 2018.

15/18

slide-31
SLIDE 31

References III

Sergey Goncharov, Christoph Rauch, and Lutz Schr¨

  • der. Unguarded

recursion on coinductive resumptions. In Mathematical Foundations of Programming Semantics, MFPS 2015, 2015. Sergey Goncharov, Julian Jakob, and Renato Neves. A semantics for hybrid iteration. In Sven Schewe and Lijun Zhang, editors, 29th International Conference on Concurrency Theory (CONCUR 2018), 2018. Stefan Milius and Tadeusz Litak. Guard your daggers and traces: Properties of guarded (co-)recursion. Fund. Inform., 150:407–449, 2017.

  • R. Milner. Communication and concurrency. Prentice-Hall, Inc., Upper

Saddle River, NJ, USA, 1989. Eugenio Moggi. Notions of computation and monads. Inf. Comput., 93: 55–92, 1991.

16/18

slide-32
SLIDE 32

References IV

Gordon Plotkin and John Power. Notions of computation determine

  • monads. In FoSSaCS’02, volume 2303, pages 342–356, 2002.

17/18

slide-33
SLIDE 33

A Monad of (In)Finite Traces (with Iteration)

❼ The unit of TX “ pX ˆ Nat‹q Y Natω sends x to px, x yq ❼ Given f : X Ñ TY , f ‹px, τq “ # py, τ `

` τ 1q

if f pxq “ py, τ 1q, τ `

` π

if f pxq “ π, f ‹pπq “ π. ❼ f : X Ñinr pY ` Zq ˆ Nat‹ Y Natω if for every x P X, f pxq P Z ˆ Nat‹ implies f pxq P Z ˆ Nat

`

❼ Given f : X Ñinr TpY ` Xq “ pY ` Xq ˆ Nat‹ Y Natω, f :pxq “ $ ’ ’ & ’ ’ % py, τ1 `

` ¨ ¨ ¨ ` ` τnq

if f pxq “ pin2 x1, τ1q, . . . , f pxnq “ pin1 y, τnq, τ1 `

` ¨ ¨ ¨ ` ` τn´1 ` ` π

if f pxq “ pin2 x1, τ1q, . . . , f pxnq “ π, τ1 `

` τ2 ` ` ¨ ¨ ¨

if f pxq “ pin2 x1, τ1q, f px1q “ pin1 x2, τ2q, . . .

18/18