SlideShare a Scribd company logo
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

PPTX
PDF
PDF
用十分鐘看懂 《近代科學發展史》
PPTX
純粋関数型アルゴリズム入門
PDF
Mat 257
PDF
部内勉強会 数え上げの基礎
PPTX
Bisection method
PDF
二分探索法で作る再帰呼び出しできるCプリプロセッサマクロ
用十分鐘看懂 《近代科學發展史》
純粋関数型アルゴリズム入門
Mat 257
部内勉強会 数え上げの基礎
Bisection method
二分探索法で作る再帰呼び出しできるCプリプロセッサマクロ

What's hot (14)

PDF
Deep drive into rust programming language
PDF
Nonsmooth Optimization
PDF
自動定理証明の紹介
PDF
用十分鐘學會 《微積分、工程數學》及其應用
PDF
solucionario de purcell 3
PPT
台大電機中的數學
PDF
用十分鐘理解 《微分方程》
PDF
Railway Oriented Programming
PPTX
Presentaton on Polynomials....Class 10
PDF
張量與相對論
PDF
RationalExpressionsReview.pdf
DOC
Three dimensional geometry
PPT
L15 the differentials &amp; parametric equations
PPT
Introduction to differential equation
Deep drive into rust programming language
Nonsmooth Optimization
自動定理証明の紹介
用十分鐘學會 《微積分、工程數學》及其應用
solucionario de purcell 3
台大電機中的數學
用十分鐘理解 《微分方程》
Railway Oriented Programming
Presentaton on Polynomials....Class 10
張量與相對論
RationalExpressionsReview.pdf
Three dimensional geometry
L15 the differentials &amp; parametric equations
Introduction to differential equation
Ad

Similar to Revisiting Combinators (20)

PDF
Optimization introduction
PDF
Comonads in Haskell
PPT
Convergence Criteria
PDF
Relaxation methods for the matrix exponential on large networks
PDF
Monadologie
PPTX
Strong functional programming
PDF
Matrix calculus
PPTX
Good functional programming is good programming
PPT
Planning Under Uncertainty With Markov Decision Processes
PDF
Microsoft Word Practice Exercise Set 2
PDF
FirstAndSecond.pptx.pdf
PPT
13-hashing.ppt computer networks introduction
PDF
Real World Haskell: Lecture 5
PDF
TI1220 Lecture 6: First-class Functions
PDF
Introduction to complex networks
PPT
PARSING.ppt
PDF
chapter5_marked_optimization_with_SGD.pdf
PPT
lecture07 dicrete mathematics relation .ppt
PDF
ML+Hadoop at NYC Predictive Analytics
PDF
Applied Machine Learning For Search Engine Relevance
Optimization introduction
Comonads in Haskell
Convergence Criteria
Relaxation methods for the matrix exponential on large networks
Monadologie
Strong functional programming
Matrix calculus
Good functional programming is good programming
Planning Under Uncertainty With Markov Decision Processes
Microsoft Word Practice Exercise Set 2
FirstAndSecond.pptx.pdf
13-hashing.ppt computer networks introduction
Real World Haskell: Lecture 5
TI1220 Lecture 6: First-class Functions
Introduction to complex networks
PARSING.ppt
chapter5_marked_optimization_with_SGD.pdf
lecture07 dicrete mathematics relation .ppt
ML+Hadoop at NYC Predictive Analytics
Applied Machine Learning For Search Engine Relevance
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Programs and apps: productivity, graphics, security and other tools
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Approach and Philosophy of On baking technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Mobile App Security Testing_ A Comprehensive Guide.pdf
sap open course for s4hana steps from ECC to s4
Diabetes mellitus diagnosis method based random forest with bat algorithm
Big Data Technologies - Introduction.pptx

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?