Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Defining lambda addition without using the successor function

I am familiar with defining the ADD function on top of the SUCC function, such as in the following:

const ONE = f => a => f(a);   
const SUCC = n => f => a => f(n(f)(a));        // SUCC = λnfa.f(nfa)
const ADD = n1 => n2 => n1(SUCC)(n2);          // ADD  = λnk.n SUCC k 
console.log(ADD(ONE)(ONE)(x => x+1)(0));
// 2

However, how would I define add if there wasn’t a successor function already defined? I tried using substitution to get the following, but I’m not sure why it’s not working. What do I have screwed up here?

const ONE = f => a => f(a);
const ADD = n1 => n2 => f => a => n1(f(n1(f)(a)))(n2);
console.log(ADD(ONE)(ONE)(x => x+1)(0));
// TypeError: f is not a function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const ADD = n1 => n2 => n1(n => f => a => f(n(f)(a)))(n2)
// ADD = λnk.n (λnfa.f (n f a)) k

When doing substitution, you simply replace the term to be substituted with its definition:

  • n1 => n2 => n1(SUCC)(n2)
  • definition of SUCC: n => f => a => f(n(f)(a))
  • replace SUCC with the above definition: n1 => n2 => n1(n => f => a => f(n(f)(a)))(n2)

Another way you could define ADD is like this:

const ADD = n1 => n2 => f => x => n1(f)(n2(f)(x))
// ADD = λnkfx.n f (k f x)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading