SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
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 :-)

Weitere ähnliche Inhalte

Was ist angesagt?

JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in ScalaAyush Mishra
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with SwiftFatih Nayebi, Ph.D.
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Java Tutorial Lab 8
Java Tutorial Lab 8Java Tutorial Lab 8
Java Tutorial Lab 8Berk Soysal
 
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaDamian Jureczko
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parametersKnoldus Inc.
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++•sreejith •sree
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and StatementsWebStackAcademy
 
Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3BeeNear
 

Was ist angesagt? (20)

JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
Scala functions
Scala functionsScala functions
Scala functions
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 
Templates
TemplatesTemplates
Templates
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Java Tutorial Lab 8
Java Tutorial Lab 8Java Tutorial Lab 8
Java Tutorial Lab 8
 
Teach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with ScalaTeach Yourself some Functional Programming with Scala
Teach Yourself some Functional Programming with Scala
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Functional object
Functional objectFunctional object
Functional object
 
ScalaTrainings
ScalaTrainingsScalaTrainings
ScalaTrainings
 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
Scala
ScalaScala
Scala
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3
 

Ähnlich wie Refinement Types for Haskell

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskellfaradjpour
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfSheba41
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptBrendan Eich
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notesGOKULKANNANMMECLECTC
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with PythonSushant Mane
 
5 conceptos progamacion2-tema4
5 conceptos progamacion2-tema45 conceptos progamacion2-tema4
5 conceptos progamacion2-tema4Elba Sepúlveda
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabBilawalBaloch1
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppthenokmetaferia1
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 

Ähnlich wie Refinement Types for Haskell (20)

Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
introduction to matlab.pptx
introduction to matlab.pptxintroduction to matlab.pptx
introduction to matlab.pptx
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
Extensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScriptExtensible Operators and Literals for JavaScript
Extensible Operators and Literals for JavaScript
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
bobok
bobokbobok
bobok
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with Python
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
5 conceptos progamacion2-tema4
5 conceptos progamacion2-tema45 conceptos progamacion2-tema4
5 conceptos progamacion2-tema4
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
Functions
FunctionsFunctions
Functions
 
Data Handling
Data Handling Data Handling
Data Handling
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 

Kürzlich hochgeladen

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 

Kürzlich hochgeladen (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 

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