SlideShare a Scribd company logo
Refinement types for Haskell
Martin Ockajak from Zürich
Software Engineer
@martin_ockajak
Outline
●
Motivation
●
Refinement types
●
Liquid Haskell
●
Practical considerations
Motivation
Standard type system
●
Allows expressing certain properties of programs
●
Type safety
●
Verifiable without running the program
●
Static type checking
●
Integrated with the compilation
●
Testing still needed
●
Can we do better ?
Possible improvements
●
Prevent more programming errors
●
Division by zero
●
Missing keys in maps
●
Infinite loops
●
Express properties of programs in greater detail
●
Keep the ability to automatically verify type safety
●
Verification must be a decidable problem
●
No proofs by the programmer required
Refinement types
Refinement types
●
Consist of
●
Type
●
Standard or refinement
●
Predicate
●
Propositional logic
●
Can describe valid inputs and outputs of functions
●
Type safe if the predicate is valid for all inputs
Predicate
●
Boolean operators
●
&& , || , not , => , <=> , true , false
●
Arithmetic operators
●
+ , - , * , / , mod
●
Relations
●
== , /= , < , > , <= , >=
Liquid Haskell
Liquid Haskell
●
Static refinement type verifier
●
Completely automatic
●
Translates refinement types into verification conditions
●
Satisfiability modulo theories formulas
●
Uses an SMT solver to verify those conditions
●
Without executing the program or enumerating inputs
●
Project at University of California - San Diego
●
http://goto.ucsd.edu/~rjhala/liquid/haskell/blog/about/
Defining refinement types
●
Positive is a subtype of NonZero
●
Positive values are a subset of NonZero values
{-@ type NonZero = {v: Int | v /= 0 } @-}
{-@ type Positive = {v: Int | v > 0 } @-}
{-@ type Odd = {v: Int | v mod 2 == 1 } @-}
{-@ one :: NonZero @-}
{-@ one :: Positive @-}
{-@ one :: Odd @-}
one :: Int
one = 1
{-@ odds :: [Odd] @-}
odds :: [Int]
odds = [1, 3, 7]
Refining function results
{-@ two :: {v: Int | v mod 2 == 0 } @-}
{-@ one, two :: NonZero @-}
two :: Int
two = 1 + 1
{-@ size :: [a] -> {v: Int | v >= 0 } @-}
size :: [a] -> Int
size [] = 0
size (x:xs) = 1 + size xs
{-@ positive :: n:Int -> { v: Bool | Prop v <=> n > 0 } @-}
positive :: Int -> Bool
positive n = n > 0
Refining function arguments
{-@ crash :: {v: String | false } -> a @-}
crash :: String -> a
crash message = error message
{-@ divide :: Int -> NonZero -> Int @-}
divide :: Int -> Int -> Int
divide n 0 = crash "division by zero"
divide n d = n `div` d
correctDivide :: Int
correctDivide = divide 1 1
incorrectDivide :: Int
incorrectDivide = divide 1 0
Defining predicates
{-@ predicate Positive N = N > 0 @-}
{-@ predicate Even N = N mod 2 == 0 @-}
{-@ predicate PositiveOdd N = Positive N && not Even N @-}
{-@ type Even = { v: Int | Even v } @-}
{-@ three :: { v: Int | PositiveOdd v || v == 4 } @-}
three :: Int
three = 5 - 2
Measure functions
●
Can be used inside refinement type definitions
●
Single expression for every data constructor
●
Propositional logic only
data List a = Emp
| (:::) a (List a)
{-@ measure len @-}
len :: List a -> Int
len Emp = 0
len (x:::xs) = 1 + len xs
{-@ first :: {v: List a | len v > 0 } -> a @-}
first Emp = crash "empty list"
first (x:::xs) = x
Refining data types
●
Parametrized type alias used to specify list length
data Triple a = Triple (List a)
{-@ type ListN a N = {v: List a | len v == N} @-}
{-@ data Triple a = Triple (ListN a 3) @-}
correctTriple = Triple (1 ::: (2 ::: (3 ::: Emp)))
Inline functions and assumptions
●
Inline functions can be used inside measures
●
Assumptions allow describing non-verifiable functions
{-@ inline increment2 @-}
increment2 :: Int -> Int
increment2 n = n + 2
{-@ measure doubleLen @-}
doubleLen :: List a -> Int
doubleLen Emp = 0
doubleLen (x:::xs) = increment2 (doubleLen xs)
{-@ assume abs :: (Num a) => a -> {v: a | v > 0 } @-}
Recursion
{-@ type NonNegative a = {v: a | v >= 0 } @-}
{-@ type Natural a = {v: a | v > 0 } @-}
{-@ fact :: (Integral a) => NonNegative a -> Natural a @-}
fact :: (Integral a) => a -> a
fact 0 = 1
fact n = n * fact (n – 1)
correctFact = fact 3
incorrectFact = fact (-1)
Practical considerations
Practicality – Liquid Haskell
●
Compatible with several SMT solvers
●
Incremental checking support
●
Decent documentation
●
Still experimental
Thank you :-)

More Related Content

PDF
Comparing Haskell & Scala
PDF
Scala for Java Developers
PDF
javascript objects
PDF
JavaScript objects and functions
PPT
Extractors & Implicit conversions
PDF
3.1 javascript objects_DOM
PPT
JavaScript Objects
PDF
Scala - core features
Comparing Haskell & Scala
Scala for Java Developers
javascript objects
JavaScript objects and functions
Extractors & Implicit conversions
3.1 javascript objects_DOM
JavaScript Objects
Scala - core features

What's hot (20)

PDF
JavaScript - Chapter 6 - Basic Functions
PPTX
The JavaScript Programming Language
PDF
Introducing Pattern Matching in Scala
PPTX
Principles of functional progrmming in scala
PPT
Scala functions
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
PDF
An introduction to functional programming with Swift
PPT
Templates
PPTX
Scala Back to Basics: Type Classes
PPTX
Java Tutorial Lab 8
PDF
Teach Yourself some Functional Programming with Scala
PDF
An Introduction to Part of C++ STL
PDF
Implicit conversion and parameters
PPT
standard template library(STL) in C++
PPT
Functional object
PDF
ScalaTrainings
PDF
JavaScript - Chapter 4 - Types and Statements
PPTX
ODP
Knolx session
PDF
Fii Practic Frontend - BeeNear - laborator3
JavaScript - Chapter 6 - Basic Functions
The JavaScript Programming Language
Introducing Pattern Matching in Scala
Principles of functional progrmming in scala
Scala functions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
An introduction to functional programming with Swift
Templates
Scala Back to Basics: Type Classes
Java Tutorial Lab 8
Teach Yourself some Functional Programming with Scala
An Introduction to Part of C++ STL
Implicit conversion and parameters
standard template library(STL) in C++
Functional object
ScalaTrainings
JavaScript - Chapter 4 - Types and Statements
Knolx session
Fii Practic Frontend - BeeNear - laborator3
Ad

Similar to Refinement Types for Haskell (20)

PDF
Liquid Haskell: Theorem Proving for All
PDF
Haskell for data science
PDF
Introduction to Functional Languages
KEY
An Introduction to Functional Programming using Haskell
PPT
haskell5.ppt is a marketing document lol
PDF
Functional programming with haskell
PDF
Real World Haskell: Lecture 6
PDF
Reasoning about laziness
ODP
03. haskell refresher quiz
PDF
[FLOLAC'14][scm] Functional Programming Using Haskell
PPTX
Dependent Types with Idris
PPTX
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
PDF
02. haskell motivation
PDF
10. haskell Modules
PDF
Refined types (FP-Syd)
PDF
Why Haskell Matters
PDF
Real World Haskell: Lecture 2
PPTX
Introduction to Dependently Types: Idris
PDF
High-Performance Haskell
PDF
[Expert Fridays] Александр Чичигин - Как перестать бояться и полюбить COQ
Liquid Haskell: Theorem Proving for All
Haskell for data science
Introduction to Functional Languages
An Introduction to Functional Programming using Haskell
haskell5.ppt is a marketing document lol
Functional programming with haskell
Real World Haskell: Lecture 6
Reasoning about laziness
03. haskell refresher quiz
[FLOLAC'14][scm] Functional Programming Using Haskell
Dependent Types with Idris
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
02. haskell motivation
10. haskell Modules
Refined types (FP-Syd)
Why Haskell Matters
Real World Haskell: Lecture 2
Introduction to Dependently Types: Idris
High-Performance Haskell
[Expert Fridays] Александр Чичигин - Как перестать бояться и полюбить COQ
Ad

Recently uploaded (20)

PPTX
Transform Your Business with a Software ERP System
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
medical staffing services at VALiNTRY
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
Transform Your Business with a Software ERP System
Design an Analysis of Algorithms II-SECS-1021-03
System and Network Administraation Chapter 3
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
Nekopoi APK 2025 free lastest update
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Introduction to Artificial Intelligence
Design an Analysis of Algorithms I-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
Reimagine Home Health with the Power of Agentic AI​
Odoo POS Development Services by CandidRoot Solutions
medical staffing services at VALiNTRY
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
How Creative Agencies Leverage Project Management Software.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
wealthsignaloriginal-com-DS-text-... (1).pdf
How to Migrate SBCGlobal Email to Yahoo Easily

Refinement Types for Haskell

  • 2. Martin Ockajak from Zürich Software Engineer @martin_ockajak
  • 5. Standard type system ● Allows expressing certain properties of programs ● Type safety ● Verifiable without running the program ● Static type checking ● Integrated with the compilation ● Testing still needed ● Can we do better ?
  • 6. Possible improvements ● Prevent more programming errors ● Division by zero ● Missing keys in maps ● Infinite loops ● Express properties of programs in greater detail ● Keep the ability to automatically verify type safety ● Verification must be a decidable problem ● No proofs by the programmer required
  • 8. Refinement types ● Consist of ● Type ● Standard or refinement ● Predicate ● Propositional logic ● Can describe valid inputs and outputs of functions ● Type safe if the predicate is valid for all inputs
  • 9. Predicate ● Boolean operators ● && , || , not , => , <=> , true , false ● Arithmetic operators ● + , - , * , / , mod ● Relations ● == , /= , < , > , <= , >=
  • 11. Liquid Haskell ● Static refinement type verifier ● Completely automatic ● Translates refinement types into verification conditions ● Satisfiability modulo theories formulas ● Uses an SMT solver to verify those conditions ● Without executing the program or enumerating inputs ● Project at University of California - San Diego ● http://goto.ucsd.edu/~rjhala/liquid/haskell/blog/about/
  • 12. Defining refinement types ● Positive is a subtype of NonZero ● Positive values are a subset of NonZero values {-@ type NonZero = {v: Int | v /= 0 } @-} {-@ type Positive = {v: Int | v > 0 } @-} {-@ type Odd = {v: Int | v mod 2 == 1 } @-} {-@ one :: NonZero @-} {-@ one :: Positive @-} {-@ one :: Odd @-} one :: Int one = 1 {-@ odds :: [Odd] @-} odds :: [Int] odds = [1, 3, 7]
  • 13. Refining function results {-@ two :: {v: Int | v mod 2 == 0 } @-} {-@ one, two :: NonZero @-} two :: Int two = 1 + 1 {-@ size :: [a] -> {v: Int | v >= 0 } @-} size :: [a] -> Int size [] = 0 size (x:xs) = 1 + size xs {-@ positive :: n:Int -> { v: Bool | Prop v <=> n > 0 } @-} positive :: Int -> Bool positive n = n > 0
  • 14. Refining function arguments {-@ crash :: {v: String | false } -> a @-} crash :: String -> a crash message = error message {-@ divide :: Int -> NonZero -> Int @-} divide :: Int -> Int -> Int divide n 0 = crash "division by zero" divide n d = n `div` d correctDivide :: Int correctDivide = divide 1 1 incorrectDivide :: Int incorrectDivide = divide 1 0
  • 15. Defining predicates {-@ predicate Positive N = N > 0 @-} {-@ predicate Even N = N mod 2 == 0 @-} {-@ predicate PositiveOdd N = Positive N && not Even N @-} {-@ type Even = { v: Int | Even v } @-} {-@ three :: { v: Int | PositiveOdd v || v == 4 } @-} three :: Int three = 5 - 2
  • 16. Measure functions ● Can be used inside refinement type definitions ● Single expression for every data constructor ● Propositional logic only data List a = Emp | (:::) a (List a) {-@ measure len @-} len :: List a -> Int len Emp = 0 len (x:::xs) = 1 + len xs {-@ first :: {v: List a | len v > 0 } -> a @-} first Emp = crash "empty list" first (x:::xs) = x
  • 17. Refining data types ● Parametrized type alias used to specify list length data Triple a = Triple (List a) {-@ type ListN a N = {v: List a | len v == N} @-} {-@ data Triple a = Triple (ListN a 3) @-} correctTriple = Triple (1 ::: (2 ::: (3 ::: Emp)))
  • 18. Inline functions and assumptions ● Inline functions can be used inside measures ● Assumptions allow describing non-verifiable functions {-@ inline increment2 @-} increment2 :: Int -> Int increment2 n = n + 2 {-@ measure doubleLen @-} doubleLen :: List a -> Int doubleLen Emp = 0 doubleLen (x:::xs) = increment2 (doubleLen xs) {-@ assume abs :: (Num a) => a -> {v: a | v > 0 } @-}
  • 19. Recursion {-@ type NonNegative a = {v: a | v >= 0 } @-} {-@ type Natural a = {v: a | v > 0 } @-} {-@ fact :: (Integral a) => NonNegative a -> Natural a @-} fact :: (Integral a) => a -> a fact 0 = 1 fact n = n * fact (n – 1) correctFact = fact 3 incorrectFact = fact (-1)
  • 21. Practicality – Liquid Haskell ● Compatible with several SMT solvers ● Incremental checking support ● Decent documentation ● Still experimental