CORCON2014: Does programming really need data structures?

Marco Benini
Marco BeniniResearcher at Università dell'Insubria - Como
Does Programming Need Data Structures?
Correctness by Construction — CORCON 2014
Dr M Benini
Università degli Studi dell’Insubria
marco.benini@uninsubria.it
27th March 2014
Just a provocation?
The title of this talk is, of course, provocative. But, for the next 30 minutes,
I will take it seriously.
The aim is to show, through an elementary example, how one can change
the point of view on programming so to achieve a deeper understanding of
what means to compute. This deepening in knowledge has a practical
consequence, which is also the title of this workshop and of the project we
are involved in: correctness by construction.
So, the ultimate purpose of this talk is to address the impact of the project.
In the meanwhile, this talk allows me to introduce the idea behind my
contribution to the project.
(2 of 17)
Concrete and abstract lists
Usually, lists are defined as the elements of the free algebra over the
signature 〈{E,L},{nil:L,cons:E ×L → L}〉.
And, in the standard practice of traditional programming, they are
represented as follows:
a cell is a record in the computer memory which contains two fields:
the head which is an element in E;
the tail of the list which is a list;
in turn, a list is pointer (memory address) to a cell;
the empty list, nil, becomes the null pointer.
Thus, cons is a procedure that allocates a cell, fills the head with its first
parameter, and the tail with the second one, finally returning the its address.
(3 of 17)
Concrete manipulation of a list
As an example of program, we consider the concatenate function.
Its specification is: “Given the lists [x1,...,xn] and [y1,...,ym], concatenate
has to return the list [x1,...,xn,y1,...,ym]”.
Usually, it is implemented as
concatenate(x,y) ≡
if x = nil then return y
else q := x
while q = nil do
p := q
q := q → tail
p → tail := y
return x
(4 of 17)
Correctness
The previous algorithm is correct. In fact, when x = nil, it returns y,
satisfying the specification.
When x = nil, x = [x1,...,xn]. So, at the end of the i-th iteration step,
p = [xi ,...,xn] and q = [xi+1,...,xn], as it is immediate to prove by induction.
Also, the cycle terminates after n iterations, and p = [xn].
But, in the concrete representation of x, p → tail must be nil and the
assignment p → tail := y substitutes nil with y. So x = [x1,...,xn,y1,...,ym],
as required.
The proof sketched above uses in an essential way the concrete
representation of the x list, because the algorithm uses “list surgery”.
It is evident that the algorithm, and, thus, its correctness proof, is hardwired.
(5 of 17)
A functional derivation
Dropping list surgery, we can use the abstract formalisation of lists directly:
concatenate(x,y) ≡
if x = nil then return y
else return cons (hdx) (concatenate(tlx,y))
where hd and tl return the head and the tail of its argument, respectively.
Of course, this is a functional program, and it is justified by the following
reasoning, which can be immediately converted into a formal correctness
proof by induction on the structure of x:
1. we want that concatenate([x1,...,xn],[y1,...,ym]) = [x1,...,xn,y1,...,ym],
2. as before, if x = nil, the result is just y
3. when x = nil, concatenate([x1,...,xn],[y1,...,ym]) yields the same result
as consx1 (concatenate([x2,...,xn],[y1,...,ym]));
4. in the line above, the recursive application decreases the length of the
first argument, so recursion terminates after n steps.
(6 of 17)
Recursion versus induction
In the functional implementation of concatenate, we may interpret the
recursive schema as the computational counterpart of an inductive schema.
It is immediate to see that such an inductive schema becomes the skeleton
of the correctness proof. So, the functional program “carries” with itself a
proof of correctness, in some sense.
Usually, the functional implementation of concatenate is regarded as
inefficient because it recursively constructs a number of intermediate lists
before yielding the final results. Often, this is said to be the inevitable effect
of dropping list surgery.
(7 of 17)
Abstracting over lists
We formalised a list [x1,...,xm] as consx1 (consx2 (...(consxm nil)...)). We
can use a slightly different representation1:
λn,c. c x1 (c x2 (...(c xm n)...)) .
The key idea is to abstract over the structure of the data type, making it
part of the representation of the datum.
Alternatively, we can interpret this representation A as the abstract datum,
and the concrete one, C can be obtained by passing the instances of the
constructors A.
For example, the standard formalisation is obtained by Anilcons.
1As far as I know, the general algorithm to derive such a representation is due to
Böhm and Berarducci, and it can be traced back to Church
(8 of 17)
Interpreting abstract lists
An abstract list can be thought of as representing a term in the first-order
logical language with the equality relation symbol, and the signature of the
data type of lists.
The λ-term standing for the abstract list realises the mapping from the
logical term — the list, the body of the abstraction — into some model,
which is specified when we apply to the λ-term the way to interpret the
function symbols.
If we fix this point of view, we can write a “correct by construction”
implementation of concatenate:
concatenate ≡ λx,y,n,c. x (y nc) c .
(9 of 17)
Correctness by construction I
concatenate ≡ λx,y,n,c. x (y nc) c .
It is worth explaining the construction of this program:
1. it is a function, which takes two argument x and y;
2. it returns an abstract list, so a λ-term of the form λn,c. L, with L a
logical term in the language of lists;
3. the y abstract list gets interpreted in the same model as the result of
concatenate — and this is rendered by y nc;
4. the x abstract list gets interpreted in a model which has the same
interpretation for cons, but it interprets nil as the ‘concrete’ y.
We should remark that, in fact, this abstract implementation is, in essence,
the very same algorithm we have shown in the beginning, deprived from the
irrelevant details about the concrete data structure of lists.
(10 of 17)
Correctness by construction II
concatenate ≡ λx,y,n,c. x (y nc) c .
The above definition is a direct coding of the explanation. In turn, the
explanation can be converted into a correctness proof by observing that
the structure depicted in point (4) is a model for the theory of lists;
there is a mapping that preserves the meaning between the standard term
model and the model above;
this mapping is just the function concatenate.
The idea behind this proof is that the function concatenate, intended as a
program, is nothing but a morphism between models of the same theory.
A non-evident aspect of the explanation of concatenate is that concatenate
correctly operates in any model for the theory of lists.
(11 of 17)
One program, many meanings
For example, natural numbers, described as the structure generated by zero
and successor, are a model for lists: cons ≡ λe,l. sucl and nil ≡ 0. And
concatenate becomes just the usual addition.
For example, interpreting cons as the Cartesian product and nil as the
terminal object in a category with products, we get another model for lists.
And concatenate becomes just the Cartesian product of two products.
For example, interpreting cons as function application and nil as the identity
function, we get another model for lists. And concatenate becomes function
composition.
(12 of 17)
Interpretations and computing
A hidden aspect of interpreting a data type in a model is that computational
patterns can be rendered explicitly.
For example, if we take lists of trees as our model for lists, and we define hd
as the list containing the root elements, and tl as the list of their sons, the
abstract structure of a single tree corresponds to the procedure that
sequentially scans the tree breadth-first.
(13 of 17)
Generalising
Does it work only for lists?
The theory behind the abstract representation for data types has been
developed by Böhm and Berarducci, and it directly applies to all the data
structures that can be formalised as free algebras of terms over a
first-order signature. This holds for most of the elementary structures
which are used in the current practice of programming. In a similar way,
co-inductive data structures can be modelled as well.
For data structures which are not free (co-)algebras, there are still some
open problems, but, to some extent, they can be modelled in the same
spirit. That is, representing data as functions whose parameters describe
the “structure” of the data type.
Does it work in a “real” programming language?
As far as the programming language supports the dynamic creation of
functions, e.g., by providing abstraction, the technique can be
immediately used.
(14 of 17)
A philosophical remark
The title of this talk was “Does programming really need data structures?”.
Now, we can say that the answer is not immediately positive:
(YES) programming needs data, and data must be structured to be
represented and manipulated by a formal entity like a program;
(NO) programming does not need concrete data structures. In fact, a
program relies only on the structural properties of a data type to perform
its computation: as far as these properties are accessible, for example, as
explicit parameters, we can do without data structures;
(YES) when we conceive a program, we assume to work on data
represented according to some structure. It is possible (and, I claim,
convenient) to make this structure abstract, but a structure is still
present, and it shapes the way the computation is performed;
(NO) the abstract structure we pass to our representation of data is
nothing but an “interpretation” of a (logical) theory into a model. In
fact, we do not need to know how the model is represented, but only how
to express the mapping from the canonical model to the intended “world”
where the computation is assumed to take place.
(15 of 17)
Conclusions
In the previous slide there is a hidden assumption: that the logical theory
has a “canonical” model which can be transformed into a generic model via
a suitable mapping. This is not true in general. So the presented point of
view can be stretched only when considering logical theories having such a
classifying model — which is the case for free algebras of terms, for example.
In my recent research, I’ve shown a semantics for first-order intuitionistic
logical theories, based on a categorical setting, which has classifying models.
So, every such a theory could, in principle, be regarded as a “data structure”
in the sense of this talk.
My contribution to the CORCON research project will be to investigate
whether semantics like the above one can be effectively used to model data
structures in an programming environment.
Also, the side message of this talk is to show how even the most elementary
aspects of our project may have a non-trivial impact to the current practice
of programming. It is just a question of taking the “right” point of view,
after all. . .
(16 of 17)
The end
Tramonto, Rodi — © Marco Benini (2012)
(17 of 17)
1 of 17

Recommended

Programming modulo representations by
Programming modulo representationsProgramming modulo representations
Programming modulo representationsMarco Benini
292 views18 slides
Programming modulo representations by
Programming modulo representationsProgramming modulo representations
Programming modulo representationsMarco Benini
936 views18 slides
유한요소법(FEM)을 이용한 구조해석 by
유한요소법(FEM)을 이용한 구조해석유한요소법(FEM)을 이용한 구조해석
유한요소법(FEM)을 이용한 구조해석chimco.net
2.4K views11 slides
Presentation on dbms(relational calculus) by
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)yourbookworldanil
11.1K views17 slides
1643 y є r relational calculus-1 by
1643 y є r  relational calculus-11643 y є r  relational calculus-1
1643 y є r relational calculus-1Dr Fereidoun Dejahang
406 views17 slides
Implementation of “Parma Polyhedron Library”-functions in MATLAB by
Implementation of “Parma Polyhedron Library”-functions in MATLABImplementation of “Parma Polyhedron Library”-functions in MATLAB
Implementation of “Parma Polyhedron Library”-functions in MATLABLeo Asselborn
976 views12 slides

More Related Content

What's hot

B02402012022 by
B02402012022B02402012022
B02402012022inventionjournals
226 views11 slides
Papers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON Datatype by
Papers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON DatatypePapers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON Datatype
Papers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON DatatypeMax Klymyshyn
944 views28 slides
Recursion and Sorting Algorithms by
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting AlgorithmsAfaq Mansoor Khan
607 views73 slides
Kolmogorov Complexity, Art, and all that by
Kolmogorov Complexity, Art, and all thatKolmogorov Complexity, Art, and all that
Kolmogorov Complexity, Art, and all thatAleksandar Bradic
371 views19 slides
Programming with matlab session 4 by
Programming with matlab session 4Programming with matlab session 4
Programming with matlab session 4Infinity Tech Solutions
23.9K views18 slides
Chap11 scr by
Chap11 scrChap11 scr
Chap11 scrHirwanto Iwan
519 views31 slides

What's hot(19)

Papers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON Datatype by Max Klymyshyn
Papers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON DatatypePapers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON Datatype
Papers We Love Kyiv, July 2018: A Conflict-Free Replicated JSON Datatype
Max Klymyshyn944 views
Kolmogorov Complexity, Art, and all that by Aleksandar Bradic
Kolmogorov Complexity, Art, and all thatKolmogorov Complexity, Art, and all that
Kolmogorov Complexity, Art, and all that
Aleksandar Bradic371 views
Data structures using C by Pdr Patnaik
Data structures using CData structures using C
Data structures using C
Pdr Patnaik3.7K views
14. Query Optimization in DBMS by koolkampus
14. Query Optimization in DBMS14. Query Optimization in DBMS
14. Query Optimization in DBMS
koolkampus28.1K views
USING ADAPTIVE AUTOMATA IN GRAMMAR-BASED TEXT COMPRESSION TO IDENTIFY FREQUEN... by ijcsit
USING ADAPTIVE AUTOMATA IN GRAMMAR-BASED TEXT COMPRESSION TO IDENTIFY FREQUEN...USING ADAPTIVE AUTOMATA IN GRAMMAR-BASED TEXT COMPRESSION TO IDENTIFY FREQUEN...
USING ADAPTIVE AUTOMATA IN GRAMMAR-BASED TEXT COMPRESSION TO IDENTIFY FREQUEN...
ijcsit50 views
Karnaugh mapping allaboutcircuits by marangburu42
Karnaugh mapping allaboutcircuitsKarnaugh mapping allaboutcircuits
Karnaugh mapping allaboutcircuits
marangburu42446 views
Csc1100 lecture14 ch16_pt2 by IIUM
Csc1100 lecture14 ch16_pt2Csc1100 lecture14 ch16_pt2
Csc1100 lecture14 ch16_pt2
IIUM238 views
All aboutcircuits karnaugh maps by marangburu42
All aboutcircuits karnaugh mapsAll aboutcircuits karnaugh maps
All aboutcircuits karnaugh maps
marangburu42634 views
Data structure using c module 1 by smruti sarangi
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
smruti sarangi7.4K views
Relational Algebra & Calculus by Abdullah Khosa
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & Calculus
Abdullah Khosa3.3K views
Ctcompare: Comparing Multiple Code Trees for Similarity by DoctorWkt
Ctcompare: Comparing Multiple Code Trees for SimilarityCtcompare: Comparing Multiple Code Trees for Similarity
Ctcompare: Comparing Multiple Code Trees for Similarity
DoctorWkt389 views
Programming with effects - Graham Hutton by Wen-Shih Chao
Programming with effects - Graham HuttonProgramming with effects - Graham Hutton
Programming with effects - Graham Hutton
Wen-Shih Chao418 views

Similar to CORCON2014: Does programming really need data structures?

Master Thesis on the Mathematial Analysis of Neural Networks by
Master Thesis on the Mathematial Analysis of Neural NetworksMaster Thesis on the Mathematial Analysis of Neural Networks
Master Thesis on the Mathematial Analysis of Neural NetworksAlina Leidinger
96 views121 slides
Towards a New Data Modelling Architecture - Part 1 by
Towards a New Data Modelling Architecture - Part 1Towards a New Data Modelling Architecture - Part 1
Towards a New Data Modelling Architecture - Part 1JEAN-MICHEL LETENNIER
282 views17 slides
Applied parallel coordinates for logs and network traffic attack analysis by
Applied parallel coordinates for logs and network traffic attack analysisApplied parallel coordinates for logs and network traffic attack analysis
Applied parallel coordinates for logs and network traffic attack analysisUltraUploader
476 views29 slides
Statistics lab 1 by
Statistics lab 1Statistics lab 1
Statistics lab 1University of Salerno
305 views19 slides
Introduction to R by
Introduction to RIntroduction to R
Introduction to RUniversity of Salerno
478 views19 slides
Aad introduction by
Aad introductionAad introduction
Aad introductionMr SMAK
1K views50 slides

Similar to CORCON2014: Does programming really need data structures?(20)

Master Thesis on the Mathematial Analysis of Neural Networks by Alina Leidinger
Master Thesis on the Mathematial Analysis of Neural NetworksMaster Thesis on the Mathematial Analysis of Neural Networks
Master Thesis on the Mathematial Analysis of Neural Networks
Alina Leidinger96 views
Applied parallel coordinates for logs and network traffic attack analysis by UltraUploader
Applied parallel coordinates for logs and network traffic attack analysisApplied parallel coordinates for logs and network traffic attack analysis
Applied parallel coordinates for logs and network traffic attack analysis
UltraUploader476 views
Aad introduction by Mr SMAK
Aad introductionAad introduction
Aad introduction
Mr SMAK1K views
Fusing Transformations of Strict Scala Collections with Views by Philip Schwarz
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
Philip Schwarz20 views
Machine learning (11) by NYversity
Machine learning (11)Machine learning (11)
Machine learning (11)
NYversity192 views
Functional Programming in JavaScript by Will Livengood
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
Will Livengood873 views
Csit77406 by csandit
Csit77406Csit77406
Csit77406
csandit35 views
Scala. Introduction to FP. Monads by Kirill Kozlov
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
Kirill Kozlov1.1K views
Discovering Novel Information with sentence Level clustering From Multi-docu... by irjes
Discovering Novel Information with sentence Level clustering  From Multi-docu...Discovering Novel Information with sentence Level clustering  From Multi-docu...
Discovering Novel Information with sentence Level clustering From Multi-docu...
irjes316 views

More from Marco Benini

Point-free semantics of dependent type theories by
Point-free semantics of dependent type theoriesPoint-free semantics of dependent type theories
Point-free semantics of dependent type theoriesMarco Benini
432 views20 slides
The Graph Minor Theorem: a walk on the wild side of graphs by
The Graph Minor Theorem: a walk on the wild side of graphsThe Graph Minor Theorem: a walk on the wild side of graphs
The Graph Minor Theorem: a walk on the wild side of graphsMarco Benini
319 views27 slides
Explaining the Kruskal Tree Theore by
Explaining the Kruskal Tree TheoreExplaining the Kruskal Tree Theore
Explaining the Kruskal Tree TheoreMarco Benini
557 views33 slides
The Graph Minor Theorem: a walk on the wild side of graphs by
The Graph Minor Theorem: a walk on the wild side of graphsThe Graph Minor Theorem: a walk on the wild side of graphs
The Graph Minor Theorem: a walk on the wild side of graphsMarco Benini
293 views31 slides
Dealing with negative results by
Dealing with negative resultsDealing with negative results
Dealing with negative resultsMarco Benini
242 views58 slides
Variations on the Higman's Lemma by
Variations on the Higman's LemmaVariations on the Higman's Lemma
Variations on the Higman's LemmaMarco Benini
518 views30 slides

More from Marco Benini(20)

Point-free semantics of dependent type theories by Marco Benini
Point-free semantics of dependent type theoriesPoint-free semantics of dependent type theories
Point-free semantics of dependent type theories
Marco Benini432 views
The Graph Minor Theorem: a walk on the wild side of graphs by Marco Benini
The Graph Minor Theorem: a walk on the wild side of graphsThe Graph Minor Theorem: a walk on the wild side of graphs
The Graph Minor Theorem: a walk on the wild side of graphs
Marco Benini319 views
Explaining the Kruskal Tree Theore by Marco Benini
Explaining the Kruskal Tree TheoreExplaining the Kruskal Tree Theore
Explaining the Kruskal Tree Theore
Marco Benini557 views
The Graph Minor Theorem: a walk on the wild side of graphs by Marco Benini
The Graph Minor Theorem: a walk on the wild side of graphsThe Graph Minor Theorem: a walk on the wild side of graphs
The Graph Minor Theorem: a walk on the wild side of graphs
Marco Benini293 views
Dealing with negative results by Marco Benini
Dealing with negative resultsDealing with negative results
Dealing with negative results
Marco Benini242 views
Variations on the Higman's Lemma by Marco Benini
Variations on the Higman's LemmaVariations on the Higman's Lemma
Variations on the Higman's Lemma
Marco Benini518 views
Dealing with negative results by Marco Benini
Dealing with negative resultsDealing with negative results
Dealing with negative results
Marco Benini390 views
Well Quasi Orders in a Categorical Setting by Marco Benini
Well Quasi Orders in a Categorical SettingWell Quasi Orders in a Categorical Setting
Well Quasi Orders in a Categorical Setting
Marco Benini590 views
Proof-Theoretic Semantics: Point-free meaninig of first-order systems by Marco Benini
Proof-Theoretic Semantics: Point-free meaninig of first-order systemsProof-Theoretic Semantics: Point-free meaninig of first-order systems
Proof-Theoretic Semantics: Point-free meaninig of first-order systems
Marco Benini377 views
Point-free foundation of Mathematics by Marco Benini
Point-free foundation of MathematicsPoint-free foundation of Mathematics
Point-free foundation of Mathematics
Marco Benini583 views
Fondazione point-free della matematica by Marco Benini
Fondazione point-free della matematicaFondazione point-free della matematica
Fondazione point-free della matematica
Marco Benini394 views
Numerical Analysis and Epistemology of Information by Marco Benini
Numerical Analysis and Epistemology of InformationNumerical Analysis and Epistemology of Information
Numerical Analysis and Epistemology of Information
Marco Benini747 views
L'occhio del biologo: elementi di fotografia by Marco Benini
L'occhio del biologo: elementi di fotografiaL'occhio del biologo: elementi di fotografia
L'occhio del biologo: elementi di fotografia
Marco Benini731 views
Constructive Adpositional Grammars, Formally by Marco Benini
Constructive Adpositional Grammars, FormallyConstructive Adpositional Grammars, Formally
Constructive Adpositional Grammars, Formally
Marco Benini491 views
Marie Skłodowska Curie Intra-European Fellowship by Marco Benini
Marie Skłodowska Curie Intra-European FellowshipMarie Skłodowska Curie Intra-European Fellowship
Marie Skłodowska Curie Intra-European Fellowship
Marco Benini463 views
Algorithms and Their Explanations by Marco Benini
Algorithms and Their ExplanationsAlgorithms and Their Explanations
Algorithms and Their Explanations
Marco Benini508 views
June 22nd 2014: Seminar at JAIST by Marco Benini
June 22nd 2014: Seminar at JAISTJune 22nd 2014: Seminar at JAIST
June 22nd 2014: Seminar at JAIST
Marco Benini411 views
Fondazione point-free della matematica by Marco Benini
Fondazione point-free della matematicaFondazione point-free della matematica
Fondazione point-free della matematica
Marco Benini1K views
Adgrams: Categories and Linguistics by Marco Benini
 Adgrams: Categories and Linguistics Adgrams: Categories and Linguistics
Adgrams: Categories and Linguistics
Marco Benini541 views
Intuitionistic First-Order Logic: Categorical semantics via the Curry-Howard ... by Marco Benini
Intuitionistic First-Order Logic: Categorical semantics via the Curry-Howard ...Intuitionistic First-Order Logic: Categorical semantics via the Curry-Howard ...
Intuitionistic First-Order Logic: Categorical semantics via the Curry-Howard ...
Marco Benini531 views

Recently uploaded

