R eusable S emant ic S pecif icat ions of P rogramming Languages - - PDF document

r eusable s emant ic s pecif icat ions of p rogramming
SMART_READER_LITE
LIVE PREVIEW

R eusable S emant ic S pecif icat ions of P rogramming Languages - - PDF document

R eusable S emant ic S pecif icat ions of P rogramming Languages Jose Emilio Labra Gayo Department of Computer Science University of Oviedo - Spain April - 2002 1 This talk in 1 We need formal techniques to specify the semantics of


slide-1
SLIDE 1

1

R eusable S emant ic S pecif icat ions of P rogramming Languages

Jose Emilio Labra Gayo Department of Computer Science University of Oviedo - Spain

April - 2002

1

This talk in 1’

  • We need formal techniques to specify the semantics of

programming languages, but...

– Traditional existing techniques lack modularity and reusability

  • In my PhD, I proposed a technique that combines:

– Monads (...inspired from Haskell...) – Generic programming (...catamorphisms...)

  • I specified

– Simple functional, imperative, object-oriented and logic programming languages

2

slide-2
SLIDE 2

2

3

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Programming Languages

  • Programming Language

– Notation used by a programmer (person) to transmit to the computer (machine) an intended behaviour

Introduction

Programmer Computer Program

program Sum int x, y; begin read x; read y; write x+y end

Intended behaviour: Add 2 integers Trade-off between the needs of humans and computers 4

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Lenguage Processors

  • Language Processor

– Analizes the intended behaviour of programs written in a language – Either executes the instructions (interpreter)

  • r builds an equivalent program in a different language (compiler)

Introduction

Programmer

Program

Interpreter Data Results

  • The language processor is obtained from the specification of the

programming language

– That specification must describe useful computations – And it should be architecture independent

slide-3
SLIDE 3

3

5

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Language Processors

Traditional Modular decomposition of a language processor – Identifies processes Introduction

Program

Lexical analysis Syntax analysisr Static analysis Execution/ Code Generation

Problem Given a language processor formed by Arithmetic expressions, loops and conditionals

  • Where is the code corresponding to arithmetic expressions?
  • Which modules do we have to modify to add boolean expressions?
  • Is it possible to reuse the source code of this processor to build the

processor of a new language?

GNU C++ compiler gcc 1.0 278949 lines of code 168 separate files 6

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Language Processors

  • A different kind of modular decomposition...

Introduction

Arithmetic Expressions While loops Conditionals Boolean Expressions

Modularity

Language A

Abstractions Local declarations Objects Arithmetic Expressions

Reusability

Language B

slide-4
SLIDE 4

4

7

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Semantic Specification

  • Semantic specification = Description of program behaviour
  • Usually made in natural language

? It is flexible and there is experience in its application ? Modular, reusable, readable

(well…, at least, in principle)

? It is also ambiguous and poorly rigorous ? It is not possible:

  • Prove properties of programs
  • Check correctness of a given language processor
  • Automatically obtain a language processor

Introduction

  • Ideal requisites of a semantic specification technique

Non ambiguity Prove properties Modularity Reusability Automatically generate language processor (or prototype) Flexibility Experience Readability ...A labeled st at ement is execut ed by execut ing t he immediat ely cont ained St at ement. I f t he st at ement is labeled by an I dent if ier and t he cont ained St at ement complet es abr upt ly because of a br eak wit h t he same I dent if ier , t hen t he labeled st at ement complet es nor mally. I n all ot her cases of abr upt complet ion of t he St at ement , t he labeled st at ement complet es abr upt ly f or t he same r eason.... (J ava Language Specif icat ion 2nd. Ed)

Comparison of Specification Techniques

  • Introduction
  • Comparison of semantic specification techniques

– Structured operational semantics – Natural semantics – Denotational semantics – Axiomatic semantics – Algebraic semantics – Abstract State Machines (ASM) – Action Semantics

  • Modular Monadic Semantics
  • Generic Programming
  • Reusable Monadic Semantics
  • Conclusions

8

slide-5
SLIDE 5

5

9

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

[Plotkin, 81]

  • Operations are defined using inference rules

Structured Operational Semantics

Comparison of techniques

eval

? Const n? n

eval

? Add e1 e2? v 1 + v 2

eval

v 1 ? e1?

eval

v 2 ? e2?

? Flexibility ? Experience ? Modularity ? Reusability ? Proof ? Prototype Assessment

eval : Expr? Int

eval

? Const n, ? ? n

eval

? Add e1 e2,? ? v 1 + v 2

eval

v 1 ? e1,? ?

eval

v 2 ? e2,? ?

eval

? Var x, ? ? lkp x ?

If we add variables...

eval : (Expr, Env )? Int 10

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Natural Semantics

Comparison of techniques

[Kahn, 87]

  • Similar to structured operational semantics but it just describes the

relationship between the initial and final state

  • Several tools to develop prototypes from natural semantic

specifications (Centaur, RML, etc.)

  • Several real languages specified using this approach (e.g. ML)
  • Same modularity and reusability problems as structured operational

semantics

? Modularity ? Reusability ? Proof Assessment ? Flexibility ? Experience ? Prototype

slide-6
SLIDE 6

6

11

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Denotational Semantics

[Strachey , Scott, 69]

  • Each component of the language = mathematical entity defined using

ambda calculus and domain theory

  • Facilitates proof of properties

? Modularity ? Reusability ? Prototype ? Experience Assessment ? Flexibility ? Proof

?_ ?

: Term ? I nt

?

Add e1 e2?= ? e1?+ ? e2?

?

Const n?= n

?_ ?

: Ter m? Env ? I nt

?

Add e1 e2?? = ? e1? ? + ? e2??

?

Const n?? = n

?

Var x? ? = lkp ? x If we add variables ...

Comparison of techniques

12

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Axiomatic Semantics

? Flexibility ? Modularity ? Reusability ? Prototype Assessment ? Proof ? Experience [Floyd 67, Hoare 69]

  • Language properties are described using axioms in a logical system with

assertions, preconditions and postconditions

  • Facilitates the proof of properties
  • Experience in its application to imperative languages
  • Several implementations can follow the axioms ?

Difficult automatization

  • f prototype generation

Comparison of techniques

slide-7
SLIDE 7

7

13

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Algebraic Semantics

  • Roots: Algebraic specification techniques
  • Algebraic specification:

– Signature (sorts and operations) – Axioms (properties) using logical equations

  • Algebra = sets + functions (implements an specification)
  • Combines denotational, operational and axiomatic semantics
  • Several specification systems (Obj, ASL, ...)
  • In [Goguen 96] the specifications are monolithic

? Modularity ? Reusability ? Experience ? Flexibility Assessment ? Proof ? Prototype

Comparison of techniques

14

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Abstract State Machines

  • Roots: Evolving Algebra and Operational Semantics
  • Algorithm = state abstraction + transition rules between elements of that

state

  • Modularity is achieved by specializing the abstract state
  • Several industrial applications in the specification of languages and

protocols

  • Tools for prototype generation

Assessment ? Prototype ? Experience ? Modularity ? Proof ? Flexibility ? Reusability

Comparison of techniques

slide-8
SLIDE 8

8

15

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Action Semantics

[Mosses 92]

  • Assigns actions to language constructions
  • Primitive actions and action combinators
  • Types of actions are described using facets that represent computational

aspects ? semantic modularity

  • Emphasis on readability
  • Action notation uses operational semantics
  • Recent developments trying to add reusability [Doh,Mosses 01]

Assessment ? Modularity ? Prototype ? Readability ? Proof ? Experience ? Flexibility ? Reusability

Comparison of techniques

Modular Monadic Semantics

  • Introduction
  • Comparison of techniques
  • Modular Monadic Semantics

– Monads – Monad transformers – Extensible domains – Assessment

  • Generic Programming
  • Reusable Monadic Semantics
  • Conclusions

16

slide-9
SLIDE 9

9

17

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Monads

[Moggi, 89]

  • A Monad M captures the intuitive notion of computation

M a = computation that returns values of type a Operations ret urn :: a ? M a (À =) :: M a ? (a ? M b) ? M b which satisfy a number of properties read2 : I O Bool read2 = do c1 ? get Char c2 ? get Char ret urn (c1 == c 2 )

do-notation

read2 : I O Bool read2 = get Char À = ?c1? get Char À = ?c2? ret urn (c1 == c 2 )

Example: read 2 characters and compare if they are equal

  • Haskell uses the predefined IO monad for Input/Output
  • Several operations:

get Char : I O Char put Char : Char ? I O ()

Modular Monadic Semantics

18

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Monads and Semantics

?_ ?

: Term ? I nt

?

Add e1 e2?= ? e1?+ ? e2?

?

Const n?= n

Traditional specification

?_ ?: Term ?

M I nt

?

Add e1 e2 ?= do v1 ? ? e1? v2 ? ? e2? ret urn (v1 +v2 )

?

Const n?= ret urn n

Using monads

... The above definitions are still valid

?

Var x? = do ? ? rdEnv ret urn lkp ? x

Using Monads

We j ust modif y t he t ype of M ... If you add variables

?_ ?

: Ter m? Env ? I nt

?

Add e1 e2?? = ? e1?? + ? e2? ?

?

Const n?? = n

?

Var x? ? = lkp ? x

Traditional specification

We must modify previous specifications

Modular Monadic Semantics

slide-10
SLIDE 10

10

19

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Types of monads

Exceptions

r aise : Exc ? M a handle: M a ? (Exc ? M a) ? M a f et ch : M St at e set : St at e ? M () updat e : (St at e ? St at e) ? M St at e

State modification

r dEnv : M Env inEnv : Env ? M a ? M a

Environment access Input/Output

get : M Char put : Char ? M ()

... Others like non-determinism, Concurrency, etc. Backtracking

f ailure : M a d : M a ? M a ? M a

Continuations

callcc : ((a ? M b) ? M a) ? M a

Each operation satisfies a number of axioms

Example:

f ailure d m ? m

Modular Monadic Semantics

Problem: Composition

  • f monads

Different monads for different types of computations

20

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Monad Transformer

  • Monad transformer [Liang, Hudak, Jones 95]
  • A monad transformer ? takes a monad M and obtains a new monad

M’ adding new operations ? : M ? M’

  • Conditions:

– M’ must really be a monad

– The behaviour of the old operations of M must be the same in M’ – We must take into account the interaction between computational features – Actually, this lifting is made by hand

Modular Monadic Semantics

slide-11
SLIDE 11

11

21

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Monad Transformers

St at eT ? ExcT ? EnvT ? Cont T ?

ExcT ? m v ? m (? + v) St at eT ? m v ? ? ? m (v, ? ) EnvT ? m v ? ? ? m v Cont T ? m v ? (v ? m ? ) ? m ?

... others... BackT

BackT m v ? ? ? (v ? m ? ? m ? ) ? m ? ? m ?

Final Monad

M = (MT1 · MT2 ·...· MTn) BasicMonad This composition is not commutative

Modular Monadic Semantics

22

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Extensible domains

a || b ? L a | R b class Subt ype a b where ? : a ? b ? : b ? a inst ance Subt ype a (a || b) wher e ... inst ance (Subt ype a b) ? Subt ype a (x || b) wher e ... Example: Value = I nt || Bool || () x : Value x = ? True Multi-parameter type class Value = I nt || Bool || Char | | ()

Modular Monadic Semantics

slide-12
SLIDE 12

12

23

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Assessment

  • Modular monadic semantics [Liang, Hudak, Jones 95]
  • Monad transformers ?

Semantic modularity

  • Axiomatization of different types of monads ?

Proof of properties

  • Monolithic interpreters ?

They don’t tackle reusability

  • Flexibility

– It is only applied to a functional language with imperative features – In principle, it could be applied to other programming language paradigms

  • A prototype generation tool is not developed, but it could be done

Assessment ? Modularity ? Proof ? Flexibility ? Prototype ? Reusability ? Experience

Modular Monadic Semantics

Generic Programming

  • Introduction
  • Comparison of techniques
  • Modular monadic semantics
  • Generic Programming

– Recursive definitions (functor,fixed-point, algebras ) – Catamorphisms – Monadic catamorphisms – Bicatamorphims

  • Language Prototyping System
  • Conclusions

24

slide-13
SLIDE 13

13

25

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Extensible recursive definitions

Generic Programming

Term ? Add Term Term | Const Int (F ? G) x ? F x | | G x P e ? Mult e e V e ? Var Name Expr ? ? (T ? P ? V) Extension Sum of functors In (Const 3) In (Mult (In (Const 3)) (In (Const 4))) In (Var “x”) . . .

Problem: Add multiplications, variables ...

T e ? Add e e | Const Int Term ? ? T In (Const 3) In (Add (In (Const 3)) (In (Const 4))) . . . Non-recursive Functor Format ? F ? In (F (?F)) Recursive definition Fixed-point

Solution: Separate 1.- Format 2.- Recursive definition

x ? F x 26

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Functors, Algebras and Catamorphisms

Generic Programming

Functor F map : (a ? b) ? F a ? F b Initial F-algebra I n : F ? F ? ? F is an initial object in the category of F-algebras, i.e. ? F-algebra ?:F a ? a there is a unique ? ??: ?F ? a such that: F ?F ?F I n F a a ? ? ?? map ? ??

? ??= catamorphism or fold ? _?: (F a ?

a) ? ?F ? a map : (a ? b) ? T a ? T b map f (Add e1 e2) = Add (f e1) (f e2) map f (Const n) = Const n F a a ? ?’ h map h F b b F-algebra ? F a ? a (a is called the carrier) Homomorphism between F-algebras ? and ?’ a b f map f F a F b Example

It could be obtained automatically

slide-14
SLIDE 14

14

27

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Catamorphisms and Semantics

Generic Programming

[Duponcheel 95]

  • We define F-álgebras that take the computational structure as carrier
  • The interpreter is obtained as a catamorphism

Reusable and independent specifications ? T : T (Comp Value) ? Comp Value ? T (Add e1 e2) = ? e1?À = ?v1 .

?

e2?À = ?v2 . ret urn (v1 +v2 ) ? T (Const n) = ret urn n ? P : M (Comp Value) ? Comp Value ? P (Mult e1 e2) = ? e1?À = ?v1 .

?

e2?À = ?v2 . ret urn (v1 ? v2 ) ? V : V (Comp Value) ? Comp Value ? V (Var x) = rdEnv À = ?? . lkp x ? Comp ? EnvT I dM Value ? I nt I nt er Ex pr : Expr ? Comp Value I nt er Expr =? ? T ? ? M ? ? V ? The interpreter is automatically

  • btained as a catamorphism

? _?: (F a ? a) ? ?F ? a ? _?: (F (M V) ? M V) ? ?F ? M V

28

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Monadic Catamorphisms

Monadic Function a ?

M b Representation a b a b c f g f @ g

Composition of monadic functions

@ : (b ? M c) ? (a ? M b) ? a ? M c f @ g = ?x ? g x À = f

Monadic extension of a functor

mapm:(a ? M b) ? (F a ? M (F b)) a b f map f F a F b mapm : (a ? M b) ? T a ? M (T b) mapm f (Add e1 e2) = do v1 ? f e1 v2 ? f e2 r et ur n (Add v1 v2) mapm f (Const n) = r et ur n ( Const n) Recursive evaluation from left to right Example It could be automatically

  • btained

Generic Programming

slide-15
SLIDE 15

15

29

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Monadic Catamorphisms

Monadic F-algebra Initial monadic F-algebra Combination of catamorphisms and monadic catamorphisms

?

_,_?: (F a ? M a) ? (G (M a) ? M a) ? ? F? G ? M a

?

_,_?: (F (M a) ? M a) ? (G a ? M a) ? ? F? G ? M a I nm : F ? F ? M ?F I nm= ret urn . I n ? :: F a ? M a

F ? F ? F I nm F a a ? ? ?? mapm ? ??

? _?: (F a ? M a) ? ?F ? M a Monadic Catamorphism

F ? F ? F I n F a a ? ? ?? map ? ??

Catamorphism ? _?: (F a ? a) ? ?F ? a ? _?: (F (M a) ? M a) ? ?F ? M a

F a a F ? F ? F

Generic Programming

30

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Monadic catamorphisms and semantics

  • We define monadic F-algebras

? T ; T Value ? Comp Value ? T (Add v1 v2) = ret urn ?(?v1 + ?v2) ? T (Const n) = ret urn ?n ? P

; P Value ?

Comp Value ? P (Mult v1 v2) = ret urn ? (? v1 ? ? v2)

Reusable and independent specifications Separates recursive evaluation from especification

Comp ? I O Value ? I nt | | () I nterExpr : Expr ? Comp Value I nterExpr = ?? T ? ? P

? ? V ?

Autoamatic generation of the interpreter as a monadic catamorphism

? V ; V Value ? Comp Value ? V (Var x) = do ? ? r dEnv lkp x ?

Generic Programming

slide-16
SLIDE 16

16

31

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Bicatamorphisms

  • Several syntactic mutually revursive categories

(motivating example: expressions and commands)

  • Requires: n-sorted algebras (in this presentation, we take n=2)

BiFunctor F

bimapF : (a ? c) ? (b ? d) ? (F a b ? F c d)

Fixed-point of two bifunctors ? 1FG ? I n1 (F ? 1FG ? 2FG)

? 2FG ? I n

2 (G ? 1FG ? 2FG)

Sum of bifunctors ( F ¢ G) a b ? F a b || G a b S e c ? Seq c c A e c ? Name := e W e c ? While e c I e c ? I F e c c

Commands BiFunct or C = S ¢ A ¢ W ¢ I

F1

2

F2

2a b ? F b

From functor to bifunctor a b ? F a

L I mp ? ? 2 EC Imperative Language Expr essions BiFunct or

E ? (T ? P ? V)1

2

Generic Programming

32

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Bicatamorphisms

F ? 1FG ? 2FG ? 1 FG I n1 F a b a ? 1 ? ? 1,? 2?

1

bimapF ? ? 1,? 2?

1

G ? 1 FG ? 2 FG ? 2 FG I n2 G a b b ? 2 ? ? 1,? 2?

2

bimapG ? ? 1,? 2?

2

bicatamorphism

FG-bialgebra

? ?1:F a b ? a, ? 2 : G a b ? b ?

Inicial FG-bialgebra:

?I n1: F ? 1FG ? 2FG? ? 1 FG, I n2: G ? 1FG ? 2FG ? ? 2 FG ? ? ? 1,? 2?

1 : (F a b ?

a) ? (G a b ? b) ? ? 1FG ? a ? ? 1,? 2?

2 : (F a b ?

a) ? (G a b ? b) ? ? 2FG ? b

FG-bialgebras over the sum of bifunctors

¢ 1

: (F a b ?

a)? (G a b ? a)? (F ¢ G) a b ? a ¢ 2 :(F a b ? b)? (G a b ? b)? (F ¢ G) a b ? b

From algebras to bialgebras

? 1

2 : (F a ?

a) ? ( F1

2 a b ?

a )

: (F a ?

a) ? ( a b ? a ) ? 2

2

F2

2

Auxiliary Functions

Generic Programming

slide-17
SLIDE 17

17

33

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Bicatamorphisms and Semantics

?S: S (Comp Value) (Comp ()) ? Comp () ?S (Seq c1 c2) = do { c1 ; c2 } ?S: S (Comp Value) (Comp ()) ? Comp () ?S (Seq c1 c2) = do { c1 ; c2 }

We define FG-bialgebras over the computational structure

?I : I (Comp Value) (Comp ()) ? Comp () ?I (I F e c1 c2) = do v ? e if v / = 0 t hen c1 else c2 ?I : I (Comp Value) (Comp ()) ? Comp () ?I (I F e c1 c2) = do v ? e if v / = 0 t hen c1 else c2 ?W : W (Comp Value) (Comp ()) ? Comp () ?W (WHI LE e c) = loop where loop = do v ? e if v t hen c À = ?_ ? loop else ret urn () ?W : W (Comp Value) (Comp ()) ? Comp () ?W (WHI LE e c) = loop where loop = do v ? e if v t hen c À = ?_ ? loop else ret urn ()

Difference between expressions and commands Independent Specifications

?A : A (Comp Value) (Comp ()) ? Comp () ?A (x := e) = do ? ? f et ch ? ? r dEnv v ? e set (upd ? x v) ?A : A (Comp Value) (Comp ()) ? Comp () ?A (x := e) = do ? ? f et ch ? ? r dEnv v ? e set (upd ? x v)

Automatic generation of interpreter

I nt er LI mp: L I mp? Comp () I nt er LI mp= ? ? 1(? T ? ? P

? ? V), ? S ¢ 2 ? A ¢ 2 ? I ¢ 2 ? W ? 2

I nt er LI mp: L I mp? Comp () I nt er LI mp= ? ? 1(? T ? ? P

? ? V), ? S ¢ 2 ? A ¢ 2 ? I ¢ 2 ? W ? 2 2

Generic Programming

Language Prototyping System

  • Introduction
  • Comparison of techniques
  • Modular Monadic Semantics
  • Generic Programming
  • Language Prototyping System

– Architecture – Interactive Framework – Functional Language – Object Oriented language – Logic programming language

  • Conclusions

34

slide-18
SLIDE 18

18

35

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Architecture of the System

Language Prototyping System

Interactive Framework Semantic Blocks Common Tools Monad Transformers Types of Monads St at eMonad EnvMonad Cont Monad ... EnvT St at eT Cont T ... Syntax Parser Pretty Language definitions Semantics

Implementation

Domain specific language embedded in Haskell GHC 5.02 compiler Haskell 98 + multi-parameter type classes and overlapping instances

36

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Interactive Framework

  • Problem: Different languages (with different types) in

the same data structure

  • Solution: Existential types + type classes [Laufer 94]

class Lclass t wher e exec :: t ? I O () Language ? ? t ? Lclass . MkL t inst ance Lclass Language wher e exec (MkL x) = exec x L1 = … L2 = … inst ance Lclass L1 wher e exec x = ... inst ance Lclass L2 wher e exec x = ... … ls :: [Language] ls = [MkL lang1, MkL lang2, ...] class Lclass t wher e exec :: t ? I O () Language ? ? t ? Lclass . MkL t inst ance Lclass Language wher e exec (MkL x) = exec x L1 = … L2 = … inst ance Lclass L1 wher e exec x = ... inst ance Lclass L2 wher e exec x = ... … ls :: [Language] ls = [MkL lang1, MkL lang2, ...] Existential type includes elements from LClass Each element of Lclass can be included in the list Access the exec function from elements of Language Diferent languages can belong to LClass

Language Prototyping System

slide-19
SLIDE 19

19

37

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Functional Language

  • Features :

– Higher order functions

  • Different evaluation methods: by name, by value, by need

– Recursive local declarations

L f un ? ? (T ? P ? V ? F ? D ? I O ? R ? X ? C C ) let f act = ?n ? if n == 0 t hen 1 else n ? f act (n - 1) in f act 5

Language Prototyping System

– Other features of ML-like languages

  • First class continuations (callcc)
  • Input/Output (r ead/ writ e)
  • References and assignments (r ef, !, :=)
  • Exceptions (raise/handle)
  • Syntactic structure: A functor for each category

38

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Functional Language

Funct ion ? Comp Value ? Comp Value Value ? I nt | | Bool | | Funct ion Comp ? (Cont T . St at eT . EnvT . ErrT) I O

  • Computational structure
  • Semantic definitions

? X : X Value ? Comp Value ? X = ... I nt er Lf un : L f un ? Comp Value I nt er Lf un= ? ? T ? ? P

? ? V, ? F ? ? D ? ? I O ? ? R ? ? X ? ? CC ? ? I O ?

Combination between catamorphisms and monadic catamorphisms ? X : X (Comp Value) ? Comp Value ? X = ...

Monadic Algebras Algebras

Language Prototyping System

slide-20
SLIDE 20

20

39

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

¿What is an object?

Object Oriented Language

let p = Obj locals: x = 0 met hods: get = x set = ?n ? x := n eq = ?p ? self get == p get in p set 2 ; p get Internal state Access Methods self-reference Method selection

¿What is a class?

let Cell = Class locals: x = 0 met hods: get = x set = ?n ? x := n eq = ?p ? self get == p get p = new C ell in p set 2 ; p get Object generator Syntax similar to that of objects

Language Prototyping System

40

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

let Cont Cell = Class ext ends Cell locals: cont = 0 met hods: set = ?n ? super set ; cont := cont + 1 get Cont = cont

Inheritance: Subclasses

Object Oriented Language

It can define new attributes and methods It can redefine methods Access to methods of class super

Dynamic/static binding

let p = new Cell q = new Cont Cell er ase = ?c ? c set 0 in er ase p; er ase q Static binding: method set from Cell Dynamic binding: method set from ContCell

Language Prototyping System

slide-21
SLIDE 21

21

41

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Object Oriented Language

Obj e ? Obj [(Name,e)] [(Label,e)] -- obj ect s Send e ? Send e Label -- Met hod select ion Class e ? Class [(Name,e)] [(Label,e)] -- classes New e ? New e -- I nst ance gener at ion SubClass e ? SubClass e [(Name,e)] [(Label,e)] -- subclasses L OO ? ? (Obj ? Send ? Class ? New ? SubClass ? T ? P ? V ? F ? D)

Syntax structure

Obj ect ? Recor d (Comp Value) Gener at or ? Obj ect ? Obj ect Class ? Comp Obj ect Value ? I nt | | Bool | | Funct ion | | Obj ect | | Class Comp ? (St at eT . EnvT . ErrT) I O

Computational structure: Fixed-point semantics [Cardelli 84, Reddy 88] Semantic definitions using F-algebras . . .

? : (a ? a) ? a ? f = f (? f)

Language Prototyping System

42

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

? p(X). { X/ a } X=a { X/ f (X’) } ? p(X’). { X’/ a } X=f (a) { X’/ f (X’’) } ? p(X’’ ).

. . .

Resolution tree

Pure Logic Programming Language

p(a). p(f (X)):-p(X). Renaming Unification Several answers with backtracking Result = Answer Substition Example

Language Prototyping System

?p(X). Program Goal

slide-22
SLIDE 22

22

43

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Pure Logic Programming Language

Language Prototyping System

Pr olog ? ?G Prolog Terms PT ? Cons Name -- Const ant | Var Name -- Var iable | F Name [P T] -- Funct or G g ? g ? g

  • - Conj unct ion

| g ? g

  • - Disj unct ion

| Free (Name ? g)

  • - Free variable

| call Name PT

  • - Pr edicat e call

| P T $ P T

  • - Unif icat ion

| Def Name Name g g -- Local def init ions p(a). p(f (X)) ? p(X). ?- p(X). Def p v (v $ Cons a ? Free (?x. v $ F f [x] ? call p x )) (Free (?x. call p x)) is codified as Example Goals

44

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Pure Logic Programming Language

Comp ? (BackT . St at eT . EnvT . ErrT) I O

Interpreter Computational structure

? G : G (Comp Subst) ? Comp Subst ? G (g1 ? g2) = r dEnv À = ?(?,?). g1 À = ??’. inEnv(?,?’) g2 ? G (g1 ? g2) = r dEnv À = ?(?,?). inEnv(?,?) g1 d inEnv(?,?) g1

  • I nt er P

rolog : Pr olog ?

Comp Subst I nt er P

rolog = ?

? P?

Language Prototyping System

slide-23
SLIDE 23

23

45

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Logic Programming + Arithmetic

Language Prototyping System

? X is 2+3. X=5 2 + 3 Ã 5 { X/5 }

We want to add the arithmetic predicate is

Arithmetic evaluation unification Requisite : Reuse previous specifications

  • Theoretical Framework:many-sorted algebras (n=2)
  • We define bifunctors, 2-sorted algebras and bicatamorphisms

46

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Logic Programming + Arithmetic

Language Prototyping System

A g e ? Term is e

Bif unct or

? A : A (Comp Subst) (Comp I nt) ? Comp Subst ? A (t is e) = e À = ?n. r dEnv À = ?(?, ?). unif yS t (Cons n) ? I nt er Pr olog+ : Pr olog+? Comp Subst I nt er Pr olog+

= ?? 1 ? G¢ 1 ? I, ? 1 (? T ? ? P) ? 1 2 2

I nt erpret er Pr olog+? ? 1 (G1 ¢ A) (T ? P)2

2 2

Synt ax

slide-24
SLIDE 24

24

Conclusions

  • Introduction
  • Comparison of techniques
  • Modular monadic semantics
  • Generic programming
  • Language prototyping system
  • Conclusions

– Assessment – Future work

47 48

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Assessment

  • Modularity and reusability using semantic blocks
  • Proof of properties

– Axiomatization of monads and monad transformers – Static typechecking of specifications

  • Haskell type system with multiparameter type classes and
  • verlapping instances
  • Automatic generation of interpreters
  • Flexibility

– Representative Languages of each paradigm

  • Real Languages? Not yet
  • Readability?

Conclusions

Assessment ? Modularity ? Reusability ? Proof ? Flexibility ? Prototype ? Readability ? Experience

slide-25
SLIDE 25

25

49

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

? ? ? ? ? ? ? ? ?

Summary of assessments

Conclusions

? ? ? ? ? ? ? ? ? Natural language

  • Struct. Operational Sem.

Natural Sem. Denotational Sem. Axiomatic Sem. Algebraic Sem. Abstract State Machines Action Sem. Modular Monadic Sem. Reusable Monadic Semantics ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? N

  • n

a m b i g u i t y M

  • d

u l a r i t y R e u s a b i l i t y P r

  • f

P r

  • t
  • t

y p e R e a d a b i l i t y E x p e r i e n c e F l e x i b i l i t y 50

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

About the implementation technique

Conclusions

  • Domain specific language embedded in Haskell
  • Advantages

– Easier development – Haskell type system incorporation – Access to Haskell development tools (debugger, profiler, IDE...)

  • Disadvantages

– Haskell dependency – Haskell Type system limitations

  • Multi-parameter type classes, overlapping instances, first-class polymorphism,

extensible records and variants, ...

– Mixture of Error messages

slide-26
SLIDE 26

26

51

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Future Work (1)

  • Theoretical work

– Axiomatization of types of monads – Computational structures taxonomy – Combination of monad transformers – Development and application of

  • n-catamorphisms and monadic n-catamorphisms
  • Coalgebras
  • Comonads
  • Cata-, ana-, hylomorphisms
  • Arrows [Hughes 00]
  • Integration of concurrency and parallelism
  • Compilers from specifications [Harrison, Kamin 00]

Conclusions

52

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Future Work (2)

  • Implementation of the system

– Improve current implementation

  • First class polymorphism + extensible records
  • Generic Haskell [Hinze 00]
  • Run-time Modification of specified languages

– Independent domain-specific meta-language

  • Language Development Environment

– Independent (Graphical User Interface) – Integrated in others like ASF+SDF, SmartTools , etc.

  • Real languages specification
  • Design of new programming languages

– Multiparadigm – Internet computing – Flexibility (towards natural language?)

Conclusions

slide-27
SLIDE 27

27

53

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Future Work (3)

  • XML genericity is obtained by separating structure from content
  • There are techniques to describe structure (DTDs, schemas, constraints)

Conclusions

content Structure (DTD, schemas, constraints,...) XML document

When clients obtain a web document with a known structure they act accordingly When clients obtain a program with a known semantics they execute it accordingly

content Semantics (monads...) XPL program

  • In a programming language
  • We could separate behaviour (semantics) from content
  • There are techniques to describe semantics
  • Possible Applications:
  • Secure Dynamic pages
  • E-learning programming languages (IDEFIX project)

54

Reusable Monadic S emant ics of P rogramming Languages - J . E. Labra

Further information

  • My Homepage

ht t p:/ / lsi.uniovi.es/ ~labra Links to ... – ht t p:/ / lsi.uniovi.es/ ~labra/ publicat ions.ht ml Several papers – ht t p:/ / lsi.uniovi.es/ ~labra/ t hesis.ht ml PhD Thesis in PDF (spanish)

  • About Oviedo...

The End