SlideShare uma empresa Scribd logo
1 de 26
Without ⊥    Turing
            Complete



Codata       Comonad
Life without ⊥
 loop :: Int -> Int
 loop n = 1 + loop n


  loop 0 = 1 + loop 0


         0 = 1

        Int(⊥)
Life without ⊥   Simpler language design
Strict vs lazy

 -- a function returning the first argument
 first a b = a

 -- with strict evaluation
 first 1 ⊥ = ⊥

 -- with lazy evaluation
 first 1 ⊥ = 1
Life without ⊥   Simpler language design
Pattern matching

 -- will not match if (a, b) is ⊥
 first (a, b) = a

 -- a bottom value can be "lifted” to a pair
    of bottom values
 (⊥a, ⊥b) = ⊥
Life without ⊥    Simpler language design
& Operator
  True    &   True    =   True     ⊥ & y = ?
  True    &   False   =   False    x & ⊥ = ?
  False   &   True    =   False
  False   &   False   =   False



  ⊥ & y = ⊥
  x & ⊥ = False if x = False
  x & ⊥ = ⊥ otherwise
Life without ⊥   Simpler language design
Reduction
 a = true
 if a then b else c       ==>      b




 -- is there always a normal form?
 -- is it unique?
 (YES <=> Strongly “Church-Rosser”
Life without ⊥

 -- So far, so good 


 -- Not Turing complete!
 -- Non termination?
Turing complete   L  Interpreter for L?
   -- the interpreter
   eval code input = result
   -- the interpreter breaker
   evil code = 1 + eval code code

   -- by definition of eval + evil “number”
   eval 666 666 = evil 666
   -- by definition of evil
   evil 666 = 1 + (eval 666 666)
   -- 'evil 666'  0 = 1
   evil 666 = 1 + evil 666
Not Turing
     Complete
-- The rules of termination
Termination   Complete case analysis
  -- taking the first element of a list
  head a :: List a a -> a
  head Nil default           = default
  head (Cons a rest) default = a

  data NonEmptyList a = NCons a (List a)

  -- taking the first element of a
     non-empty list
  head a :: NonEmptyList a -> a
  head (NCons a rest) = a
Termination   Complete case analysis

    -- Arithmetic operators?
    1 / 0
    0 / 0
Termination   Non-covariant type recursion


     data Silly a = Very (Silly a -> a)

     bad a :: Silly a -> a
     bad (Very f) = f (Very f)

     -- infinite recursion, again…
     ouch :: a
     ouch = bad (Very bad)
Termination   Structural recursion


  factorial :: Nat -> Nat
  factorial Zero       = 0
  factorial (Suc Zero) = 1

  -- we recurse with a sub-component
     of (Suc n)
  factorial (Suc n)    = (Suc n) *
                         (factorial n)
Termination   Structural recursion

 -- Ackermann function
 ack :: Nat Nat -> Nat
 ack 0 n = n + 1

 -- m + 1 is a shortcut for (Suc m)
 ack (m + 1) 0       = ack m 1
 ack (m + 1) (n + 1) = ack m (ack (m + 1) n)


 -- every provably terminating function
 -- with first-order logic => a lot
Termination   Structural recursion

 -- Naive power function
 pow :: Nat -> Nat ->
 pow x n = 1,                   if n == 0
         = x * (pow x (n - 1)), otherwise


 -- Faster
 pow :: Nat -> Nat -> Nat
 pow x n = 1,                       if n == 0
         = x * pow (x * x) (n / 2), if odd n
         = pow (x * x) (n / 2),     otherwise
Termination    Structural recursion
 -- representation of a binary digit
 data Bit = On | Off
 -- built-in
 bits :: Nat -> List Bit

 -- primitive recursive now
 pow :: Nat -> Nat -> Nat
 pow x n = pow1 x (bits n)

 pow1   :: Nat -> List Bit -> Nat
 pow1   x n = 1
 pow1   x (Cons On r) = x * (pow1 (x * x) r)
 pow1   x (Cons Off r) = pow1 (x * x) r
Codata for
 “infinite”
computations
-- How to program an OS?
Codata             A new keyword


 -- in Haskell
 data Stream a = Cons a (Stream a)


 -- in SFP
 -- (Cocons a rest) is in normal form
 codata Colist a = Conil | a <> Colist a
Codata              A new rule
 -- functions on codata must always use a
 -- coconstructor for their result
 function a :: Colist a -> Colist a
 function a <> rest = 'xxx' <> (function ‟yyy‟)


 -- looks familiar I suppose?
 ones :: Colist Nat
 ones = 1 <> ones

 fibonacci :: Colist Nat
 fibonacci = f 0 1
             where f a b =
                       a <> (fibonacci b (a + b))
Codata        A new proof mode

 -- iterate a function:
 -- x, f x, f (f x), f (f (f x)),...
 iterate f x = x <> iterate f (f x)

 -- map a function on a colist
 comap f Conil = Conil
 comap f a <> rest = (f a) <> (comap f rest)


 -- can you prove that?
 iterate f (f x) = comap f (iterate f x)
Codata      A new proof mode
 iterate f (f x)
 -- 1. by definition of iterate
 = (f x) <> iterate f (f (f x))
                               Bisimilarity!
 -- 2. by hypothesis
 = (f x) <> comap f (iterate f (f x))

 -- 3. by definition of comap
 = comap f (x <> iterate f (f x))

 -- 4. by definition of iterate
 = comap f (iterate f x)
Codata          Limitations

 -- not primary corecursive, but ok
 evens = 2 <> (comap (+2) evens)

 -- infinite lists
 codata Colist a = a <> Colist a

 cotail a :: Colist a -> Colist a
 cotail a <> rest = rest

 -- don't do this at home
 bad = 1 <> (cotail bad)
                   Count coconstructors!
A co-era is
      opening

extract :: W a -> a
cobind :: W a -> b -> W a -> W b
Comonad     A simple example

 -- a Colist of Nats
 nats = 0 <> comap (+1) nats

 -- take the first 2 elements of a Colist
 firstTwo a :: Colist a -> (a, a)
 firstTwo a <> b <> rest = (a, b)

 -- cobind firstTwo to nats
 cobind firstTwo nats =
 (0, 1) <> (1, 2) <> (2, 3) <> …
Costate         Intuitions
 -- State
 -- “return a result based on an observable
 state”
 -- thread mutable state
 State (s -> (s, a))

 -- Costate
 -- “return a result based on the internal
 state and an external event”
 -- aka „an Object‟, „Store‟
 Costate (e, e -> a)

Mais conteúdo relacionado

Mais procurados

Joel Spencer – Finding Needles in Exponential Haystacks
Joel Spencer – Finding Needles in Exponential Haystacks Joel Spencer – Finding Needles in Exponential Haystacks
Joel Spencer – Finding Needles in Exponential Haystacks Yandex
 
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"OdessaJS Conf
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"OdessaJS Conf
 
Recursion and Functional Programming
Recursion and Functional ProgrammingRecursion and Functional Programming
Recursion and Functional Programmingsathish316
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)Sylvain Hallé
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programmingLukasz Dynowski
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Sylvain Hallé
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python Chetan Giridhar
 
Fourier series Introduction
Fourier series IntroductionFourier series Introduction
Fourier series IntroductionRizwan Kazi
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaN Masahiro
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)Sylvain Hallé
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & howlucenerevolution
 
The Ring programming language version 1.5.3 book - Part 35 of 184
The Ring programming language version 1.5.3 book - Part 35 of 184The Ring programming language version 1.5.3 book - Part 35 of 184
The Ring programming language version 1.5.3 book - Part 35 of 184Mahmoud Samir Fayed
 
Data made out of functions
Data made out of functionsData made out of functions
Data made out of functionskenbot
 
Topic: Fourier Series ( Periodic Function to change of interval)
Topic: Fourier Series ( Periodic Function to  change of interval)Topic: Fourier Series ( Periodic Function to  change of interval)
Topic: Fourier Series ( Periodic Function to change of interval)Abhishek Choksi
 

Mais procurados (20)

Joel Spencer – Finding Needles in Exponential Haystacks
Joel Spencer – Finding Needles in Exponential Haystacks Joel Spencer – Finding Needles in Exponential Haystacks
Joel Spencer – Finding Needles in Exponential Haystacks
 
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
 
Recursion and Functional Programming
Recursion and Functional ProgrammingRecursion and Functional Programming
Recursion and Functional Programming
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)
 
3rd Semester Electronic and Communication Engineering (2013-December) Questio...
3rd Semester Electronic and Communication Engineering (2013-December) Questio...3rd Semester Electronic and Communication Engineering (2013-December) Questio...
3rd Semester Electronic and Communication Engineering (2013-December) Questio...
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings
 
Diving into byte code optimization in python
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python
 
Fourier series Introduction
Fourier series IntroductionFourier series Introduction
Fourier series Introduction
 
PAIP 第1章 Lisp入門
PAIP 第1章 Lisp入門PAIP 第1章 Lisp入門
PAIP 第1章 Lisp入門
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 
Rcpp11 genentech
Rcpp11 genentechRcpp11 genentech
Rcpp11 genentech
 
Rcpp11 useR2014
Rcpp11 useR2014Rcpp11 useR2014
Rcpp11 useR2014
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & how
 
The Ring programming language version 1.5.3 book - Part 35 of 184
The Ring programming language version 1.5.3 book - Part 35 of 184The Ring programming language version 1.5.3 book - Part 35 of 184
The Ring programming language version 1.5.3 book - Part 35 of 184
 
Data made out of functions
Data made out of functionsData made out of functions
Data made out of functions
 
Topic: Fourier Series ( Periodic Function to change of interval)
Topic: Fourier Series ( Periodic Function to  change of interval)Topic: Fourier Series ( Periodic Function to  change of interval)
Topic: Fourier Series ( Periodic Function to change of interval)
 

Semelhante a Strong functional programming

Monadologie
MonadologieMonadologie
Monadologieleague
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture gVlad Patryshev
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonshin
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Bryan O'Sullivan
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Theories of continuous optimization
Theories of continuous optimizationTheories of continuous optimization
Theories of continuous optimizationOlivier Teytaud
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentalsMauro Palsgraaf
 
Organizing Numerical Theories using Axiomatic Type Classes
Organizing Numerical Theories using Axiomatic Type ClassesOrganizing Numerical Theories using Axiomatic Type Classes
Organizing Numerical Theories using Axiomatic Type ClassesLawrence Paulson
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monadsrkaippully
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
2.6 more computations of derivatives
2.6 more computations of derivatives2.6 more computations of derivatives
2.6 more computations of derivativesmath265
 
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...Alex Pruden
 
하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3Kwang Yul Seo
 
Algebra cheat sheet
Algebra cheat sheetAlgebra cheat sheet
Algebra cheat sheetalex9803
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
Otter 2016-11-28-01-ss
Otter 2016-11-28-01-ssOtter 2016-11-28-01-ss
Otter 2016-11-28-01-ssRuo Ando
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 

Semelhante a Strong functional programming (20)

Monadologie
MonadologieMonadologie
Monadologie
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
0708 ch 7 day 8
0708 ch 7 day 80708 ch 7 day 8
0708 ch 7 day 8
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Theories of continuous optimization
Theories of continuous optimizationTheories of continuous optimization
Theories of continuous optimization
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentals
 
Organizing Numerical Theories using Axiomatic Type Classes
Organizing Numerical Theories using Axiomatic Type ClassesOrganizing Numerical Theories using Axiomatic Type Classes
Organizing Numerical Theories using Axiomatic Type Classes
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Hands on lua
Hands on luaHands on lua
Hands on lua
 
Dynamic programing
Dynamic programingDynamic programing
Dynamic programing
 
2.6 more computations of derivatives
2.6 more computations of derivatives2.6 more computations of derivatives
2.6 more computations of derivatives
 
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
 
하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3
 
Algebra cheat sheet
Algebra cheat sheetAlgebra cheat sheet
Algebra cheat sheet
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Otter 2016-11-28-01-ss
Otter 2016-11-28-01-ssOtter 2016-11-28-01-ss
Otter 2016-11-28-01-ss
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 

Mais de Eric Torreborre

wire-all-the-things-lambda-days-2023.pdf
wire-all-the-things-lambda-days-2023.pdfwire-all-the-things-lambda-days-2023.pdf
wire-all-the-things-lambda-days-2023.pdfEric Torreborre
 
The many faces of modularity
The many faces of modularityThe many faces of modularity
The many faces of modularityEric Torreborre
 
What haskell taught us when we were not looking
What haskell taught us when we were not lookingWhat haskell taught us when we were not looking
What haskell taught us when we were not lookingEric Torreborre
 
Wire once, rewire twice! (Haskell exchange-2018)
Wire once, rewire twice! (Haskell exchange-2018)Wire once, rewire twice! (Haskell exchange-2018)
Wire once, rewire twice! (Haskell exchange-2018)Eric Torreborre
 
Pratical eff-functional-conf
Pratical eff-functional-confPratical eff-functional-conf
Pratical eff-functional-confEric Torreborre
 
Specs2, from starters to dessert and... a look in the kitchen
Specs2, from starters to dessert and... a look in the kitchenSpecs2, from starters to dessert and... a look in the kitchen
Specs2, from starters to dessert and... a look in the kitchenEric Torreborre
 
Streaming, effects and beautiful folds: a winning trilogy
Streaming, effects and beautiful folds: a winning trilogyStreaming, effects and beautiful folds: a winning trilogy
Streaming, effects and beautiful folds: a winning trilogyEric Torreborre
 
Pratical eff-scalaitaly-2017
Pratical eff-scalaitaly-2017Pratical eff-scalaitaly-2017
Pratical eff-scalaitaly-2017Eric Torreborre
 
Pratical eff monad at Scaladays Chicago
Pratical eff monad at Scaladays ChicagoPratical eff monad at Scaladays Chicago
Pratical eff monad at Scaladays ChicagoEric Torreborre
 
The Eff monad, one monad to rule them all
The Eff monad, one monad to rule them allThe Eff monad, one monad to rule them all
The Eff monad, one monad to rule them allEric Torreborre
 
Data generation, the hard parts
Data generation, the hard partsData generation, the hard parts
Data generation, the hard partsEric Torreborre
 
Origami, a monadic fold library for Scala
Origami, a monadic fold library for ScalaOrigami, a monadic fold library for Scala
Origami, a monadic fold library for ScalaEric Torreborre
 
Datatypes for the real world
Datatypes for the real worldDatatypes for the real world
Datatypes for the real worldEric Torreborre
 
Specs2 whirlwind tour at Scaladays 2014
Specs2 whirlwind tour at Scaladays 2014Specs2 whirlwind tour at Scaladays 2014
Specs2 whirlwind tour at Scaladays 2014Eric Torreborre
 
Epic success \/ failure, refactoring to *real* FP
Epic success \/ failure, refactoring to *real* FPEpic success \/ failure, refactoring to *real* FP
Epic success \/ failure, refactoring to *real* FPEric Torreborre
 

Mais de Eric Torreborre (20)

wire-all-the-things-lambda-days-2023.pdf
wire-all-the-things-lambda-days-2023.pdfwire-all-the-things-lambda-days-2023.pdf
wire-all-the-things-lambda-days-2023.pdf
 
The many faces of modularity
The many faces of modularityThe many faces of modularity
The many faces of modularity
 
What haskell taught us when we were not looking
What haskell taught us when we were not lookingWhat haskell taught us when we were not looking
What haskell taught us when we were not looking
 
Wire once, rewire twice! (Haskell exchange-2018)
Wire once, rewire twice! (Haskell exchange-2018)Wire once, rewire twice! (Haskell exchange-2018)
Wire once, rewire twice! (Haskell exchange-2018)
 
Pratical eff-functional-conf
Pratical eff-functional-confPratical eff-functional-conf
Pratical eff-functional-conf
 
