esolang

Interfrac

Fractions added the way every child first tries it, top to top and bottom to bottom.

Orby, 2019 · Turing complete: no · ●○○ easy · esolangs.org

Tested with 4 known programs · Test method

Run it

The idea

Orby made Interfrac in 2019 around a story. In 1994, at the age of ten, Orby found that (a / b) + (c / d) = (a + c) / (b + d), and others tried to correct it. Interfrac is the first language that adds fractions this way.

Read the story

A program is a list of fractions. It accepts the integer n when some of its fractions, each used as often as needed, add up this way to n / n. Accepting is halting, and the program prints nothing. The specification argues that Interfrac is NP-complete. It can solve subset sum, and integer linear programming can predict every program.

The machine

PartIs
-10/20, 10/-5The program, fractions separated by commas
inputThe integer n; an empty input is 0
haltsSome sum of one or more fractions is n / n
runs foreverNo sum is

This implementation tries every sum of one fraction, then of two, then of three, and halts at the first n / n.

Example

Both halt.

Both halt. In the first, -10/20 + 10/-5 + 10/-5 = (-10 + 10 + 10) / (20 + -5 + -5) = 10 / 10. Every positive multiple of 10 is accepted the same way. On 5 it never halts, since every sum of its tops is a multiple of 10.

In the second, -2/5 + 1/-3 + 1/-2 = 0 / 0, so it halts on empty input. The specification points out that -2/5 + -2/5 + 1/-7 = -3 / 3 does not accept -3. Yet 4 times -2/5, 2 times 1/-7 and 3 times 1/-3 make (-8 + 2 + 3) / (20 - 14 - 9) = -3 / -3, so -3 is accepted after all. The search finds it among the sums of nine fractions.

Patterns it teaches

  • Nondeterminism: a program accepts when any choice of fractions works, the N in NP.

Further

Declarative paradigm

Write your own in Run Convert a program from Interfrac

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: interfrac.

Command line

shib run interfrac -e '-10/20, 10/-5' --input '10'
shib manual interfrac

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/interfrac.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org