+ All Categories
Home > Documents > Motivation Theoretical Background - Universitetet i Bergennwpt00/Proceedings/Labra-slides.pdf ·...

Motivation Theoretical Background - Universitetet i Bergennwpt00/Proceedings/Labra-slides.pdf ·...

Date post: 08-Oct-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
5
1 1 (http://www.ii.uib.no/~nwpt00) Modular Development of Interpreters from Semantic Building Blocks Jose E. Labra G., Juan M. Cueva, Mª Candida Luengo D. Department of Computer Science University of Oviedo (Spain) 12th Nordic Workshop on Programming Theory http://www.ii.uib.no/~nwpt00 2 (http://www.ii.uib.no/~nwpt00) Overview of the Talk Motivation Language Prototyping System Theoretical background Architecture of the System Example: MLambda Conclusions and Future Work Motivation Language Prototyping System Theoretical background Architecture of the System Example: MLambda Conclusions and Future Work 3 (http://www.ii.uib.no/~nwpt00) Motivation Oviedo3 Project Oviedo3 Project Integral OO Operating System Integral OO Operating System Semantic Specification of several Abstract Machines Semantic Specification of several Abstract Machines Modular Interpreters Modular Interpreters Tool for Modular Development of Interpreters Tool for Modular Development of Interpreters 4 (http://www.ii.uib.no/~nwpt00) Theoretical Background Denotational Semantics (Strachey, Scott 67) Semantic functions = Inductive Definitions over the syntax Σ Domains = characterise the range of values V Σ V Problems: Modularity? Extensibility? Monadic Semantics (E. Moggi, 89) Separation between values and computations A Monad Μ models a computation Different monads Different computations State, environment, continuations, non-determinism,... Σ →Μ V Problem: Composing Monads?
Transcript
Page 1: Motivation Theoretical Background - Universitetet i Bergennwpt00/Proceedings/Labra-slides.pdf · • Motivation • Language Prototyping System – Theoretical background – A rc

1

1��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Modular Development of Interpretersfrom Semantic Building Blocks

Jose E. Labra G., Juan M. Cueva, Mª Candida Luengo D.

Department of Computer ScienceUniversity of Oviedo (Spain)

12th Nordic Workshop on Programming Theoryhttp://www.ii.uib.no/~nwpt00

2��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Overview of the Talk

• Motivation

• Language Prototyping System

– Theoretical background

– Architecture of the System

– Example: MLambda

• Conclusions and Future Work

• Motivation

• Language Prototyping System

– Theoretical background

– Architecture of the System

– Example: MLambda

• Conclusions and Future Work

3��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Motivation

Oviedo3 ProjectOviedo3 Project Integral OO Operating SystemIntegral OO Operating System

Semantic Specification of several Abstract MachinesSemantic Specification of several Abstract Machines

Modular InterpretersModular Interpreters Tool for Modular Developmentof Interpreters

Tool for Modular Developmentof Interpreters

4��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Theoretical Background

• Denotational Semantics (Strachey, Scott 67)

• Semantic functions = Inductive Definitions over the syntax Σ• Domains = characterise the range of values V

Σ → V

• Problems: Modularity? Extensibility?

• Monadic Semantics (E. Moggi, 89)

• Separation between values and computations

• A Monad Μ models a computation

• Different monads ⇒ Different computations

– State, environment, continuations, non-determinism,...

Σ → Μ V

• Problem: Composing Monads?

Page 2: Motivation Theoretical Background - Universitetet i Bergennwpt00/Proceedings/Labra-slides.pdf · • Motivation • Language Prototyping System – Theoretical background – A rc

2

5��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Theoretical Background

• Modular Interpreters using Monads (Wadler, 92, Espinosa, 93, Steele Jr, 94)

• Modular Monadic Semantics (S. Liang, P. Hudak, M. Jones, 95)

• Monad Transfor mers add new features to a given monad

• Several monad transformers:

– Exceptions, State transformer, Environment reader, Continuations, Non-determinism, Input-Output, Resumptions

Comput at i on = ( MT1 · MT2 · . . . · MTn) Basi cMonad

• Subtyping Mechanism

– Allows domain value extensibility in Haskell

– Requires non-standard extensions to Haskell type systemV = V1 + V2 + ... + Vn

6��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Theoretical Background

• Initial Algebra and Folds (Meijer, Fokkinga, Paterson 91)

– Recursive datatype= least fixpoint of a pattern functor

– Polynomial Functors

• Applying folds to MMS (L. Duponcheel, 95)

Σ = µ�

where����������� ��� �����

– Modular Syntax Specification

• Merging Monads with Folds (Meijer, Jeuring, 95)

• Applying Monadic Folds to MMS (Labra, 99)

– Separation between recursive evaluation and semantic specification

7��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Language Prototyping System

• Embedded Domain Specific Language for Interpreter Definition

• Host Language = Haskell

• Interactive Interpreter Framework

– Allows run time interpreted language selection

• 3 examples implemented (by now)

– MLambda: Functional language with different call mechanisms and imperative features

– While: Simple Imperative programming language

– IL: Intermediate Language

8��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

LPSArchitecture

InterpreterFrameworkBasic

Theory

CommonTools

MonadTransformers

SemanticMonads

StateMonad

EnvMonad

ContMonad

...

EnvT

StateT

ContT

...

Structure

Parser

Pretty

LanguageDefinitions

Semantics

Page 3: Motivation Theoretical Background - Universitetet i Bergennwpt00/Proceedings/Labra-slides.pdf · • Motivation • Language Prototyping System – Theoretical background – A rc

3

9��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

LPSArchitecture: Semantic Monads

ExcMonad raise: ∀a . ε → M ahandle: ∀a . M a → (ε → M a) → M a

fetch: M σset: σ → M ()StateMonad

rdEnv : M ρinEnv: ∀a . ρ → M a → M aEnvMonad

ContMonad callcc: ∀a, b . (a → M b) → M a) → M a

IOMonad get: M Charput: Char → M ()

DebugMonad infoDebug: String → M ()

A type constructor Mreturn: ∀ a . a → M abind: ∀a b. M a → (a → M b) → M b

Monad

... more monads with their operations...

do-notation

m `bind` (\x → f)≡do { x ←m; f}

do-notation

m `bind` (\x → f)≡do { x ←m; f}

10��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

LPSArchitecture: Monad Transformers

• A monad transformer transforms a monad M into a new monad M’with new operations

mt :: M → M’

• Conditions– M’ must really be a monad

– The old operations from Mshould behave in the same way in M’

– This lifting of operations is made manually for some computational features

StateT σ

ExcT ε

EnvT ρ

ContT α

ExcT ε m v = m (ε + v)

StateT σ m v = σ → m (v, σ)

EnvT ρ m v = ρ → m v

ContT α m v = (v → m α) → m α

DebugT DebugT m v = StateT DebugInfo m v

IOT IOT m v = StateT IOChannels m v

... more monad transformers

11��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

LPSArchitecture: Interactive Framework• Different languages (with different types) in the same data structure

• Independent development of each language under a common interface

• Solution: Existential types + type classes (Laufer, 94)

class (Show s) ⇒ Syntax s whereexec :: s → Computation

data Language = ∀s ∈ Syntax . MkL s

instance Syntax Language whereexec (MkL s) = exec s

instance Syntax L1 whereexec s = ...

ls :: [Language]ls = [MkL lang1, MkL lang2, ...]

test :: [Language] → Int → Computationtest ls n = exec (ls !! n)

class (Show s) ⇒ Syntax s whereexec :: s → Computation

data Language = ∀s ∈ Syntax . MkL s

instance Syntax Language whereexec (MkL s) = exec s

instance Syntax L1 whereexec s = ...

ls :: [Language]ls = [MkL lang1, MkL lang2, ...]

test :: [Language] → Int → Computationtest ls n = exec (ls !! n)

MkL ::(∃s ∈Syntax . s) → Language

Any type ∈ Syntax can be included in the list

12��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Example: MLambda

• Functional Programming Language

– lambda abstraction (call by value)

• 2 primitive types: Booleans and Integers (no type checking)

• Imperative features

– input/output, exceptions, first-class continuations, references and assignment

• Simple Debugger new 0;0 : = r ead v;l et f ac =

\ x - > i f ( x < 0) t hen r ai se “ negat i ve ar g”el se i f ( x==0) t hen 1

el se x * f ac ( x - 1)i n f ac ( get 0)

new 0;0 : = r ead v;l et f ac =

\ x - > i f ( x < 0) t hen r ai se “ negat i ve ar g”el se i f ( x==0) t hen 1

el se x * f ac ( x - 1)i n f ac ( get 0)

Page 4: Motivation Theoretical Background - Universitetet i Bergennwpt00/Proceedings/Labra-slides.pdf · • Motivation • Language Prototyping System – Theoretical background – A rc

4

13��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

MLambda: Structure of the Language• Extensible Abstract Syntax using Least Fixpoint of pattern functors

• Parser: Parsec Library (Daan Leijen, 2000)

N x = Num Int | x `Add` x | ...

V x = Var String

C x = B Bool | x ‘Less‘ x | ...

D x = Let String x x

F x = Lambda String x | Apply x x

R x = Get x | New x | Assign x x | Seq x x

