Exploring Infinite Precision in Lambda Calculus

BY Mark Howell 6 August 20246 MINS READ
article cover

We want to talk about a project based on the work of u/DaVinci103. Since its inception, the bruijn programming language had support for positive and negative integers. One thing I love about lambda calculus encodings for numbers is that they are usually infinitely precise – only limited by your computer’s memory. Still, lazy (call-by-need) or optimal reducers can sometimes do magical optimizations automatically that would have to be hard-coded in other languages. It’s natural to want to extend the integers to arbitrary-precision fractions, and later real and complex numbers. Encoding rational numbers, it turns out, is as easy as writing two integers as a (Church) pair. The relevant arithmetic operations are fairly trivial to implement as well. However, for the real numbers, I couldn’t come up with any elegant solution: This is, until I saw a great post on Reddit about this exact topic. So, in this article, I want to show how the new arbitrary-precision arithmetic of bruijn is implemented, including some “more mathematical” real/complex formulas and ways to approximate their result. I also want to use this opportunity to introduce you to coding in bruijn and lambda calculus in general. Note that all of the definitions (including library imports) have a 1:1 correspondence to pure lambda calculus – bruijn is just syntactic sugar (syntax explanation). We’re literally crafting complex formulas from the bottom up. You can click on any identifier to jump to its definition.

Integer

Natural numbers in lambda calculus are most commonly encoded as Church numerals. They can be extended to integers by writing them as a pair. While the implementations are quite small and elegant, any number now takes O(2^n) space. Instead, I use balanced ternary numbers. Theoretically, one could use any base, but an investigation by Torben Mogensen showed that bigger bases result in much more complicated implementations without significant performance benefits.
Balanced ternary numbers provide a very elegant way to denote negative numbers by assigning one of its three digits to a negative multiplier. This encoding reduces the space needed to O(log n).

Rational

The rational numbers represent the ratio between two integers. The obvious implementation is to use a pair of two balanced ternary numbers. Since we have the restriction of a non-zero denominator, we subtract 1 from the denominator in the encoding.
Operators for rational numbers can be implemented by comparing the product of the numerators and denominators. Basic operations like addition, subtraction, multiplication, and division can be implemented similarly.

Real

The missing insight I got from Reddit is a lambda calculus translation of the fact that real numbers can be defined as the infinite limit of a function that maps natural to rational numbers. For example, for some x ∈ R, there exists f_x: N → Q, such that x = lim_{n→∞} f_x(n). This would typically be defined as a sequence that converges to the desired real number.
Approximating a real number to an arbitrarily precise rational number can then be done by applying some natural number. In theory, we could use any base here, yet I still use balanced ternary.

Operators

To check equality of two real numbers, we compare the infinite limits of their respective sequences. For other binary operators, a small combinator trick "joins" the argument of both sides, such that we only ever need to apply a single number to any real number to get its approximation.

Power

For positive numbers, the power function can be implemented using the exponential and logarithmic functions. The exponential function can be approximated using Taylor series, while the logarithmic function can be approximated using a series expansion.

Derivation

The derivative of a function can be approximated using the limit definition of the derivative. This involves calculating the difference quotient and taking the limit as the increment approaches zero.

Constants

Interesting constants like e and π can be calculated using series expansions. For example, e can be calculated using the Taylor series of the exponential function, and π can be approximated using a series expansion.

Trigonometry

Trigonometric functions like arctan, sin, and cos can be approximated using series expansions. These functions are left as an exercise to the reader.

Methods

There are many ways to translate real approximations like Taylor series to lambda calculus. Depending on your needs, different methods may be more optimal.

Unary

If you use unary/Church numerals instead, you get repeated iteration for free. A Church numeral is essentially a repeated application of a function. This can make certain calculations easier but also takes more space.

Recurrence

Recurrence relations can be resolved using a variadic fixed-point combinator. However, this doesn't necessarily give performance improvements over normal iteration using y-recursion.

Complex

Complex numbers are just a pair of real numbers. The approximation arguments of the two real numbers are lifted to the outside, such that we only ever need to apply a single natural number. Operators for complex numbers can be implemented using rational operations directly.

Lessons

Working with bruijn and pure lambda calculus is satisfying and enjoyable, but it comes with performance disadvantages. If someone wanted to use this for actual programs, they would need to invest a huge amount of work to make all of these functions efficient and accurate. This article serves mostly as a fun proof of concept and notebook.
Remember these 3 key ideas for your startup:

  1. Efficiency in Encoding: Using balanced ternary numbers instead of larger bases can significantly improve the efficiency of your algorithms. This can be crucial for startups looking to optimize their computational resources.

  2. Lambda Calculus for Precision: Lambda calculus allows for infinitely precise calculations, limited only by your computer's memory. This can be a game-changer for startups dealing with high-precision computations.

  3. Iterative Improvements: The article demonstrates the importance of iterative improvements and optimizations. For startups, this means continuously refining your algorithms and processes to achieve better performance and accuracy.
    Edworking is the best and smartest decision for SMEs and startups to be more productive. Edworking is a FREE superapp of productivity that includes all you need for work powered by AI in the same superapp, connecting Task Management, Docs, Chat, Videocall, and File Management. Save money today by not paying for Slack, Trello, Dropbox, Zoom, and Notion.

Image: Lambda Calculus Representation
For more details, see the original source.

article cover
About the Author: Mark Howell Linkedin

Mark Howell is a talented content writer for Edworking's blog, consistently producing high-quality articles on a daily basis. As a Sales Representative, he brings a unique perspective to his writing, providing valuable insights and actionable advice for readers in the education industry. With a keen eye for detail and a passion for sharing knowledge, Mark is an indispensable member of the Edworking team. His expertise in task management ensures that he is always on top of his assignments and meets strict deadlines. Furthermore, Mark's skills in project management enable him to collaborate effectively with colleagues, contributing to the team's overall success and growth. As a reliable and diligent professional, Mark Howell continues to elevate Edworking's blog and brand with his well-researched and engaging content.

Trendy NewsSee All Articles
CoverVisual Prompt Injections: Essential Guide for StartupsThe Beginner's Guide to Visual Prompt Injections explores vulnerabilities in AI models like GPT-4V, highlighting security risks for startups and offering strategies to mitigate potential data compromises.
BY Mark Howell 2 mo ago
CoverGraph-Based AI: Pioneering Future Innovation PathwaysGraph-based AI, developed by MIT's Markus J. Buehler, bridges unrelated fields, revealing shared complexity patterns, accelerating innovation by uncovering novel ideas and designs, fostering unprecedented growth opportunities.
BY Mark Howell 2 mo ago
CoverRevolutionary Image Protection: Watermark Anything with Localized MessagesWatermark Anything enables embedding multiple localized watermarks in images, balancing imperceptibility and robustness. It uses Python, PyTorch, and CUDA, with COCO dataset, under CC-BY-NC license.
BY Mark Howell 2 mo ago
CoverJungle Music's Role in Shaping 90s Video Game SoundtracksJungle music in the 90s revolutionized video game soundtracks, enhancing fast-paced gameplay on PlayStation and Nintendo 64, and fostering a cultural revolution through its energetic beats and immersive experiences.
BY Mark Howell 2 mo ago
CoverMastering Probability-Generating Functions: A Guide for EntrepreneursProbability-generating functions (pgfs) are mathematical tools used in probability theory for data analysis, risk management, and predictive modeling, crucial for startups and SMEs in strategic decision-making.
BY Mark Howell 2 mo ago
CoverMastering Tokenization: Key to Successful AI ApplicationsTokenization is crucial in NLP for AI apps, influencing data processing. Understanding tokenizers enhances AI performance, ensuring meaningful interactions and minimizing Garbage In, Garbage Out issues.
BY Mark Howell 23 October 2024
CoverReviving Connection: What We Lost with the Decline of Letter WritingThe shift from handwritten letters to digital communication has reduced personal connection, depth, and attentiveness, impacting how we communicate and relate in both personal and business contexts.
BY Mark Howell 23 October 2024
CoverLichess Move: Behind-the-Scenes Technical BreakdownWhen you make a move on lichess.org, it triggers real-time data exchanges via WebSocket, updates game state, and ensures seamless gameplay using Redis Pub/Sub and MongoDB.
BY Mark Howell 23 October 2024
Try EdworkingA new way to work from  anywhere, for everyone for Free!
Sign up Now