1978 NASA News Release Log by
1978 NASA News Release Log1978 NASA News Release Log
1978 NASA News Release Logpurrterminator
9 views146 slides
Small ruminant keepers’ knowledge, attitudes and practices towards peste des ... by
Small ruminant keepers’ knowledge, attitudes and practices towards peste des ...Small ruminant keepers’ knowledge, attitudes and practices towards peste des ...
Small ruminant keepers’ knowledge, attitudes and practices towards peste des ...ILRI
5 views6 slides
"How can I develop my learning path in bioinformatics? by
"How can I develop my learning path in bioinformatics?"How can I develop my learning path in bioinformatics?
"How can I develop my learning path in bioinformatics?Bioinformy
23 views13 slides
Disinfectants & Antiseptic by
Disinfectants & AntisepticDisinfectants & Antiseptic
Disinfectants & AntisepticSanket P Shinde
10 views36 slides
Metatheoretical Panda-Samaneh Borji.pdf by
Metatheoretical Panda-Samaneh Borji.pdfMetatheoretical Panda-Samaneh Borji.pdf
Metatheoretical Panda-Samaneh Borji.pdfsamanehborji
16 views29 slides
PRINCIPLES-OF ASSESSMENT by
PRINCIPLES-OF ASSESSMENTPRINCIPLES-OF ASSESSMENT
PRINCIPLES-OF ASSESSMENTrbalmagro
12 views12 slides

Recently uploaded(20)

Small ruminant keepers’ knowledge, attitudes and practices towards peste des ... by ILRI
Small ruminant keepers’ knowledge, attitudes and practices towards peste des ...Small ruminant keepers’ knowledge, attitudes and practices towards peste des ...
Small ruminant keepers’ knowledge, attitudes and practices towards peste des ...
ILRI5 views
"How can I develop my learning path in bioinformatics? by Bioinformy
"How can I develop my learning path in bioinformatics?"How can I develop my learning path in bioinformatics?
"How can I develop my learning path in bioinformatics?
Bioinformy23 views
Metatheoretical Panda-Samaneh Borji.pdf by samanehborji
Metatheoretical Panda-Samaneh Borji.pdfMetatheoretical Panda-Samaneh Borji.pdf
Metatheoretical Panda-Samaneh Borji.pdf
samanehborji16 views
PRINCIPLES-OF ASSESSMENT by rbalmagro
PRINCIPLES-OF ASSESSMENTPRINCIPLES-OF ASSESSMENT
PRINCIPLES-OF ASSESSMENT
rbalmagro12 views
Conventional and non-conventional methods for improvement of cucurbits.pptx by gandhi976
Conventional and non-conventional methods for improvement of cucurbits.pptxConventional and non-conventional methods for improvement of cucurbits.pptx
Conventional and non-conventional methods for improvement of cucurbits.pptx
gandhi97618 views
application of genetic engineering 2.pptx by SankSurezz
application of genetic engineering 2.pptxapplication of genetic engineering 2.pptx
application of genetic engineering 2.pptx
SankSurezz9 views
ENTOMOLOGY PPT ON BOMBYCIDAE AND SATURNIIDAE.pptx by MN
ENTOMOLOGY PPT ON BOMBYCIDAE AND SATURNIIDAE.pptxENTOMOLOGY PPT ON BOMBYCIDAE AND SATURNIIDAE.pptx
ENTOMOLOGY PPT ON BOMBYCIDAE AND SATURNIIDAE.pptx
MN7 views
CSF -SHEEBA.D presentation.pptx by SheebaD7
CSF -SHEEBA.D presentation.pptxCSF -SHEEBA.D presentation.pptx
CSF -SHEEBA.D presentation.pptx
SheebaD711 views
A Ready-to-Analyze High-Plex Spatial Signature Development Workflow for Cance... by InsideScientific
A Ready-to-Analyze High-Plex Spatial Signature Development Workflow for Cance...A Ready-to-Analyze High-Plex Spatial Signature Development Workflow for Cance...
A Ready-to-Analyze High-Plex Spatial Signature Development Workflow for Cance...
InsideScientific49 views
Distinct distributions of elliptical and disk galaxies across the Local Super... by Sérgio Sacani
Distinct distributions of elliptical and disk galaxies across the Local Super...Distinct distributions of elliptical and disk galaxies across the Local Super...
Distinct distributions of elliptical and disk galaxies across the Local Super...
Sérgio Sacani31 views
MODULE-9-Biotechnology, Genetically Modified Organisms, and Gene Therapy.pdf by KerryNuez1
MODULE-9-Biotechnology, Genetically Modified Organisms, and Gene Therapy.pdfMODULE-9-Biotechnology, Genetically Modified Organisms, and Gene Therapy.pdf
MODULE-9-Biotechnology, Genetically Modified Organisms, and Gene Therapy.pdf
KerryNuez124 views
A training, certification and marketing scheme for informal dairy vendors in ... by ILRI
A training, certification and marketing scheme for informal dairy vendors in ...A training, certification and marketing scheme for informal dairy vendors in ...
A training, certification and marketing scheme for informal dairy vendors in ...
ILRI13 views

CORCON2014: Does programming really need data structures?

  • 1. Does Programming Need Data Structures? Correctness by Construction — CORCON 2014 Dr M Benini Università degli Studi dell’Insubria marco.benini@uninsubria.it 27th March 2014
  • 2. Just a provocation? The title of this talk is, of course, provocative. But, for the next 30 minutes, I will take it seriously. The aim is to show, through an elementary example, how one can change the point of view on programming so to achieve a deeper understanding of what means to compute. This deepening in knowledge has a practical consequence, which is also the title of this workshop and of the project we are involved in: correctness by construction. So, the ultimate purpose of this talk is to address the impact of the project. In the meanwhile, this talk allows me to introduce the idea behind my contribution to the project. (2 of 17)
  • 3. Concrete and abstract lists Usually, lists are defined as the elements of the free algebra over the signature 〈{E,L},{nil:L,cons:E ×L → L}〉. And, in the standard practice of traditional programming, they are represented as follows: a cell is a record in the computer memory which contains two fields: the head which is an element in E; the tail of the list which is a list; in turn, a list is pointer (memory address) to a cell; the empty list, nil, becomes the null pointer. Thus, cons is a procedure that allocates a cell, fills the head with its first parameter, and the tail with the second one, finally returning the its address. (3 of 17)
  • 4. Concrete manipulation of a list As an example of program, we consider the concatenate function. Its specification is: “Given the lists [x1,...,xn] and [y1,...,ym], concatenate has to return the list [x1,...,xn,y1,...,ym]”. Usually, it is implemented as concatenate(x,y) ≡ if x = nil then return y else q := x while q = nil do p := q q := q → tail p → tail := y return x (4 of 17)
  • 5. Correctness The previous algorithm is correct. In fact, when x = nil, it returns y, satisfying the specification. When x = nil, x = [x1,...,xn]. So, at the end of the i-th iteration step, p = [xi ,...,xn] and q = [xi+1,...,xn], as it is immediate to prove by induction. Also, the cycle terminates after n iterations, and p = [xn]. But, in the concrete representation of x, p → tail must be nil and the assignment p → tail := y substitutes nil with y. So x = [x1,...,xn,y1,...,ym], as required. The proof sketched above uses in an essential way the concrete representation of the x list, because the algorithm uses “list surgery”. It is evident that the algorithm, and, thus, its correctness proof, is hardwired. (5 of 17)
  • 6. A functional derivation Dropping list surgery, we can use the abstract formalisation of lists directly: concatenate(x,y) ≡ if x = nil then return y else return cons (hdx) (concatenate(tlx,y)) where hd and tl return the head and the tail of its argument, respectively. Of course, this is a functional program, and it is justified by the following reasoning, which can be immediately converted into a formal correctness proof by induction on the structure of x: 1. we want that concatenate([x1,...,xn],[y1,...,ym]) = [x1,...,xn,y1,...,ym], 2. as before, if x = nil, the result is just y 3. when x = nil, concatenate([x1,...,xn],[y1,...,ym]) yields the same result as consx1 (concatenate([x2,...,xn],[y1,...,ym])); 4. in the line above, the recursive application decreases the length of the first argument, so recursion terminates after n steps. (6 of 17)
  • 7. Recursion versus induction In the functional implementation of concatenate, we may interpret the recursive schema as the computational counterpart of an inductive schema. It is immediate to see that such an inductive schema becomes the skeleton of the correctness proof. So, the functional program “carries” with itself a proof of correctness, in some sense. Usually, the functional implementation of concatenate is regarded as inefficient because it recursively constructs a number of intermediate lists before yielding the final results. Often, this is said to be the inevitable effect of dropping list surgery. (7 of 17)
  • 8. Abstracting over lists We formalised a list [x1,...,xm] as consx1 (consx2 (...(consxm nil)...)). We can use a slightly different representation1: λn,c. c x1 (c x2 (...(c xm n)...)) . The key idea is to abstract over the structure of the data type, making it part of the representation of the datum. Alternatively, we can interpret this representation A as the abstract datum, and the concrete one, C can be obtained by passing the instances of the constructors A. For example, the standard formalisation is obtained by Anilcons. 1As far as I know, the general algorithm to derive such a representation is due to Böhm and Berarducci, and it can be traced back to Church (8 of 17)
  • 9. Interpreting abstract lists An abstract list can be thought of as representing a term in the first-order logical language with the equality relation symbol, and the signature of the data type of lists. The λ-term standing for the abstract list realises the mapping from the logical term — the list, the body of the abstraction — into some model, which is specified when we apply to the λ-term the way to interpret the function symbols. If we fix this point of view, we can write a “correct by construction” implementation of concatenate: concatenate ≡ λx,y,n,c. x (y nc) c . (9 of 17)
  • 10. Correctness by construction I concatenate ≡ λx,y,n,c. x (y nc) c . It is worth explaining the construction of this program: 1. it is a function, which takes two argument x and y; 2. it returns an abstract list, so a λ-term of the form λn,c. L, with L a logical term in the language of lists; 3. the y abstract list gets interpreted in the same model as the result of concatenate — and this is rendered by y nc; 4. the x abstract list gets interpreted in a model which has the same interpretation for cons, but it interprets nil as the ‘concrete’ y. We should remark that, in fact, this abstract implementation is, in essence, the very same algorithm we have shown in the beginning, deprived from the irrelevant details about the concrete data structure of lists. (10 of 17)
  • 11. Correctness by construction II concatenate ≡ λx,y,n,c. x (y nc) c . The above definition is a direct coding of the explanation. In turn, the explanation can be converted into a correctness proof by observing that the structure depicted in point (4) is a model for the theory of lists; there is a mapping that preserves the meaning between the standard term model and the model above; this mapping is just the function concatenate. The idea behind this proof is that the function concatenate, intended as a program, is nothing but a morphism between models of the same theory. A non-evident aspect of the explanation of concatenate is that concatenate correctly operates in any model for the theory of lists. (11 of 17)
  • 12. One program, many meanings For example, natural numbers, described as the structure generated by zero and successor, are a model for lists: cons ≡ λe,l. sucl and nil ≡ 0. And concatenate becomes just the usual addition. For example, interpreting cons as the Cartesian product and nil as the terminal object in a category with products, we get another model for lists. And concatenate becomes just the Cartesian product of two products. For example, interpreting cons as function application and nil as the identity function, we get another model for lists. And concatenate becomes function composition. (12 of 17)
  • 13. Interpretations and computing A hidden aspect of interpreting a data type in a model is that computational patterns can be rendered explicitly. For example, if we take lists of trees as our model for lists, and we define hd as the list containing the root elements, and tl as the list of their sons, the abstract structure of a single tree corresponds to the procedure that sequentially scans the tree breadth-first. (13 of 17)
  • 14. Generalising Does it work only for lists? The theory behind the abstract representation for data types has been developed by Böhm and Berarducci, and it directly applies to all the data structures that can be formalised as free algebras of terms over a first-order signature. This holds for most of the elementary structures which are used in the current practice of programming. In a similar way, co-inductive data structures can be modelled as well. For data structures which are not free (co-)algebras, there are still some open problems, but, to some extent, they can be modelled in the same spirit. That is, representing data as functions whose parameters describe the “structure” of the data type. Does it work in a “real” programming language? As far as the programming language supports the dynamic creation of functions, e.g., by providing abstraction, the technique can be immediately used. (14 of 17)
  • 15. A philosophical remark The title of this talk was “Does programming really need data structures?”. Now, we can say that the answer is not immediately positive: (YES) programming needs data, and data must be structured to be represented and manipulated by a formal entity like a program; (NO) programming does not need concrete data structures. In fact, a program relies only on the structural properties of a data type to perform its computation: as far as these properties are accessible, for example, as explicit parameters, we can do without data structures; (YES) when we conceive a program, we assume to work on data represented according to some structure. It is possible (and, I claim, convenient) to make this structure abstract, but a structure is still present, and it shapes the way the computation is performed; (NO) the abstract structure we pass to our representation of data is nothing but an “interpretation” of a (logical) theory into a model. In fact, we do not need to know how the model is represented, but only how to express the mapping from the canonical model to the intended “world” where the computation is assumed to take place. (15 of 17)
  • 16. Conclusions In the previous slide there is a hidden assumption: that the logical theory has a “canonical” model which can be transformed into a generic model via a suitable mapping. This is not true in general. So the presented point of view can be stretched only when considering logical theories having such a classifying model — which is the case for free algebras of terms, for example. In my recent research, I’ve shown a semantics for first-order intuitionistic logical theories, based on a categorical setting, which has classifying models. So, every such a theory could, in principle, be regarded as a “data structure” in the sense of this talk. My contribution to the CORCON research project will be to investigate whether semantics like the above one can be effectively used to model data structures in an programming environment. Also, the side message of this talk is to show how even the most elementary aspects of our project may have a non-trivial impact to the current practice of programming. It is just a question of taking the “right” point of view, after all. . . (16 of 17)
  • 17. The end Tramonto, Rodi — © Marco Benini (2012) (17 of 17)