haskell compiler as theorem prover
play

Haskell: Compiler as Theorem-Prover Greg Price ( price ) 2007 Nov 19 - PowerPoint PPT Presentation

Haskell: Compiler as Theorem-Prover Greg Price ( price ) 2007 Nov 19 code samples: http://cluedumps.mit.edu/wiki/2007/11-19 Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 1 / 26 Software Transactional Memory 1


  1. Haskell: Compiler as Theorem-Prover Greg Price ( price ) 2007 Nov 19 code samples: http://cluedumps.mit.edu/wiki/2007/11-19 Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 1 / 26

  2. Software Transactional Memory 1 Protocol Types 2 More theorems 3 The Big Picture 4 References 5 Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 2 / 26

  3. Software Transactional Memory Concurrency: locking Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 4 / 26

  4. Software Transactional Memory Concurrency: locking costly, deadlocks, bugs. Optimistic transactions, restarting Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 4 / 26

  5. Software Transactional Memory Concurrency: locking costly, deadlocks, bugs. Optimistic transactions, restarting Worse bugs: void f() { void g() { begin_transaction(); begin_transaction(); if (x != y) x++; launch_missiles(); y++; end_transaction(); end_transaction(); } } Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 4 / 26

  6. Software Transactional Memory Concurrency: locking costly, deadlocks, bugs. Optimistic transactions, restarting Worse bugs: void f() { void g() { begin_transaction(); begin_transaction(); if (x != y) x++; launch_missiles(); y++; end_transaction(); end_transaction(); } } Restart side effects? Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 4 / 26

  7. Software Transactional Memory Concurrency: locking costly, deadlocks, bugs. Optimistic transactions, restarting Worse bugs: void f() { void g() { begin_transaction(); begin_transaction(); if (x != y) x++; launch_missiles(); y++; end_transaction(); end_transaction(); } } Restart side effects? & all the old bugs too Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 4 / 26

  8. Software Transactional Memory Solution: f = atomically $ do xv <- readTVar x yv <- readTVar y if xv /= yv then launch_missiles_soon else return () g = atomically $ do xv <- readTVar x; writeTVar x (xv+1) yv <- readTVar y; writeTVar y (yv+1) (see example STMExample ) Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 6 / 26

  9. Software Transactional Memory Solution: f = atomically $ do xv <- readTVar x yv <- readTVar y if xv /= yv then launch_missiles_soon else return () g = atomically $ do xv <- readTVar x; writeTVar x (xv+1) yv <- readTVar y; writeTVar y (yv+1) (see example STMExample ) can’t have (non-transactional) side effects Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 6 / 26

  10. Software Transactional Memory Solution: f = atomically $ do xv <- readTVar x yv <- readTVar y if xv /= yv then launch_missiles_soon else return () g = atomically $ do xv <- readTVar x; writeTVar x (xv+1) yv <- readTVar y; writeTVar y (yv+1) (see example STMExample ) can’t have (non-transactional) side effects no special compiler support (except runtime) Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 6 / 26

  11. Software Transactional Memory Solution: f = atomically $ do xv <- readTVar x yv <- readTVar y if xv /= yv then launch_missiles_soon else return () g = atomically $ do xv <- readTVar x; writeTVar x (xv+1) yv <- readTVar y; writeTVar y (yv+1) (see example STMExample ) can’t have (non-transactional) side effects no special compiler support (except runtime) other bugs ruled out too Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 6 / 26

  12. STM: Guaranteeing No Side Effects pure Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 8 / 26

  13. STM: Guaranteeing No Side Effects pure putStr "hello" :: IO () Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 8 / 26

  14. STM: Guaranteeing No Side Effects pure an IO action putStr "hello" :: IO () Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 8 / 26

  15. STM: Guaranteeing No Side Effects pure an IO action putStr "hello" :: IO () sequenced: do { ...; f :: IO a; ... } Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 8 / 26

  16. STM: Guaranteeing No Side Effects pure an IO action putStr "hello" :: IO () sequenced: do { ...; f :: IO a; ... } executed only through main : main :: IO () main = do putStr "Hello world!\n" launch_missiles Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 8 / 26

  17. STM: Guaranteeing No Side Effects pure an IO action putStr "hello" :: IO () sequenced: do { ...; f :: IO a; ... } executed only through main : main :: IO () main = do putStr "Hello world!\n" launch_missiles ⇒ side effects only through type IO a Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 8 / 26

  18. STM: Guaranteeing No Side Effects side effects only through type IO a Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 10 / 26

  19. STM: Guaranteeing No Side Effects side effects only through type IO a atomically :: STM a -> IO a Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 10 / 26

  20. STM: Guaranteeing No Side Effects side effects only through type IO a atomically :: STM a -> IO a newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM () Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 10 / 26

  21. STM: Guaranteeing No Side Effects side effects only through type IO a atomically :: STM a -> IO a newTVar :: a -> STM (TVar a) readTVar :: TVar a -> STM a writeTVar :: TVar a -> a -> STM () do { ...; f :: STM a; ... } (same) Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 10 / 26

  22. Software Transactional Memory 1 Protocol Types 2 More theorems 3 The Big Picture 4 References 5 Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 11 / 26

  23. Protocol Types spec :: Spec ((Snd Int :+: Snd String) :->: End) IOChan a protocol spec Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 13 / 26

  24. Protocol Types spec :: Spec ((Snd Int :+: Snd String) :->: End) IOChan � �� � s a protocol spec Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 13 / 26

  25. Protocol Types spec :: Spec ((Snd Int :+: Snd String) :->: End) IOChan � �� � s a protocol spec accept spec request spec Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 13 / 26

  26. Protocol Types spec :: Spec ((Snd Int :+: Snd String) :->: End) IOChan � �� � s a protocol spec accept spec :: (Extend M (ChanCap c s) e e’ n) => LinearT IO e e’ (LVar n) request spec :: (Dual s s’, Extend M (ChanCap c s’) e e’ n) => LinearT IO e e’ (LVar n) Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 13 / 26

  27. Protocol Types spec :: Spec ((Snd Int :+: Snd String) :->: End) IOChan � �� � s a protocol spec accept spec :: (Extend M (ChanCap c s) e e’ n) => LinearT IO e e’ (LVar n) request spec :: (Dual s s’, Extend M (ChanCap c s’) e e’ n) => LinearT IO e e’ (LVar n) runLinearT (accept spec >>>= ...) :: IO a executes protocol exactly Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 13 / 26

  28. Protocol Types: Means of Proof runLinearT :: LinearT IO Empty Empty a -> IO a Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 15 / 26

  29. Protocol Types: Means of Proof runLinearT :: LinearT IO Empty Empty a -> IO a environments of capabilities Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 15 / 26

  30. Protocol Types: Means of Proof runLinearT :: LinearT IO Empty Empty a -> IO a environments of capabilities send :: (Evolve n c (Snd a :->: x) e x e’) => LVar n -> a -> LinearT IO e e’ () recv :: (Evolve n c (Rcv a :->: x) e x e’) => LVar n -> LinearT IO e e’ a Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 15 / 26

  31. Protocol Types: Means of Proof runLinearT :: LinearT IO Empty Empty a -> IO a environments of capabilities send :: (Evolve n c (Snd a :->: x) e x e’) => LVar n -> a -> LinearT IO e e’ () recv :: (Evolve n c (Rcv a :->: x) e x e’) => LVar n -> LinearT IO e e’ a sel1 :: (Evolve n c ((x1:+:x2):->:y) e (x1:->:y) e’) => LVar n -> LinearT IO e e’ () Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 15 / 26

  32. Protocol Types: Generic Building Blocks data T class Prop a data F instance Prop T instance Prop F Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 17 / 26

  33. Protocol Types: Generic Building Blocks data T class Prop a data F instance Prop T instance Prop F class Prop b => Equal x y b | x y -> b Greg Price ( price ) () Haskell: Compiler as Theorem-Prover 2007 Nov 19 17 / 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend