SlideShare a Scribd company logo
clojure.spec:
a lisp-flavoured

type system
@sbelak
simon@goopti.com
Clojure at a glance
• (lisp (running-on :JVM))
• Functional, dynamic, immutable
• Excellent concurrency and state management
support
• Unparalleled data manipulation
Spec: a lisp-flavoured type system
Motivation
• Communication (docs are not enough)
• A lot of computation in Clojure is encoded with
(naked) data
• Generative (property based) testing
• Manual parsing and error reporting is tedious and
error prone
Enter clojure.spec
Writing a spec should enable automatic:
• Validation
• Error reporting
• Destructuring
• Instrumentation
• Test-data generation
• Generative test generation
*http://clojure.org/about/spec
Parsing with Derivatives
A Functional Pearl
Matthew Might David Darais
University of Utah
might@cs.utah.edu, david.darais@gmail.com
Daniel Spiewak
University of Wisconsin, Milwaukee
dspiewak@uwm.edu
Abstract
We present a functional approach to parsing unrestricted context-
free grammars based on Brzozowski’s derivative of regular expres-
sions. If we consider context-free grammars as recursive regular ex-
pressions, Brzozowski’s equational theory extends without modifi-
cation to context-free grammars (and it generalizes to parser combi-
nators). The supporting actors in this story are three concepts famil-
iar to functional programmers—laziness, memoization and fixed
points; these allow Brzozowski’s original equations to be translit-
erated into purely functional code in about 30 lines spread over
three functions.
Yet, this almost impossibly brief implementation has a draw-
back: its performance is sour—in both theory and practice. The
culprit? Each derivative can double the size of a grammar, and with
it, the cost of the next derivative.
Fortunately, much of the new structure inflicted by the derivative
is either dead on arrival, or it dies after the very next derivative.
To eliminate it, we once again exploit laziness and memoization
The derivative of regular expressions [1], if gently tempered
with laziness, memoization and fixed points, acts immediately
as a pure, functional technique for generating parse forests from
arbitrary context-free grammars. Despite—even because of—its
simplicity, the derivative transparently handles ambiguity, left-
recursion, right-recursion, ill-founded recursion or any combina-
tion thereof.
1.1 Outline
• After a review of formal languages, we introduce Brzozowski’s
derivative for regular languages. A brief implementation high-
lights its rugged elegance.
• As our implementation of the derivative engages context-free
languages, non-termination emerges as a problem.
• Three small, surgical modifications to the implementation (but
not the theory)—laziness, memoization and fixed points—
guarantee termination. Termination means the derivative can
Parsing with derivates
• Brzozowski derivative u−1S = { v ∈ Σ*: uv ∈ S }

the set of all rest-strings obtainable from a string in S by cutting off its prefix u
• code along: 

blog.klipse.tech/clojure/2016/10/02/parsing-with-derivatives-regular.html
Regular expressions (for data)
+
arbitrary predicates
+
data transformations (conformers)
clojure.spec
Spec: a lisp-flavoured type system
Two schools of
thinking
System paradigmLanguage paradigm
infoq.com/presentations/Mixin-based-Inheritance
realworldclojure.com/the-system-paradigm
[Language:] a formal system of signs governed by
grammatical rules of combination to communicate
meaning
[System:] a set of interacting or interdependent
components forming an integrated whole
The system paradigm
1. Nibble at the problem from different directions
2. Compose partial solutions into the final solution
In lisp you build systems by altering the living, running one right in front of you 

— R. Gabriel
Pascal is for building pyramids—imposing, breathtaking, static structures built
by armies pushing heavy blocks into place. Lisp is for building organisms. 

— A. Perlis
The Artificial Intelligence people were endeavouring to
write programs that no one knew how to write. The idea
that you could sit down and say: 'Well, here is my
problem, here are the requirements, let me come up with
a specification and now code that up' (was) completely
crazy as far as the AI people were concerned' The only
way to write AI programs, then (as it still is now) was by
taking an exploratory approach to development. The
only way to do it was to experiment. 'Let me try this, let
me add that. Let me try to add this fuzzy concept, let me
try to add a scheduler, let me add agendas, let me add
resources, let me have resource-limitations' ... you're not
constructing it like making a ton of source code and
compiling it periodically, you're constructing it the way
you construct a city: build some of it, it's running all the
time, so it's kind of like a live programming language.
— R. Gabriel
Live programming
No clear delineation
between environment
and work code
spec: a fully interactive
à la carte type system?
“Composition is about
decomposing.”
— E. Normand
“Good design is not about making
grand plans, but about taking
things apart.”
— R. Hickey
Destructuring
• Pull apart and name
• Factor out data shape
Data macros
• Recursive transformations into canonical form
• Do more without code macros
* juxt.pro/blog/posts/data-macros.html
Spec: a lisp-flavoured type system
Spec: a lisp-flavoured type system
Decomplect validation
and encoding
Generative testing
• Limitations
• sequences with internal structure (time series
etc.)
• generic higher-order functions (e.g. map)
• Uncovers numerical instabilities
• Better mocking in the REPL
TDD 

vs. 

REPL-driven development
vs.
spec-driven development?
Everything is data

(aka. the lisp way)
• generation
• introspection
• parsable errors
Building on top of
spec
Queryable data
descriptions
Turn spec into a graph
order
promo code
user
account age
country
always always
alwaysmaybe
Case study: autogenerating materialised views
Kafka
Materialised
views
Events
External data
Automatic view generation
• Event & attribute ontology
• Manual (via spec)
• Inferred
• Statistical analysis (seasonality
detection, outlier removal, …)
Onyx Onyx
Onyx
Automatic view generation
1. Walk spec registry
2. Apply rules
1. Define new view (spec)
2. Trigger Onyx job that creates the view
⤾
Missing pieces
github.com/arohner/spectrum — static analysis of
(spec annotated) Clojure code
github.com/stathissideris/spec-provider — infer spec
from examples
github.com/typedclojure/auto-annotation — infer
spec from tests
Takeouts
Decomplect
everything
Everything should be
live and interactive
Blurring the line between
environment and work is
a powerful idea
Queryable data
descriptions supercharge
interactive development
and are a great building
block for automation
Questions
@sbelak
simon@goopti.com
clojure.org/about/spec
matt.might.net/papers/might2011derivatives.pdf
infoq.com/presentations/Mixin-based-Inheritance
realworldclojure.com/the-system-paradigm
juxt.pro/blog/posts/data-macros.html
blog.klipse.tech/clojure/2016/10/02/parsing-with-derivatives-regular.html
indiegogo.com/projects/typed-clojure-clojure-spec-auto-annotations#/
github.com/arohner/spectrum
github.com/stathissideris/spec-provider
purelyfunctional.tv/issues/clojure-gazette-192-composition-is-about-decomposing

More Related Content

What's hot (20)

PDF
Artmosphere Demo
Keira Zhou
 
PDF
Logs aggregation and analysis
Divante
 
PDF
Vocanic Map Reduce Lite
Shreeniwas Iyer
 
PPTX
NRD: Nagios Result Distributor
Jose Luis Martínez
 
PDF
Airflow introduction
Chandler Huang
 
PDF
Airflow presentation
Ilias Okacha
 
PPTX
Airflow - a data flow engine
Walter Liu
 
PDF
Managing data workflows with Luigi
Teemu Kurppa
 
PPTX
Reactive Programming and RxJS
Denis Gorbunov
 
PDF
Thinking Functionally with Clojure
John Stevenson
 
PPTX
Apache Airflow (incubating) NL HUG Meetup 2016-07-19
Bolke de Bruin
 
PDF
Forecasting time series powerful and simple
Ivo Andreev
 
PPTX
IoT Supercharged: Complex event processing for MQTT with Eclipse technologies
Istvan Rath
 
PPTX
Hyperloglog Lightning Talk
Simon Prickett
 
PDF
How I learned to time travel, or, data pipelining and scheduling with Airflow
PyData
 
PDF
spaCy lightning talk for KyivPy #21
Anton Kasyanov
 
PDF
Apache Airflow
Sumit Maheshwari
 
PPTX
Apache Airflow overview
NikolayGrishchenkov
 
PDF
Predictive Datacenter Analytics with Strymon
Vasia Kalavri
 
PPTX
Xcore meets IncQuery: How the New Generation of DSLs are Made
Istvan Rath
 
Artmosphere Demo
Keira Zhou
 
Logs aggregation and analysis
Divante
 
Vocanic Map Reduce Lite
Shreeniwas Iyer
 
NRD: Nagios Result Distributor
Jose Luis Martínez
 
Airflow introduction
Chandler Huang
 
Airflow presentation
Ilias Okacha
 
Airflow - a data flow engine
Walter Liu
 
Managing data workflows with Luigi
Teemu Kurppa
 
Reactive Programming and RxJS
Denis Gorbunov
 
Thinking Functionally with Clojure
John Stevenson
 
Apache Airflow (incubating) NL HUG Meetup 2016-07-19
Bolke de Bruin
 
Forecasting time series powerful and simple
Ivo Andreev
 
IoT Supercharged: Complex event processing for MQTT with Eclipse technologies
Istvan Rath
 
Hyperloglog Lightning Talk
Simon Prickett
 
How I learned to time travel, or, data pipelining and scheduling with Airflow
PyData
 
spaCy lightning talk for KyivPy #21
Anton Kasyanov
 
Apache Airflow
Sumit Maheshwari
 
Apache Airflow overview
NikolayGrishchenkov
 
Predictive Datacenter Analytics with Strymon
Vasia Kalavri
 
Xcore meets IncQuery: How the New Generation of DSLs are Made
Istvan Rath
 

Similar to Spec: a lisp-flavoured type system (20)

PDF
Living with-spec
Simon Belak
 
PDF
Living with-spec
Simon Belak
 
PPTX
Introduction to Software - Coder Forge - John Mulhall
John Mulhall
 
