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:
◼
  • "Applications of Monadic Programming, Part 1, Questions & Answers"
  • The corresponding presentation is below. It is also available here in its original form.

    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?
    

    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

    In[]:=
    dsTitanic
    In[]:=
    obj=ClConUnit[dsTitanic]⟹​​ClConSplitData[0.8]⟹​​ClConEchoDataSummary⟹​​ClConMakeClassifier["NearestNeighbors"]⟹​​ClConClassifierMeasurements⟹ClConEchoValue⟹​​ClConROCPlot;
    »
    summaries:trainingData
    1 id
    Min
    2
    1st Qu
    306.25
    Mean
    648.303
    Median
    651
    3rd Qu
    977.5
    Max
    1309
    ,
    2 passengerClass
    3rd
    563
    1st
    277
    2nd
    207
    ,
    3 passengerAge
    Min
    -1
    1st Qu
    10
    Median
    20
    Mean
    23.8223
    3rd Qu
    40
    Max
    80
    ,
    4 passengerSex
    male
    682
    female
    365
    ,
    5 passengerSurvival
    died
    647
    survived
    400
    ,testData
    1 id
    Min
    1
    1st Qu
    382
    Median
    678
    Mean
    681.763
    3rd Qu
    1001
    Max
    1301
    ,
    2 passengerClass
    3rd
    146
    2nd
    70
    1st
    46
    ,
    3 passengerAge
    Min
    -1
    1st Qu
    0
    Median
    20
    Mean
    22.4618
    3rd Qu
    30
    Max
    80
    ,
    4 passengerSex
    male
    161
    female
    101
    ,
    5 passengerSurvival
    died
    162
    survived
    100
    
    »
    value:Accuracy0.717557,Precisiondied0.734043,survived0.675676,Recalldied0.851852,survived0.5
    »
    ROC plot(s):died
    ,survived
    
    In[]:=
    obj⟹ClConTakeClassifier
    Out[]=
    ClassifierFunction
    Input type:
    Mixed (number: 4)
    Classes: died,survived
    Method: NearestNeighbors
    Number of training examples: 1047
    

    Schematically

    ClCon
    Color Worm
    Brass pipe

    References
    

    Monadic pipelines invention (via data wrangling)

    Here is a dataset (Titanic data):
    In[]:=
    RandomSample[dsTitanic,4]
    Out[]=
    id
    passengerClass
    passengerAge
    passengerSex
    passengerSurvival
    597
    2nd
    30
    male
    survived
    900
    3rd
    30
    female
    survived
    744
    3rd
    20
    female
    died
    966
    3rd
    20
    male
    died

    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

    Let us consider the execution steps of the WL pipeline:

    Python pipeline

    〉
    obj = dsTitanic.copy()
    obj = obj[((obj["passengerAge"]> 10))]
    obj = obj.groupby(["passengerClass", "passengerSex"])
    obj = obj.size()

    What is a monad? (informally speaking)

    Unix examples

    |

    find . -name "*nb" | grep -i monad | xargs -Iaaa date -r aaa

    dc

    3 5 + 4 * p

    R tidyverse

    〉
    dsTitanic %>%
    rename(age = passengerAge, sex = passengerSex, class = passengerClass) %>%
    filter(age > 10) %>%
    group_by(class, sex) %>%
    count()

    What is a monad? (formally speaking)

    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

    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

    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.
  • These points are clarified below. For more complete discussions see [Wk1] or [H3].

    The programming of the basic Maybe monad

    It is fairly easy to program the basic monad Maybe discussed in [Wk1].
    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

    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

    Monads-shmonads

    ◼
  • Monads come from Category Theory.
  • ◼
  • Also known as the “Abstract nonsense” theory.
  • ◼
  • Justifications of monads usage ...
  • ◼
  • Connections of monads usage ...
  • ◼
  • It seems that monad programming advocates are missing the point...
  • ◼
  • Consequences of these observations.
  • 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?

    Monadic programming is at the core of my “DSLs for Computation Workflows” project.
    Here is a development cycle clarification diagram:

    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

    The end goal with my software monads is the generation of (reliable, executable) code using natural language commands.

    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?

    First encounter

    Here is a Machine Learning (ML) classification monad pipeline:

    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)

    WL

    Here we construct a pipeline (using built-in WL functions):
    Here we evaluated it:
    Take a more detailed look:

    Python

    Here is pipeline that pipeline in Python:

    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

    See the System Dynamics workflows paclet:
    ◼
  • [AAp4] Anton Antonov, MonadicSystemDynamics WL paclet, (2023), Wolfram Language Paclet Repository.
  • 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 2

    Functional parsers provide a good example of monadic space with multiple operations.

    Simple parser

    Make a parser for a family of (two) simple sentences :

    More complicated parser

    Parsing and interpretation

    Rewrite the parser above with suitable interpretation:

    References

    See the paclet:
    ◼
  • [AAp8] Anton Antonov, FunctionalParsers WL paclet, (2023), Wolfram Language Paclet Repository.
  • Tracing monad

    The analogy between software monads and the Decorator design pattern can exemplified with the TraceMonad.

    Usage in other programming languages

    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

    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)

    Command

    Decorator

    Interpreter

    References

    Articles

    [Wk1] Wikipedia entry: Monad (functional programming), URL: https://en.wikipedia.org/wiki/Monad_(functional_programming) .
    [Wk2] Wikipedia entry: Monad transformer, URL: https://en.wikipedia.org/wiki/Monad_transformer .
    [H1] Haskell.org article: Monad laws, URL: https://wiki.haskell.org/Monad_laws.
    [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 .)

    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

    [AAf1] Anton Antonov, LLMTextualAnswer WL paclet, (2024), Wolfram Language Paclet Repository.
    [AAp1] Anton Antonov, MonadMakers WL paclet, (2023), Wolfram Language Paclet Repository.
    [AAp2] Anton Antonov, MonadicContextualClassification WL paclet, (2024), Wolfram Language Paclet Repository.
    [AAp3] Anton Antonov, MonadicQuantileRegression WL paclet, (2023-2024), Wolfram Language Paclet Repository.
    [AAp4] Anton Antonov, MonadicSystemDynamics WL paclet, (2023), 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.
    [AAp7] Anton Antonov, NLPTemplateEngine WL paclet, (2024), Wolfram Language Paclet Repository.
    [AAp8] Anton Antonov, FunctionalParsers WL paclet, (2023), Wolfram Language Paclet Repository.

    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.
    [AAv4] Anton Antonov, NLP Template Engine, Part 1, (2021), YouTube/AntonAntonov.
    [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.
    [AAv7] Anton Antonov, Raku for Prediction, (2021), The Raku Conference 2021.

    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