Leibniz by example
Context predator-prey
uses functions/derivatives-ℝ→ℝ
1 Example: the predator-prey equations
The predator-prey equations, also known as the Lotka-Volterra equations, describe the dynamics of two interacting species in an ecosystem in terms of non-linear differential equations.
pp1: 𝒟(prey) = (prey-growth-rate × prey) - (predation-rate × predators × prey)
pp2: 𝒟(predators) = (predator-growth-rate × predators × prey) - (predator-loss-rate × predators)
In the absence of predators, the prey exihibits exponential growth described by prey-growth-rate : ℝp.
The number of prey decreases by predation, which is predation-rate : ℝp times the number of encounters between individuals of each species. The latter is taken to be proportional to both prey and predators.
In the absence of prey, the number of predators decreases by starvation, described by predator-loss-rate : ℝp.
The number of predators grows with the availability of food, which, like predation, is proportional to both prey and predators with the proportionality constant predator-growth-rate : ℝp.
Context predator-prey-explanation
extends predator-prey
2 A guide to reading this example
Everything typeset on a light blue background is Leibniz code. Everything else is plain text.
The boldface parts (pp1, pp2) are equation labels that can be used to refer to a specific equation.
ℝ stands for the real numbers, ℝp for the positive real numbers, and ℝ→ℝ for real functions of one real variable. If f : ℝ→ℝ is such a function, then 𝒟(f) is the derivative of f. All these definitions come from the imported context "functions/ℝ→ℝ" whose definition you can see here. Yes, that link should be in the example itself, and it will be.
3 Why Leibniz?
Compare the example in the first section with the beginning of the Wikipedia entry on the same topic, which uses traditional mathematical notation. If Wikipedia adopted Leibniz, what would it gain?
A machine-readable version of the predator-prey equations, generated from the same input and therefore identical in content. You can look at it here. It’s an XML file, which your browser may not display nicely, but you can always download it and open it in a text editor. A Leibniz-aware solver for differential equations could read this file, prompt you for parameter values and initial values, and compute and plot a solution. And if you had two Leibniz-aware solvers, you could compare their output, knowing for sure that they work on the same equations. Better yet, they work on the same equations that you have read and understood while reading the explanation. No more mistakes in transcribing equations to code!
A more precise notation. For example, in the Wikipedia text, it is not immediately clear that x and y are functions of time, whereas α, β, γ, δ are constants. In Leibniz, you have to say what each object is, and you get an error message if you try to take the derivative of something that is not a function. More generally, everything typeset on a blue background has been checked for consistency and completeness.
4 What else can you do with Leibniz?
This simple example doesn’t illustrate all the features of Leibniz. Moreover, Leibniz is far from complete at this time. Here are some things that you can do, or will soon be able to do, using Leibniz:
Express algorithms. Many computational models and methods in science combine equations with algorithms. We write the equations in papers using mathematical notation, and the algorithms in software source code using programming languages. Leibniz can express both, and render both in a human-readable and in a machine-readable form. See here and here for simple examples of algorithms in Leibniz.
Transform equations and algorithms. Start from a general model and then specialize it. Introduce approximations. Switch from exact arithmetic to floating-point arithmetic (as in this example). Most importantly, do all this in a human-readable document that your peers can verify, rather than in hard-to-read software source code.
Document all levels of a computational research project, from basic concepts to data anlysis workflows, using a single language, at the level of detail that you consider appropriate. You can just write an equation and stop there, as in the above example. But you can also use Leibniz to write an algorithm for solving differential equations. The general idea is that you document your research in Leibniz, but delegate what you consider technical details to Leibniz-aware software tools.
Work with complex models using computational tools. Suppose your scientific model is an equation with 2000 terms. Writing it down on paper is pointless. Writing it down as a Fortran subroutine is more useful, but you cannot do anything else with that subroutine than compute specific values. If you write it down in Leibniz, you can write software to analyze it, e.g. check that every second term is positive or whatever else you know about your model. You can also compute specific values in Leibniz, and use them to test your Fortran code.
Define scientific concepts with the precision of a formal language. There is no better remedy against sloppy thinking than a computer that forces you to respect your own definitions. See this article collection for essays on this little-known aspect of computing and for practical examples.