SlideShare a Scribd company logo
Theorem Proving for All
Niki Vazou
+
Haskell
Refinement Types
=
take :: [a] -> Int -> [a]
> take [1,2,3] 2
> [1,2]
Haskell
> take [1,2,3] 500
> ???
take :: [a] -> Int -> [a]
Haskell
Refinement Types
take :: xs:[a] -> {i:Int | i < len xs} -> [a]
> take [1,2,3] 500
> Refinement Type Error!
take :: xs:[a] -> {i:Int | i < len xs} -> [a]
II. Application: Speed up Parsing
III. Expressiveness: Theorem Proving
I. Static Checks: Fast & Safe CodeStatic Checks
Expressiveness
I. Static Checks: Fast & Safe CodeStatic Checks
Buffer overread in OpenSSL. 2015
The Heartbleed Bug
in
module Data.Text where
take :: t:Text -> i:Int -> Text
> take "hat" 500
> *** Exception: Out Of Bounds!
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
take :: t:Text -> i:Int -> Text
take t i | i < len t
= Unsafe.take t i
take t i
= error “Out Of Bounds!”
Runtime Checks
Safe, but slow!
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
take :: t:Text -> i:Int -> Text
take t i | i < len t
= Unsafe.take t i
take t i
= error “Out Of Bounds!”
No Checks
Fast, but unsafe!
No Checks
> take "hat" 500
> “hat584562594SOHNUL…
Overread
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
take :: t:Text -> i:Int -> Text
take t i | i < len t
= Unsafe.take t i
take t i
= error “Out Of Bounds!”
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
take :: t:Text -> i:Int -> Text
take t i | i < len t
= Unsafe.take t i
take t i
= error “Out Of Bounds!”
i < len t
Static Checks
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
take :: t:Text -> i:Int -> Text
take t i | i < len t
= Unsafe.take t i
take t i
= error “Out Of Bounds!”
Static Checks
take :: t:Text -> i:{i < len t} -> Texti < len t
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe.takeWord16 i t
take i t
= error “Out Of Bounds!”
take :: t:Text -> i:Int -> Text
take t i | i < len t
= Unsafe.take t i
take t i
= error “Out Of Bounds!”
Static Checks
take :: t:Text -> i:{i < len t} -> Texti < len t
Static Checks
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe. = error “Out Of Bounds!”
take :: t:Text -> i:{i < len t} -> Text
take t i
= Unsafe.take t i
take :: t:Text -> i:Int -> Text
take i t | i < len t
= Unsafe. = error “Out Of Bounds!”
take :: t:Text -> i:{i < len t} -> Text
take t i
= Unsafe.take t i
Static Checks
> take "hat" 500
Type Error
OK
Error
Code
Checks valid arguments, under facts.
Refinement Types
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
len x = 3 => v = 500 => v < len x
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
SMT-
query
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
SMT-
invalid
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
Checker reports Error
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
Checker reports Error
Checker reports Error
len x = 3 => v = 500 => v < len x
take :: t:Text -> {v | v < len t} -> Text
heartbleed = let x = "hat"
in take x 500
Checks valid arguments, under facts.
2
2
OK SMT-
valid
Checks valid arguments, under facts.
Static Checks
OK
Error
Code
I. Static Checks: Fast & Safe Code
II. Application: Speed up Parsing
III. Expressiveness: Theorem Proving
Static Checks
II. Application: Speed up Parsing
III. Expressiveness: Theorem Proving
I. Static Checks: Fast & Safe CodeStatic Checks
Application: Speed up Parsing
II. Application: Speed up ParsingApplication: Speed up Parsing
DEMO
Provably Correct & Faster Code!
Application: Speed up Parsing
SMT-Automatic Verification
SMT-Automatic Verification
How expressive can we get?
II. Application: Speed up Parsing
III. Expressiveness: Theorem Proving
I. Static Checks: Fast & Safe CodeStatic Checks
Expressiveness
III. Expressiveness: Theorem ProvingExpressiveness
Proof is in pen-and-paper :(
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
Theorem: For any x,
Proof.
reverse [x] = [x]
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
Proof is not machine checked.
Theorem: For any x, reverse [x] = [x]
Proof.
reverse [x]
— obviously!
= [x]
QED
Proof is not machine checked.
Theorem: For any x, reverse [x] = [x]
Proof.
Proof is not machine checked.
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
Theorem: For any x, reverse [x] = [x]
Check it with Liquid Haskell!
Proof.
Theorems as Refinement Types
Theorem:
For any x, reverse [x] = [x]
x:a ! { v:() | reverse [x] = [x] }
Refinement Type:
SMT equality
x:a ! { reverse [x] = [x] }
Theorems as Refinement Types
Theorem:
For any x, reverse [x] = [x]
Refinement Type:
Proof.
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
x:a ! { reverse [x] = [x] }
How to connect theorem with proof?
Proofs are programs
Theorems are types
— Curry & Howard
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
Proof as a Haskell function
singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] }
singletonP x
=
— applying ++ on [] and [x]
==. [x]
*** QED
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
singletonP x
= reverse [x]
— applying reverse on [x]
==. reverse [] ++ [x]
— applying reverse on []
==. [] ++ [x]
— applying ++ on [] and [x]
==. [x]
*** QED
Proof as a Haskell function
singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] }
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
singletonP x
= reverse [x]
— applying reverse on [x]
==. reverse [] ++ [x]
— applying reverse on []
==. [] ++ [x]
— applying ++ on [] and [x]
==. [x]
*** QED
singletonP :: x:a ! { reverse [x] = [x] }
How to encode equality?
x:a ! { reverse [x] = [x] }
Equational Operator in (Liquid) Haskell
(==.) :: x:a -> y:{ a | x = y }
-> {v:a | v = x && v = y }
x ==. y = y
checks both arguments are equal
returns 2nd argument,
to continue the proof!
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
singletonP x
= reverse [x]
— applying reverse on [x]
===. reverse [] ++ [x]
— applying reverse on []
==. [] ++ [x]
— applying ++ on [] and [x]
==. [x]
*** QED
singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] }
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
singletonP x
= reverse [x]
— applying reverse on [x]
===. reverse [] ++ [x]
— applying reverse on []
==. [] ++ [x]
— applying ++ on [] and [x]
==. [x]
*** QED
singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] }
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
singletonP x
= reverse [x]
— applying reverse on [x]
===. reverse [] ++ [x]
— applying reverse on []
==. [] ++ [x]
— applying ++ on [] and [x]
==. [x]
*** QED
singletonP :: x:a ! { reverse [x] = [x] }
How to encode QED?
x:a ! { reverse [x] = [x] }
Define QED as data constuctor…
(***) :: a -> QED -> ()
_ *** QED = ()
data QED = QED
… that casts anything into a proof
(i.e., a unit value).
reverse [x]
— applying reverse on [x]
= reverse [] ++ [x]
— applying reverse on []
= [] ++ [x]
— applying ++ on [] and [x]
= [x]
QED
singletonP x
= reverse [x]
— applying reverse on [x]
===. reverse [] ++ [x]
— applying reverse on []
==. [] ++ [x]
— applying ++ on [] and [x]
==. [x]
*** QED
singletonP :: x:a ! { reverse [x] = [x] }
Theorem Proving in Haskell
x:a ! { reverse [x] = [x] }
singletonP :: x:a ! { reverse [x] = [x] }
Theorems are Types
Theorem Application is Function Call
singletonP 1 :: { reverse [1] = [1] }
x:a ! { reverse [x] = [x] }
singletonP1 :: { reverse [1] = [1] }
singletonP1
= reverse [1]
? singletonP 1
==. [1]
*** QED
Theorem Application is Function Call
(?) :: a -> () -> a
x ? _ = x
Reasoning about Haskell Programs in Haskell!
Equational operators (==., ?, QED, ***)
Theorem Proving for All
let us encode proofs as Haskell functions
checked by Liquid Haskell.
How to encode inductive proofs?
Theorem Proving for All
Reasoning about Haskell Programs in Haskell!
Theorem: For any list x, reverse (reverse x) = x.
Proof.
involutionP []
= reverse (reverse [])
— applying inner reverse
= reverse []
— applying reverse
= []
QED
involutionP (x:xs)
= reverse (reverse (x:xs))
— applying inner reverse
= reverse (reverse xs ++ [x])
— distributivity on (reverse xs) [x]
= reverse [x] ++ reverse (reverse xs)
— involution on xs
= reverse [x] ++ xs
— singleton on x
= [x] ++ xs
— applying ++
= x:([] ++ xs)
— applying ++
= (x:xs)
QED
Base Case: Inductive Case:
Theorem: For any list x, reverse (reverse x) = x.
Proof.
involutionP []
= reverse (reverse [])
— applying inner reverse
= reverse []
— applying reverse
= []
QED
involutionP (x:xs)
= reverse (reverse (x:xs))
— applying inner reverse
= reverse (reverse xs ++ [x])
— distributivity on (reverse xs) [x]
= reverse [x] ++ reverse (reverse xs)
— involution on xs
= reverse [x] ++ xs
— singleton on x
= [x] ++ xs
— applying ++
= x:([] ++ xs)
— applying ++
= (x:xs)
QED
Base Case: Inductive Case:
Step 1: Define a recursive function!
Step 1: Define a recursive function!
Proof.
involutionP []
== reverse (reverse [])
— applying inner reverse
= reverse []
— applying reverse
= []
QED
involutionP (x:xs)
== reverse (reverse (x:xs))
— applying inner reverse
= reverse (reverse xs ++ [x])
— distributivity on (reverse xs) [x]
= reverse [x] ++ reverse (reverse xs)
— involution on xs
= reverse [x] ++ xs
— singleton on x
= [x] ++ xs
— applying ++
= x:([] ++ xs)
— applying ++
= (x:xs)
QED
Step 2: Use equational operators
Theorem: For any list x, reverse (reverse x) = x.
Proof.
involutionP []
==.=reverse (reverse [])
— applying inner reverse
==. reverse []
— applying reverse
==. []
*** QED
involutionP (x:xs)
==. reverse (reverse (x:xs))
— applying inner reverse
==. reverse (reverse xs ++ [x])
— distributivity on (reverse xs) [x]
==. reverse [x] ++ reverse (reverse xs)
— involution on xs
==. reverse [x] ++ xs
— singleton on x
==. [x] ++ xs
— applying ++
==. x:([] ++ xs)
— applying ++
==. (x:xs)
*** QED
Step 3: Lemmata are function calls!Step 2: Use equational operators
Theorem: For any list x, reverse (reverse x) = x.
Proof.
Step 3: Lemmata are function calls!
involutionP []
==.=reverse (reverse [])
— applying inner reverse
==. reverse []
— applying reverse
==. []
*** QED
involutionP (x:xs)
==. reverse (reverse (x:xs))
— applying inner reverse
==. reverse (reverse xs ++ [x])
? distributivityP (reverse xs) [x]
==. reverse [x] ++ reverse (reverse xs)
? involutionP xs
==. reverse [x] ++ xs
? singletonP x
==. [x] ++ xs
— applying ++
==. x:([] ++ xs)
— applying ++
==. (x:xs)
*** QED
Theorem: For any list x, reverse (reverse x) = x.
Proof.
involutionP []
==.=reverse (reverse [])
— applying inner reverse
==. reverse []
— applying reverse
==. []
*** QED
involutionP (x:xs)
==. reverse (reverse (x:xs))
— applying inner reverse
==. reverse (reverse xs ++ [x])
? distributivityP (reverse xs) [x]
==. reverse [x] ++ reverse (reverse xs)
? involutionP xs
==. reverse [x] ++ xs
? singletonP x
==. [x] ++ xs
— applying ++
==. x:([] ++ xs)
— applying ++
==. (x:xs)
*** QED
Note: Inductive hypothesis is recursive call!
Theorem: For any list x, reverse (reverse x) = x.
Proof.
involutionP []
==.=reverse (reverse [])
— applying inner reverse
==. reverse []
— applying reverse
==. []
*** QED
involutionP (x:xs)
==. reverse (reverse (x:xs))
— applying inner reverse
==. reverse (reverse xs ++ [x])
? distributivityP (reverse xs) [x]
==. reverse [x] ++ reverse (reverse xs)
? involutionP xs
==. reverse [x] ++ xs
? singletonP x
==. [x] ++ xs
— applying ++
==. x:([] ++ xs)
— applying ++
==. (x:xs)
*** QED
Question: Is the proof well founded?
Theorem: For any list x, reverse (reverse x) = x.
Used to encode pen-and-pencil proofs
https://bit.ly/2yjvJo3
“Theorem Proving for All”, Haskell’18
and function optimizations.
“LWeb: Information Flow Security for
Multi-Tier Web Applications”, POPL’19
https://bit.ly/2EcyDAh
Used to encode pen-and-pencil proofs
or even sophisticated security proofs.
“Liquidate your assets”
https://bit.ly/2Ht3uIG
Used to encode pen-and-pencil proofs
or encode resource analysis.
To be presented at IMDEA:
by Martin Handley
Tue March 19 @10.45
Used to encode pen-and-pencil proofs
But, proof interaction is missing.
II. Application: Speed up Parsing
III. Expressiveness: Theorem Proving
I. Static Checks: Fast & Safe Code
Thanks!
Theorem Proving for All
@nikivazou

More Related Content

ODP
Promise
DOCX
Java binary subtraction
PDF
The Ring programming language version 1.5.1 book - Part 23 of 180
PDF
ゲーム理論NEXT 期待効用理論第6回 -3つの公理と期待効用定理-
PDF
Automated theorem proving for special functions: the next phase
PPTX
2.7 chain rule short cuts
PDF
The Chain Rule, Part 2
PDF
The Ring programming language version 1.5.3 book - Part 77 of 184
Promise
Java binary subtraction
The Ring programming language version 1.5.1 book - Part 23 of 180
ゲーム理論NEXT 期待効用理論第6回 -3つの公理と期待効用定理-
Automated theorem proving for special functions: the next phase
2.7 chain rule short cuts
The Chain Rule, Part 2
The Ring programming language version 1.5.3 book - Part 77 of 184

What's hot (20)

PDF
Derivatives of Trigonometric Functions, Part 2
PPTX
String in .net
PPTX
Oh Composable World!
PDF
The Ring programming language version 1.6 book - Part 38 of 189
PDF
The Ring programming language version 1.3 book - Part 50 of 88
KEY
関数潮流(Function Tendency)
PDF
The Ring programming language version 1.4 book - Part 18 of 30
PDF
Mx/G(a,b)/1 With Modified Vacation, Variant Arrival Rate With Restricted Admi...
KEY
Haskellで学ぶ関数型言語
PPTX
Millionways
PDF
kan r_ifels
PDF
Program Language - Fall 2013
PDF
Some properties of two-fuzzy Nor med spaces
PDF
The Chain Rule, Part 1
PDF
The Ring programming language version 1.5.2 book - Part 24 of 181
PDF
The Ring programming language version 1.3 book - Part 16 of 88
PPTX
Recurrent Networks and LSTM deep dive
PDF
Dirac demo (quantum mechanics with C++). Please note: There is a problem with...
PDF
Digital Electronics
PDF
Clojure functions midje
Derivatives of Trigonometric Functions, Part 2
String in .net
Oh Composable World!
The Ring programming language version 1.6 book - Part 38 of 189
The Ring programming language version 1.3 book - Part 50 of 88
関数潮流(Function Tendency)
The Ring programming language version 1.4 book - Part 18 of 30
Mx/G(a,b)/1 With Modified Vacation, Variant Arrival Rate With Restricted Admi...
Haskellで学ぶ関数型言語
Millionways
kan r_ifels
Program Language - Fall 2013
Some properties of two-fuzzy Nor med spaces
The Chain Rule, Part 1
The Ring programming language version 1.5.2 book - Part 24 of 181
The Ring programming language version 1.3 book - Part 16 of 88
Recurrent Networks and LSTM deep dive
Dirac demo (quantum mechanics with C++). Please note: There is a problem with...
Digital Electronics
Clojure functions midje
Ad

