Peano goes Maybe

Just for the fun of it, let's represent Nats as Maybies in Haskell.

import Prelude hiding (succ)
-- A strange representation of Nats
newtype Nat = Nat { getNat :: Maybe Nat }
-- Peano zero
zero :: Nat
zero = Nat Nothing
-- Peano successor
succ :: Nat -> Nat
succ = Nat . Just
-- Primitive recursion for addition
add :: Nat -> Nat -> Nat
add x = maybe x (succ . add x) . getNat
-- Convert primitive Int into strange Nat
fromInt :: Int -> Nat
fromInt 0 = Nat Nothing
fromInt x = succ (fromInt (x-1))
-- Convert strange Nat into primitive Int
toInt :: Nat -> Int
toInt = maybe 0 ((+1) . toInt) . getNat
-- Let's test
main = print $ toInt (add (fromInt 20) (fromInt 22))

I wrote this code in response to a student question, whether and, if so, how one could code recursive functions on maybies. This inspired me towards the exam question as to how the above code compares to more straightforward code which would uses an algebraic datatype with Zero and Succ constructors instead of maybies.


An ontological approach to technology documentation

SE talk at Chalmers, Gothenburg, Sweden

An ontological approach to technology documentation

Room 473 / Wed March 1 - 11:00 - 12:00 

Speaker: Ralf Lämmel, University of Koblenz-Landau

Abstract: In this talk, I am going to present an ontological approach to software technology documentation. That is, usage scenarios of a technology (such as an object/relational mapper, a web-application framework, or a model transformation) are captured in terms of the involved entities (e.g., artifacts, languages, abstract processes, programming paradigms, functions, and function applications) and the relationships between them (e.g., membership, conformance, transformation, usage, and reference). I am going to discuss language and tool support for and experiences with developing such technology documentation. In the SoftLang team at Koblenz, we work on the related but broader notion of "linguistic software architecture" or "megamodeling". I will briefly discuss applications of megamodeling other than technology documentation, namely build management and regression testing. More information: http://www.softlang.org/mega

Slidesin preparation


The Haskell Road to Software Language Engineering and Metaprogramming

FP talk at Chalmers, Gothenburg, Sweden

The Haskell Road to Software Language Engineering and Metaprogramming

2017-02-24, 10.00, conference room 8103, Rännvägen 6, Johanneberg.  

Speaker: Ralf Lämmel, University of Koblenz-Landau

In this talk, I would like to sketch my upcoming textbook on software languages http://www.softlang.org/book while putting on the hat of a Haskell programmer. Overall, the book addresses many issues of software language engineering and metaprogramming: internal and external DSLs, object-program representation, parsing, template processing, pretty printing, interpretation, compilation, type checking, software analysis, software transformation, rewriting, attribute grammars, partial evaluation, program generation, abstraction interpretation, concrete object syntax, and a few more. Haskell plays a major role in the book in that Haskell is used for the implementation for all kinds of language processors, even though some other programming languages (Python, Java, and Prolog) and domain-specific languages (e.g., for syntax definition) are leveraged as well. I hope the talk will be interactive and help me to finish the material and possibly give the audience some ideas about FP- and software language-related education and knowledge management.



Software Language Book ready for review and limited access

I am happy to be done with the draft of the software language book. Just sent it off to Springer for the final verdict/review.

Please find the book's frontmatter (including table of contents, preface, and acknowledgment) as well as the first technical chapter ("The notion of software language") online:


If you like to review the draft book or use it already in class room, please get in touch. The draft is sent to Springer and I hope to receive Springer's Ok+input within three months and finalize the book accordingly no later than May 2017. While under review and further scrutiny, I am going to perform self-motivated proof-reading and fine tuning. (There are some obvious dimensions for the final mile: index, exercises, English, formatting, clarity, bibliography.)

I am going to have a sabbatical Mid of February - End of October 2017. I am super-über-motivated to visit a few places, give guest lectures drawn from the book, and discuss use of the book in teaching, and, of course, engage on research along the way. No matter what, there is going to be extensive slide, video, and code material complementing the book by the end of summer.

All the code that's in the book and a lot more is available online anyway:


BTW, the book's repo is megamodel-managed; see here:

Happy New Year

PS: Just received word from Springer regarding schedule:
  • 1-15 January: Identification of reviewers :-)
  • 15 January - 7 April: Reviewing
  • 7 April - 15 May: Revision
  • 15 May - 1 July: Copy editing
  • 1 July - 1 October: Production
  • 23 - 24 October: Outing at SLE 2017 conference


Update on the Software Languages Book

Deleting this post as it superseded by newer updates.


Scoped global variables in Prolog

It just so happens that I needed global variables in some Prolog code.

In fact, I needed more carefully scoped global variables. SWI-Prolog's global variables are really global. (Well, they are thread-scoped, for what it matters.) This is not good, if you need a lot of global variables and maybe even in different parts of an application.

An uninspiring approach would be to fabricate global variable names in a manner that they are scoped internally by some name prefix. It was more fun to achieve scope by means of actually using one truly global variable to provide many scoped variables. Here is a demo:

?- logvars:get(myscope, a, X).                            

?- logvars:get(myscope, a, Y).

?- logvars:get(myscope, a, X), logvars:get(myscope, a, Y).
X = Y .

?- logvars:get(myscope, a, X), logvars:get(myscope, b, Y).
true .

Here is the code:


Inlined below:

% (C) 2016 Ralf Laemmel
:- module(logvars, []).

get(+S, +N, -V): Retrieve the value V of the global variable named N
and "scoped" by the global variable S. The variable N is
"automatically" created with a fresh logical variable V as initial

get(S, N, V) :-
    ( nb_current(S, _) ->
          nb_setval(S, _) ),
    nb_getval(S, (Ns, Vs)),
    varnth0(Pos, Ns, N),
    nth0(Pos, Vs, V).   

varnth0(-Pos, ?Ns, +N): given a list Ns of atoms with a variable tail,
find the name N in Ns or, if not present, unify the first variable
position of Ns with N, and return the position as Pos. This is a
helper for get/3.

varnth0(Pos1, [N1|Ns], N2) :-
    ( N1 == N2 ->
   Pos1 = 0;
          varnth0(Pos2, Ns, N2),
          Pos1 is Pos2 + 1 ).
varnth0(0, [N1|_], N2) :-
    N1 = N2.


Megamodels of Coupled Transformations

While having fun at UvA in A'dam by means of teaching Haskell in a pre-master summer school, I also manage to walk across the street to see folks at CWI. Will be giving a presentation on coupled transformations and megamodeling.

Title: Megamodels of Coupled Transformations

Abstract: Many software engineering contexts involve a collection of coupled artifacts, i.e., changing one artifact may challenge consistency between artifacts of the collection. A coupled software transformation (CX) is meant to transform one or more artifacts of such a collection while preserving consistency. There are many forms of coupling—depending on technological space and application domain and solution approach. We axiomatize and illustrate important forms of coupling within the Prolog-based software language repository YAS (Yet Another SLR (Software Language Repository)) while relying on a higher-level predicate logic-based megamodeling language LAL for axiomatization and a lower-level megamodeling language Ueber for build management and testing.

Date and Time: 19 August, 11:00am

Related links: