the w-calculus: a synchronous framework for the verified

12
The W-calculus: A Synchronous Framework for the Veri๏ฌed Modelling of Digital Signal Processing Algorithms Emilio Jesรบs Gallego Arias ร‰quipe 2 , Inria Paris Paris, France [email protected] Pierre Jouvelot MINES ParisTech, PSL University Paris, France [email protected] Sylvain Ribstein France [email protected] Dorian Desblancs ร‰cole normale supรฉrieure Paris-Saclay France [email protected] Abstract We introduce the W-calculus, an extension of the call-by- value ฮป-calculus with synchronous semantics, designed to be ๏ฌ‚exible enough to capture di๏ฌ€erent implementation forms of Digital Signal Processing algorithms, while permitting a direct embedding into the Coq proof assistant for mecha- nized formal veri๏ฌcation. In particular, we are interested in the di๏ฌ€erent implementations of classical DSP algorithms such as audio ๏ฌlters and resonators, and their associated high-level properties such as Linear Time-invariance. We describe the syntax and denotational semantics of the W-calculus, providing a Coq implementation. As a ๏ฌrst ap- plication of the mechanized semantics, we prove that every program expressed in a restricted syntactic subset of W is linear time-invariant, by means of a characterization of the property using logical relations. This ๏ฌrst semantics, while convenient for mechanized reasoning, is still not useful in practice as it requires re-computation of previous steps. To improve on that, we develop an imperative version of the se- mantics that avoids recomputation of prior stream states. We empirically evaluate the performance of the imperative se- mantics using a staged interpreter written in OCaml, which, for an input program in W , produces a specialized OCaml program, which is then fed to the optimizing OCaml com- piler. The approach provides a convenient path from the high-level semantical description to low-level e๏ฌƒcient code. Publication rights licensed to ACM. ACM acknowledges that this contribu- tion was authored or co-authored by an employee, contractor or a๏ฌƒliate of a national government. As such, the Government retains a nonexclusive, royalty-free right to publish or reproduce this article, or to allow others to do so, for Government purposes only. FARM โ€™21, August 27, 2021, Virtual, Republic of Korea ยฉ 2021 Copyright held by the owner/author(s). Publication rights licensed to ACM. ACM ISBN 978-1-4503-8613-5/21/08. . . $15.00 hps://doi.org/10.1145/3471872.3472970 CCS Concepts: โ€ข Computer systems organization โ†’ Re- al-time languages; โ€ข Software and its engineering โ†’ Formal software veri๏ฌcation. Keywords: Digital Signal Processing, Programming Langua- ge Semantics, Synchronous Programming, Formal Veri๏ฌca- tion, Linear Time-invariance ACM Reference Format: Emilio Jesรบs Gallego Arias, Pierre Jouvelot, Sylvain Ribstein, and Do- rian Desblancs. 2021. The W-calculus: A Synchronous Framework for the Veri๏ฌed Modelling of Digital Signal Processing Algorithms. In Proceedings of the 9th ACM SIGPLAN International Workshop on Functional Art, Music, Modelling, and Design (FARM โ€™21), August 27, 2021, Virtual, Republic of Korea. ACM, New York, NY, USA, 12 pages. hps://doi.org/10.1145/3471872.3472970 1 Introduction Real-time Digital Signal Processing (DSP) lies at the frontier between the physical reality and the digital world. Mobile applications, data acquisition, wireless radio,...: real-time DSP is pervasive and used in every digital device. One key domain where DSP thrives is music, where all digital audio formats can be interpreted as clocked โ€œstreamsโ€ of discrete โ€œsamplesโ€. which can be tweaked and processed via programming. The realm of computer music has thus been a steady provider of music-oriented DSP programming languages that o๏ฌ€er a wide variety of approaches to handle domain-speci๏ฌc applications. DSP programming is however notoriously known for pos- ing di๏ฌƒcult challenges. For instance, crucial properties are often not preserved under composition, making modular rea- soning di๏ฌƒcult; stream, bu๏ฌ€er, timing, and memory handling is costly and error prone if done manually. There is a wide spectrum of research trying to provide de๏ฌnitive answers to all or some of the previous problems. In particular, some domain-speci๏ฌc languages (DSL) for DSP aim to provide more convenient programming models, allowing programmers to produce code closer to the mathematical 35

Upload: others

Post on 13-Jan-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

The W-calculus:

A Synchronous Framework for the Verified Modelling

of Digital Signal Processing Algorithms

Emilio Jesรบs Gallego Ariasร‰quipe ๐œ‹๐‘Ÿ 2, Inria Paris

Paris, [email protected]

Pierre JouvelotMINES ParisTech, PSL University

Paris, [email protected]

Sylvain RibsteinFrance

[email protected]

Dorian Desblancsร‰cole normale supรฉrieure Paris-Saclay

[email protected]

Abstract

We introduce theW-calculus, an extension of the call-by-value ฮป-calculus with synchronous semantics, designed to beflexible enough to capture different implementation formsof Digital Signal Processing algorithms, while permitting adirect embedding into the Coq proof assistant for mecha-nized formal verification. In particular, we are interested inthe different implementations of classical DSP algorithmssuch as audio filters and resonators, and their associatedhigh-level properties such as Linear Time-invariance.

We describe the syntax and denotational semantics of theW-calculus, providing a Coq implementation. As a first ap-plication of the mechanized semantics, we prove that everyprogram expressed in a restricted syntactic subset ofW islinear time-invariant, by means of a characterization of theproperty using logical relations. This first semantics, whileconvenient for mechanized reasoning, is still not useful inpractice as it requires re-computation of previous steps. Toimprove on that, we develop an imperative version of the se-mantics that avoids recomputation of prior stream states. Weempirically evaluate the performance of the imperative se-mantics using a staged interpreter written in OCaml, which,for an input program inW , produces a specialized OCamlprogram, which is then fed to the optimizing OCaml com-piler. The approach provides a convenient path from thehigh-level semantical description to low-level efficient code.

Publication rights licensed to ACM. ACM acknowledges that this contribu-tion was authored or co-authored by an employee, contractor or affiliate ofa national government. As such, the Government retains a nonexclusive,royalty-free right to publish or reproduce this article, or to allow others todo so, for Government purposes only.FARM โ€™21, August 27, 2021, Virtual, Republic of Koreaยฉ 2021 Copyright held by the owner/author(s). Publication rights licensedto ACM.ACM ISBN 978-1-4503-8613-5/21/08. . . $15.00https://doi.org/10.1145/3471872.3472970

CCSConcepts: โ€ขComputer systems organizationโ†’Re-

al-time languages; โ€ข Software and its engineering โ†’Formal software verification.

Keywords: Digital Signal Processing, Programming Langua-ge Semantics, Synchronous Programming, Formal Verifica-tion, Linear Time-invarianceACM Reference Format:

Emilio Jesรบs GallegoArias, Pierre Jouvelot, Sylvain Ribstein, andDo-rian Desblancs. 2021. TheW-calculus: A Synchronous Frameworkfor the Verified Modelling of Digital Signal Processing Algorithms.In Proceedings of the 9th ACM SIGPLAN International Workshop onFunctional Art, Music, Modelling, and Design (FARM โ€™21), August 27,2021, Virtual, Republic of Korea. ACM, New York, NY, USA, 12 pages.https://doi.org/10.1145/3471872.3472970

1 Introduction

Real-time Digital Signal Processing (DSP) lies at the frontierbetween the physical reality and the digital world. Mobileapplications, data acquisition, wireless radio,...: real-timeDSP is pervasive and used in every digital device.One key domain where DSP thrives is music, where all

digital audio formats can be interpreted as clocked โ€œstreamsโ€of discrete โ€œsamplesโ€. which can be tweaked and processedvia programming. The realm of computer music has thusbeen a steady provider of music-oriented DSP programminglanguages that offer a wide variety of approaches to handledomain-specific applications.

DSP programming is however notoriously known for pos-ing difficult challenges. For instance, crucial properties areoften not preserved under composition, making modular rea-soning difficult; stream, buffer, timing, and memory handlingis costly and error prone if done manually.There is a wide spectrum of research trying to provide

definitive answers to all or some of the previous problems. Inparticular, some domain-specific languages (DSL) for DSP aimto provide more convenient programming models, allowingprogrammers to produce code closer to the mathematical

35

FARM โ€™21, August 27, 2021, Virtual, Republic of Korea E.J. Gallego Arias, P. Jouvelot, S. Ribstein, and D. Desblancs

specification of the process they intend to implement, whilecumbersome or repetitive details are taken care of by thetoolchain. On the other side, program verification techniquesaim to support rigorous code analysis, pointing out possibledefects or problems, and constructing correctness certificatesthat can provide guarantees in high-assurance environments.

However, practical, provably-correct development of DSPsystems is still an open problem: the use โ€” due to efficiencyand compatibility โ€” of low-level implementation languagesdoes not mix well with the mathematical nature of the spec-ifications; existing state-of-the-art verification techniqueseither incur an insurmountable overheard, or are severelylimited in what they can prove.

1.1 Design Goals

The core goal of this work is to provide a unified core mech-anism to bridge performance and verification concerns forDSP programming.With the introduction of the W-calculus, we start an

experiment in DSL design with a triple objective: a) to havea good basis for a declarative DSP language amenable tostate-of-the-art interactive verification, able to handle com-plex properties, such as linearity, bound properties, or filterequivalence; b) to ensure the language can accommodatethe performance requirements of real-time processing, inparticular in terms of memory allocation; and c) to providea formal basis upon which to develop a full-fledged usablefront-end and compiler, amenable to use by DSP experts.We have been careful to ensure that the language re-

mains low-level enough to distinguish among different im-plementation strategies, such as the different forms for filters,which are extremely important when reasoning about nu-merical properties. While we donโ€™t address the verification offloating-point numerical properties of filters in this paper, itis important that our semantics remain compatible with thisfuture line of work; thus approaches to program interpreta-tion that may involve non-stable numerical transformationshave been ruled out.

1.2 Key Contributions and Structure of the Paper

We present the work in two main parts.1. Section 2 describes theW-calculus, including syntax,

design discussion, examples, and formal denotationalsemantics, defined using the Coq integrative proofassistant. Section 2.4 develops a concrete use case ofthe mechanized semantics: a proof that every well-typed program in theW-calculus is a linear function,in the sense of Linear Time-Invariant (LTI) systems.

2. Section 3 introduces an imperative semantics for theW-calculus programs, provided these programs per-form only bounded accesses on streamsโ€™ past values.This allows for buffers to be statically allocated. We

implement the imperative semantics as a staged inter-preter, which produces low-level, efficient code. Ex-perimental run-time performance evaluation is donecomparing the efficiency of the generated code versushand-written versions of the same programs.

We conclude the paper with discussion on related (Sec-tion 4) and future (Section 5) work.The source code for programs and proofs referenced in

this paper can be found at https://github.com/ejgallego/mini-

wagner-coq/.

1.3 Mathematical Preliminaries

We assume the reader to be familiar with basic functionalprogramming notation and typing judgments. Unless oth-erwise specified, we use as our mathematical and logicaluniverse Coqโ€™s type theory, and in particular the mathemati-cal objects such as matrices and number structures providedby the Mathematical Components library. While we do useLATEX-improved notation in the paper, all the definitions havea direct correspondence with their Coq counterpart. In thesecond part of the paper, we will assume familiarity withOCamlโ€™s syntax.In particular, we rely on the definition of a numeric type

R, or R, which in our case is assumed to be an integral do-main (basically a non-zero commutative ring), n-ary tuplesof elements, written as n.-tuple A for elements of type A,with elements written using list-like notation [:: e1, ...,en], and one-column matrices of dimension ๐‘Ž, written asโ€™cV[R]_a. All these types are already equipped with theircorresponding operations, such as nth for accessing the n-thelement of a tuple or list.

On the DSP side, we mainly follow the conventions in [54].In this setting, streams are time-indexed functions, usuallyreturning samples; negative-time access is assumed to returnzero, so one can write ๐‘ฆ (๐‘›) = ๐‘ฅ1 (๐‘›โˆ’1) +๐‘ฅ2 (๐‘›โˆ’2) to define adelay ๐‘ฆ that takes two streams ๐‘ฅ1 and ๐‘ฅ2 and delays them bydifferent amounts, 1 and 2, before mixing them. We also usea shift operator for streams defined as ๐‘ฅ |๐‘˜ (๐‘›) = ๐‘ฅ (๐‘›โˆ’๐‘˜). It isalso common to write ๐‘ฅ โ€ฒ for ๐‘ฅ โ€ฒ(๐‘›) = ๐‘ฅ (๐‘›โˆ’1), that is to say, aone-sample delay. We restrict ourselves to causal definitions,that is to say, filters cannot refer to values in the future orintroduce ill-defined definitional loops. In this setting, thedefinition of a linear time-invariant filter ๐‘“ is such that

๐‘“ (๐‘ฅ |๐‘˜ ) = ๐‘“ (๐‘ฅ)|๐‘˜ and๐‘“ (๐‘ (๐‘ฅ1 + ๐‘ฅ2)) = ๐‘ ๐‘“ (๐‘ฅ1) + ๐‘ ๐‘“ (๐‘ฅ2).

2 TheW-calculus

2.1 Syntax

The syntax of W expressions ๐‘’, ๐‘“ and types ๐œ is given inFigure 1. Given a base sample type R,W โ€™s types are eithern-tuples or functions from one tuple to another. For example,a stereo-to-mono program will have type R2 โ†’ R1, etc. Wewrite R for R1 when it is clear from the context.

36

TheW-calculus: A Synchronous Framework for the Verified Modelling of . . . FARM โ€™21, August 27, 2021, Virtual, Republic of Korea

types expressions

๐œ ::= R๐‘Ž ๐‘Ž โˆˆ N| R๐‘Ž โ†’ R๐‘

๐‘Ž, ๐‘ โˆˆ N

๐‘’, ๐‘“ ::= ๐‘ฅ๐‘˜ ๐‘˜ โˆˆ N๐‘ฅ โˆˆ V

| _๐‘ฅ .๐‘’

| ๐‘“ ๐‘’

| ๐‘ โˆ— ๐‘’ ๐‘ โˆˆ R| ๐‘’1 + ๐‘’2| ๐œ‹๐‘– (๐‘’) ๐‘– โˆˆ N, ๐‘– > 0| (๐‘’1, ๐‘’2)| feed ๐‘ฅ .๐‘’

Figure 1. Syntax of W expressions and types

Expressions, which denote discrete โ€œstreamsโ€ of quantifiedsamples, are standard, except for two cases: variables andfeedback. A variable ๐‘ฅ ๐‘— denotes ๐‘ฅ โ€™s value at time step ๐‘› โˆ’ ๐‘— ,where ๐‘› is the current time step; note that we write ๐‘ฅ for๐‘ฅ0 when it is clear from the context, and let ๐‘ฅ = ๐‘’1 in ๐‘’2for (_๐‘ฅ .๐‘’2) ๐‘’1. The second interesting case is the feedbackexpression feed ๐‘ฅ .๐‘’ , which implements causal self-referentialexpressions. For example, assuming the addition to W ofstraightforward syntactic extensions such as infix operators,constants and the like, feed ๐‘ฅ .๐‘ฅ + 1 will implement a counter,with value ๐‘›, where ๐‘› is again the global time. Referencesto ๐‘ฅ inside ๐‘’ are always assumed to start in the previoustime step, to avoid causality problems; thus the previousexample can be read in mathematical form (not W โ€™s syntax)as ๐‘ฅ๐‘›+1 = ๐‘ฅ๐‘› + 1, assuming ๐‘ฅ at time step 0 or less is alwayszero, for any ๐‘ฅ .Typing is also standard in our system, with rules shown

in Figure 2. Note that products are the concatenated tuplesof their projections, instead of the usual structural approach.

2.2 Examples

This calculus, while simple, can already be used to implementsome core DSP primitives, as shown in Figure 3.

The examples have been taken from [55], and show a typ-ical second-order infinite impulse response filter, in its twoimplementation forms; note howW does actually distinguishthese two filters which, modulo floating-point arithmetic,are extensionally equivalent. We use the dot product ยฎ๐‘ ยท ๐‘ฅ asan abbreviation for ๐‘0 ร— ๐‘ฅ0 + ... + ๐‘๐‘Žโˆ’1 ร— ๐‘ฅ๐‘Žโˆ’1, assuming ยฎ๐‘ isthe constant tuple [๐‘0; ...;๐‘๐‘Žโˆ’1] (in mathematical notation).

The third example is awaveguide resonator, a simple circuitimplementing the wave equation. We write the example firstusing a notation natural to the DSP expert, which we thendesugar to the correspondingW expression, using a feedbackover a single variable of the product.

ฮ“ ๐‘ฅ = R๐‘Ž

ฮ“ โŠข ๐‘ฅ๐‘˜ :R๐‘ŽVar

ฮ“, ๐‘ฅ : R๐‘Ž โŠข ๐‘’ :R๐‘ฮ“ โŠข _๐‘ฅ.๐‘’ :R๐‘Ž โ†’ R๐‘

Lam

ฮ“ โŠข ๐‘“ :R๐‘Ž โ†’ R๐‘ ฮ“ โŠข ๐‘’ :R๐‘Žฮ“ โŠข ๐‘“ ๐‘’ :R๐‘

App

๐‘ โˆˆ R ฮ“ โŠข ๐‘’ :R๐‘Žฮ“ โŠข ๐‘ โˆ— ๐‘’ :R๐‘Ž

Scale

ฮ“ โŠข ๐‘’1 :R๐‘Ž ฮ“ โŠข ๐‘’2 :R๐‘Žฮ“ โŠข ๐‘’1 + ๐‘’2 :R๐‘Ž

Add

ฮ“ โŠข ๐‘’ :R๐‘Ž 1 โ‰ค ๐‘– โ‰ค ๐‘Ž

ฮ“ โŠข ๐œ‹๐‘– (๐‘’) :R1Proj

ฮ“ โŠข ๐‘’1 :R๐‘Ž ฮ“ โŠข ๐‘’2 :R๐‘ฮ“ โŠข (๐‘’1, ๐‘’2) :R๐‘Ž+๐‘

Prod

ฮ“, ๐‘ฅ : R๐‘Ž โŠข ๐‘’ :R๐‘Žฮ“ โŠข feed ๐‘ฅ .๐‘’ :R๐‘Ž

Feed

Figure 2. W typing rules

2.3 Semantics

We present the semantics forW programs as time-indexedfunctions mapping syntactic W objects to objects in themathematical universe of Coq. Our use of indexing allowsfor by-construction causal semantical definition of streams,while relying only on induction (no co-induction on infinitestreams).The interpretation โŸฆ๐œโŸง๐‘˜

Tfor a type ๐œ at time step ๐‘˜ is de-

fined in Figure 4. The reader can observe that the index ๐‘˜is only relevant for functions, but, indeed, here is the coreidea of our interpretation for stream transformers: a streamtransformer from ๐œ โ†’ ๐œŽ at time ๐‘˜ is a function that will take๐‘˜ + 1 values of the input ๐œ , for the past ๐‘˜ steps plus the actualvalue, producing the value for the current time step. Notethat, in this model, functions at time ๐‘˜ can access all pastvalues of their arguments, without a bound.

The semantics for well-typed expressions is defined inFigure 5. An interpretation function up to time ๐‘›, I{๐‘›} , for awell-typed expression ฮ“ โŠข ๐‘’ :๐œ will take a bounded time step๐‘˜ < ๐‘›, a well-formed environment for time step ๐‘˜ , env ฮ“ ๐‘˜ ,and will produce an element of the interpretation of its type๐œ . Environments are defined as heterogeneous lists, mappingeach typed variable to its history of values. Recall that in ourcontext, values in environments cannot be functional ones.The initial interpretation I{0} will map every expression tothe corresponding zero value for the type.

37

FARM โ€™21, August 27, 2021, Virtual, Republic of Korea E.J. Gallego Arias, P. Jouvelot, S. Ribstein, and D. Desblancs

Second-order recursive filter, DF-I

df1 โ‰ก _๐‘ฅ. feed ๐‘ฆ. ยฎ๐‘ ยท ๐‘ฅ + ยฎ๐‘Ž ยท ๐‘ฆยฎ๐‘Ž ยท ๐‘ฆ = โŸจ๐‘Ž0, ๐‘Ž1 โŸฉ ยท โŸจ๐‘ฆ0, ๐‘ฆ1 โŸฉ ยฎ๐‘ = โŸจ๐‘0, ๐‘1, ๐‘2 โŸฉ

Second-order recursive filter, DF-II

df2 โ‰ก _๐‘ฅ. let ๐‘ฃ = feed ๐‘ฃ . ๐‘ฅ + ยฎ๐‘Ž ยท ๐‘ฃ in ยฎ๐‘ ยท ๐‘ฃ

Waveguide resonator

feed ๐‘ฅ = ๐ถ ยท (๐บ ยท ๐‘ฅ โ€ฒ + ๐‘ฆ โ€ฒ) โˆ’ ๐‘ฆ โ€ฒ๐‘ฆ = ๐ถ ยท (๐บ ยท ๐‘ฅ โ€ฒ + ๐‘ฆ โ€ฒ) +๐บ ยท ๐‘ฅ โ€ฒ

wgr = feed ๐‘ง.let ๐‘ฅ = ๐œ‹1 (๐‘ง) in let ๐‘ฆ = ๐œ‹2 (๐‘ง) in(๐ถ ยท (๐บ ยท ๐‘ฅ + ๐‘ฆ) โˆ’ ๐‘ฆ,๐ถ ยท (๐บ ยท ๐‘ฅ + ๐‘ฆ) +๐บ ยท ๐‘ฅ)

Figure 3. Example W programs

โŸฆR๐‘ŽโŸง๐‘˜Tโ‰œ tuple๐‘Ž R

โŸฆR๐‘Ž โ†’ R๐‘โŸง๐‘˜Tโ‰œ tuple๐‘˜+1 โŸฆR๐‘ŽโŸงT โ†’ โŸฆR๐‘โŸงT

Definition tyI k t : Type :=match t with

| tpair a โ‡’ 'cV[R]_a| tfun a b โ‡’ k.+1.โˆ’tuple 'cV[R]_aโ†’ 'cV[R]_bend.

Figure 4. Semantics of W types

Once we have defined what an interpretation is, the coreof the semantics is given by the interpretation transformerS. We write P, ฮ“ โŠข ๐‘’ :๐œ , ๐‘˜ < ๐‘› + 1, and ฮ˜๐‘˜ , for the first ar-guments (after unfolding the definition of I{๐‘›+1}), such thatSPโŸฆฮ“ โŠข ๐‘’ :๐œโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜

has type โŸฆ๐œโŸง๐‘˜Tas required. The interpreta-

tion transformer is then defined structurally on expressions,so that, in particular, there is no problem regarding the ter-mination requirements imposed by Coq, and termination isdetected by Coqโ€™s regular guard checker.There are a few interesting cases in this definition. For

variables, the environment will contain the history of allthe previous values of this variable; so we can just access it,or return the 0-interpretation if the access is out of bounds.Functions are interpreted as regular mathematical functions;function application builds the full history of the argumentprior to passing it to the interpretation of the function. Thefeedback case is, however, a bit more tricky, as we now needto interpret the feedback expression itself, but over previous

time steps. While we could strengthen our termination con-dition in Coq, to include both the structural and time-basedorders to define a well-founded recursion, this usually leadsto a complex proof setup, so we chose to untie the recur-sive call by moving the previous-time interpretation as aparameter P : I{๐‘›} .

We can thus define S by induction on the terms, and thendefine the absolute interpretation function I{๐‘›} by a simpleinduction on time.

2.4 Linearity

While the previous semantics may seem naturally correct,details are subtle in our context, and validation is required(the whole point, in fact, of encoding it inside a theoremprover).We have proved without too much hassle a few basic

properties such as that expected equations and programequivalences do hold, as well as checking inside Coq thatthe output is what is expected, as we can run the semantics,albeit quite slowly.For this section, we focus however on what we think is

a more interesting property that applies to the full set ofwell-typedW programs: linearity.

The avid reader may have noticed by now that our coresyntax lacks constants, and indeed this omission is not acoincidence. We will fix this problem in the next section aswe move towards more practical uses, but for now, we willforget about constants and proceed to prove a theorem thatstates that โ€œall well-typed programs in the W -calculus arelinearโ€.

38

TheW-calculus: A Synchronous Framework for the Verified Modelling of . . . FARM โ€™21, August 27, 2021, Virtual, Republic of Korea

S : I{๐‘›} โ†’ I{๐‘›+1} I{๐‘›} โ‰ก expr ฮ“ ๐œ โ†’ โˆ€ ๐‘˜, ๐‘˜ < ๐‘› โ†’ env ฮ“ ๐‘˜ โ†’ โŸฆ๐œโŸง๐‘˜T

ฮ˜๐‘˜ : env ฮ“ ๐‘˜ โ‰ก (๐‘ฅ โˆˆ V) โ†’ tuple๐‘˜+1 โŸฆฮ“ ๐‘ฅโŸงT

SPโŸฆฮ“ โŠข ๐‘ฅ๐‘š :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ nth๐‘š (ฮ˜๐‘˜ ๐‘ฅ) 0๐‘Ž, where 0๐‘Ž โ‰ก init ๐‘Ž (fun _โ†’ 0)

SPโŸฆฮ“ โŠข _๐‘ฅ .๐‘’ :R๐‘Ž โ†’ R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ fun histโ†’ SPโŸฆฮ“, ๐‘ฅ : R๐‘Ž โŠข ๐‘’ :R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โŠŽ{๐‘ฅโ†hist}

SPโŸฆฮ“ โŠข ๐‘“ ๐‘’ :R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ SPโŸฆฮ“ โŠข ๐‘“ :R๐‘Ž โ†’ R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜

hist

where hist โ‰ก [SPโŸฆeโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜;SPโŸฆeโŸง๐‘˜โˆ’1<๐‘›+1ฮ˜๐‘˜โˆ’1

; ...;SPโŸฆeโŸง0<๐‘›+1ฮ˜0]

and e โ‰ก ฮ“ โŠข ๐‘’ :R๐‘Ž

SPโŸฆฮ“ โŠข ๐‘ โˆ— ๐‘’ :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ ๐‘ โˆ— SPโŸฆฮ“ โŠข ๐‘’ :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜

SPโŸฆฮ“ โŠข ๐‘’1 + ๐‘’2 :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ SPโŸฆฮ“ โŠข ๐‘’1 :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜

+ SPโŸฆฮ“ โŠข ๐‘’2 :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜

SPโŸฆฮ“ โŠข ๐œ‹๐‘– (๐‘’) :R1โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ [nth ๐‘– (SPโŸฆฮ“ โŠข ๐‘’ :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜

) 0]

SPโŸฆฮ“ โŠข (๐‘’1, ๐‘’2) :R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ (SPโŸฆฮ“ โŠข ๐‘’1 :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜

) ++ (SPโŸฆฮ“ โŠข ๐‘’2 :R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜)

SPโŸฆฮ“ โŠข let ๐‘ฅ = ๐‘’1in ๐‘’2 :R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ SPโŸฆฮ“, ๐‘ฅ : R๐‘Ž โŠข ๐‘’2 :R๐‘โŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โŠŽ{๐‘ฅโ†hist }

where hist โ‰ก [SPโŸฆeโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜;SPโŸฆeโŸง๐‘˜โˆ’1<๐‘›+1ฮ˜๐‘˜โˆ’1

; ...;SPโŸฆeโŸง0<๐‘›+1ฮ˜0]

and e โ‰ก ฮ“ โŠข ๐‘’1 :R๐‘Ž

SPโŸฆฮ“ โŠข feed ๐‘ฅ .๐‘’ :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โ‰œ SPโŸฆฮ“, ๐‘ฅ : R๐‘Ž โŠข ๐‘’ :R๐‘ŽโŸง๐‘˜<๐‘›+1ฮ˜๐‘˜โŠŽ{๐‘ฅโ†hist }

where hist โ‰ก [PโŸฆfbโŸง๐‘˜โˆ’1<๐‘›ฮ˜๐‘˜โˆ’1; ...;PโŸฆfbโŸง0<๐‘›ฮ˜0

] with fb โ‰ก ฮ“ โŠข feed ๐‘ฅ .๐‘’ :R๐‘Ž

Figure 5. W single-step semantics of expressions, with P : I{๐‘›}

From Section 1.3, we know that a function ๐‘“ : Rโ†’ R islinear iff ๐‘“ (๐‘ฅ + ๐‘ฆ) = ๐‘“ (๐‘ฅ) + ๐‘“ (๐‘ฆ). However, this notion istoo weak to be of direct use in our programming languagecontext as a) not all expressions are functions, and b) pro-gram expressions are indeed open, so the property has to beextended as to be meaningful under an environment ฮ“.

As is often the case in interactive theorem proving, findingthe right theorem and definition statements does amountto the large majority of the work the proof engineer or re-searcher has to do.In our case, we will take advantage of the well-known

technique of โ€œlogical relationsโ€: relations over values indexedby program types, and closed over functional values in sucha way that functions must send related inputs to relatedoutputs.We first specify what linearitymeans for values ofWtypes using the ternary, type-indexed relation rel_additive:

Fixpoint rel_additive k t {struct t}: tyI k t โ†’ tyI k tโ†’ tyI k tโ†’ Prop :=match t with

| tpair m โ‡’ fun x1 x2 x3โ‡’ x1 = x2 โˆ’ x3

| tfun m n โ‡’ fun f1 f2 f3โ‡’forall (x1 x2 x3 : k.+1.โˆ’tuple (tyI k m)),x1 = x2 โˆ’ x3 โ†’ f1 x1 = f2 x2 โˆ’ f3 x3

end.

In the above code, we have two cases. For basic values, we justrequire that the first element of the relation is the subtractionof the third from the second. For functional values, we dorequire that the functional values behave in a linear way.Note that, in the first versions of this work, we were able toget away with a unary relation, as one can substitute overthe equalities; however, we felt that this presentation is morestandard and allows us to define the upcoming fundamentallemma in a way more accessible for readers familiar withlogical relations.

Note also that the use of the additive property ๐‘“ (๐‘ฅ โˆ’ ๐‘ฆ) =๐‘“ (๐‘ฅ) โˆ’ ๐‘“ (๐‘ฆ) makes it more convenient to integrate our codewith the standard linear algebra libraries of Coqโ€™s SSReflectMathematical Components library, as it captures both lin-earity and preservation of negation.

39

FARM โ€™21, August 27, 2021, Virtual, Republic of Korea E.J. Gallego Arias, P. Jouvelot, S. Ribstein, and D. Desblancs

The fundamental lemma, rel_additive_fund, does gener-alize the relation over values to a relation over open expres-sions:Lemma rel_additive_fund

ฮ“ t ( e : expr ฮ“ t) n ( I : I n) ( HI : hlinP' I) :forall k ( hk : k < n.+1) (ฮ˜1 ฮ˜2 ฮ˜3 : env k ฮ“) ,env_additive ฮ˜1 ฮ˜2 ฮ˜3 โ†’let: v1 := exprI I hk e ฮ˜1 in

let: v2 := exprI I hk e ฮ˜2 in

let: v3 := exprI I hk e ฮ˜3 in

rel_additive v1 v2 v3.

env_additive states that ฮ˜1 = ฮ˜2 โˆ’ ฮ˜3, and the HI assump-tion ensures that the interpretation I is, for the feedback case,linear with respect to environments, which is a sub-case ofrel_additive, but specialized to non-functional types; thisis enough due to our current typing restrictions on the argu-ments of feedbacks and functions.

As a corollary of the fundamental lemma, we obtain:Corollary funD n k (p : k <= n) a b

( f : expr [::] ( tfun a b)) :let ( f : k.+1.โˆ’tuple ' cV_aโ†’ 'cV_b) := exprIn p f tt in

additive f.

which reads as โ€œgiven an arbitrary time step ๐‘› and a well-typed closed function ๐‘“ , the interpretation of ๐‘“ at ๐‘˜ is addi-tiveโ€.

Note that this process can be repeated for scaling, that isto say ๐‘“ (๐‘๐‘ฅ) = ๐‘ ๐‘“ (๐‘ฅ), this time obtaining:Corollary funP n k (p : ( k <= n)%nat) a b

( f : expr [::] ( tfun a b)) : lmorphism (exprIn p f tt).

where lmorphism is the Coq definition of a โ€œlinear morphismโ€,which implies both additivity and scalability.

3 Running Ahead: Imperative Semantics

Up to this point, we have used the formal semantics in Fig-ure 5 to reason aboutW programs and their properties; whilethis is nice, and it works reasonably well, we are still far fromsomething usable as a programming framework.Indeed, the semantics of Section 2.3 recompute the full

time history of arguments at every function application node.This is simple to understand mathematically, and the locality(or referential transparency) of the definitions helps in formalreasoning, but it is not going to work in actual uses, as thisargument re-computation is highly impractical and requiresan unbounded amount of memory.

In this section, we focus on the concrete case when accessto variablesโ€™ history is bounded. This way, we can allocate abuffer at every application point โ€” and feedback, of course โ€”that will store the argumentโ€™s past values up to the inferredbound in the buffer to avoid recomputing.

We claim that this produces a reasonable execution model,and will proceed to experimentally benchmark our programsto show so.

๐œ ::= ยท ยท ยท | ๐ผ๐‘›๐‘’, ๐‘“ ::= ๐‘ฅ ๐‘ฅ โˆˆ V

| ยท ยท ยท| ๐‘’ [๐‘’idx]| ๐‘“ [ptrโ†ข ๐‘’] ptr โˆˆ Ptr| [ptrโ†ข feed ๐‘ฅ . ๐‘’] ptr โˆˆ Ptr

Figure 6. UpdatedW syntax and types, with buffer alloca-tion and array indexes

3.1 Language Extensions for Allocations and

Variables-as-Arrays

Before moving ahead with the imperative semantics, weslightly tweak and extend the syntax from Figure 1 to providea more comfortable programming experience, at the cost ofadding non-linear constructions; in particular:โ€ข variables now lose the subindex and denote arrays โ€“we also introduce a type of ordinals ๐ผ๐‘› (integers lessthan ๐‘›), and a safe array-access operator ๐‘ฅ [๐‘–];โ€ข feedback and application expressions now store a poin-ter to a heap-allocated circular buffer, where past val-ues for the history-tracking expressions are stored.

Figure 6 shows the new indexing operator ๐‘’ [๐‘’idx], with typ-ing rule [] : R๐‘› โ†’ ๐ผ๐‘› โ†’ R, and the extra pointer in bothapplication and feedback nodes. We writeWptr for this up-dated version when required.

Bounded Access. The new syntax implements boundedaccess by default, as the type ๐ผ๐‘› is only inhabited by integers{0, . . . , ๐‘› โˆ’ 1}; however, on the raw machine we will presentsoon, type information is erased and indeed an out-of-boundsaccess will lead to undefined behavior. This is just a particulardesign choice, but any method that provides, for a functionalexpression ๐‘’ , the bounds on access to their argument wouldwork for us.

The idea of using bounded access to improve our machinecan be understood by looking at Figure 7. There, we can seehow the bound allows us to avoid allocating a new memorycell for the value of the argument in the ๐‘› + 1 step, and wecan instead use a standard circular buffer.

Note that this goes beyond memoization (which is anyhowimpractical for DSP), as memoization does avoid the multiplecomputations of all the previous values for arguments offunctions, but allocation is still necessary for the new ones.

Allocation and Heaps. Once we have a bound for ourfunctional terms, we have to allocate a buffer in the heapand update the pointer for each expression. Note that it iscrucial that buffers are not overlapping obviously, and ini-tialized to zero, as is common in DSP (though we could pro-vide pragmas for different initialization setups, usually fromsome probability distribution, which is a common patternfor physically-based sound processing).

40

TheW-calculus: A Synchronous Framework for the Verified Modelling of . . . FARM โ€™21, August 27, 2021, Virtual, Republic of Korea

โŸฆ๐‘“ ๐‘ŽโŸง๐‘› = โŸฆ๐‘“ โŸง๐‘› [:: โŸฆ๐‘ŽโŸง๐‘› ; ...; โŸฆ๐‘ŽโŸง๐‘›โˆ’๐‘‘+1; โŸฆ๐‘ŽโŸง๐‘›โˆ’๐‘‘ ; ...; โŸฆ๐‘ŽโŸง0 ]

โŸฆ๐‘“ ๐‘ŽโŸง๐‘›+1 = โŸฆ๐‘“ โŸง๐‘›+1 [:: โŸฆ๐‘ŽโŸง๐‘›+1; โŸฆ๐‘ŽโŸง๐‘› ; ...; โŸฆ๐‘ŽโŸง๐‘›โˆ’๐‘‘+1; โŸฆ๐‘ŽโŸง๐‘›โˆ’๐‘‘ ; ...; โŸฆ๐‘ŽโŸง0 ] unbounded history access

โŸฆ๐‘“ ๐‘ŽโŸง๐‘› = โŸฆ๐‘“ โŸง๐‘› [:: โŸฆ๐‘ŽโŸง๐‘› ; ...; โŸฆ๐‘ŽโŸง๐‘›โˆ’๐‘‘+1; โŸฆ๐‘ŽโŸง๐‘›โˆ’๐‘‘ ]

โŸฆ๐‘“ ๐‘ŽโŸง๐‘›+1 = โŸฆ๐‘“ โŸง๐‘›+1 [:: โŸฆ๐‘ŽโŸง๐‘›+1; โŸฆ๐‘ŽโŸง๐‘› ; ...; โŸฆ๐‘ŽโŸง๐‘›โˆ’๐‘‘+1 ] bounded access, fixed-size circular buffer

Figure 7. History dynamics (โŸฆ๐‘’โŸง๐‘› is, informally, the value of expression ๐‘’ at time step ๐‘›; [:: ...; ...] is a list)

An important remark to keep in mind regarding allocationof history buffers is that the following code has to work fine.Assume fir : R๐‘› โ†’ R๐‘š and ๐‘ฅ,๐‘ฆ : R๐‘› , then

(fir ๐‘ฅ, fir ๐‘ฆ)has to work properly, while sharing the implementation offir; the above expression will allocate two buffers. Otherlanguages do actually unfold fir and insert the buffers atdelay time; however this creates cache locality and code sizeissues. Moreover, it makes it hard to remain in a call-by-value setting. We tried some different approaches to thisproblem, which took us quite a while, but have settled withthis solution for now.

3.2 An OCaml-Based Interpreter

After the introduction of buffers to store the history of func-tion arguments, we can define an interpreter or compilationscheme for Wptr that should be capable of real-time DSP.Even if the history space and time consumption problem hasbeen solved by the use of buffers, our language still featuresfirst-class functions, and thus in particular closures; thus thecompilation scheme is not trivial, and we must eliminateor manage allocations introduced by _-expressions, e.g., in(_๐‘ฅ.๐‘ฅ0 + ๐‘ฅ1) ๐‘’ .We thus face the tension between the need for rapid pro-

totyping and experimentation, and the need for an optimiz-ing compiler that can produce code competing with hand-written DSP routines.

We have found a middle ground by taking advantage ofthe fact that the only overhead in the imperative version ofthe semantics is interpretation and closure allocation. Wecan put to use three key components to write a toy Wptr

compiler producing performant, allocation-free code:1. first, we define anOCaml interpreter forWptr (Figure 8)

that takes advantage of Generalized Algebraic DataTypes (GADTs) to representWptr closures as OCamlclosures โ€“ this step is critical, as we will see;

2. second, we instrument ourWptr interpreter usingMetaO-Caml โ€“ this staged interpreter can take any Wptr pro-gram as input and produce a specialized version of theinterpreter for the particular input program;

3. last, we use the optimizing FLambda OCaml compiler,which is able to compile the code generated in theprevious steps, eliminating all closures, and produc-ing pretty fast machine code, allocation-free in all ourexamples.

We show the โ€œdenotationalโ€ and imperative interpreters inFigure 8 side-by-side, to help the reader spot the differences.The denotational version is a straight lifting of the interpre-tation function SPโŸฆโŸง defined above in Coq to OCaml.

The most interesting case for the imperative interpreter isthe application case. As the reader can see, we first computethe value for the argument at the current time, then we pushit to the buffer, passing the pointer to the interpretation ofthe function, which is an OCaml function thanks to our useof GADTs. The feedback node follows a similar scheme.Once we have the interpreter, we proceed to stage it in a

standard way โ€” as shown in Figure 9 โ€” so we can producespecialized code for eachWptr input program. The generatedcode contains closures, but for our examples (see below), allof themwill be eliminated by the OCaml optimizing compiler.

3.3 Experimental Evaluation

We perform an informal experimental evaluation of our com-pilation scheme. In particular, we compare 3 different im-plementations โ€” imperative interpreter โ€œbasicโ€, staged inter-preter โ€œgenโ€, hand-made code โ€œhmโ€ โ€” of 3 examples:โ€ข fir, a simple finite-impulse response filter of 15th order;โ€ข iir, a simple infinite impulse filter of 15th order;โ€ข com, the composition of fir and iir.

Results can be seen in Table 1. The time column denotes timeper run of 10 samples. The allocation columns โ€œmWdโ€ andโ€œmjWdโ€ refer to specifics of the OCamlโ€™s garbage collector,but we can interpret them as โ€œminorโ€ and โ€œmajorโ€ allocations,with the latter being very expensive, and the former being alesser worry but still a cost in terms of CPU instructions.

The โ€œbasicโ€ version provides a reasonable baseline. How-ever we allocate a large amount of memory on each cycle

41

FARM โ€™21, August 27, 2021, Virtual, Republic of Korea E.J. Gallego Arias, P. Jouvelot, S. Ribstein, and D. Desblancs

let rec machine_n :

type a. int โ†’ a expr โ†’ env โ†’ a list =

fun step e env โ†’if step < 0 then []

else machine step e env :: machine_n (step-1) e env

and machine : type a . int โ†’ a expr โ†’ env โ†’ a =

fun step e env โ†’ match e with

| Var (idx, id) โ†’lookup env id idx

| Lam e โ†’fun hist โ†’ machine step e (hist :: env)

| App (ef, ea) โ†’let a_hist = machine_n step ea env in

machine step ef env a_hist

| Feed e โ†’let e_hist = machine_n (step-1) (Feed e) env in

machine step e (e_hist::env)

|...

let heap = ... (* pre-allocated *)

let rec imachine : expr โ†’ env โ†’ value =

fun e env โ†’ match e with

| Var (idx, id) โ†’let p = ptr_of id env in

lookup heap p idx

| Lam e โ†’fun ptr โ†’ imachine e (ptr :: env)

| App (p,ef,ea) โ†’let va = imachine ea env in

shift_one heap ptr va;

imachine ef env ptr

| Feed (ptr,e) โ†’let v = imachine e (ptr::env) in

shift_one heap ptr v;

v

|...

let rec repeat : int โ†’ (unit โ†’ 'a) โ†’ 'a = fun n c โ†’if n = 0 then c ()

else (c (); repeat (n-1) c)

let eval e env n = repeat n (fun () โ†’ imachine e env)

Figure 8. Denotational vs. imperative interpretations

Figure 9. Staged Interpreter, Selected Caseslet rec eval : type a. env โ†’ a expr โ†’ a code =

fun g e โ†’ match e with

| Cst r โ†’let module M = Lift_array(Lift_float) in M.lift r

| Var id โ†’.< Array.(unsafe_get .~(heap) .~(List.nth g id)) >.

| Idx (vec, id) โ†’.< let v = .~(eval g vec) in

let i = .~(eval g id) in

Array.(unsafe_get v i) >.

| Add (e1,e2) โ†’.< let v1 = .~(eval g e1) in

let v2 = .~(eval g e2) in

WArray.map2 (+.) v1 v2 >.

| Lam e โ†’.<fun p โ†’ .~(eval (.<p>.::g) e)>.

| App (p, f, e) โ†’.< let v = .~(eval (.<p>.::g) e) in

shift_one v Array.(unsafe_get .~(heap) p);

.~(eval g f) p >.

|...

for the composed example due to closures, plus the interpre-tation overhead; at some point we even hit the major heap,which is costly.

The generated code performs much better, surpassing thehand-written code in one instance. Keeping into account

Table 1. Time (per sample) and memory allocation (per run)for each test; percentages are time with respect to the slowestcase (com basic)

Name Time (ns) mWd (w) mjWd (w) Percentagefir basic 4,982.48 789.01 0.11 47.54%fir gen 188.42 21.00 1.80%fir hm 85.21 2.00 0.81%iir basic 4,765.38 701.01 45.47%iir gen 184.08 43.00 1.76%iir hm 212.46 2.00 2.03%com basic 10,479.81 1,522.02 0.31 100.00%com gen 394.78 83.00 3.77%com hm 287.33 46.00 2.74%

that the generated code was not optimized in any otherway, we think the imperative execution model performsaccording to our goals, and that it has been validated interms of potential real-time performance. Thus, writing amore refined compilation back-end seems to be worth it.Note that we wrote the hand-made code in OCaml, but

we inspected the generated assembly closely, and indeed itseems to be competitive with C code. We did some more in-formal comparison of tuned C code generated from Faust vs.our hand-made setup, and while OCaml performs a bit worsethan gcc, it is still below a 2x-overhead in the worst case.We believe our hand-made baseline to be a good referencepoint for now.

42

TheW-calculus: A Synchronous Framework for the Verified Modelling of . . . FARM โ€™21, August 27, 2021, Virtual, Republic of Korea

To compile the code, we used OCamlโ€™s Flambda com-piler, version 4.07.1, with the -O3 - unbox-closures op-tions. For MetaOCaml, we used the 4.07.1+BER version, andthe core-bench benchmarking framework generously opensourced by Jane Street.

3.4 Equivalence of Semantics

An open question at this point is whether one can show thatthe semantics of the denotational interpreter is equivalentto the semantics of the imperative version with buffers. Theproof here becomes a bit more complex, and in this paper wejust present a sketch of the idea and leave the fully formalizedproof for future work.

The difficulties here are standard, and have been discussedin the context of verified compilation of synchronous pro-gramming languages [15]. Concretely, we may try to relateboth interpreters using a logical or simulation relation, butin this case the types are not telling the whole story. For anexpression ฮ“ โŠข ๐‘’ :๐œ , in the denotational case, ฮ“-compatibleenvironments ฮ˜ will contain the full story for the binders,while in the imperative case, all they contain is a pointer onthe buffer; so the buffer invariant cannot be easily stated.

There likely exist many possibilities to overcome this, butwe intend to follow an idea similar to the one in Velus [15]and extend our definition of well-typed judgements to exposea heap shape ฮ”, so that ฮ” | ฮ“ โŠข ๐‘’ :๐œ denotes a well-typed ex-pression, and pointers in application nodes refer to a uniquelocation in the heap. This way we expect to be able to de-fine a predicate relating heaps plus imperative environmentsto denotational environments, and prove that the one-stepevaluation preserves this relation, thus making induction onthe number of steps work.

4 Related Work

Domain-Specific Languages for DSP. The work presen-ted here lies at the intersection of several domains, our influ-ences and related work are quite varied. We briefly surveyrelated work, classified in a non-mutually exclusive way.W is directly inspired by the Faust programming lan-

guage [48], and originally arose as a method to study / pro-vide Faust with a mechanically specified operational seman-tics, including multi-rate [7, 33, 49]. Faust has a clear deno-tational model, but the particular implementation strategyis left open so its cost semantics will vary depending on thechosen compilation trade-offs. Moreover, Faust point-freepresentation is harder to connect with standard program-ming languages techniques such as logical relations, whichrely on a typical โ€œfunctional calculusโ€ presentation. Picking aparticular evaluation order in W allowed us to reason aboutbuffers / arrays, which is very convenient in DSP, so weended up adding them as a first-order primitive for program-mersโ€™ use. Another key goal of this work was to provide a

semantics in which to interpret (future) multi-rate programswith a reasonable cost model (see Section 5 for more details).

A language that feels very similar in syntax toW is Arrp [37,38], an array-based language. Arrp uses optimizing polyhe-dral compilation to produce very efficient code, while at thesame time providing the programmer with more flexibilitythanW in terms of array manipulation.W exposes arrays,but only as primitive data-types, so at the moment arrayoperations are compiled directly to the underlying array op-erations in the semantics. We havenโ€™t explored what kind ofoptimizations would be possible in this front.

Feldspar [5] is a DSL embedded inHaskell that can produceefficient code for DSP algorithms using a custom compilationstrategy. CSound and Supercollider are music-processing lan-guages that include DSP capabilities, usually based on thenotion of UGens, custom C routines that process audio ona callback basis. Max/MSP and PureData are graphical pro-gramming languages allowing the definition of signal data-flow processing diagrams that are then interpreted. Somemore recent languages aimed at music DSP are also Vult andSoul. All these languages were, in one way or another, con-sidered during the design of the W framework, in particularwhen thinking about the requirements that the W calculushad to fulfil in order to ultimately become a possible com-pilation target for them, and thus deserve to be mentionedhere. We also refer the reader to the survey on [6], whichcompares the different expressivities of music-oriented DSPlanguages.

Synchronous Programming. There exists a long tradi-tion of research studying systems for real- and constrained-time programming, among which one of the most success-ful is the Synchronous Paradigm, in which programs are as-sumed to react instantaneously to changes of input, whichare driven by some specific temporal scale.Arising from the study of Kahn Proccess Networks [34],

the field of synchronous programming has produced veryimportant theoretical and practical contributions. As a veryincomplete list of synchronous languages, we could men-tion Lustre [22], Esterel [10, 11], Signal [9, 28], Lucid Syn-chrone [21, 23], and Zelus [16, 17].

Particularly recent relevant work to us is the research ontime refinement [42], causality analysis [8], constructive se-mantics [44], and the work of Adrien Guatto [30]. Indeed,Guattoโ€™s work is quite close in spirit to our proposal, ashe introduces a synchronous language with integer clocks,while most of his metatheory is based on techniques devel-oped in the functional programming community, such asstep indexing [3, 12, 46]. A linear-based type system ensuresthat all well-typed programs are safe and can be compiledto finite circuits.W can be considered a simple synchronouslanguage, with the main constraint that no clock informationis required at run time; moreoverW programs have a unique

43

FARM โ€™21, August 27, 2021, Virtual, Republic of Korea E.J. Gallego Arias, P. Jouvelot, S. Ribstein, and D. Desblancs

typing derivation and the interpretation of programs doesnโ€™tdepend on the typing derivation tree.

Data-FlowProgramming. Data-flow Programming is par-ticularly well suited to signal processing, and data-flow dia-grams are commonly used to write and specify algorithms.Data-flow systems are typically characterized by their sched-uling power, from synchronous data-flow [39] to more com-plex but less performant proposals that admit no static orcyclic schedules. A few remarkable systems that we looked atduringW โ€™s design phase are Ptolemy [20, 50], StreamIt [58],and SDF3 [57].

Functional Reactive Programming. Arrows and Func-tional Reactive Programming [25, 31, 32, 40, 41, 47] pro-vide an elegant framework to express many interesting pro-gramming patterns based on time. A more recent develop-ment [36] extends FRP to provide a leak-free full formalsemantics; we have found a great deal of inspiration on thiseffort. Co-effects type systems [19, 26] provide a type-basedmethod to quantify resource and buffering dependencies. Avery relevant work on completeness of stream fusion is [35].

Formal Verification of DSP and Cyber-Physical Sys-tems. An early effort for the deductive verification of asynchronous system is found in [13]. More recently, Cรฉ-dric Augerโ€™s PhD thesis [4] made significant progress; it hasbeen recently put to work to deliver the first certified Lus-tre compiler [14, 18], which ensures the correctness of thecompilation of Lustre programs with respect to a denota-tional model. The VERIDRONE project [52] aims to verifythe control engine of a drone with respect to safety condi-tions such as zoning. There exists a long line of work on theverification of mathematical properties for DSP by Tahar etal. [1, 2, 45, 53, 56], mainly using the Isabelle theorem prover.Closest to our attempt to verify the imperative heap al-

location pass is the work on the Velus verified compiler, aswell as the techniques required in order to manage memorylocal to nodes.

5 Future Work

Throughout the paper we have already hinted at possiblefuture work directions; in particular, we can distinguish 4concrete lines of work that we are pursuing.

Multirate Processing. A key design goal of W was toaccommodate multirate processing from the start. While thework presented here exclusively deals with the single-ratecase, we have already developed a simple version of periodicregular clocks that allows to implement several multi-rateprimitives in a simple way. In this setup, stream types areannotated with a rate parameter, so that R, representing astream of samples in our setup, becomes R@1. Additional

subtyping rules such as

ฮ“ ๐‘ฅ = R๐‘Ž@๐‘Ÿ

ฮ“ โŠข ๐‘ฅ :R๐‘Ž [๐‘Ÿ ]ฮ“ ๐‘ฅ = R๐‘Ž@๐‘Ÿ ๐‘ฅ is ๐‘˜-bounded

ฮ“ โŠข ๐‘ฅ :R๐‘Ž [๐‘Ÿ๐‘˜]

implement conversion from rates to arrays (of type R๐‘Ž [๐‘Ÿ ]) sothey can be manipulated; the left side is the simple version,whereas the right version does allow โ€œflatteningโ€ of a multi-rate stream that has been bounded up to ๐‘˜ steps into anarray. The introduction of stream rates now requires anemit operation, which we write {๐‘’1, . . . , ๐‘’๐‘›} for emitting ๐‘›elements at one concrete time step. In this discipline, wecan now write several code examples (we note [๐‘’1, ..., ๐‘’๐‘›] anarray constant with values ๐‘’๐‘– ):

up : R@1โ†’ R@2up = _๐‘ฅ . {๐‘ฅ, 0}

down : R@2โ†’ R@1down = _๐‘ฅ. {๐‘ฅ [0]}

pack : R@2โ†’ R2@1pack = _๐‘ฅ.{[๐‘ฅ [0], ๐‘ฅ [1]]}

unpack : R2@1โ†’ R@2unpack = _๐‘ฅ .{๐‘ฅ [0], ๐‘ฅ [1]}

FFTw : R@16โ†’ R[128]@1FFTw = _๐‘ฅ . {FFT ๐‘ฅ}

The first 4 examples perform straightforward rate conver-sion, with the particularity that both pack and unpack aresemantically the identity function after type erasure, as theunderlying pointers are the same. A more interesting exam-ple is the last one, windowed FFT. In this case, we assume aprimitive FFT : R[128] โ†’ R[128]; then, the subtyping rulewill infer a bound for the input stream ๐‘˜ = 112, so ๐‘ฅ willbe given type R[128], with a buffer of that particular size,which gets shifted by 16 samples at each time step. Manyother interesting examples are possible in this framework,such as filters with different control and data rates. Notethat this discipline leads naturally to the introduction of twofunction types: a regular functional type for primitives notaware of streams (such as FFT above), and a stream processortype, where the arguments must be streams with rates.

Front End and Type Inference. As the reader may havenoticed, the core calculus presented in this paper is notenough to compile programs, as we still need to computeaccess bounds on variables as to properly size the requiredbuffers in the compilation scheme. To fill that gap, we havedeveloped an extension of the type system to track variableusage using co-effects (see for example [19]). The idea is thatevery variable in the environment gets an annotation thatreflects how many of their past values were used. This infor-mation is also carried over functional types, so we can thenknow a bound on the history access of each stream proces-sor by just looking at their type. Note that typing rules nowcrucially rely on the particular evaluation strategy! In ourcase, we believe our current call-by-value discipline providesby far the better adapted scenario, but other approaches are

44

TheW-calculus: A Synchronous Framework for the Verified Modelling of . . . FARM โ€™21, August 27, 2021, Virtual, Republic of Korea

possible. The basic rules are now:(๐‘ฅ :๐‘˜ R๐‘Ž) โˆˆ ฮ“ฮ“ โŠข ๐‘ฅ :R๐‘Ž [๐‘˜]

ฮ“, ๐‘ฅ :๐‘˜ ๐œ โŠข ๐‘’ :๐œŽฮ“ โŠข _๐‘ฅ . ๐‘’ :๐œ โ†’๐‘˜ ๐œŽ

so, for example, the FFT example becomes:FFTw : R@16โ†’112 R[128]@1FFTw = _๐‘ฅ . {FFT ๐‘ฅ}

We have implemented a full front end for this system, namedโ€œWagnerโ€, which includes a parser and a bidirectional type-checker; the above approach seems to work fine in practice,with bidirectional type propagation being a key point inorder to correctly compute bounds. We took many hintsregarding type information dataflow from the approach im-plemented by the Mathematical Components library [29] intheir matrix implementation.

Formally Verified Imperative Semantics. Work is un-derway to implement the strategy outlined in Section 3.4 asa formally verified Coq proof. In particular, we have definedthe imperative machine inside Coq using a heap monad (astate monad where the state is the global heap), defined atyping relation for allocated programs that implies pointerindependence, and are in the process of coding down thecorresponding soundness relations. Additionally, we couldconsider implementing a program logic in the style of [27].

More Principled Code Generation. Additionally, morework in the back end is planned. The current solution usingMetaOCaml was quick to implement and provided goodresults; however it is not practical as a daily driver for DSPdevelopment, where the compiler is expected to target low-level languages such as C or Rust. We thus plan to implementprogram generation targeting a simple imperative C-likeformat by specialization + defunctionalization [24, 51], whichshould suffice to generate reusable code.

The above are the main items in our work plan. Howevera few other more speculative possibilities do include:a) higher-order streams are in general very difficult to im-

plement efficiently (see for example [30]) and likely notvery useful for our intended application. In the case ofW ,adding a shift index to closures would suffice to translaterelative access to the environment, but would likely incurnon-acceptable run-time costs;

b) dynamic dataflow graphs, since, so far, our calculus has afixed-dataflow structure; it could be interesting to allowsome dynamism here, but the way to do it is not yet clear;

c) modules, modular compilation and ML-style interfaceswill prove quite useful in the construction of DSP systemsand libraries, as well as to better structure componentswith a complex control and data input-output structure.

6 Conclusion

We have presented theW-calculus, a formal calculus for theencoding of real-time digital signal processors, and provided

its mechanized denotational semantics and a mechanizedproof that every program written in the calculus is linear,using logical relations.

Additionally, we have developed an efficient imperative in-terpreter, and performed an experimental evaluation of someselected examples using program specialization to validateour approach.We believe that the presented work constitutes a good

basis upon which to (1) keep studying formally verified se-mantics and properties of the language and programs writtenin it and (2) evolve towards a usable high-level DSP languagethat can be used to quickly and easily define efficient pro-cessing components.

Acknowledgments

This work has been supported by the ANR FEEVER project(ANR-13-BS02-0008). We thank Yann Orlarey, Adrien Guatto,and Timothy Bourke for interesting discussions and ideas.

References

[1] Behzad Akbarpour and Sofiรจne Tahar. 2004. A Methodology for theFormal Verification of FFT Algorithms in HOL. In FMCAD 2004. https:

//doi.org/10.1007/978-3-540-30494-4_4

[2] Behzad Akbarpour and Sofiรจne Tahar. 2006. An approach for theformal verification of DSP designs using Theorem proving. IEEE Trans.on CAD of Integrated Circuits and Systems. https://doi.org/10.1109/

TCAD.2005.857314

[3] Andrew W. Appel and David A. McAllester. 2001. An indexed modelof recursive types for foundational proof-carrying code. ACM Trans.Program. Lang. Syst.. https://doi.org/10.1145/504709.504712

[4] Cรฉdric Auger. 2013. Compilation Certifiรฉe de SCADE/LUSTRE. Thรจsede Doctorat. Universitรฉ Paris-Sud. http://tel.archives-ouvertes.fr/tel-00818169/.

[5] Emil Axelsson, Koen Claessen, Mary Sheeran, Josef Svenningsson,David Engdal, and Anders Persson. 2011. The Design and Implemen-tation of Feldspar. Springer Berlin Heidelberg, Berlin, Heidelberg,121โ€“136. https://doi.org/10.1007/978-3-642-24276-2_8

[6] Karim Barkati and Pierre Jouvelot. 2013. Synchronous programmingin audio processing: A lookup table oscillator case study. ACM Comput.Surv.. https://doi.org/10.1145/2543581.2543591

[7] Karim Barkati, Haisheng Wang, and Pierre Jouvelot. 2014. Faustine: AVector Faust Interpreter Test Bed for Multimedia Signal Processing โ€”System Description. In FLOPS 2014.

[8] Albert Benveniste, Timothy Bourke, Benoรฎt Caillaud, Bruno Pagano,and Marc Pouzet. 2014. A type-based analysis of causality loops inhybrid systemsmodelers. InHSCCโ€™14. https://doi.org/10.1145/2562059.

2562125

[9] Albert Benveniste, Paul Le Guernic, and Christian Jacquemot. 1991.Synchronous Programming with Events and Relations: the SIGNALLanguage and Its Semantics. Sci. Comput. Program.. https://doi.org/

10.1016/0167-6423(91)90001-E

[10] Gรฉrard Berry. 2000. The foundations of Esterel. In Proof, Language,and Interaction, Essays in Honour of Robin Milner, Gordon D. Plotkin,Colin Stirling, and Mads Tofte (Eds.). The MIT Press, 425โ€“454.

[11] Gรฉrard Berry and Georges Gonthier. 1992. The Esterel SynchronousProgramming Language: Design, Semantics, Implementation. Sci.Comput. Program. 19, 2 (1992), 87โ€“152. https://doi.org/10.1016/0167-

6423(92)90005-V

[12] Lars Birkedal, Rasmus Ejlers Mรธgelberg, Jan Schwinghammer, andKristian Stรธvring. 2012. First steps in synthetic guarded domain theory:

45

FARM โ€™21, August 27, 2021, Virtual, Republic of Korea E.J. Gallego Arias, P. Jouvelot, S. Ribstein, and D. Desblancs

step-indexing in the topos of trees. Logical Methods in Computer Science8, 4 (2012). https://doi.org/10.2168/LMCS-8(4:1)2012

[13] Sylvain Boulmรฉ and Grรฉgoire Hamon. 2001. Certifying Synchrony forFree. In LPAR 2001. https://doi.org/10.1007/3-540-45653-8_34

[14] Timothy Bourke, Lรฉlio Brun, Pierre-Evariste Dagand, Xavier Leroy,Marc Pouzet, and Lionel Rieg. 2017. A Formally Verified Compiler forLustre. In PLDI 2017.

[15] Timothy Bourke, Lรฉlio Brun, and Marc Pouzet. 2020. Mechanized se-mantics and verified compilation for a dataflow synchronous languagewith reset. In POPL 2020. https://doi.org/10.1145/3371112

[16] Timothy Bourke, Jean-Louis Colaรงo, Bruno Pagano, Cรฉdric Pasteur, andMarc Pouzet. 2015. A Synchronous-Based Code Generator for ExplicitHybrid Systems Languages. In CC 2015. https://doi.org/10.1007/978-

3-662-46663-6_4

[17] Timothy Bourke and Marc Pouzet. 2013. Zรฉlus: a synchronous lan-guage with ODEs. In HSCC 2013. https://doi.org/10.1145/2461328.

2461348

[18] Timothy Bourke, Pierre ร‰variste Dagand, Marc Pouzet, and Lionel Rieg.2017. Verifying clock-directed modular code generation for Lustre. InJFLAโ€™17.

[19] Aloรฏs Brunel, Marco Gaboardi, Damiano Mazza, and Steve Zdancewic.2014. A Core Quantitative Coeffect Calculus. In Programming Lan-guages and Systems. https://doi.org/10.1007/978-3-642-54833-8_19

[20] Joseph T. Buck, Soonhoi Ha, Edward A. Lee, and David G. Messer-schmitt. 1994. Ptolemy: A Framework for Simulating and PrototypingHeterogenous Systems. Int. Journal in Computer Simulation.

[21] Paul Caspi, Grรฉgoire Hamon, and Marc Pouzet. 2008. Synchronousfunctional programming: The Lucid Synchrone experiment. Real-TimeSystems: Description and Verification Techniques: Theory and Tools..

[22] Paul Caspi, Daniel Pilaud, Nicolas Halbwachs, and John Plaice. 1987.Lustre: A Declarative Language for Programming Synchronous Sys-tems. In POPL 1987. https://doi.org/10.1145/41625.41641

[23] Paul Caspi and Marc Pouzet. 1996. Synchronous Kahn Networks. InICFP โ€™96. https://doi.org/10.1145/232627.232651

[24] Olivier Danvy. 2008. Defunctionalized interpreters for programminglanguages. In ICFP 2008. https://doi.org/10.1145/1411204.1411206

[25] Conal Elliott and Paul Hudak. 1997. Functional Reactive Animation.In ICFP โ€™97. https://doi.org/10.1145/258948.258973

[26] Marco Gaboardi, Shin-ya Katsumata, Dominic A. Orchard, FlavienBreuvart, and Tarmo Uustalu. 2016. Combining effects and coeffectsvia grading. In ICFP 2016. https://doi.org/10.1145/2951913.2951939

[27] Emilio Jesรบs Gallego Arias, Pierre Jouvelot, and Olivier Hermant. 2015.A Taste of Sound Reasoning in Faust. In Proceedings of the 13th LinuxAudio Conference. https://github.com/ejgallego/mini-faust-coq

[28] Abdoulaye Gamatiรฉ, Thierry Gautier, Paul Le Guernic, and Jean-PierreTalpin. 2007. Polychronous design of embedded real-time applications.ACM Trans. Softw. Eng. Methodol.. https://doi.org/10.1145/1217295.

1217298

[29] Georges Gonthier. 2011. Point-Free, Set-Free Concrete Linear Algebra.In ITP 2011. https://doi.org/10.1007/978-3-642-22863-6_10

[30] Adrien Guatto. 2016. A Synchronous Functional Language with IntegerClocks. Ph.D. Dissertation. ร‰cole Normale Supรฉrieure.

[31] John Hughes. 2000. Generalising monads to arrows. Sci. Comput.Program.. https://doi.org/10.1016/S0167-6423(99)00023-4

[32] JohnHughes. 2004. Programmingwith Arrows. InAdvanced FunctionalProgramming, 5th International School, AFP 2004. https://doi.org/10.

1007/11546382_2

[33] Pierre Jouvelot and Yann Orlarey. 2011. Dependent vector types fordata structuring in multirate Faust. Computer Languages, Systems &Structures. https://doi.org/10.1016/j.cl.2011.03.001

[34] Gilles Kahn. 1974. The Semantics of Simple Language for ParallelProgramming. In IFIP Congress.

[35] Oleg Kiselyov, Aggelos Biboudis, Nick Palladinos, and Yannis Smarag-dakis. 2017. Stream Fusion, to Completeness. In POPL 2017. https:

//doi.org/10.1145/3009837.3009880

[36] Neelakantan R. Krishnaswami. 2013. Higher-order functional reactiveprogramming without spacetime leaks. In ICFPโ€™13. https://doi.org/10.

1145/2500365.2500588

[37] Jakob Leben. 2016. Arrp: a functional language with multi-dimensionalsignals and recurrence equations. In FARM@ICFP 2016. https://doi.

org/10.1145/2975980.2975983

[38] Jakob Leben and George Tzanetakis. 2019. Polyhedral Compilationfor Multi-dimensional Stream Processing. ACM Trans. Archit. CodeOptim. https://doi.org/10.1145/3330999

[39] Edward A. Lee and David G. Messerschmitt. 1987. Static Scheduling ofSynchronous Data Flow Programs for Digital Signal Processing. IEEETrans. Computers. https://doi.org/10.1109/TC.1987.5009446

[40] Hai Liu, Eric Cheng, and Paul Hudak. 2009. Causal commutativearrows and their optimization. In ICFP 2009. https://doi.org/10.1145/

1596550.1596559

[41] Hai Liu, Eric Cheng, and Paul Hudak. 2011. Causal commutativearrows. J. Funct. Program.. https://doi.org/10.1017/S0956796811000153

[42] Louis Mandel, Cรฉdric Pasteur, and Marc Pouzet. 2015. Time refinementin a functional synchronous language. Sci. Comput. Program.. https:

//doi.org/10.1016/j.scico.2015.07.002

[43] Louis Mandel, Florence Plateau, and Marc Pouzet. 2010. Lucy-n: an-Synchronous Extension of Lustre. In MPC 2010. https://doi.org/10.

1007/978-3-642-13321-3_17

[44] Michael Mendler, Thomas R. Shiple, and Gรฉrard Berry. ConstructiveBoolean circuits and the exactness of timed ternary simulation. FormalMethods in System Design. https://doi.org/10.1007/s10703-012-0144-6

[45] Otmane Aรฏt Mohamed, Cรฉsar A. Muรฑoz, and Sofiรจne Tahar (Eds.). 2008.TPHOLs 2008.

[46] Hiroshi Nakano. 2000. A Modality for Recursion. In LICS 2000. https:

//doi.org/10.1109/LICS.2000.855774

[47] Henrik Nilsson, John Peterson, and Paul Hudak. 2003. FunctionalHybrid Modeling. In PADL 2003. https://doi.org/10.1007/3-540-36388-

2_25

[48] Yann Orlarey, Dominique Fober, and Stephane Letz. 2004. Syntacticaland semantical aspects of Faust. Soft Comput.. https://doi.org/10.1007/

s00500-004-0388-1

[49] Yann Orlarey and Pierre Jouvelot. 2016. Signal Rate Inference forMultidimensional Faust. In IFL 2016. https://doi.org/10.1145/3064899.

3064902

[50] Josรฉ Luis Pino, Soonhoi Ha, Edward A Lee, and Joseph T Buck. 1995.Software synthesis for DSP using Ptolemy.

[51] John C. Reynolds. 1998. Definitional Interpreters for Higher-OrderProgramming Languages. High. Order Symb. Comput.. https://doi.org/

10.1023/A:1010027404223

[52] Daniel Ricketts, Gregory Malecha, and Sorin Lerner. 2016. ModularDeductive Verification of Sampled-Data Systems. In EMSOFT.

[53] Umair Siddique, Mohamed Yousri Mahmoud, and Sofiรจne Tahar. 2014.On the Formalization of Z-Transform in HOL. In ITP 2014. https:

//doi.org/10.1007/978-3-319-08970-6_31

[54] Julius Orion Smith III. 2007. Introduction to Digital Filters: with AudioApplications. W3K Publishing. https://ccrma.stanford.edu/~jos/filters/

[55] Julius Orion Smith III. 2007. Mathematics of the Discrete FourierTransform (DFT): with Audio Applications (2nd ed.). W3K Publish-ing. https://ccrma.stanford.edu/~jos/mdft/

[56] Anis Souari, Amjad Gawanmeh, Sofiรจne Tahar, and Mohamed LassaadAmmari. Design and verification of a frequency domain equalizer.Microelectronics Journal (2014). https://doi.org/10.1016/j.mejo.2013.10.

012

[57] Sander Stuijk, Marc Geilen, and Twan Basten. 2006. SDF3: SDF ForFree.. In ACSD.

[58] William Thies, Michal Karczmarek, and Saman P. Amarasinghe. 2002.StreamIt: A Language for Streaming Applications. In CC 2002. https:

//doi.org/10.1007/3-540-45937-5_14

46