Similar to Liquid Haskell: Theorem Proving for All (20)

PDF
Refinement Types for Haskell
PDF
[Expert Fridays] Александр Чичигин - Как перестать бояться и полюбить COQ
PDF
Reasoning about laziness
PDF
Intro To Agda
PDF
Functional programming with haskell
ODP
03. haskell refresher quiz
PDF
Scope Graphs: A fresh look at name binding in programming languages
PDF
Introduction to Functional Languages
PDF
Refined types (FP-Syd)
PPTX
Dependent Types with Idris
PDF
[FLOLAC'14][scm] Functional Programming Using Haskell
PDF
Introduction to idris
PPTX
Introduction to Dependently Types: Idris
KEY
An Introduction to Functional Programming using Haskell
KEY
Programming haskell chapter10
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
PDF
Real World Haskell: Lecture 6
PDF
SAT/SMT solving in Haskell
PPT
Pl vol1
PPT
Pl vol1
Refinement Types for Haskell
[Expert Fridays] Александр Чичигин - Как перестать бояться и полюбить COQ
Reasoning about laziness
Intro To Agda
Functional programming with haskell
03. haskell refresher quiz
Scope Graphs: A fresh look at name binding in programming languages
Introduction to Functional Languages
Refined types (FP-Syd)
Dependent Types with Idris
[FLOLAC'14][scm] Functional Programming Using Haskell
Introduction to idris
Introduction to Dependently Types: Idris
An Introduction to Functional Programming using Haskell
Programming haskell chapter10
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Real World Haskell: Lecture 6
SAT/SMT solving in Haskell
Pl vol1
Pl vol1
Ad

More from Facultad de Informática UCM (20)

PDF
¿Por qué debemos seguir trabajando en álgebra lineal?
PDF
TECNOPOLÍTICA Y ACTIVISMO DE DATOS: EL MAPEO COMO FORMA DE RESILIENCIA ANTE L...
PDF
DRAC: Designing RISC-V-based Accelerators for next generation Computers
PDF
uElectronics ongoing activities at ESA
PDF
Tendencias en el diseño de procesadores con arquitectura Arm
PDF
Formalizing Mathematics in Lean
PDF
Introduction to Quantum Computing and Quantum Service Oriented Computing
PPTX
Computer Design Concepts for Machine Learning
PDF
Inteligencia Artificial en la atención sanitaria del futuro
PDF
Design Automation Approaches for Real-Time Edge Computing for Science Applic...
PDF
Estrategias de navegación para robótica móvil de campo: caso de estudio proye...
PPTX
Fault-tolerance Quantum computation and Quantum Error Correction
PDF
Cómo construir un chatbot inteligente sin morir en el intento
PDF
Automatic generation of hardware memory architectures for HPC
PDF
Type and proof structures for concurrency
PDF
Hardware/software security contracts: Principled foundations for building sec...
PDF
Jose carlossancho slidesLa seguridad en el desarrollo de software implementad...
PDF
Do you trust your artificial intelligence system?
PDF
Redes neuronales y reinforcement learning. Aplicación en energía eólica.
PDF
Challenges and Opportunities for AI and Data analytics in Offshore wind
¿Por qué debemos seguir trabajando en álgebra lineal?
TECNOPOLÍTICA Y ACTIVISMO DE DATOS: EL MAPEO COMO FORMA DE RESILIENCIA ANTE L...
DRAC: Designing RISC-V-based Accelerators for next generation Computers
uElectronics ongoing activities at ESA
Tendencias en el diseño de procesadores con arquitectura Arm
Formalizing Mathematics in Lean
Introduction to Quantum Computing and Quantum Service Oriented Computing
Computer Design Concepts for Machine Learning
Inteligencia Artificial en la atención sanitaria del futuro
Design Automation Approaches for Real-Time Edge Computing for Science Applic...
Estrategias de navegación para robótica móvil de campo: caso de estudio proye...
Fault-tolerance Quantum computation and Quantum Error Correction
Cómo construir un chatbot inteligente sin morir en el intento
Automatic generation of hardware memory architectures for HPC
Type and proof structures for concurrency
Hardware/software security contracts: Principled foundations for building sec...
Jose carlossancho slidesLa seguridad en el desarrollo de software implementad...
Do you trust your artificial intelligence system?
Redes neuronales y reinforcement learning. Aplicación en energía eólica.
Challenges and Opportunities for AI and Data analytics in Offshore wind

Recently uploaded (20)

PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
Artificial Intelligence
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT
Project quality management in manufacturing
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Mechanical Engineering MATERIALS Selection
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Digital Logic Computer Design lecture notes
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
UNIT 4 Total Quality Management .pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Lecture Notes Electrical Wiring System Components
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Safety Seminar civil to be ensured for safe working.
Artificial Intelligence
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Project quality management in manufacturing
Foundation to blockchain - A guide to Blockchain Tech
Mechanical Engineering MATERIALS Selection
Operating System & Kernel Study Guide-1 - converted.pdf
573137875-Attendance-Management-System-original
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
bas. eng. economics group 4 presentation 1.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Digital Logic Computer Design lecture notes

Liquid Haskell: Theorem Proving for All

  • 1. Theorem Proving for All Niki Vazou
  • 3. take :: [a] -> Int -> [a] > take [1,2,3] 2 > [1,2] Haskell
  • 4. > take [1,2,3] 500 > ??? take :: [a] -> Int -> [a] Haskell
  • 5. Refinement Types take :: xs:[a] -> {i:Int | i < len xs} -> [a]
  • 6. > take [1,2,3] 500 > Refinement Type Error! take :: xs:[a] -> {i:Int | i < len xs} -> [a]
  • 7. II. Application: Speed up Parsing III. Expressiveness: Theorem Proving I. Static Checks: Fast & Safe CodeStatic Checks Expressiveness
  • 8. I. Static Checks: Fast & Safe CodeStatic Checks
  • 9. Buffer overread in OpenSSL. 2015 The Heartbleed Bug
  • 10. in
  • 11. module Data.Text where take :: t:Text -> i:Int -> Text > take "hat" 500 > *** Exception: Out Of Bounds!
  • 12. take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe.takeWord16 i t take i t = error “Out Of Bounds!” take :: t:Text -> i:Int -> Text take t i | i < len t = Unsafe.take t i take t i = error “Out Of Bounds!” Runtime Checks Safe, but slow!
  • 13. take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe.takeWord16 i t take i t = error “Out Of Bounds!” take :: t:Text -> i:Int -> Text take t i | i < len t = Unsafe.take t i take t i = error “Out Of Bounds!” No Checks Fast, but unsafe!
  • 14. No Checks > take "hat" 500 > “hat584562594SOHNUL… Overread take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe.takeWord16 i t take i t = error “Out Of Bounds!” take :: t:Text -> i:Int -> Text take t i | i < len t = Unsafe.take t i take t i = error “Out Of Bounds!”
  • 15. take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe.takeWord16 i t take i t = error “Out Of Bounds!” take :: t:Text -> i:Int -> Text take t i | i < len t = Unsafe.take t i take t i = error “Out Of Bounds!” i < len t Static Checks
  • 16. take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe.takeWord16 i t take i t = error “Out Of Bounds!” take :: t:Text -> i:Int -> Text take t i | i < len t = Unsafe.take t i take t i = error “Out Of Bounds!” Static Checks take :: t:Text -> i:{i < len t} -> Texti < len t
  • 17. take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe.takeWord16 i t take i t = error “Out Of Bounds!” take :: t:Text -> i:Int -> Text take t i | i < len t = Unsafe.take t i take t i = error “Out Of Bounds!” Static Checks take :: t:Text -> i:{i < len t} -> Texti < len t
  • 18. Static Checks take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe. = error “Out Of Bounds!” take :: t:Text -> i:{i < len t} -> Text take t i = Unsafe.take t i
  • 19. take :: t:Text -> i:Int -> Text take i t | i < len t = Unsafe. = error “Out Of Bounds!” take :: t:Text -> i:{i < len t} -> Text take t i = Unsafe.take t i Static Checks > take "hat" 500 Type Error
  • 20. OK Error Code Checks valid arguments, under facts. Refinement Types
  • 21. take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts. len x = 3 => v = 500 => v < len x
  • 22. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts.
  • 23. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts.
  • 24. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts.
  • 25. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts.
  • 26. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts.
  • 27. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts. SMT- query
  • 28. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts. SMT- invalid
  • 29. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts. Checker reports Error
  • 30. len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts. Checker reports Error
  • 31. Checker reports Error len x = 3 => v = 500 => v < len x take :: t:Text -> {v | v < len t} -> Text heartbleed = let x = "hat" in take x 500 Checks valid arguments, under facts. 2 2 OK SMT- valid
  • 32. Checks valid arguments, under facts. Static Checks OK Error Code
  • 33. I. Static Checks: Fast & Safe Code II. Application: Speed up Parsing III. Expressiveness: Theorem Proving Static Checks
  • 34. II. Application: Speed up Parsing III. Expressiveness: Theorem Proving I. Static Checks: Fast & Safe CodeStatic Checks Application: Speed up Parsing
  • 35. II. Application: Speed up ParsingApplication: Speed up Parsing DEMO
  • 36. Provably Correct & Faster Code! Application: Speed up Parsing SMT-Automatic Verification
  • 38. II. Application: Speed up Parsing III. Expressiveness: Theorem Proving I. Static Checks: Fast & Safe CodeStatic Checks Expressiveness
  • 39. III. Expressiveness: Theorem ProvingExpressiveness
  • 40. Proof is in pen-and-paper :( reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED Theorem: For any x, Proof. reverse [x] = [x]
  • 41. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED Proof is not machine checked. Theorem: For any x, reverse [x] = [x] Proof.
  • 42. reverse [x] — obviously! = [x] QED Proof is not machine checked. Theorem: For any x, reverse [x] = [x] Proof.
  • 43. Proof is not machine checked. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED Theorem: For any x, reverse [x] = [x] Check it with Liquid Haskell! Proof.
  • 44. Theorems as Refinement Types Theorem: For any x, reverse [x] = [x] x:a ! { v:() | reverse [x] = [x] } Refinement Type: SMT equality
  • 45. x:a ! { reverse [x] = [x] } Theorems as Refinement Types Theorem: For any x, reverse [x] = [x] Refinement Type:
  • 46. Proof. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED x:a ! { reverse [x] = [x] } How to connect theorem with proof?
  • 47. Proofs are programs Theorems are types — Curry & Howard
  • 48. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED Proof as a Haskell function singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] } singletonP x = — applying ++ on [] and [x] ==. [x] *** QED
  • 49. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED singletonP x = reverse [x] — applying reverse on [x] ==. reverse [] ++ [x] — applying reverse on [] ==. [] ++ [x] — applying ++ on [] and [x] ==. [x] *** QED Proof as a Haskell function singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] }
  • 50. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED singletonP x = reverse [x] — applying reverse on [x] ==. reverse [] ++ [x] — applying reverse on [] ==. [] ++ [x] — applying ++ on [] and [x] ==. [x] *** QED singletonP :: x:a ! { reverse [x] = [x] } How to encode equality? x:a ! { reverse [x] = [x] }
  • 51. Equational Operator in (Liquid) Haskell (==.) :: x:a -> y:{ a | x = y } -> {v:a | v = x && v = y } x ==. y = y checks both arguments are equal returns 2nd argument, to continue the proof!
  • 52. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED singletonP x = reverse [x] — applying reverse on [x] ===. reverse [] ++ [x] — applying reverse on [] ==. [] ++ [x] — applying ++ on [] and [x] ==. [x] *** QED singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] }
  • 53. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED singletonP x = reverse [x] — applying reverse on [x] ===. reverse [] ++ [x] — applying reverse on [] ==. [] ++ [x] — applying ++ on [] and [x] ==. [x] *** QED singletonP :: x:a ! { reverse [x] = [x] }x:a ! { reverse [x] = [x] }
  • 54. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED singletonP x = reverse [x] — applying reverse on [x] ===. reverse [] ++ [x] — applying reverse on [] ==. [] ++ [x] — applying ++ on [] and [x] ==. [x] *** QED singletonP :: x:a ! { reverse [x] = [x] } How to encode QED? x:a ! { reverse [x] = [x] }
  • 55. Define QED as data constuctor… (***) :: a -> QED -> () _ *** QED = () data QED = QED … that casts anything into a proof (i.e., a unit value).
  • 56. reverse [x] — applying reverse on [x] = reverse [] ++ [x] — applying reverse on [] = [] ++ [x] — applying ++ on [] and [x] = [x] QED singletonP x = reverse [x] — applying reverse on [x] ===. reverse [] ++ [x] — applying reverse on [] ==. [] ++ [x] — applying ++ on [] and [x] ==. [x] *** QED singletonP :: x:a ! { reverse [x] = [x] } Theorem Proving in Haskell x:a ! { reverse [x] = [x] }
  • 57. singletonP :: x:a ! { reverse [x] = [x] } Theorems are Types Theorem Application is Function Call singletonP 1 :: { reverse [1] = [1] } x:a ! { reverse [x] = [x] }
  • 58. singletonP1 :: { reverse [1] = [1] } singletonP1 = reverse [1] ? singletonP 1 ==. [1] *** QED Theorem Application is Function Call (?) :: a -> () -> a x ? _ = x
  • 59. Reasoning about Haskell Programs in Haskell! Equational operators (==., ?, QED, ***) Theorem Proving for All let us encode proofs as Haskell functions checked by Liquid Haskell.
  • 60. How to encode inductive proofs? Theorem Proving for All Reasoning about Haskell Programs in Haskell!
  • 61. Theorem: For any list x, reverse (reverse x) = x. Proof. involutionP [] = reverse (reverse []) — applying inner reverse = reverse [] — applying reverse = [] QED involutionP (x:xs) = reverse (reverse (x:xs)) — applying inner reverse = reverse (reverse xs ++ [x]) — distributivity on (reverse xs) [x] = reverse [x] ++ reverse (reverse xs) — involution on xs = reverse [x] ++ xs — singleton on x = [x] ++ xs — applying ++ = x:([] ++ xs) — applying ++ = (x:xs) QED Base Case: Inductive Case:
  • 62. Theorem: For any list x, reverse (reverse x) = x. Proof. involutionP [] = reverse (reverse []) — applying inner reverse = reverse [] — applying reverse = [] QED involutionP (x:xs) = reverse (reverse (x:xs)) — applying inner reverse = reverse (reverse xs ++ [x]) — distributivity on (reverse xs) [x] = reverse [x] ++ reverse (reverse xs) — involution on xs = reverse [x] ++ xs — singleton on x = [x] ++ xs — applying ++ = x:([] ++ xs) — applying ++ = (x:xs) QED Base Case: Inductive Case: Step 1: Define a recursive function!
  • 63. Step 1: Define a recursive function! Proof. involutionP [] == reverse (reverse []) — applying inner reverse = reverse [] — applying reverse = [] QED involutionP (x:xs) == reverse (reverse (x:xs)) — applying inner reverse = reverse (reverse xs ++ [x]) — distributivity on (reverse xs) [x] = reverse [x] ++ reverse (reverse xs) — involution on xs = reverse [x] ++ xs — singleton on x = [x] ++ xs — applying ++ = x:([] ++ xs) — applying ++ = (x:xs) QED Step 2: Use equational operators Theorem: For any list x, reverse (reverse x) = x.
  • 64. Proof. involutionP [] ==.=reverse (reverse []) — applying inner reverse ==. reverse [] — applying reverse ==. [] *** QED involutionP (x:xs) ==. reverse (reverse (x:xs)) — applying inner reverse ==. reverse (reverse xs ++ [x]) — distributivity on (reverse xs) [x] ==. reverse [x] ++ reverse (reverse xs) — involution on xs ==. reverse [x] ++ xs — singleton on x ==. [x] ++ xs — applying ++ ==. x:([] ++ xs) — applying ++ ==. (x:xs) *** QED Step 3: Lemmata are function calls!Step 2: Use equational operators Theorem: For any list x, reverse (reverse x) = x.
  • 65. Proof. Step 3: Lemmata are function calls! involutionP [] ==.=reverse (reverse []) — applying inner reverse ==. reverse [] — applying reverse ==. [] *** QED involutionP (x:xs) ==. reverse (reverse (x:xs)) — applying inner reverse ==. reverse (reverse xs ++ [x]) ? distributivityP (reverse xs) [x] ==. reverse [x] ++ reverse (reverse xs) ? involutionP xs ==. reverse [x] ++ xs ? singletonP x ==. [x] ++ xs — applying ++ ==. x:([] ++ xs) — applying ++ ==. (x:xs) *** QED Theorem: For any list x, reverse (reverse x) = x.
  • 66. Proof. involutionP [] ==.=reverse (reverse []) — applying inner reverse ==. reverse [] — applying reverse ==. [] *** QED involutionP (x:xs) ==. reverse (reverse (x:xs)) — applying inner reverse ==. reverse (reverse xs ++ [x]) ? distributivityP (reverse xs) [x] ==. reverse [x] ++ reverse (reverse xs) ? involutionP xs ==. reverse [x] ++ xs ? singletonP x ==. [x] ++ xs — applying ++ ==. x:([] ++ xs) — applying ++ ==. (x:xs) *** QED Note: Inductive hypothesis is recursive call! Theorem: For any list x, reverse (reverse x) = x.
  • 67. Proof. involutionP [] ==.=reverse (reverse []) — applying inner reverse ==. reverse [] — applying reverse ==. [] *** QED involutionP (x:xs) ==. reverse (reverse (x:xs)) — applying inner reverse ==. reverse (reverse xs ++ [x]) ? distributivityP (reverse xs) [x] ==. reverse [x] ++ reverse (reverse xs) ? involutionP xs ==. reverse [x] ++ xs ? singletonP x ==. [x] ++ xs — applying ++ ==. x:([] ++ xs) — applying ++ ==. (x:xs) *** QED Question: Is the proof well founded? Theorem: For any list x, reverse (reverse x) = x.
  • 68. Used to encode pen-and-pencil proofs https://bit.ly/2yjvJo3 “Theorem Proving for All”, Haskell’18 and function optimizations.
  • 69. “LWeb: Information Flow Security for Multi-Tier Web Applications”, POPL’19 https://bit.ly/2EcyDAh Used to encode pen-and-pencil proofs or even sophisticated security proofs.
  • 70. “Liquidate your assets” https://bit.ly/2Ht3uIG Used to encode pen-and-pencil proofs or encode resource analysis. To be presented at IMDEA: by Martin Handley Tue March 19 @10.45
  • 71. Used to encode pen-and-pencil proofs But, proof interaction is missing.
  • 72. II. Application: Speed up Parsing III. Expressiveness: Theorem Proving I. Static Checks: Fast & Safe Code Thanks! Theorem Proving for All @nikivazou