1
2
3
4
5
I gave a version of this presentation during Wolfram High School Summer Research Program 2025 to attendees of Wolfram Summer School 2025:
Abstract
Abstract
In this presentation software monads and monadic programming are introduced via question and answers. Various examples are used.
It is the first part of a three part series of presentations/lectures:
◼
Part 1: Introduction via Questions & Answers
◼
Part 2: Monadic programming applications overview and examples
◼
Part 3: Programming of software monads in Wolfram Language
(In can be said that the lectures are an overview of my monadic work since 2017.)
Who I am?
Who I am?
Questions
Questions
◼
Who is the audience of these talks?
◼
What is a good showcase of monadic programming?
◼
Which applications of monadic programming?
◼
Graceful failure handling
◼
Rapid specification of computational workflows
◼
Algebraic structure of written code
◼
How are software monads defined?
◼
How to explain monadic pipelines in an easy to understand manner?
◼
How can monadic programming be “invented”?
◼
Is knowing monadic programming required?
◼
Is coding faster or better using monads?
◼
Is monadic programming supported in Wolfram Language?
◼
How monads are programmed?
◼
What are the limitations of monad programming or using software monads?
◼
Are people thinking in a linear manner?
◼
Are you ready to support it as a user interface?
◼
Can monadic pipelines be combined?
◼
Yes, with corresponding unit replacement.
◼
Or designing and implementing suitable “algebra” between the monadic operations.
◼
Why do you use monadic programming?
◼
Rapid specification of computational workflows
◼
Knowledge transfer
◼
Why did you invest in monad programming so much?
◼
Analogies and invariants across different computational domains.
◼
When did you start using and developing software monads in Wolfram Language?
◼
In 2017 after teaching a data science class for Digital Humanities (in Oxford.)
◼
At first I used Wolfram Language for DSL’s grammars; moved to Raku in 2019.
Showcase
Showcase
In[]:=
dsTitanic
In[]:=
obj=ClConUnit[dsTitanic]⟹ClConSplitData[0.8]⟹ClConEchoDataSummary⟹ClConMakeClassifier["NearestNeighbors"]⟹ClConClassifierMeasurements⟹ClConEchoValue⟹ClConROCPlot;
»
summaries:trainingData
,
,
,
,
,testData
,
,
,
,
1 id | ||||||||||||
|
2 passengerClass | ||||||
|
3 passengerAge | ||||||||||||
|
4 passengerSex | ||||
|
5 passengerSurvival | ||||
|
1 id | ||||||||||||
|
2 passengerClass | ||||||
|
3 passengerAge | ||||||||||||
|
4 passengerSex | ||||
|
5 passengerSurvival | ||||
|
»
value:Accuracy0.717557,Precisiondied0.734043,survived0.675676,Recalldied0.851852,survived0.5
»
ROC plot(s):died
,survived
In[]:=
obj⟹ClConTakeClassifier
Out[]=
ClassifierFunction
Schematically
Schematically
ClCon
Color Worm
Brass pipe
References
References
Monadic pipelines invention (via data wrangling)
Monadic pipelines invention (via data wrangling)
In[]:=
RandomSample[dsTitanic,4]
Out[]=
WL & R pipelines
WL & R pipelines
Here is a natural language specification of a data transformation workflow:
Remark: We call this kind of specifications Domain Specific Language (DSL) specifications.
Here is how this DSL specification is translated to R and WL:
WL pipeline
WL pipeline
Let us consider the execution steps of the WL pipeline:
Python pipeline
Python pipeline
obj = dsTitanic.copy()
obj = obj[((obj["passengerAge"]> 10))]
obj = obj.groupby(["passengerClass", "passengerSex"])
obj = obj.size()
obj = obj[((obj["passengerAge"]> 10))]
obj = obj.groupby(["passengerClass", "passengerSex"])
obj = obj.size()
What is a monad? (informally speaking)
What is a monad? (informally speaking)
Unix examples
Unix examples
|
|
find . -name "*nb" | grep -i monad | xargs -Iaaa date -r aaa
dc
dc
3 5 + 4 * p
R tidyverse
R tidyverse
dsTitanic %>%
rename(age = passengerAge, sex = passengerSex, class = passengerClass) %>%
filter(age > 10) %>%
group_by(class, sex) %>%
count()
rename(age = passengerAge, sex = passengerSex, class = passengerClass) %>%
filter(age > 10) %>%
group_by(class, sex) %>%
count()
What is a monad? (formally speaking)
What is a monad? (formally speaking)
The monad definition
The monad definition
Here are operators for a monad associated with a certain symbol M:
1. monad unit function ("return" in Haskell notation) is Unit[x_]:=M[x];
2. monad bind function (">>=" in Haskell notation) is a rule like Bind[M[x_],f_]:=f[x] with MatchQ[f[x],M[_]] giving True.
Note that:
◼
the function Bind unwraps the content of M[_] and gives it to the function f;
Here is an illustration formula showing a monad pipeline:
From the definition and formula it should be clear that if for the result f[x] of Bind the test MatchQ[f[x],_M] is True then the result is ready to be fed to the next binding operation in monad's pipeline. Also, it is easy to program the pipeline functionality with Fold:
Using postfix function application
The monad laws
The monad laws
Here is a table with the laws:
Remark: The monad laws are satisfied for every symbol in Mathematica / WL with List being the unit operation and Apply being the binding operation.
Premonitions
Premonitions
Expected monadic programming features
Expected monadic programming features
and having certain programming experiences -- we can expect the following features when using monadic programming.
◼
Computations that can be expressed with monad pipelines are easy to construct and read.
◼
By programming the binding function we can tuck-in a variety of monad behaviours -- this the so called “programmable semicolon” feature of monadic programming.
The programming of the basic Maybe monad
The programming of the basic Maybe monad
Here is the minimal code to get a functional Maybe monad (for a more detailed exposition of code and explanations see [AA7]):
Extensions with polymorphic behavior
Extensions with polymorphic behavior
For example the extension of the Maybe monad to handle of Dataset objects is fairly easy and straightforward.
Here is the formula of the Maybe monad pipeline extended with Dataset objects:
Here is an example of a polymorphic function definition for the Maybe monad:
See [AA7] for more detailed examples of polymorphism in monadic programming with Mathematica / WL.
A complete discussion can be found in [PW1]. (The main message of [PW1] is the poly-functional and polymorphic properties of monad implementations.)
Polymorphic monads in R’s dplyr
Polymorphic monads in R’s dplyr
Monads-shmonads
Monads-shmonads
◼
◼
◼
Justifications of monads usage ...
◼
Connections of monads usage ...
◼
It seems that monad programming advocates are missing the point...
◼
Consequences of these observations.
Side notes
Side notes
◼
Why look into Category Theory?
◼
Cartesian closed categories and Functional programming.
◼
How Category Theory is like New Kind of Science?
◼
Re-defining mathematical equivalence.
Why I use monads?
Why I use monads?
Monadic programming is at the core of my “DSLs for Computation Workflows” project.
Here is a development cycle clarification diagram:
Rapid specification
Rapid specification
Consider the following -- intentionally short and non-specific -- computational workflow specifications:
Here we generate the code -- note the only the list of commands is given to the function Concretize, [AAp1]:
Here we tabulate the code generation results (i.e. computational templates that are filled-in):
End goal
End goal
The end goal with my software monads is the generation of (reliable, executable) code using natural language commands.
Pareto within Pareto
Pareto within Pareto
We can go even further and say that within each ML workflow 75% of the time we use the same workflow.
Pipelines? What pipelines?
Pipelines? What pipelines?
First encounter
First encounter
Here is a Machine Learning (ML) classification monad pipeline:
Re-done with LLMs
Re-done with LLMs
One possible way to use LLMs in software pipelines can be illustrated like this:
Remark: Every time the pipeline is executed splitting is different -- RandomSample is used. (Hence the need of SeedRandom above.)
Pipelines (Quantile Regression)
Pipelines (Quantile Regression)
WL
WL
Here we construct a pipeline (using built-in WL functions):
Here we evaluated it:
Take a more detailed look:
Python
Python
Here is pipeline that pipeline in Python:
Not just data science and machine learning workflows
Not just data science and machine learning workflows
Obviously this approach can be used for any type of computational workflows.
Here is an example of an Epidemiology Modeling workflow:
References
References
See the System Dynamics workflows paclet:
◼
How it is done?
How it is done?
We have two types of Domain Specific Languages (DSL’s) for data wrangling:
1
.Software package for data transformations
2
.Data wrangling DSL that is a subset of a spoken language
These two DSL's are combined: the natural language commands of the latter are translated into the former.
By executing those translations we interpret commands of spoken DSL's into data transformation computational results.
Note, that we assume that there is a separate system that converts speech into text.
More complicated monadic spaces 1
More complicated monadic spaces 1
More complicated monadic spaces 2
More complicated monadic spaces 2
Functional parsers provide a good example of monadic space with multiple operations.
Simple parser
Simple parser
Make a parser for a family of (two) simple sentences :
More complicated parser
More complicated parser
Parsing and interpretation
Parsing and interpretation
Rewrite the parser above with suitable interpretation:
References
References
See the paclet:
◼
Tracing monad
Tracing monad
The analogy between software monads and the Decorator design pattern can exemplified with the TraceMonad.
Usage in other programming languages
Usage in other programming languages
Monadic pipelines in Julia, Python, R, and Raku
Monadic pipelines in Julia, Python, R, and Raku
◼
Most (all) of the Machine Learning monads have been implemented in Python and R
◼
Except contextual classification (ClCon).
◼
Data wrangling pipeline code generation is implemented in Julia, Python, R, Raku
Make a language from ground-up
Make a language from ground-up
For some programming languages (e.g. Haskell) the concept of a “monad” is fundamental and used to build the whole language.
Connections to Design Patterns (by GoF)
Connections to Design Patterns (by GoF)
References
References
Articles
Articles
[Wk1] Wikipedia entry: Monad (functional programming), URL: https://en.wikipedia.org/wiki/Monad_(functional_programming) .
[SH2] Sheng Liang, Paul Hudak, Mark Jones, “Monad transformers and modular interpreters”, (1995), Proceedings of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages. New York, NY: ACM. pp. 333–343. doi:10.1145/199448.199528.
[PW1] Philip Wadler, “The essence of functional programming”, (1992), 19’th Annual Symposium on Principles of Programming Languages, Albuquerque, New Mexico, January 1992.
[RW1] Hadley Wickham et al., dplyr: A Grammar of Data Manipulation, (2014), tidyverse at GitHub, URL: https://github.com/tidyverse/dplyr .
(See also, http://dplyr.tidyverse.org .)
(See also, http://dplyr.tidyverse.org .)
Notebooks
Notebooks
[AAn1] Anton Antonov, "Making robust LLM computational pipelines from software engineering perspective", (2024), Wolfram Community.
[AAn2] Anton Antonov, "Handwritten Arabic characters classifiers comparison", (2022), Wolfram Community.
[AAn3] Anton Antonov, "LSA methods comparison over random mandalas deconstruction -- WL", (2022), Wolfram Community.
[AAn4] Anton Antonov, "LSA methods comparison over random mandalas deconstruction -- Python", (2022), Wolfram Community.
[AAn5] Anton Antonov, "Bethlehem stars: classifying randomly generated mandalas", (2020), Wolfram Community.
Functions, paclets
Functions, paclets
[AAp2] Anton Antonov, MonadicContextualClassification WL paclet, (2024), Wolfram Language Paclet Repository.
[AAp3] Anton Antonov, MonadicQuantileRegression WL paclet, (2023-2024), Wolfram Language Paclet Repository.
[AAp5] Anton Antonov, MonadicLatentSemanticAnalysis WL paclet, (2023), Wolfram Language Paclet Repository.
[AAp6] Anton Antonov, MonadicSparseMatrixRecommender WL paclet, (2024), Wolfram Language Paclet Repository.
Videos
Videos
[AAv1] Anton Antonov, Monadic Programming: With Application to Data Analysis, Machine Learning and Language Processing, (2017), Wolfram Technology Conference 2017 presentation. YouTube/WolframResearch.
[AAv2] Anton Antonov, Simplified Machine Learning Workflows Overview, (2022), Wolfram Technology Conference 2022 presentation. YouTube/WolframResearch.
[AAv3] Anton Antonov, Natural Language Processing Template Engine, (2022), Wolfram Technology Conference 2022 presentation. YouTube/WolframResearch.
[AAv5] Anton Antonov, Multi-Language Data Acquisition Conversational Agent, (2020), Wolfram Technology Conference 2020 presentation. YouTube/WolframResearch.
[AAv6] Anton Antonov, Robust LLM pipelines (Mathematica, Python, Raku), (2024), YouTube/AntonAntonov.
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Applications of monadic programming - Part 1: questions & answers
by Anton Antonov
Wolfram Community, STAFF PICKS, August 3, 2025
https://community.wolfram.com/groups/-/m/t/3524007
by Anton Antonov
Wolfram Community, STAFF PICKS, August 3, 2025
https://community.wolfram.com/groups/-/m/t/3524007