Top Banner
Generating Heap- bounded Programs Walid Taha , Stephan Ellner (Rice University) Hongwei Xi (Boston University)
23

Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Jan 18, 2016

Download

Documents

Stanley Harper
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Generating Heap-bounded Programs

Walid Taha, Stephan Ellner (Rice University)

Hongwei Xi (Boston University)

Page 2: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Why do languages matter?The need for well thought-out languages (or

“models”) will only increase in the future– Applications are constantly growing in complexity

– The need for simplicity is constant

– Can we think outside our languages?

– Using old languages means making old mistakes

Carefully designed languages allow us to– Ensure strong safety properties

– Preserve desirable reasoning principles

– Ultimately: Reduce developer/owner cost

Page 3: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Embedded systems:An area of growing demand

C/C++

C

FORTRAN

Assembly/C

Page 4: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Trouble with High-level Languages

High-level languages deprive programmer of control over basic resources

Programming embedded systems requires attention to various kinds of resources:– Concrete resources:

– Abstract resources:• Critical sections, capabilities, power, bandwidth, availability,

security, freedom of race conditions…

Resource-bounded languages provide guarantees

But they limit expressivity

Can we combine the best of both worlds?

Page 5: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

I OP

LFPL[Hofmann, ’99/00]

Traditional Programming

OPI1

I2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

Resource Aware Programming[EMSOFT ’03]

P1I1 P2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

I2

O

Page 6: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Insertion sort

This can be resource-bounded program:

let rec insert(a,l) =

case l of

nil -> cons(a, nil)

| cons(b,t) -> if a < b

then cons(a, cons(b, t))

else cons(b, insert(a, t))

let rec sort(l) =

case l of

nil -> nil

| cons(a,t) -> insert(a, sort(t))

Page 7: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

I OP

LFPL[Hofmann, ’99/00]

Traditional Programming

OPI1

I2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

Resource Aware Programming[EMSOFT ’03]

P1I1 P2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

I2

O

Page 8: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Hoffman’s (RB) LFPL

• Allows heap-bounded manipulation of dynamic data structures

• Can be translated into imperative C programs that have competitive performance (without optimizations)

• How does it work:

– You want to avoid duplicating structures

• Linear types. Default: Variables used only once

Page 9: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Insertion sort in LFPL

This is a resource-bounded program [Hoffman’99/00]

let rec insert(d,a,l) =

case l of

nil -> cons(a, nil) at d

| cons(b,t) at e -> if a < b

then cons(a, cons(b, t) at e) at d

else cons(b, insert(e,a, t)) at d

let rec sort(l) =

case l of

nil -> nil

| cons(a,t) at d -> insert(d, a, sort(t))

Page 10: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

I OP

LFPL[Hofmann, ’99/00]

Traditional Programming

OPI1

I2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

Resource Aware Programming[EMSOFT ’03]

P1I1 P2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

I2

O

Page 11: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Multi-stage programming (MSP)• Provide abstraction mechanisms like: polymorphism, higher-order

functions, exceptions, …

• “Abstractions without guilt” through prg. generation

• Without damaging the static typing discipline

ApproachBuild

“f (x,y)”

Combine

F X

Syntactic correctness?

Reject “f (x,)”

Type correctness?

Reject “7 (8)”

String

Datatype

MSP

Page 12: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Multi-stage programming (MSP)

Three staging annotations:

Construct Example Result

Brackets: a=<2*4> a=<2*4>

Escape: b=<9+~a> b=<9+2*4>

Run: c= .! b c=17

Simplified typing rules:

Given of type We get of type

X T <X> <T>

X <T> ~X T

X <T> .! X T

Page 13: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Small example: Exponentiation

Single stage:

let rec exp(n:int, x:real):real =

if n = 0

then 1.0

else if even (n)

then sqr (exp(n div 2,x))

else x * (exp(n - 1,x));;

xnPn

x

Page 14: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Small example: Exponentiation

Two stage:

let rec exp(n:int, x:<real>):<real>=

if n = 0

then <1.0>

else if even (n)

then <sqr ~(exp(n div 2,x))>

else <~x * ~(exp(n - 1,x))>;;

Only things in gold are left for second stage

xnn P1 P2

x

Page 15: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

To use the staged exp we simply write:

<f(x) = ~(exp(5,<x>))>;;

The result for the second stage is:

<f(x)= x*(sqr(sqr(x*1.0)))>

Small example: Exponentiation

Just 4 ops!

No recursion

Page 16: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

I OP

LFPL[Hofmann, ’99/00]

Traditional Programming

OPI1

I2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

Resource Aware Programming[EMSOFT ’03]

P1I1 P2

Multi-Stage Programming (MSP) [Taha, Sheard ’97]

OI1 P1 P2

I2

I2

O

Page 17: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Why RAP (= MSP + RB) is hard

Two key points:

• Want to guarantee RB before generation

• Must somehow isolate expressive features, even when used in same language. Naïve combination doesn’t work. For LFPL:

– Can just combine non-linear and linear environments

• Some first-stage values must be linear

• Some second-stage values can be non-linear

Page 18: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

RAP language for LFPL

• GeHB: A language for “Generating Heap-Bounded programs”

• Intuitively: Program/configure on PC, execute on embedded/real-time platform

• All done in one language that integrates the two platforms very closely

• Static analysis (type system) guarantees that – The two platforms never get confused

– Before any programs are generated, we know that all generated programs will be resource bounded.

Page 19: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Type System

Page 20: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Summary of Results

• Evaluation (generation) preserves types

– Standard type preservation

• Code values are easily turned into LFPL code

– Requires a form of let-floating

• Can only be done for well-typed terms

• Generated LFPL code is well-typed

– And Hofmann shows how to translate to C

• No lost expressivity (GeHB is strictly more expressive)

Page 21: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Safe, fast, and generic insert. sort

let rec sort_gen(myLess,list) =

let [less] = myLess in (* Co-monadic co-bind *)

<let rec insert(d,a,l) =

case l of

nil -> cons(a, nil) at d

| cons(b,t) at d' -> if ~ (less(<a> ,<b> ))

then cons(a, cons(b, t) at d') at d

else cons(b, insert(a, d', t)) at d

in let rec sort(l) =

case l of

nil -> nil

| cons(a,t) at d -> insert(d, a, sort(t))

in sort(~list)>

Page 22: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Summary

• GP languages give us expressivity– But no guarantees about resources

• RB languages give us resource bound– But limit expressivity

• MSP allows us to express staging

• Carefully designed RAP languages give the programmer more control without becoming too low-level or unsafe.

Claim: Underlying idea is general & tractable.

Page 23: Generating Heap- bounded Programs Walid Taha, Stephan Ellner (Rice University) Hongwei Xi (Boston University)

Future work on RAPSuccess requires targeting three areas:

1. Design an theory• Lots of basic PL research needed here

• In RB languages, MSP, and how to combine

2. Applications• Currently: device drivers and n.w. interface

cards

3. Implementation and engineering aspects• Multiple target platforms

• Essential for technology transition