Implementation of a Pragmatic Translation from Haskell into - - PowerPoint PPT Presentation

implementation of a pragmatic translation from haskell
SMART_READER_LITE
LIVE PREVIEW

Implementation of a Pragmatic Translation from Haskell into - - PowerPoint PPT Presentation

Implementation of a Pragmatic Translation from Haskell into Isabelle/HOL Patrick Bahr pa-ba@arcor.de NICTA Sydney, TU Wien December 17, 2008 Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 1 / 29


slide-1
SLIDE 1

Implementation of a Pragmatic Translation from Haskell into Isabelle/HOL

Patrick Bahr pa-ba@arcor.de

NICTA Sydney, TU Wien

December 17, 2008

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 1 / 29

slide-2
SLIDE 2

Outline

1

Introduction Haskell vs. Isabelle/HOL Motivation Goals

2

Translating Haskell into Isabelle/HOL Haskell vs. Isabelle/HOL Implementation

3

Conclusions

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 2 / 29

slide-3
SLIDE 3

Outline

1

Introduction Haskell vs. Isabelle/HOL Motivation Goals

2

Translating Haskell into Isabelle/HOL Haskell vs. Isabelle/HOL Implementation

3

Conclusions

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 3 / 29

slide-4
SLIDE 4

Haskell vs. Isabelle/HOL

Haskell in a nutshell

purely functional programming language non-strict semantics (mostly implemented by lazy evaluation) comprehensive type system: Hindley-Milner (restricted Fω) + type classes uses monads to allow side effects

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 4 / 29

slide-5
SLIDE 5

Haskell vs. Isabelle/HOL

Haskell in a nutshell

purely functional programming language non-strict semantics (mostly implemented by lazy evaluation) comprehensive type system: Hindley-Milner (restricted Fω) + type classes uses monads to allow side effects

Isabelle/HOL in a nutshell

Isabelle: generic theorem prover HOL: Isabelle formulation of classical higher-order logic based on simply typed lambda calculus (system F1) comparatively weak type system extended with type classes

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 4 / 29

slide-6
SLIDE 6

Haskell vs. Isabelle/HOL

Haskell in a nutshell

purely functional programming language non-strict semantics (mostly implemented by lazy evaluation) comprehensive type system: Hindley-Milner (restricted Fω) + type classes uses monads to allow side effects

Isabelle/HOL in a nutshell

Isabelle: generic theorem prover HOL: Isabelle formulation of classical higher-order logic based on simply typed lambda calculus (system F1) comparatively weak type system extended with type classes more details when we come to the implementation

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 4 / 29

slide-7
SLIDE 7

Motivation

Program verification

Haskell’s semantics allows comparatively easy reasoning there is no theorem prover for Haskell! translate Haskell into language of a generic theorem prover

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 5 / 29

slide-8
SLIDE 8

Motivation

Program verification

Haskell’s semantics allows comparatively easy reasoning there is no theorem prover for Haskell! translate Haskell into language of a generic theorem prover

Example: l4.verified project

aim: formalisation and verification of a microkernel prototype implementation in Haskell translation into Isabelle/HOL executable model reasoning about executable model in Isabelle/HOL

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 5 / 29

slide-9
SLIDE 9

Translation

Goals

cover a large subset of Haskell’s syntax result should be easily readable

◮ preserve syntactic structure as much as possible ◮ translate syntactic sugar as well

keep reasoning simple Isabelle/HOL

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 6 / 29

slide-10
SLIDE 10

Translation

Goals

cover a large subset of Haskell’s syntax result should be easily readable

◮ preserve syntactic structure as much as possible ◮ translate syntactic sugar as well

keep reasoning simple Isabelle/HOL Translation is neither sound nor complete!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 6 / 29

slide-11
SLIDE 11

Translation

Goals

cover a large subset of Haskell’s syntax result should be easily readable

◮ preserve syntactic structure as much as possible ◮ translate syntactic sugar as well

keep reasoning simple Isabelle/HOL Translation is neither sound nor complete!

Implementation

implementation language: Haskell based on existing work from TU Munich

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 6 / 29

slide-12
SLIDE 12

Outline

1

Introduction Haskell vs. Isabelle/HOL Motivation Goals

2

Translating Haskell into Isabelle/HOL Haskell vs. Isabelle/HOL Implementation

3

Conclusions

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 7 / 29

slide-13
SLIDE 13

Haskell vs. Isabelle/HOL – Non-strictness/Partiality

in Isabelle/HOL only total functions are definable recursive definitions need termination proof Haskell is Turing-complete partial functions definable Haskell’s semantics is non-strict

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 8 / 29

slide-14
SLIDE 14

Haskell vs. Isabelle/HOL – Non-strictness/Partiality

in Isabelle/HOL only total functions are definable recursive definitions need termination proof Haskell is Turing-complete partial functions definable Haskell’s semantics is non-strict

Example (Haskell)

from :: Int -> [Int] from n = n : from (n+1)

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 8 / 29

slide-15
SLIDE 15

Haskell vs. Isabelle/HOL – Non-strictness/Partiality

in Isabelle/HOL only total functions are definable recursive definitions need termination proof Haskell is Turing-complete partial functions definable Haskell’s semantics is non-strict

Example (Haskell)

from :: Int -> [Int] from n = n : from (n+1) from does not terminate for any input not definable in Isabelle/HOL due to non-strictness this function is still usable in Haskell

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 8 / 29

slide-16
SLIDE 16

Haskell vs. Isabelle/HOL – Non-strictness/Partiality

in Isabelle/HOL only total functions are definable recursive definitions need termination proof Haskell is Turing-complete partial functions definable Haskell’s semantics is non-strict

Example (Haskell)

from :: Int -> [Int] from n = n : from (n+1) from does not terminate for any input not definable in Isabelle/HOL due to non-strictness this function is still usable in Haskell

Example (Haskell)

nPrimes :: Int -> [Int] nPrimes n = take n (filter isPrime (from 1))

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 8 / 29

slide-17
SLIDE 17

Haskell vs. Isabelle/HOL – Non-strictness/Partiality

in Isabelle/HOL only total functions are definable recursive definitions need termination proof Haskell is Turing-complete partial functions definable Haskell’s semantics is non-strict

Example (Haskell)

from :: Int -> [Int] from n = n : from (n+1) from does not terminate for any input not definable in Isabelle/HOL due to non-strictness this function is still usable in Haskell

Example (Haskell)

nPrimes :: Int -> [Int] nPrimes n = take n (filter isPrime (from 1)) Definitions that depend on non-strictness have to be avoided!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 8 / 29

slide-18
SLIDE 18

Haskell vs. Isabelle/HOL – Local Function Definitions

Haskell allows recursive function definitions in local contexts (using let or where where) in Isabelle/HOL recursive function definitions are only allowed at the top level

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 9 / 29

slide-19
SLIDE 19

Haskell vs. Isabelle/HOL – Local Function Definitions

Haskell allows recursive function definitions in local contexts (using let or where where) in Isabelle/HOL recursive function definitions are only allowed at the top level

Example (Haskell)

sumLen :: Int -> [a] -> [a] -> Int sumLen s l1 l2 = let len [] = 0 len (x:xs) = len xs + s in len l1 + len l2

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 9 / 29

slide-20
SLIDE 20

Haskell vs. Isabelle/HOL – Local Function Definitions II

local function definitions have to be moved to the top level closures have to be made explicit

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 10 / 29

slide-21
SLIDE 21

Haskell vs. Isabelle/HOL – Local Function Definitions II

local function definitions have to be moved to the top level closures have to be made explicit

Example (Isabelle/HOL)

fun len1 where " len1 _ N i l = 0" | " len1 s ( x # xs ) = len1 s xs + s " fun sumLen : : " i n t = > ’ a l i s t = > ’ a l i s t = > i n t " where "sumLen s l 1 l 2 = ( l e t l e n = len1 s in l e n l 1 + l en l 2 )"

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 10 / 29

slide-22
SLIDE 22

Haskell vs. Isabelle/HOL – Local Function Definitions II

local function definitions have to be moved to the top level closures have to be made explicit

Example (Isabelle/HOL)

fun len1 where " len1 _ N i l = 0" | " len1 s ( x # xs ) = len1 s xs + s " fun sumLen : : " i n t = > ’ a l i s t = > ’ a l i s t = > i n t " where "sumLen s l 1 l 2 = ( l e t l e n = len1 s in l e n l 1 + l en l 2 )" Our implementation is able to make these transformations!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 10 / 29

slide-23
SLIDE 23

Haskell vs. Isabelle/HOL – Order of Definitions

in Haskell definitions can appear in any order in Isabelle/HOL:

◮ an identifier has to be defined before usage ◮ mutual recursive definitions have to be made in parallel Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 11 / 29

slide-24
SLIDE 24

Haskell vs. Isabelle/HOL – Order of Definitions

in Haskell definitions can appear in any order in Isabelle/HOL:

◮ an identifier has to be defined before usage ◮ mutual recursive definitions have to be made in parallel

Our implementation reorders definitions accordingly!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 11 / 29

slide-25
SLIDE 25

Haskell vs. Isabelle/HOL – Polymorphism

Haskell: polymorphism over type constructors (of arbitrary kind) Isabelle/HOL: polymorphism over types only

Example (type constructors)

types (constructors of kind ∗): Int, [Bool], Int -> Bool, . . . type constructors of first-order kind: list ([]: ∗ → ∗), sum (Either: ∗ → (∗ → ∗)) type constructor of higher-order kind: Tree: (∗ → ∗) → (∗ → ∗) data Tree c a = Node a (c (Tree c a))

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 12 / 29

slide-26
SLIDE 26

Haskell vs. Isabelle/HOL – Ad Hoc Polymorphism

Haskell: type classes + constructor classes Isabelle/HOL: type classes only

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 13 / 29

slide-27
SLIDE 27

Haskell vs. Isabelle/HOL – Ad Hoc Polymorphism

Haskell: type classes + constructor classes Isabelle/HOL: type classes only

Example (classes)

type class: class (Eq a, Show a) => Num a where (+), (-), (*) :: a -> a -> a negate :: a -> a . . .

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 13 / 29

slide-28
SLIDE 28

Haskell vs. Isabelle/HOL – Ad Hoc Polymorphism

Haskell: type classes + constructor classes Isabelle/HOL: type classes only

Example (classes)

type class: class (Eq a, Show a) => Num a where (+), (-), (*) :: a -> a -> a negate :: a -> a . . . constructor class: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 13 / 29

slide-29
SLIDE 29

Haskell vs. Isabelle/HOL – Ad Hoc Polymorphism II

monad class is not definable in Isabelle/HOL! monads are crucial for practical Haskell programs monads can be used to describe computations with side effects

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 14 / 29

slide-30
SLIDE 30

Haskell vs. Isabelle/HOL – Ad Hoc Polymorphism II

monad class is not definable in Isabelle/HOL! monads are crucial for practical Haskell programs monads can be used to describe computations with side effects

Our solution

Translate only instances of the class Monad! each monad instance has to use different names for the operation e.g. one monad uses >>=, return; another one uses >>=’, return ’ type inference has to be performed to rename the operations correctly not full type inference is used, only a simple heuristics

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 14 / 29

slide-31
SLIDE 31

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-32
SLIDE 32

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns

Example

In Haskell: f :: [Int] -> [Int] f l@(_:_) = 0 : l f l@ ([]) = 1 : l

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-33
SLIDE 33

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns

Example

In Haskell: f :: [Int] -> [Int] f l@(_:_) = 0 : l f l@ ([]) = 1 : l In Isabelle/HOL: fun f where " f ( a0 # a1 ) = ( l e t l = ( a0 # a1 ) in 0 # l )" | " f N i l = ( l e t l = N i l in 1 # l )"

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-34
SLIDE 34

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns labelled fields in data types

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-35
SLIDE 35

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns labelled fields in data types

Example (Haskell)

data MyRecord = A { aField1 :: String , common1 :: Bool , common2 :: Int } | B { bField1 :: Bool , bField2 :: Int , common1 :: Bool , common2 :: Int } | C Bool Int String

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-36
SLIDE 36

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns labelled fields in data types

Example (Haskell)

data MyRecord = A { aField1 :: String , common1 :: Bool , common2 :: Int } | B { bField1 :: Bool , bField2 :: Int , common1 :: Bool , common2 :: Int } | C Bool Int String This is reduced to an ordinary data type!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-37
SLIDE 37

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns labelled fields in data types guards

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-38
SLIDE 38

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns labelled fields in data types guards

Example (Haskell)

insert :: Int -> [Int] -> [Int] insert n [] = [n] insert n (m:ms) | n < m = n:m:ms | otherwise = m: insert n ms

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-39
SLIDE 39

Haskell vs. Isabelle/HOL – Misc.

Further things that are taken care of in the translation as-patterns labelled fields in data types guards

Example (Haskell)

insert :: Int -> [Int] -> [Int] insert n [] = [n] insert n (m:ms) | n < m = n:m:ms | otherwise = m: insert n ms Guards are reduced to if-then-else expressions!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 15 / 29

slide-40
SLIDE 40

Overall Design of Implementation – Parsing

parse each Haskell module to a syntax tree imported modules are located and parsed as well parser only verifies context-free part of the syntax syntactically correct Haskell program is assumed

  • 1. Parsing

Haskell syntax trees Haskell modules Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 16 / 29

slide-41
SLIDE 41

Overall Design of Implementation – Preprocessing

Guards are transformed into if-then-else expressions. Local function definitions are transformed into top-level function definitions. Keywords and identifiers defined in the Isabelle/HOL library are renamed.

  • 1. Parsing
  • 2. Preprocessing

Haskell syntax trees (simplified) Haskell syntax trees Haskell modules Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 17 / 29

slide-42
SLIDE 42

Overall Design of Implementation – Analysis

Some global information about the program is collected: type annotations the module where an identifier was defined what an identifier refers to (type, function etc.) associativity and precedence

  • f defined operators
  • 1. Parsing
  • 2. Preprocessing
  • 3. Analysis

Haskell syntax trees (simplified) Haskell syntax trees Haskell modules Context Information Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 18 / 29

slide-43
SLIDE 43

Overall Design of Implementation – Conversion

Definitions are reordered according to their dependencies. Haskell syntax trees are translated into Isabelle/HOL syntax trees.

  • 1. Parsing
  • 2. Preprocessing
  • 4. Conversion
  • 3. Analysis

Isabelle syntax trees (intermediate) Haskell syntax trees (simplified) Haskell syntax trees Haskell modules Context Information Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 19 / 29

slide-44
SLIDE 44

Overall Design of Implementation – Adaptation

Renaming of predefined identifiers, e.g.: Int → int [] → Nil ++ → @

  • 1. Parsing
  • 2. Preprocessing
  • 4. Conversion
  • 5. Adaptation
  • 3. Analysis

Isabelle syntax trees (intermediate) Haskell syntax trees (simplified) Haskell syntax trees Haskell modules Isabelle syntax trees Context Information Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 20 / 29

slide-45
SLIDE 45

Overall Design of Implementation – Printing

Isabelle/HOL syntax trees are written into theory files.

  • 1. Parsing
  • 2. Preprocessing
  • 4. Conversion
  • 5. Adaptation
  • 6. Printing
  • 3. Analysis

Isabelle syntax trees (intermediate) Haskell syntax trees (simplified) Haskell syntax trees Haskell modules Isabelle syntax trees Isabelle theories Context Information Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 21 / 29

slide-46
SLIDE 46

Outline

1

Introduction Haskell vs. Isabelle/HOL Motivation Goals

2

Translating Haskell into Isabelle/HOL Haskell vs. Isabelle/HOL Implementation

3

Conclusions

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 22 / 29

slide-47
SLIDE 47

Summary

Original implementation covered

case, if-then-else, and let expressions list comprehensions where bindings as-patterns guards mutually recursive functions and data type definitions simple pattern bindings definitions and instantiations of type classes

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 23 / 29

slide-48
SLIDE 48

Summary

Original implementation covered

case, if-then-else, and let expressions list comprehensions where bindings ✘ as-patterns ✘ guards ✘ mutually recursive functions and data type definitions simple pattern bindings definitions and instantiations of type classes Some parts of the translations were unsound!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 23 / 29

slide-49
SLIDE 49

Summary II

Our Contributions

✔ mutually recursive function and data type definitions ✔ as-patterns ✔ guards data types with labelled fields closures in local function definitions monomorphic uses of monads

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 24 / 29

slide-50
SLIDE 50

Summary II

Our Contributions

✔ mutually recursive function and data type definitions ✔ as-patterns ✔ guards data types with labelled fields closures in local function definitions monomorphic uses of monads

What is missing

constructor type classes polymorphic uses of monads non-simple pattern bindings irrefutable patterns

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 24 / 29

slide-51
SLIDE 51

Conclusions

What do we have

translation is unsound! most of the Haskell 98 language can be translated resulting Isabelle/HOL formalisation is close to Haskell program comparatively easy reasoning in Isabelle/HOL adequate translation for most purposes l4.verified

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 25 / 29

slide-52
SLIDE 52

Conclusions

What do we have

translation is unsound! most of the Haskell 98 language can be translated resulting Isabelle/HOL formalisation is close to Haskell program comparatively easy reasoning in Isabelle/HOL adequate translation for most purposes l4.verified

Alternative Approach

logic HOLCF is well suited to formalise partiality and non-strictness even constructor classes can be formalised reasoning in Isabelle/HOLCF is more complicated

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 25 / 29

slide-53
SLIDE 53

Coping with Large Data Types

Dealing with syntax trees ⇒ dealing with large data types.

Data Types Defining Haskell Syntax Trees

500 lines of Haskell code 51 data types “largest” data type contains 45 constructors

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 26 / 29

slide-54
SLIDE 54

Coping with Large Data Types

Dealing with syntax trees ⇒ dealing with large data types.

Data Types Defining Haskell Syntax Trees

500 lines of Haskell code 51 data types “largest” data type contains 45 constructors You don’t want to write all the code for all those data types and each

  • f their constructors!

If you have to write it you only want to write it once!

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 26 / 29

slide-55
SLIDE 55

Coping with Large Data Types

Dealing with syntax trees ⇒ dealing with large data types.

Data Types Defining Haskell Syntax Trees

500 lines of Haskell code 51 data types “largest” data type contains 45 constructors You don’t want to write all the code for all those data types and each

  • f their constructors!

⇒ Generic Programming + Code Generation If you have to write it you only want to write it once! ⇒ Modularity

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 26 / 29

slide-56
SLIDE 56

Generic Programming

“Scrap Your Boilerplate”

Problem Addressed by SYB

traverse a data structure to transform or query it

  • nly a few parts of the data structure are relevant

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 27 / 29

slide-57
SLIDE 57

Generic Programming

“Scrap Your Boilerplate”

Problem Addressed by SYB

traverse a data structure to transform or query it

  • nly a few parts of the data structure are relevant

Example

compute free variables of an expression transform where clauses into let expressions

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 27 / 29

slide-58
SLIDE 58

Generic Programming

“Scrap Your Boilerplate”

Problem Addressed by SYB

traverse a data structure to transform or query it

  • nly a few parts of the data structure are relevant

Example

compute free variables of an expression transform where clauses into let expressions

Difficulties when Applying SYB in our Setting

  • ften context information is necessary

We want to define a piece of context information only once.

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 27 / 29

slide-59
SLIDE 59

Environments

Data Structure as a Tree

A B C

= changes environment

A and B needed A and C needed

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 28 / 29

slide-60
SLIDE 60

Environments

Data Structure as a Tree

A B C

= changes environment

A and B needed A and C needed

Defining Environments by a -> (e -> e)

a is the type of the current node e is the type of the environment

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 28 / 29

slide-61
SLIDE 61

Extending SYB by Environment Propagation

Extension to SYB

allows to define environments allows to combine environments provides traversal strategies with environment propagation

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 29 / 29

slide-62
SLIDE 62

Extending SYB by Environment Propagation

Extension to SYB

allows to define environments allows to combine environments provides traversal strategies with environment propagation

Generalisation of Environment Propagation

non-uniform propagation monadic computations to define an environment

Patrick Bahr (NICTA Sydney, TU Wien) Translating Haskell into Isabelle/HOL December 17, 2008 29 / 29