liquid has iquid haskel ell l
play

Liquid Has iquid Haskel ell: l: Refj efjned, ned, refm fmectiv - PowerPoint PPT Presentation

Liquid Has iquid Haskel ell: l: Refj efjned, ned, refm fmectiv ective, and cl and clas assy Ryan Scott PL Wonks March 23, 2018 Refj fjnem nements ts divide :: Int -> {v:Int | v /= 0} -> Int divide n d = n `div` d Refj


  1. Liquid Has iquid Haskel ell: l: Refj efjned, ned, refm fmectiv ective, and cl and clas assy Ryan Scott PL Wonks March 23, 2018

  2. Refj fjnem nements ts divide :: Int -> {v:Int | v /= 0} -> Int divide n d = n `div` d

  3. Refj fjnem nement r ent refm efmection ction {-@ reflect fib @-} fib :: Int -> Int fib i | i == 0 = 0 | i == 1 = 1 | otherwise = fib (i-1) + fib (i-2) fibOne :: {fib 1 == 1} fibOne = trivial *** QED

  4. Refj fjnem nement r t refm efmection + type ction + type clas lasses? ses?

  5. Refj fjnem nement r t refm efmection + type ction + type clas lasses? ses? class Semigroup a where (<>) :: a -> a -> a

  6. Refj fjnem nement r t refm efmection + type ction + type clas lasses? ses? class Semigroup a where (<>) :: a -> a -> a class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z }

  7. Refj fjnem nement r t refm efmection + type ction + type clas lasses? ses? class Semigroup a where (<>) :: a -> a -> a class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z }

  8. Refj fjnem nement r t refm efmection + type ction + type clas lasses? ses? A t l class Semigroup a where e a s (<>) :: a -> a -> a t , n o t class Semigroup a => VerifiedSemigroup a where t o d appendAssoc a y . :: x:a -> y:a -> z:a . . -> { x <> (y <> z) == (x <> y) <> z }

  9. Why Why not? ot?

  10. Wh Why y not not? Desugari Desugaring ng class Semigroup a where (<>) :: a -> a -> a

  11. Wh Why y not not? Desugari Desugaring ng class Semigroup a where -- Surface syntax (<>) :: a -> a -> a

  12. Wh Why y not not? Desugari Desugaring ng class Semigroup a where -- Surface syntax (<>) :: a -> a -> a data Semigroup a { -- GHC core syntax (<>) :: a -> a -> a }

  13. De Desugar aring ng i ins nstances es instance Semigroup Unit where Unit <> Unit = Unit

  14. De Desugar aring ng i ins nstances es instance Semigroup Unit where Unit <> Unit = Unit semigroupUnit :: Semigroup Unit semigroupUnit = Semigroup { (<>) = appendUnit } appendUnit :: Unit -> Unit -> Unit appendUnit Unit Unit = Unit

  15. Desugar Desugaring functions ing functions smashList :: Semigroup a => a -> [a] -> a smashList x [] = x smashList x (y:ys) = smashList (x <> y) ys

  16. Desugar Desugaring functions ing functions smashList :: Semigroup a => a -> [a] -> a smashList x [] = x smashList x (y:ys) = smashList (x <> y) ys smashList :: Semigroup a -> a -> [a] -> a smashList _ x [] = x smashList dSemigroup x (y:ys) = smashList dSemigroup ((<>) dSemigroup x y) ys

  17. Key i y ins nsight ht Any refjned type involving type classes must be able to survive the translation to GHC core.

  18. Fi First (naï (naïve) a e) attem empt pt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z }

  19. Fi First (naï (naïve) a e) attem empt pt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } data VerifiedSemigroup a { semigroupSuperClass :: Semigroup a , appendAssoc :: x:a -> y:a -> z:a -> { (<>) d x ((<>) d y z) == (<>) d ((<>) d x y) z } }

  20. Fi First (naï (naïve) a e) attem empt pt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } data VerifiedSemigroup a { semigroupSuperClass :: Semigroup a , appendAssoc :: x:a -> y:a -> z:a -> { (<>) d x ((<>) d y z) ? == (<>) d ((<>) d x y) z } }

  21. Fi First (naï (naïve) a e) attem empt pt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } data VerifiedSemigroup a { semigroupSuperClass :: Semigroup a , appendAssoc :: x:a -> y:a -> z:a -> -> { forall d:VerifiedSemigroup a. (<>) d x ((<>) d y z) == (<>) d ((<>) d x y) z } }

  22. Fi First (naï (naïve) a e) attem empt pt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } data VerifiedSemigroup a { semigroupSuperClass :: Semigroup a , appendAssoc :: x:a -> y:a -> z:a -> -> { forall d:VerifiedSemigroup a. (<>) d x ((<>) d y z) == (<>) d ((<>) d x y) z } }

  23. can’t shove forall forall s withi We e can’t sh hin predic pr dicat ates wil willy ly-n -nilly ly. Liquid Haskell is based on the quantifjer-free logic of linear arithmetic and uninterpreted functions (QF-ULIA). { forall d:VerifiedSemigroup a. ... } { forall d:VerifiedSemigroup a. ... } Can’t be expressed in this system.

  24. Ob Obser servation ation We can dictate the behavior of type classes in Liquid Haskell by their instances .

  25. Bett Better r at attem empt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z }

  26. Bett Better r at attem empt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } instance VerifiedSemigroup Unit where ...

  27. Bett Better r at attem empt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } $dVSUnit :: VerifiedSemigroup Unit $dVSUnit = VerifiedSemigroup { ... } (appendAssoc $dVSUnit) :: x:a -> y:a -> z:a -> { (<>) $dVSUnit x ((<>) $dVSUnit y z) == (<>) $dVSUnit ((<>) $dVSUnit x y) z && ... }

  28. Bett Better r at attem empt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } instance VerifiedSemigroup Int where ...

  29. Bett Better r at attem empt class Semigroup a => VerifiedSemigroup a where appendAssoc :: x:a -> y:a -> z:a -> { x <> (y <> z) == (x <> y) <> z } $dVSInt :: VerifiedSemigroup Int $dVSInt = VerifiedSemigroup { ... } (appendAssoc $dVSInt) :: x:a -> y:a -> z:a -> { (<>) $dVSInt x ((<>) $dVSInt y z) == (<>) $dVSInt ((<>) $dVSInt x y) z && ... }

  30. Too l oo lon ong; di g; didn’t w n’t wat atch ch We begin to extend Liquid Haskell towards supporting refjnement refmection + type classes: ● Accommodate typing rules to be instance- aware (not as simple as it looks!) ● Desugar refjnements involving type classes into refjnements involving dictionaries

Recommend


More recommend