Specs2, from starters to dessert and... a look in the kitchen
Specs2, from starters to dessert and... a look in the kitchenSpecs2, from starters to dessert and... a look in the kitchen
Specs2, from starters to dessert and... a look in the kitchen
 
Streaming, effects and beautiful folds: a winning trilogy
Streaming, effects and beautiful folds: a winning trilogyStreaming, effects and beautiful folds: a winning trilogy
Streaming, effects and beautiful folds: a winning trilogy
 
Pratical eff-scalaitaly-2017
Pratical eff-scalaitaly-2017Pratical eff-scalaitaly-2017
Pratical eff-scalaitaly-2017
 
Pratical eff monad at Scaladays Chicago
Pratical eff monad at Scaladays ChicagoPratical eff monad at Scaladays Chicago
Pratical eff monad at Scaladays Chicago
 
Pratical eff
Pratical effPratical eff
Pratical eff
 
The Eff monad, one monad to rule them all
The Eff monad, one monad to rule them allThe Eff monad, one monad to rule them all
The Eff monad, one monad to rule them all
 
Easy di-slideshare
Easy di-slideshareEasy di-slideshare
Easy di-slideshare
 
Data generation, the hard parts
Data generation, the hard partsData generation, the hard parts
Data generation, the hard parts
 
Origami, a monadic fold library for Scala
Origami, a monadic fold library for ScalaOrigami, a monadic fold library for Scala
Origami, a monadic fold library for Scala
 
Datatypes for the real world
Datatypes for the real worldDatatypes for the real world
Datatypes for the real world
 
Specs2 3.4
Specs2 3.4Specs2 3.4
Specs2 3.4
 
DSLs with fold algebras
DSLs with fold algebrasDSLs with fold algebras
DSLs with fold algebras
 
Specs2 whirlwind tour at Scaladays 2014
Specs2 whirlwind tour at Scaladays 2014Specs2 whirlwind tour at Scaladays 2014
Specs2 whirlwind tour at Scaladays 2014
 
Epic success \/ failure, refactoring to *real* FP
Epic success \/ failure, refactoring to *real* FPEpic success \/ failure, refactoring to *real* FP
Epic success \/ failure, refactoring to *real* FP
 
Vampire methods
Vampire methodsVampire methods
Vampire methods
 

Último

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Último (20)

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Strong functional programming

  • 1.
  • 2. Without ⊥ Turing Complete Codata Comonad
  • 3. Life without ⊥ loop :: Int -> Int loop n = 1 + loop n loop 0 = 1 + loop 0 0 = 1 Int(⊥)
  • 4. Life without ⊥ Simpler language design Strict vs lazy -- a function returning the first argument first a b = a -- with strict evaluation first 1 ⊥ = ⊥ -- with lazy evaluation first 1 ⊥ = 1
  • 5. Life without ⊥ Simpler language design Pattern matching -- will not match if (a, b) is ⊥ first (a, b) = a -- a bottom value can be "lifted” to a pair of bottom values (⊥a, ⊥b) = ⊥
  • 6. Life without ⊥ Simpler language design & Operator True & True = True ⊥ & y = ? True & False = False x & ⊥ = ? False & True = False False & False = False ⊥ & y = ⊥ x & ⊥ = False if x = False x & ⊥ = ⊥ otherwise
  • 7. Life without ⊥ Simpler language design Reduction a = true if a then b else c ==> b -- is there always a normal form? -- is it unique? (YES <=> Strongly “Church-Rosser”
  • 8. Life without ⊥ -- So far, so good  -- Not Turing complete! -- Non termination?
  • 9. Turing complete L  Interpreter for L? -- the interpreter eval code input = result -- the interpreter breaker evil code = 1 + eval code code -- by definition of eval + evil “number” eval 666 666 = evil 666 -- by definition of evil evil 666 = 1 + (eval 666 666) -- 'evil 666'  0 = 1 evil 666 = 1 + evil 666
  • 10. Not Turing Complete -- The rules of termination
  • 11. Termination Complete case analysis -- taking the first element of a list head a :: List a a -> a head Nil default = default head (Cons a rest) default = a data NonEmptyList a = NCons a (List a) -- taking the first element of a non-empty list head a :: NonEmptyList a -> a head (NCons a rest) = a
  • 12. Termination Complete case analysis -- Arithmetic operators? 1 / 0 0 / 0
  • 13. Termination Non-covariant type recursion data Silly a = Very (Silly a -> a) bad a :: Silly a -> a bad (Very f) = f (Very f) -- infinite recursion, again… ouch :: a ouch = bad (Very bad)
  • 14. Termination Structural recursion factorial :: Nat -> Nat factorial Zero = 0 factorial (Suc Zero) = 1 -- we recurse with a sub-component of (Suc n) factorial (Suc n) = (Suc n) * (factorial n)
  • 15. Termination Structural recursion -- Ackermann function ack :: Nat Nat -> Nat ack 0 n = n + 1 -- m + 1 is a shortcut for (Suc m) ack (m + 1) 0 = ack m 1 ack (m + 1) (n + 1) = ack m (ack (m + 1) n) -- every provably terminating function -- with first-order logic => a lot
  • 16. Termination Structural recursion -- Naive power function pow :: Nat -> Nat -> pow x n = 1, if n == 0 = x * (pow x (n - 1)), otherwise -- Faster pow :: Nat -> Nat -> Nat pow x n = 1, if n == 0 = x * pow (x * x) (n / 2), if odd n = pow (x * x) (n / 2), otherwise
  • 17. Termination Structural recursion -- representation of a binary digit data Bit = On | Off -- built-in bits :: Nat -> List Bit -- primitive recursive now pow :: Nat -> Nat -> Nat pow x n = pow1 x (bits n) pow1 :: Nat -> List Bit -> Nat pow1 x n = 1 pow1 x (Cons On r) = x * (pow1 (x * x) r) pow1 x (Cons Off r) = pow1 (x * x) r
  • 19. Codata A new keyword -- in Haskell data Stream a = Cons a (Stream a) -- in SFP -- (Cocons a rest) is in normal form codata Colist a = Conil | a <> Colist a
  • 20. Codata A new rule -- functions on codata must always use a -- coconstructor for their result function a :: Colist a -> Colist a function a <> rest = 'xxx' <> (function ‟yyy‟) -- looks familiar I suppose? ones :: Colist Nat ones = 1 <> ones fibonacci :: Colist Nat fibonacci = f 0 1 where f a b = a <> (fibonacci b (a + b))
  • 21. Codata A new proof mode -- iterate a function: -- x, f x, f (f x), f (f (f x)),... iterate f x = x <> iterate f (f x) -- map a function on a colist comap f Conil = Conil comap f a <> rest = (f a) <> (comap f rest) -- can you prove that? iterate f (f x) = comap f (iterate f x)
  • 22. Codata A new proof mode iterate f (f x) -- 1. by definition of iterate = (f x) <> iterate f (f (f x)) Bisimilarity! -- 2. by hypothesis = (f x) <> comap f (iterate f (f x)) -- 3. by definition of comap = comap f (x <> iterate f (f x)) -- 4. by definition of iterate = comap f (iterate f x)
  • 23. Codata Limitations -- not primary corecursive, but ok evens = 2 <> (comap (+2) evens) -- infinite lists codata Colist a = a <> Colist a cotail a :: Colist a -> Colist a cotail a <> rest = rest -- don't do this at home bad = 1 <> (cotail bad) Count coconstructors!
  • 24. A co-era is opening extract :: W a -> a cobind :: W a -> b -> W a -> W b
  • 25. Comonad A simple example -- a Colist of Nats nats = 0 <> comap (+1) nats -- take the first 2 elements of a Colist firstTwo a :: Colist a -> (a, a) firstTwo a <> b <> rest = (a, b) -- cobind firstTwo to nats cobind firstTwo nats = (0, 1) <> (1, 2) <> (2, 3) <> …
  • 26. Costate Intuitions -- State -- “return a result based on an observable state” -- thread mutable state State (s -> (s, a)) -- Costate -- “return a result based on the internal state and an external event” -- aka „an Object‟, „Store‟ Costate (e, e -> a)