E x = Raise String. . .

Pattern Functors

Common Definitions (Meijer 92)

Sum of functors: (F ⊕ G) x = F x + G x

Fixpoint of a functor µ F = f (µ F)

Initial F-algebra In :: F (µ F) → µ F

Fold or catamorphism (| ϕ |) (In x) = ϕ (map (| ϕ |) x)

F-algebras over Sum of FunctorsϕF ⊕ G x= ϕF x + ϕG x

Common Definitions (Meijer 92)

Sum of functors: (F ⊕ G) x = F x + G x

Fixpoint of a functor µ F = f (µ F)

Initial F-algebra In :: F (µ F) → µ F

Fold or catamorphism (| ϕ |) (In x) = ϕ (map (| ϕ |) x)

F-algebras over Sum of FunctorsϕF ⊕ G x= ϕF x + ϕG x

Abstract SyntaxML = N ⊕ V ⊕ C ⊕ D ⊕ F ⊕ R ⊕ EMLambda = µ ML

14��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

MLambda: Semantics

• Computational MonadComputation = ExcT Exc (StateT Heap (EnvT Table (ContT Answer (DebugT IO))))

• Domain ValueValue = Int + Bool + (Computation Value → Computation Value)

• For each pattern functor F, we define a F-algebra ϕF with carrier Computation Value

With monadic folds we could eliminate the recursive evaluation

ϕN (v1 `Add` v2) = return (inj (v1 + v2))

ϕCMP (B b) = return (inj b)ϕCMP (m1 `Less` m2) = do { v1 ← m1

; v2 ← m2; return (inj (v1 < v2)) }

. . .

ϕV (Var x) = do { table ← rdEnv; return (lookup table x) }

ϕN (Num n) = return (inl n)ϕN (m1 `Add` m2) = do { v1 ← m1

; v2 ← m2; return (inj (v1 + v2))}

. . .

15��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

MLambda: Semantics (II)

ϕD (Let x m1 m2) = do {table ← rdEnv; inEnv (update table x m1) m2 }

ϕF (Lambda x m) = do { table ← rdEnv; return (inj (\c → do { v ← c; inEnv (update table x (return v)) m)}

ϕF (Apply m1 m2) = do { f ← m1; table ← rdEnv; f (inEnv table m2 ) }

ϕEx (Raise msg) = raise msg

ϕR (Get m) = do{ loc ← m; lookupTable loc}ϕR (New m) = do{ v ← m; loc ← alloc (return v); return (in loc) }ϕR (Assign m1 m2) = do{ loc ← m1; val ← m2 ; updateLoc loc (return val) }ϕR (Seq m1 m2) = do {m1; m2 }

sem :: MLambda → Computation Valuesem = (| ϕ N ⊕ V ⊕ CMP ⊕ D ⊕ R ⊕ F ⊕ Ex |)

16��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Conclusions

• A very modular way to develop interpreters for programming languages

– It is possible to extend the syntax, the semantics and the domain value without modifying existing components

• An interactive system for language prototyping and tesing

• Another Embedded Domain Specific Language in Haskell

– Advantages

• Easier Development

• Reusability of semantic components

• Incorporates a fairly good Type System

• Access to other language tools

– Disadvantages

• Awkward Error Messages (even for the developer)

• Limitations of Haskell98 Type System

Page 5: Motivation Theoretical Background - Universitetet i Bergennwpt00/Proceedings/Labra-slides.pdf · • Motivation • Language Prototyping System – Theoretical background – A rc

5

17��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

Future Work and work in progress

• More theoretical work

– Monad transformers (lifting), monadic folds, coalgebras

• Deriving Compilers from MMS (Liang, 98, Harrison, Kamin, 2000)

• First class polymorphism (monads as first class values)

– Monad transformer as a function

– Extensible records (TREX in Hugs, not in GHC)

– Extensible variants (not implemented)

• Assess the development of a domain specific language for semantic specifications

• GUI for the Interactive Interpreter Framework

• Specification of practical languages (work in progress)

– A Reflective Object Oriented Abstract Machine

– An Internet programming Language

18��� � � � � � � � � � � � � � � � � � � � � � � ��� � � ��� � � � � � ��� � � � � � � ��� � � � � �� � � ! " # #

(http://www.ii.uib.no/~nwpt00)

���anguage � rototyping � ystem

http://lsi.uniovi.es/~labra/LPS/LPS.html

• Oviedo3 Project:http://www.uniovi.es/~oviedo3

���anguage � rototyping � ystem

http://lsi.uniovi.es/~labra/LPS/LPS.html

• Oviedo3 Project:http://www.uniovi.es/~oviedo3

Further Information


Recommended