A Restricted Functional Language for File System...

75
CDSL A Restricted Functional Language for File System Verification Liam O’Connor FP-Syd, October 2013 Wednesday, 16 October 13

Transcript of A Restricted Functional Language for File System...

Page 1: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

CDSLA Restricted Functional Language

for File System Verification

Liam O’ConnorFP-Syd, October 2013

Wednesday, 16 October 13

Page 2: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

seL4

2

Abstract Spec

Low Level Spec

C Implementation

Wednesday, 16 October 13

Page 3: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

seL4

2

Abstract Spec

Low Level Spec

C ImplementationP

roofP

roof

Wednesday, 16 October 13

Page 4: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

seL4

2

Abstract Spec

Low Level Spec

C ImplementationP

roofP

roof

Purely Functional

High Level, Nondeterministic

Efficient

Wednesday, 16 October 13

Page 5: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

seL4

2

Abstract Spec

Low Level Spec

C ImplementationP

roofP

roof

Purely Functional

High Level, Nondeterministic

“Interesting”

“Boring”

Efficient

Wednesday, 16 October 13

Page 6: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Typical OS

3

include3%

kernel1%

arch19%

drivers59%

fs7%

sound5%

net5%

net sound fs driversarch kernel include

Wednesday, 16 October 13

Page 7: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Typical OS

3

arch19%

drivers59%

fs7%

sound5%

net5%

net sound fs driversarch kernel include

Wednesday, 16 October 13

Page 8: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Typical OS

3

fs7%

sound5%

net5%

net sound fs driversarch kernel include

Wednesday, 16 October 13

Page 9: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Economy of Scale

4

liamoc@duvel:~$ cat /proc/filesystems | wc -l31

liamoc@tstvm:~$ cat /proc/filesystems | wc -l49

Wednesday, 16 October 13

Page 10: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Economy of Scale

4

liamoc@duvel:~$ cat /proc/filesystems | wc -l31

liamoc@tstvm:~$ cat /proc/filesystems | wc -l49

We don’t want a cathedral, we want a factory!

Wednesday, 16 October 13

Page 11: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Economy of Scale

4

liamoc@duvel:~$ cat /proc/filesystems | wc -l31

liamoc@tstvm:~$ cat /proc/filesystems | wc -l49

We don’t want a cathedral, we want a factory! DSL!

Wednesday, 16 October 13

Page 12: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Our Approach

5

Abstract Spec

DSL Spec

C Implementation

Proof

DSL Implementation

Proof

generates

Wednesday, 16 October 13

Page 13: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Wishlist

6

Wednesday, 16 October 13

Page 14: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Wishlist

• Our DSL needs to:– Establish key verification properties:

• Type/Memory Safety, Termination, Totality

– Compile to efficient C code• Destructive updates, resource disposal, no excessive copying, etc.

– Be capable of expressing code for FS operations• Create file, rename file, etc.

6

Wednesday, 16 October 13

Page 15: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Wishlist

• Our DSL needs to:– Establish key verification properties:

• Type/Memory Safety, Termination, Totality

– Compile to efficient C code• Destructive updates, resource disposal, no excessive copying, etc.

– Be capable of expressing code for FS operations• Create file, rename file, etc.

• We do NOT need to express everything in DSL– Can use abstraction– Define once, verify once (manually)– These components should be used in every file system

6

Wednesday, 16 October 13

Page 16: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

�, x : ⌧ ` t : ⇢

� ` �(x :: ⌧). t : ⌧ ! ⇢

� ` a : ⌧ ! ⇢ � ` b : ⌧

� ` a b : ⇢

NICTA Copyright 2013 From imagination to impact

Simply typed λ-calculus

7

x : ⌧ 2 �

� ` x : ⌧

Wednesday, 16 October 13

Page 17: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

�, x : ⌧ ` t : ⇢

� ` �(x :: ⌧). t : ⌧ ! ⇢

� ` a : ⌧ ! ⇢ � ` b : ⌧

