SlideShare a Scribd company logo
1 of 22
Download to read offline
Revisiting
Combinators
Edward Kmett
data LC a
= Var a
| App (LC a) (LC a)
| Lam (LC (Maybe a))
deriving (Functor, Foldable, Traversable)
instance Applicative LC where pure = Var; (<*>) = ap
instance Monad LC where
Var a >>= f = f a
App l r >>= f = App (l >>= f) (r >>= f)
Lam k >>= f = Lam $ k >>= case
Nothing -> pure Nothing
Just a -> Just <$> f a
lam :: Eq a => a -> LC a -> LC a
lam v b = Lam $ bind v <$> b where
bind v u = u <$ guard (u /= v)
A Toy Lambda Calculus
A combinator is a function that has no free
variables.
What is a Combinator?
• Moses Shönfinkel (Моисей Исаевич Шейнфинкель), “Über die Bausteine der
mathematischen Logik” 1924
• Haskell Curry, “Grundlagen der Kombinatorischen Logik” 1930
• David Turner, “Another Algorithm for Bracket Abstraction” 1979
• Smullyan “To Mock a Mockingbird” 1985
• Sabine Broda and Lúis Damas “Bracket abstraction in the combinator system Cl(K)”
1987(?)
Introducing Combinators
data CL a = V a | A (CL a) (CL a) | S | K | I | B | C
deriving (Functor, Foldable, Traversable)
instance Applicative CL where pure = V; (<*>) = ap
instance Monad CL where
V a >>= f = f a
A l r >>= f = A (l >>= f) (r >>= f)
S >>= _ = S
K >>= _ = K
I >>= _ = I
B >>= _ = B
C >>= _ = C
A Toy Combinatory Logic
S x y z = (x z) (y z) -- (<*>)
K x y = x -- const
I x = x -- id
B x y z = x (y z) -- (.)
C x y z = x z y -- flip
Y f = f (Y f) = let x = f x in x -- fix
A Field Guide
infixl 1 %
(%) = A
compile :: LC a -> CL a
compile (Var a) = V a
compile (App l r) = compile l % compile r
compile (Lam b) = compileLam b
compileLam :: LC (Maybe a) -> CL a
compileLam (Var Nothing)) = I
compileLam (Var (Just y)) = K % V y
compileLam (Ap l r) = case (sequence l, sequence r) of
(Nothing, Nothing) -> S % compileLam l % compileLam r
(Nothing, Just r') -> C % compileLam l % compile r
(Just l', Nothing) -> B % compile l' % compileLam r
(Just l', Just r') -> K % (compile l' % compile r')
compileLam (Lam b) = join $ compileLam $ dist $ compileLam b where
dist :: C (Maybe a) -> L (Maybe (C a))
dist (A l r) = App (dist l) (dist r)
dist xs = Var (sequence xs)
Abstraction Elimination
Play with this at http://pointfree.io/ or with @pl on lambdabot
a la Shönfinkel
• John Hughes, “Super Combinators: A New Implementation Method for Applicative
Languages” 1982
• Lennart Augustsson, “A compiler for Lazy ML” 1984
• Simon Peyton Jones “Implementing lazy functional languages on stock hardware: the
Spineless Tagless G-machine” 1992
• Simon Marlow, Alexey Rodriguez Yakushev and Simon Peyton Jones “Faster laziness
using dynamic pointer tagging” 2007
Supercombinators
jmp %eax
Unknown Thunk Evaluation
Every Thunk in GHC is a SPECTRE v2 Vulnerability
• Small evaluator that immediately recovers
coherence between instructions.
• Scales with the product of the SIMD-width and # of
cores, rather than one or the other
• This bypasses the spectre issues.
• This generalizes to GPU compute shading
SPMD-on-SIMD Evaluation
Why are combinator terms
bigger?
• S x y z = (x z) (y z) — application with environment
• K x y = x — ignores environment
• I x — uses environment
• These all work “one variable at a time”
We need to be able to build and manipulate ‘partial’ environments in sublinear time.
Some references:
• Abadi, Cardelli, Curien, Lévy “Explicit Substitutions” 1990
• Lescanne, “From λσ to λν a journey through calculi of explicit substitutions” 1994
• Mazzoli, “A Well-Typed Suspension Calculus” 2017
Sketching an “improved”
abstraction elimination algorithm
fst (a,b) ~> a
snd (a,b) ~> b
Evaluation by Collection
• John Hughes, “The design and implementation of programming languages” 1983
• Philip Wadler, “Fixing some space leaks with a garbage collector” 1987
• Christina von Dorrien, “Stingy Evaluation” 1989
Some extra stingy combinator evaluation rules:
A (A K x) _ -> Just x -- def K
A I x -> Just x -- def I
A S K -> Just K_ -- Hudak 1
A S K_ -> Just I -- Hudak 2
A S k@(A K (A K _)) -> Just k -- Hudak 3
A (A S (A K x)) I -> Just x -- Hudak 4, Turner p35 2
A Y k@(A K _) -> Just k -- Hudak 9
(Be careful, not all of Hudak’s rules are stingy!)
• Paul Hudak and David Kranz, “A Combinator-based Compiler for a Functional
Language” 1984
One Bit Reference Counting
S
x
y
z
x yz z
(<*>)
W.R. Stoye, T.J.W. Clarke and A.C. Norman “Some Practical Methods for Rapid Combinator Reduction”
1984
One Bit Reference Counting
C
x
y
z
x
y
z
flip
One Bit Reference Counting
B
x
y
z
x
y z
(.)
K
x
y x
const
Extra reductions that require “uniqueness” to be stingy:
A (A (A C f) x) y -> Just $ A (A f y) x
A (A (A B f) g) x -> Just $ A f (A g x)
A (A S (A K x)) (A K y) -> Just $ A K (A x y) -- Hudak 5, Turner p35 1
A (A S (A K x)) y -> Just $ A (A B x) y -- Turner p35 3
A (A S x) (A K y) -> Just $ A (A C x) y -- Turner p35 4
fst p = p (xy.x)
pair x y z = z x y
snd p = p (xy.y)
fst = CIK
snd = CI(KI)
pair = BC(CI)
Can we reduce fst (x, y) ~> x by stingy evaluation
without special casing Wadler’s rules?
• None of the S,B,C,K,I… rules introduce new cycles
on the heap except Y.
• Hash-consing!
• Eiichi Goto “Monocopy and associative
algorithms in extended Lisp” 1974
• Eelco Dolstra, “Maximal Laziness” 2008
Optionally Acyclic Heaps
• Compilers for dependently typed languages are slow. Hash consign is usually bolted in as
an afterthought. I’d like an efficient default evaluator that automatically memoizes and
addresses unification and substitution concerns.
• Daniel Dougherty “Higher-order unification via combinators” 1993
Why I Care?

More Related Content

What's hot

The java interview questions ebook - confused coders
The java interview questions ebook -  confused codersThe java interview questions ebook -  confused coders
The java interview questions ebook - confused codersYash Sharma
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...Philip Schwarz
 
MongoDB at Scale
MongoDB at ScaleMongoDB at Scale
MongoDB at ScaleMongoDB
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaKnoldus Inc.
 
Percona XtraDB Cluster ( Ensure high Availability )
Percona XtraDB Cluster ( Ensure high Availability )Percona XtraDB Cluster ( Ensure high Availability )
Percona XtraDB Cluster ( Ensure high Availability )Mydbops
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksDoug Hawkins
 
vert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVMvert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVMjbandi
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsDebasish Ghosh
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayDebasish Ghosh
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Moriyoshi Koizumi
 
Why Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your ShellWhy Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your Shelljaguardesignstudio
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadOliver Daff
 

What's hot (20)

The java interview questions ebook - confused coders
The java interview questions ebook -  confused codersThe java interview questions ebook -  confused coders
The java interview questions ebook - confused coders
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
 
MongoDB at Scale
MongoDB at ScaleMongoDB at Scale
MongoDB at Scale
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
 
Percona XtraDB Cluster ( Ensure high Availability )
Percona XtraDB Cluster ( Ensure high Availability )Percona XtraDB Cluster ( Ensure high Availability )
Percona XtraDB Cluster ( Ensure high Availability )
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
 
vert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVMvert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVM
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Rust
RustRust
Rust
 
Algebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain ModelsAlgebraic Thinking for Evolution of Pure Functional Domain Models
Algebraic Thinking for Evolution of Pure Functional Domain Models
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法
 
Applicative style programming
Applicative style programmingApplicative style programming
Applicative style programming
 
Why Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your ShellWhy Zsh is Cooler than Your Shell
Why Zsh is Cooler than Your Shell
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And Monad
 
Troubleshooting Java HotSpot VM
Troubleshooting Java HotSpot VMTroubleshooting Java HotSpot VM
Troubleshooting Java HotSpot VM
 
Introduction to Data Oriented Design
Introduction to Data Oriented DesignIntroduction to Data Oriented Design
Introduction to Data Oriented Design
 
GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
 

Similar to Revisiting Combinators

Optimization introduction
Optimization introductionOptimization introduction
Optimization introductionhelalmohammad2
 
Convergence Criteria
Convergence CriteriaConvergence Criteria
Convergence CriteriaTarun Gehlot
 
Relaxation methods for the matrix exponential on large networks
Relaxation methods for the matrix exponential on large networksRelaxation methods for the matrix exponential on large networks
Relaxation methods for the matrix exponential on large networksDavid Gleich
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Strong functional programming
Strong functional programmingStrong functional programming
Strong functional programmingEric Torreborre
 
Good functional programming is good programming
Good functional programming is good programmingGood functional programming is good programming
Good functional programming is good programmingkenbot
 
Planning Under Uncertainty With Markov Decision Processes
Planning Under Uncertainty With Markov Decision ProcessesPlanning Under Uncertainty With Markov Decision Processes
Planning Under Uncertainty With Markov Decision Processesahmad bassiouny
 
Microsoft Word Practice Exercise Set 2
Microsoft Word   Practice Exercise Set 2Microsoft Word   Practice Exercise Set 2
Microsoft Word Practice Exercise Set 2rampan
 
FirstAndSecond.pptx.pdf
FirstAndSecond.pptx.pdfFirstAndSecond.pptx.pdf
FirstAndSecond.pptx.pdfShanmukhSaiR
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Bryan O'Sullivan
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 
Introduction to complex networks
Introduction to complex networksIntroduction to complex networks
Introduction to complex networksVincent Traag
 
ML+Hadoop at NYC Predictive Analytics
ML+Hadoop at NYC Predictive AnalyticsML+Hadoop at NYC Predictive Analytics
ML+Hadoop at NYC Predictive AnalyticsErik Bernhardsson
 
Applied Machine Learning For Search Engine Relevance
Applied Machine Learning For Search Engine Relevance Applied Machine Learning For Search Engine Relevance
Applied Machine Learning For Search Engine Relevance charlesmartin14
 
lecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdflecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdfAnaNeacsu5
 
Laplace1 8merged
Laplace1 8mergedLaplace1 8merged
Laplace1 8mergedcohtran
 

Similar to Revisiting Combinators (20)

Optimization introduction
Optimization introductionOptimization introduction
Optimization introduction
 
Comonads in Haskell
Comonads in HaskellComonads in Haskell
Comonads in Haskell
 
Convergence Criteria
Convergence CriteriaConvergence Criteria
Convergence Criteria
 
Relaxation methods for the matrix exponential on large networks
Relaxation methods for the matrix exponential on large networksRelaxation methods for the matrix exponential on large networks
Relaxation methods for the matrix exponential on large networks
 
Monadologie
MonadologieMonadologie
Monadologie
 
Strong functional programming
Strong functional programmingStrong functional programming
Strong functional programming
 
Matrix calculus
Matrix calculusMatrix calculus
Matrix calculus
 
Good functional programming is good programming
Good functional programming is good programmingGood functional programming is good programming
Good functional programming is good programming
 
Planning Under Uncertainty With Markov Decision Processes
Planning Under Uncertainty With Markov Decision ProcessesPlanning Under Uncertainty With Markov Decision Processes
Planning Under Uncertainty With Markov Decision Processes
 
Microsoft Word Practice Exercise Set 2
Microsoft Word   Practice Exercise Set 2Microsoft Word   Practice Exercise Set 2
Microsoft Word Practice Exercise Set 2
 
FirstAndSecond.pptx.pdf
FirstAndSecond.pptx.pdfFirstAndSecond.pptx.pdf
FirstAndSecond.pptx.pdf
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Introduction to complex networks
Introduction to complex networksIntroduction to complex networks
Introduction to complex networks
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
ML+Hadoop at NYC Predictive Analytics
ML+Hadoop at NYC Predictive AnalyticsML+Hadoop at NYC Predictive Analytics
ML+Hadoop at NYC Predictive Analytics
 
Applied Machine Learning For Search Engine Relevance
Applied Machine Learning For Search Engine Relevance Applied Machine Learning For Search Engine Relevance
Applied Machine Learning For Search Engine Relevance
 
lecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdflecture01_lecture01_lecture0001_ceva.pdf
lecture01_lecture01_lecture0001_ceva.pdf
 
QMC: Operator Splitting Workshop, A New (More Intuitive?) Interpretation of I...
QMC: Operator Splitting Workshop, A New (More Intuitive?) Interpretation of I...QMC: Operator Splitting Workshop, A New (More Intuitive?) Interpretation of I...
QMC: Operator Splitting Workshop, A New (More Intuitive?) Interpretation of I...
 
Laplace1 8merged
Laplace1 8mergedLaplace1 8merged
Laplace1 8merged
 

Recently uploaded

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
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
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Recently uploaded (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
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
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

Revisiting Combinators

  • 2. data LC a = Var a | App (LC a) (LC a) | Lam (LC (Maybe a)) deriving (Functor, Foldable, Traversable) instance Applicative LC where pure = Var; (<*>) = ap instance Monad LC where Var a >>= f = f a App l r >>= f = App (l >>= f) (r >>= f) Lam k >>= f = Lam $ k >>= case Nothing -> pure Nothing Just a -> Just <$> f a lam :: Eq a => a -> LC a -> LC a lam v b = Lam $ bind v <$> b where bind v u = u <$ guard (u /= v) A Toy Lambda Calculus
  • 3. A combinator is a function that has no free variables. What is a Combinator?
  • 4. • Moses Shönfinkel (Моисей Исаевич Шейнфинкель), “Über die Bausteine der mathematischen Logik” 1924 • Haskell Curry, “Grundlagen der Kombinatorischen Logik” 1930 • David Turner, “Another Algorithm for Bracket Abstraction” 1979 • Smullyan “To Mock a Mockingbird” 1985 • Sabine Broda and Lúis Damas “Bracket abstraction in the combinator system Cl(K)” 1987(?) Introducing Combinators
  • 5. data CL a = V a | A (CL a) (CL a) | S | K | I | B | C deriving (Functor, Foldable, Traversable) instance Applicative CL where pure = V; (<*>) = ap instance Monad CL where V a >>= f = f a A l r >>= f = A (l >>= f) (r >>= f) S >>= _ = S K >>= _ = K I >>= _ = I B >>= _ = B C >>= _ = C A Toy Combinatory Logic
  • 6. S x y z = (x z) (y z) -- (<*>) K x y = x -- const I x = x -- id B x y z = x (y z) -- (.) C x y z = x z y -- flip Y f = f (Y f) = let x = f x in x -- fix A Field Guide
  • 7. infixl 1 % (%) = A compile :: LC a -> CL a compile (Var a) = V a compile (App l r) = compile l % compile r compile (Lam b) = compileLam b compileLam :: LC (Maybe a) -> CL a compileLam (Var Nothing)) = I compileLam (Var (Just y)) = K % V y compileLam (Ap l r) = case (sequence l, sequence r) of (Nothing, Nothing) -> S % compileLam l % compileLam r (Nothing, Just r') -> C % compileLam l % compile r (Just l', Nothing) -> B % compile l' % compileLam r (Just l', Just r') -> K % (compile l' % compile r') compileLam (Lam b) = join $ compileLam $ dist $ compileLam b where dist :: C (Maybe a) -> L (Maybe (C a)) dist (A l r) = App (dist l) (dist r) dist xs = Var (sequence xs) Abstraction Elimination Play with this at http://pointfree.io/ or with @pl on lambdabot a la Shönfinkel
  • 8. • John Hughes, “Super Combinators: A New Implementation Method for Applicative Languages” 1982 • Lennart Augustsson, “A compiler for Lazy ML” 1984 • Simon Peyton Jones “Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine” 1992 • Simon Marlow, Alexey Rodriguez Yakushev and Simon Peyton Jones “Faster laziness using dynamic pointer tagging” 2007 Supercombinators
  • 9. jmp %eax Unknown Thunk Evaluation Every Thunk in GHC is a SPECTRE v2 Vulnerability
  • 10. • Small evaluator that immediately recovers coherence between instructions. • Scales with the product of the SIMD-width and # of cores, rather than one or the other • This bypasses the spectre issues. • This generalizes to GPU compute shading SPMD-on-SIMD Evaluation
  • 11. Why are combinator terms bigger? • S x y z = (x z) (y z) — application with environment • K x y = x — ignores environment • I x — uses environment • These all work “one variable at a time”
  • 12. We need to be able to build and manipulate ‘partial’ environments in sublinear time. Some references: • Abadi, Cardelli, Curien, Lévy “Explicit Substitutions” 1990 • Lescanne, “From λσ to λν a journey through calculi of explicit substitutions” 1994 • Mazzoli, “A Well-Typed Suspension Calculus” 2017 Sketching an “improved” abstraction elimination algorithm
  • 13. fst (a,b) ~> a snd (a,b) ~> b Evaluation by Collection • John Hughes, “The design and implementation of programming languages” 1983 • Philip Wadler, “Fixing some space leaks with a garbage collector” 1987 • Christina von Dorrien, “Stingy Evaluation” 1989
  • 14. Some extra stingy combinator evaluation rules: A (A K x) _ -> Just x -- def K A I x -> Just x -- def I A S K -> Just K_ -- Hudak 1 A S K_ -> Just I -- Hudak 2 A S k@(A K (A K _)) -> Just k -- Hudak 3 A (A S (A K x)) I -> Just x -- Hudak 4, Turner p35 2 A Y k@(A K _) -> Just k -- Hudak 9 (Be careful, not all of Hudak’s rules are stingy!) • Paul Hudak and David Kranz, “A Combinator-based Compiler for a Functional Language” 1984
  • 15. One Bit Reference Counting S x y z x yz z (<*>) W.R. Stoye, T.J.W. Clarke and A.C. Norman “Some Practical Methods for Rapid Combinator Reduction” 1984
  • 16. One Bit Reference Counting C x y z x y z flip
  • 17. One Bit Reference Counting B x y z x y z (.)
  • 19. Extra reductions that require “uniqueness” to be stingy: A (A (A C f) x) y -> Just $ A (A f y) x A (A (A B f) g) x -> Just $ A f (A g x) A (A S (A K x)) (A K y) -> Just $ A K (A x y) -- Hudak 5, Turner p35 1 A (A S (A K x)) y -> Just $ A (A B x) y -- Turner p35 3 A (A S x) (A K y) -> Just $ A (A C x) y -- Turner p35 4
  • 20. fst p = p (xy.x) pair x y z = z x y snd p = p (xy.y) fst = CIK snd = CI(KI) pair = BC(CI) Can we reduce fst (x, y) ~> x by stingy evaluation without special casing Wadler’s rules?
  • 21. • None of the S,B,C,K,I… rules introduce new cycles on the heap except Y. • Hash-consing! • Eiichi Goto “Monocopy and associative algorithms in extended Lisp” 1974 • Eelco Dolstra, “Maximal Laziness” 2008 Optionally Acyclic Heaps
  • 22. • Compilers for dependently typed languages are slow. Hash consign is usually bolted in as an afterthought. I’d like an efficient default evaluator that automatically memoizes and addresses unification and substitution concerns. • Daniel Dougherty “Higher-order unification via combinators” 1993 Why I Care?