PDF
What To Leave Implicit
Martin Odersky
 
PDF
Are High Level Programming Languages for Multicore and Safety Critical Conver...
InfinIT - Innovationsnetværket for it
 
PDF
Introduction to OpenSees by Frank McKenna
openseesdays
 
PDF
Doing data science with Clojure
Simon Belak
 
PPTX
Capabilities for Resources and Effects
Martin Odersky
 
PDF
odersky-adg-v5-220711161332-10853f8f.pdf
Ilham213720
 
PPTX
The secret life of rules in Software Engineering
Jordi Cabot
 
PDF
Illustrated Code (ASE 2021)
CISPA Helmholtz Center for Information Security
 
PDF
Core JavaScript
Lilia Sfaxi
 
PDF
Simplicitly
Martin Odersky
 
PDF
computer-science_engineering_principles-of-programming-languages_introduction...
AshutoshSharma874829
 
PDF
Clojure in real life 17.10.2014
Metosin Oy
 
PDF
Rust All Hands Winter 2011
Patrick Walton
 
PDF
Seeking Clojure
chrisriceuk
 
PPTX
Guide to Destroying Codebases The Demise of Clever Code
Gabor Varadi
 
PDF
Talent42 2014 Sam Wholley -
Talent42
 
KEY
The Why and How of Scala at Twitter
Alex Payne
 
Living with-spec
Simon Belak
 
Living with-spec
Simon Belak
 
Introduction to Software - Coder Forge - John Mulhall
John Mulhall
 
What To Leave Implicit
Martin Odersky
 
Are High Level Programming Languages for Multicore and Safety Critical Conver...
InfinIT - Innovationsnetværket for it
 
Introduction to OpenSees by Frank McKenna
openseesdays
 
Doing data science with Clojure
Simon Belak
 
Capabilities for Resources and Effects
Martin Odersky
 
odersky-adg-v5-220711161332-10853f8f.pdf
Ilham213720
 
The secret life of rules in Software Engineering
Jordi Cabot
 
Core JavaScript
Lilia Sfaxi
 
Simplicitly
Martin Odersky
 
computer-science_engineering_principles-of-programming-languages_introduction...
AshutoshSharma874829
 
Clojure in real life 17.10.2014
Metosin Oy
 
Rust All Hands Winter 2011
Patrick Walton
 
Seeking Clojure
chrisriceuk
 
Guide to Destroying Codebases The Demise of Clever Code
Gabor Varadi
 
Talent42 2014 Sam Wholley -
Talent42
 
The Why and How of Scala at Twitter
Alex Payne
 
Ad

More from Simon Belak (20)

PDF
Tools for building the future
Simon Belak
 
PDF
Doing data science with clojure
Simon Belak
 
PDF
Exploratory analysis
Simon Belak
 
PDF
Levelling up your data infrastructure
Simon Belak
 
PDF
The subtle art of recommendation
Simon Belak
 
PDF
Metabase Ljubljana Meetup #2
Simon Belak
 
PDF
Metabase lj meetup
Simon Belak
 
PDF
Sketch algorithms
Simon Belak
 
PDF
Transducing for fun and profit
Simon Belak
 
PDF
Your metrics are wrong
Simon Belak
 
PDF
Writing smart contracts the sane way
Simon Belak
 
PDF
Online statistical analysis using transducers and sketch algorithms
Simon Belak
 
PDF
Data driven going to market strategy
Simon Belak
 
PDF
Odkrivanje segmentov iz podatkov
Simon Belak
 
PDF
Dao of lisp
Simon Belak
 
PDF
Predicting the future with goopti
Simon Belak
 
PDF
Doing data science with Clojure
Simon Belak
 
PDF
The log
Simon Belak
 
PDF
Doing data science with Clojure
Simon Belak
 
PDF
Turn to datadriven: the first 6 months
Simon Belak
 
Tools for building the future
Simon Belak
 
Doing data science with clojure
Simon Belak
 
Exploratory analysis
Simon Belak
 
Levelling up your data infrastructure
Simon Belak
 
The subtle art of recommendation
Simon Belak
 
Metabase Ljubljana Meetup #2
Simon Belak
 
Metabase lj meetup
Simon Belak
 
Sketch algorithms
Simon Belak
 
Transducing for fun and profit
Simon Belak
 
Your metrics are wrong
Simon Belak
 
Writing smart contracts the sane way
Simon Belak
 
Online statistical analysis using transducers and sketch algorithms
Simon Belak
 
Data driven going to market strategy
Simon Belak
 
Odkrivanje segmentov iz podatkov
Simon Belak
 
Dao of lisp
Simon Belak
 
Predicting the future with goopti
Simon Belak
 
Doing data science with Clojure
Simon Belak
 
The log
Simon Belak
 
Doing data science with Clojure
Simon Belak
 
Turn to datadriven: the first 6 months
Simon Belak
 
Ad

Recently uploaded (20)

PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 

Spec: a lisp-flavoured type system