� ` a b : ⇢

� ` P

x : ⌧,� ` P

�1�2 ` P

�2�1 ` P

x : ⌧, x : ⌧,� ` P

x : ⌧,� ` P

NICTA Copyright 2013 From imagination to impact

Simply typed λ-calculus

7

x : ⌧ 2 �

� ` x : ⌧

Wednesday, 16 October 13

Page 18: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

� ` t : ⌧ �, x : ⌧ ` t

0 : ⇢

� ` let x :: ⌧ = t in t

0 : ⇢

� ` f : ⌧ ! ⇢ � ` x : ⌧

� ` f(x) : ⇢

NICTA Copyright 2013 From imagination to impact

First Order Language

8

x : ⌧ 2 �

� ` x : ⌧

� ` P

x : ⌧,� ` P

�1�2 ` P

�2�1 ` P

x : ⌧, x : ⌧,� ` P

x : ⌧,� ` P

Wednesday, 16 October 13

Page 19: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

9

Verification Implementation

Expressivity

Wednesday, 16 October 13

Page 20: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

9

Verification Implementation

Expressivity

Wednesday, 16 October 13

Page 21: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

9

Verification Implementation

Expressivity

Wednesday, 16 October 13

Page 22: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

9

Verification Implementation

Expressivity

GC

Wednesday, 16 October 13

Page 23: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

10

Wednesday, 16 October 13

Page 24: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

• Automatic memory management (GC) is too big a burden– Many static auto-MM techniques are also either inefficient or unsafe

10

Wednesday, 16 October 13

Page 25: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

• Automatic memory management (GC) is too big a burden– Many static auto-MM techniques are also either inefficient or unsafe

• But what about manual memory management?– let x = allocData () x’ = updateData x _ = free x in x’

10

Wednesday, 16 October 13

Page 26: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Memory Management

• Automatic memory management (GC) is too big a burden– Many static auto-MM techniques are also either inefficient or unsafe

• But what about manual memory management?– let x = allocData () x’ = updateData x _ = free x in x’

• But that’s unsafe/inefficient/terrible!– Types to the rescue!

10

Wednesday, 16 October 13

Page 27: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

x : ⌧ ` x : ⌧

�1 ` t : ⌧ �2, x : ⌧ ` t

0 : ⇢

�1�2 ` let x :: ⌧ = t in t

0 : ⇢

�1 ` f : ⌧ ! ⇢ �2 ` x : ⌧

�1�2 ` f(x) : ⇢

NICTA Copyright 2013 From imagination to impact

Linear, First Order Language

11

� ` P

x : ⌧,� ` P

�1�2 ` P

�2�1 ` P

x : ⌧, x : ⌧,� ` P

x : ⌧,� ` P

Wednesday, 16 October 13

Page 28: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Safety!?

– let x = allocData y = x _ = free x in y

– let y = allocData in ()

12

x : ⌧, x : ⌧,� ` P

x : ⌧,� ` P

� ` P

x : ⌧,� ` P

– let x = allocData () x’ = updateData x _ = free x in x’

x : ⌧, x : ⌧,� ` P

x : ⌧,� ` P

Note: CDSL core syntax, not surface syntax.

Wednesday, 16 October 13

Page 29: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Safety!?

13

Wednesday, 16 October 13

Page 30: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Safety!?

• This example seems safe, but rejected by linear types:– let x = allocData () x’ = updateData x _ = free x in x’

13

Wednesday, 16 October 13

Page 31: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Safety!?

• This example seems safe, but rejected by linear types:– let x = allocData () x’ = updateData x _ = free x in x’

• But, updateData is not expressible!– It has to free x or it would be using dereliction– It could destructively update it...

13

Wednesday, 16 October 13

Page 32: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Safety!?

• This example seems safe, but rejected by linear types:– let x = allocData () x’ = updateData x _ = free x in x’

• But, updateData is not expressible!– It has to free x or it would be using dereliction– It could destructively update it...

– let x = allocData () x’ = updateData! x in x’

13

Wednesday, 16 October 13

Page 33: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Two Semantics

14

Wednesday, 16 October 13

Page 34: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Two Semantics

• Value Semantics– Imagine everything is passed by value– There is no heap (free is a no-op)– Everything is immutable– Great for reasoning

14

Wednesday, 16 October 13

Page 35: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Two Semantics

• Value Semantics– Imagine everything is passed by value– There is no heap (free is a no-op)– Everything is immutable– Great for reasoning

• Update Semantics– Some things are stored in the heap– Destructive updates actually overwrite memory– Free actually deallocates memory– Great for implementation

14

Wednesday, 16 October 13

Page 36: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Two Semantics

• Value Semantics– Imagine everything is passed by value– There is no heap (free is a no-op)– Everything is immutable– Great for reasoning

• Update Semantics– Some things are stored in the heap– Destructive updates actually overwrite memory– Free actually deallocates memory– Great for implementation

14

Linear Types allow for both views!

Wednesday, 16 October 13

Page 37: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Unboxed types

• Some things we do want passed by value– Unboxed types, integers, small structs, etc.– They shouldn’t be linear!– Functions shouldn’t be linear either, or we could only call

them once.

– Simple solution: • allow dereliction and contraction for certain types.

15

T• T]

Wednesday, 16 October 13

Page 38: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Case study: Buffer interface

– make : () -> .Buf– free : .Buf -> ()– length : .Buf -> (#U32, .Buf)

– serialise : (.Obj, .Buf) -> (.Obj, .Buf)

– deserialise : .Buf -> (.Obj, .Buf)

16

Wednesday, 16 October 13

Page 39: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Case study: Buffer interface

– make : () -> .Buf– free : .Buf -> ()– length : .Buf -> (#U32, .Buf)

– serialise : (.Obj, .Buf) -> (.Obj, .Buf)

– deserialise : .Buf -> (.Obj, .Buf)

16

Same!

Wednesday, 16 October 13

Page 40: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Case study: Buffer interface

– make : () -> .Buf– free : .Buf -> ()– length : .Buf -> (#U32, .Buf)

– serialise : (.Obj, .Buf) -> (.Obj, .Buf)

– deserialise : .Buf -> (.Obj, .Buf)

16

Same!

Same!

Wednesday, 16 October 13

Page 41: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Shareable Values

• We need (nonlinear) “look, don’t touch” references.– make : () -> .Buf– free : .Buf -> ()– length : *Buf -> #U32– serialise : (*Obj, .Buf) -> .Buf– deserialise : *Buf -> .Obj

17

�1, y : T⇥ ` e : ⌧ �2, x : ⌧, y : T• ` e

0 : ⇢

�1�2, y : T• ` let! (y) x :: ⌧ = e in e

0 : ⇢

Wednesday, 16 October 13

Page 42: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Shareable values

• Unsafe again– let buf = make () in let! (buf) buf’ = buf in let _ = free buf in length buf’

18

�1, y : T⇥ ` e : ⌧ �2, x : ⌧, y : T• ` e

0 : ⇢

�1�2, y : T• ` let! (y) x :: ⌧ = e in e

0 : ⇢

Wednesday, 16 October 13

Page 43: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Shareable values

• Unsafe again– let buf = make () in let! (buf) buf’ = buf in let _ = free buf in length buf’

18

Shareable reference leaks!

�1, y : T⇥ ` e : ⌧ �2, x : ⌧, y : T• ` e

0 : ⇢

�1�2, y : T• ` let! (y) x :: ⌧ = e in e

0 : ⇢

Wednesday, 16 October 13

Page 44: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Shareable values

• Unsafe again– let buf = make () in let! (buf) buf’ = buf in let _ = free buf in length buf’

18

Shareable reference leaks!

�1, y : T⇥ ` e : ⌧ �2, x : ⌧, y : T• ` e

0 : ⇢

�1�2, y : T• ` let! (y) x :: ⌧ = e in e

0 : ⇢

⇢ safe for T

Wednesday, 16 October 13

Page 45: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Control Flow

• This should be allowed, but it isn’t.– let x = alloc () in if condition then update(x) else x

• This is unsafe–let x = alloc () in if condition then free(x) else ()

19

x : ⌧, x : ⌧,� ` P

x : ⌧,� ` P

Wednesday, 16 October 13

Page 46: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Control Flow

• This should be allowed, but it isn’t.– let x = alloc () in if condition then update(x) else x

• This is unsafe–let x = alloc () in if condition then free(x) else ()

19

x : ⌧, x : ⌧,� ` P

x : ⌧,� ` P

�1 ` c : Bool] �2 ` t : ⌧ �2 ` e : ⌧

�1�2 ` if c then t else e : ⌧

Wednesday, 16 October 13

Page 47: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Loops

20

Wednesday, 16 October 13

Page 48: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Loops

• Our one higher order conceit– iteration schema are (external) higher order functions

– for loops are higher order function application (plus a lambda)

20

� ` e : (Arr T )•� ` map e : (T ! T ) ! (Arr T )•

�1 ` i : (⌧ ! ⇢) ! � �2, x : ⌧ ` s : ⇢

�1�2 ` for x in i do s : �

� ` e : (Arr T )⇥� ` fold e : (T ! ✏) ! ✏

� ` i : (⌧ ! ⇢) ! � � ` e : �

� ` i with e : ((⌧, �) ! (⇢, �)) ! (�, �)

Wednesday, 16 October 13

Page 49: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Loops

• Multiply all array elements by 2 (destructively)

– let arr’ = for x in map(arr) do x * 2

• Sum up an array of integers:

–let sum = for (x,y) in fold(arr) with 0 do (x + y)

• Both at the same time

– let arr’, sum = for (x,y) in map(arr) with 0 do (x*2,x + y)

21

Wednesday, 16 October 13

Page 50: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Loops

• Unsafe again..– let y = alloc () in for x in map(arr) do let _ = free(y) in x

22

�1 ` i : (⌧ ! ⇢) ! � �2, x : ⌧ ` s : ⇢

�1�2 ` for x in i do s : �

Wednesday, 16 October 13

Page 51: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Loops

• Unsafe again..– let y = alloc () in for x in map(arr) do let _ = free(y) in x

22

�1 ` i : (⌧ ! ⇢) ! � �2, x : ⌧ ` s : ⇢

�1�2 ` for x in i do s : �

�2 does not contain any linear types

Wednesday, 16 October 13

Page 52: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

23

Wednesday, 16 October 13

Page 53: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

• C has error-handling via a return-code convention– We can do better!

23

Wednesday, 16 October 13

Page 54: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

• C has error-handling via a return-code convention– We can do better!

• Solution– Add a separate syntactic layer, statements, above the

expression layer.• Move let!, for, let, and if on to the statement level.

– (and anonymous products)

– Statements are different from expressions in that they can evaluate to multiple values and they can fail.

23

Wednesday, 16 October 13

Page 55: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

• C has error-handling via a return-code convention– We can do better!

• Solution– Add a separate syntactic layer, statements, above the

expression layer.• Move let!, for, let, and if on to the statement level.

– (and anonymous products)

– Statements are different from expressions in that they can evaluate to multiple values and they can fail.

23

s : ⌧s fails ⌧f

s : ⌧ss : fails ⌧f

Wednesday, 16 October 13

Page 56: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

24

Wednesday, 16 October 13

Page 57: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

24

for each i: �i ` ei : ⌧i�i ` return ei : ⌧i

Wednesday, 16 October 13

Page 58: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

24

for each i: �i ` ei : ⌧i�i ` return ei : ⌧i

�c : ec : err] for each i: �i ` ei : ⌧i�c�i ` fail ec ei : fails ⌧i

Wednesday, 16 October 13

Page 59: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

24

for each i: �i ` ei : ⌧i�i ` return ei : ⌧i

�c : ec : err] for each i: �i ` ei : ⌧i�c�i ` fail ec ei : fails ⌧i

�1 : e : Bool] �2 : st : Tt �2 : se : Te

�1�2 ` if e then st else se : Tt t Te

Wednesday, 16 October 13

Page 60: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

fails ⌧u ⌧t

NICTA Copyright 2013 From imagination to impact

Error Handling

24

for each i: �i ` ei : ⌧i�i ` return ei : ⌧i

�c : ec : err] for each i: �i ` ei : ⌧i�c�i ` fail ec ei : fails ⌧i

�1 : e : Bool] �2 : st : Tt �2 : se : Te

�1�2 ` if e then st else se : Tt t Te

Subtyping!⌧t fails ⌧u

Wednesday, 16 October 13

Page 61: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

• Let (and Let!) only deal with the success case!

let x = fail(EINVAL, 3) ???

– We add binding (and let!) forms for failure cases too.– The most interesting form is for the possible-failure case,

which is also a branching construct:

25

handle s (x. ss) (c x. sf )

Wednesday, 16 October 13

Page 62: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Error Handling

• Let (and Let!) only deal with the success case!

let x = fail(EINVAL, 3) ???

– We add binding (and let!) forms for failure cases too.– The most interesting form is for the possible-failure case,

which is also a branching construct:

25

handle s (x. ss) (c x. sf )

We force you to handle your error cases!

Wednesday, 16 October 13

Page 63: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Datatypes

26

Wednesday, 16 October 13

Page 64: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Datatypes

• CDSL supports product (record) and sum (tagged union) types– .{ field1 : .T, field2 : .U}– .< tag1 : T, tag2 : U >

26

Wednesday, 16 October 13

Page 65: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Datatypes

• CDSL supports product (record) and sum (tagged union) types– .{ field1 : .T, field2 : .U}– .< tag1 : T, tag2 : U >

• Product types are complicated:

26

Wednesday, 16 October 13

Page 66: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Datatypes

• CDSL supports product (record) and sum (tagged union) types– .{ field1 : .T, field2 : .U}– .< tag1 : T, tag2 : U >

• Product types are complicated:

26

let sum = operation(x.field1, x.field2)

Wednesday, 16 October 13

Page 67: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Datatypes

• CDSL supports product (record) and sum (tagged union) types– .{ field1 : .T, field2 : .U}– .< tag1 : T, tag2 : U >

• Product types are complicated:

26

let sum = operation(x.field1, x.field2) ✗

Wednesday, 16 October 13

Page 68: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Record Types

• Need to smash open a record into its constituent fields

27

let token { f1, f2 } = open rec f1’, f2’ = update(f1, f2) in close token {f1 = f1’, f2 = f2’ }

Wednesday, 16 October 13

Page 69: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Record Types

• Need to smash open a record into its constituent fields

27

let token { f1, f2 } = open rec f1’, f2’ = update(f1, f2) in close token {f1 = f1’, f2 = f2’ }

for destructive update

Wednesday, 16 October 13

Page 70: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Generating Purely Functional Specs

28

SimpleObj = { a : #U8 , b : #U8, c : .Foo }

simpleobj_example (so : .SimpleObj) : .SimpleObj fails .SimpleObj= { buf <- buf_create(42) handle code { fail (code, so) } ; buf,i <- let! (so) simpleobj_serialise(buf,so,0) handle (code, buf) { free(buf); fail (code, so) } ; so2 <- simpleobj_new('_',0) handle code { free(buf); fail (code, so) } ; so2 <- let! (buf) simpleobj_unserialise(buf, so2, 0) handle (code, so2) { free(buf,so2); fail (code, so) } ; ok <- let!(so, so2) return (so.a == so2.a && so.b == so2.b) ; free(buf) ; if not(ok) then { free (so2); fail (32,so) } else { free (so); return (so2) } }

Wednesday, 16 October 13

Page 71: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Generating Purely Functional Specs

29

SimpleObj = { a : U8 , b : U8, c : Foo }

simpleobj_example (so : SimpleObj) : SimpleObj fails SimpleObj= { buf <- buf_create(42) handle code { fail (code, so) } ; buf,i <- let! (so) simpleobj_serialise(buf,so,0) handle (code, buf) { free(buf); fail (code, so) } ; so2 <- simpleobj_new('_',0) handle code { free(buf); fail (code, so) } ; so2 <- let! (buf) simpleobj_unserialise(buf, so2, 0) handle (code, so2) { free(buf,so2); fail (code, so) } ; ok <- let!(so, so2) return (so.a == so2.a && so.b == so2.b) ; free(buf) ; if not(ok) then { free (so2); fail (32,so) } else { free (so); return (so2) } }

Wednesday, 16 October 13

Page 72: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Generating Purely Functional Specs

30

SimpleObj = { a : U8 , b : U8, c : Foo }

simpleobj_example (so : SimpleObj) : SimpleObj fails SimpleObj= { buf <- buf_create(42) handle code { fail (code, so) } ; buf,i <- simpleobj_serialise(buf,so,0) handle (code, buf) { free(buf); fail (code, so) } ; so2 <- simpleobj_new('_',0) handle code { free(buf); fail (code, so) } ; so2 <- simpleobj_unserialise(buf, so2, 0) handle (code, so2) { free(buf,so2); fail (code, so) } ; ok <- return (so.a == so2.a && so.b == so2.b) ; free(buf) ; if not(ok) then { free (so2); fail (32,so) } else { free (so); return (so2) } }

Wednesday, 16 October 13

Page 73: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Generating Purely Functional Specs

31

SimpleObj = { a : U8 , b : U8, c : Foo }

simpleobj_example (so : SimpleObj) : SimpleObj fails SimpleObj= { buf <- buf_create(42) handle code { fail (code, so) } ; buf,i <- simpleobj_serialise(buf,so,0) handle (code, buf) { fail (code, so) } ; so2 <- simpleobj_new('_',0) handle code { fail (code, so) } ; so2 <- simpleobj_unserialise(buf, so2, 0) handle (code, so2) { fail (code, so) } ; ok <- return (so.a == so2.a && so.b == so2.b)

; if not(ok) then { fail (32,so) } else { return (so2) } }

Wednesday, 16 October 13

Page 74: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Generating Purely Functional Specs

32

SimpleObj = { a : U8 , b : U8, c : Foo }

simpleobj_example (so : SimpleObj) : (Err, SimpleObj) + SimpleObj= case buf_create(42) of Inl code -> Inl (code,so) Inr buf -> case simpleobj_serialise(buf,so,0) of

Inl (code,buf) -> Inl (code,so) Inr (buf,i) -> case simpleobj_new('_',0) of Inl code -> Inl (code,so) Inr so2 -> case simpleobj_unserialise(buf, so2, 0) of Inl (code,so2) -> Inl (code,so) Inr so2 -> let ok = (so.a == so2.a && so.b == so2.b) in if not(ok) then Inl (32,so)

else Inr (so2)

Wednesday, 16 October 13

Page 75: A Restricted Functional Language for File System Verificationcode.ouroborus.net/fp-syd/past/2013/2013-10-OConner-CDSL.pdf · NICTA Copyright 2013 From imagination to impact Wishlist

NICTA Copyright 2013 From imagination to impact

Current Status

• We have– A paper about our overall project (not just CDSL) in PLOS

this year.– A working (but unverified) compiler to C– Formalised type system + dynamics on paper– Formalised dynamic semantics in Isabelle– Some outdated safety proofs in Agda– A good feeling about proof work remaining to be done– A prototype of another DSL for disk (de-)serialisation that

generates CDSL– A syntax headache

33

Wednesday, 16 October 13