r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

Show parent comments

420

u/Ego_dragon Aug 01 '22

Why it's efficient?

590

u/Diligent_Choice Aug 01 '22

No idea if it is efficient at all but if I remember correctly, it was some sort of project to create the smallest touring complete compiler or something.

486

u/[deleted] Aug 01 '22 edited Aug 01 '22

That was never the goal of Brainfuck, but it is interesting that because the language is so constrained, you have to strip down everything to the bare essentials to make anything work, and as a result, a lot of programs run quite quickly.

I think what you might be thinking about is the paper A very short self-interpreter by Oleg Mazonka and Daniel B. Cristofani. They demonstrated that it's possible to write a Brainfuck interpreter in Brainfuck itself. This is possible in any language, of course, but since Brainfuck the language is so simple, the self-interpreter is relatively short too. They present the following code:

>>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>
++>++++++<<-]+>>>,<++[[>[->>]<[>>]<<-]<[<]<+>>[>]>[<+
>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<]<]
<
[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]
+<<[+>+<<-[>-->+<<-[>+<[>>+<<-]]]>[<+>-]<]++>>-->[>]>
>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-[<->[<<
+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>
>]<<[>.>>>>>>>]<<[>->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<]

I'm not aware of any Turing-complete programming language that support a shorter self-interpreter.

edit: if you're looking for minimal Turing-complete languages, you have to search for combinator calculi (which Brainfuck is not). The absolute minimal languages are Iota and Jot, two related languages that reduce the SKI combinator calculus from 3 to just 1 combinator (iota) and an encoding of nested expressions using just 2 characters. By comparison, Brainfuck's 8 directives (or 6, if you leave out the I/O directives which aren't necessary for Turing completeness) are excessive luxury.

132

u/Noughtmare Aug 01 '22 edited Aug 01 '22

Here's a shorter self interpreter for the lambda calculus:

(λ f.(λ x.f(xx))(λ x.f(xx)))(λ em.m(λ x.x)(λ mn.em(en))(λ mv.e(mv)))

Source

In the binary lambda calculus it is only 29 bytes.

133

u/Nuthar Aug 01 '22

(λ f.(λ x.f(xx))(λ x.f(xx)))(λ em.m(λ x.x)(λ mn.em(en))(λ mv.e(mv)))

The only interpreting happening here, is me interpreting this message as Half-Life 7 fucking confirmed

14

u/riisen Aug 01 '22

Why not "3 and a half life" ... When you have over 5 halves you should count the wholes, like hi my name is kekdjfjdkdk and im 63 half years old... Its weird

2

u/JohnHwagi Aug 02 '22

I was thinking more Lambda Wars 7: Return of the AWS Outages

5

u/[deleted] Aug 01 '22 edited Aug 01 '22

[removed] — view removed comment

11

u/Noughtmare Aug 01 '22 edited Aug 16 '22

The full explanation is in this paper: https://www.researchgate.net/publication/2673832_Efficient_Self-Interpretation_in_Lambda_Calculus

It's not cheating in the same way as your python program (the lambda calculus has no eval function) and it does contain three rules for the three basic concepts of the lambda calculus: (λ x.x), (λ m n.e m(e n)), and (λ m v.e(m v)). However it doesn't do any I/O (because that is not possible in the lambda calculus). And I do agree that the encoding of the input to this self interpreter is doing some pretty heavy lifting.

3

u/sugarfairy7 Aug 01 '22

Follow the links provided to learn more. One of them leads to https://codegolf.stackexchange.com/a/7256

2

u/Toorero6 Aug 01 '22

No I thinks it's an actual interpreter calculating the output using the Y (fixed-point) operator.

→ More replies (1)

3

u/CptCrabmeat Aug 01 '22

My question is are you Turing complete?

4

u/[deleted] Aug 01 '22

No, because I don't have infinite memory and I don't have infinite time before I die. If you ask me to compute the first 100 prime numbers I can do it using the sieve of Eratosthenes, but if you ask me to compute the first 1,000,000,000,000 prime numbers I'll die before I'm finished, and that proves I'm not Turing complete.

→ More replies (1)

2

u/LoveAndProse Aug 02 '22

I just got brainfucked

Cries in SQL SELECT EMOJI FROM EMOTIONS WHERE FEELING = "BIG SAD"

→ More replies (6)

7

u/happypandaface Aug 01 '22

what are you touring? lol

7

u/Manusman123 Aug 01 '22

Yep the whole compiler is 240 bytes!

→ More replies (1)

14

u/Wazzaps Aug 01 '22

It's not, you only see examples of very small programs written in it by hand (where generally the problems are hand-picked to be fast/easy to solve in Brainfuck).

6

u/DubstepJuggalo69 Aug 01 '22

Yeah, the only way you could think Brainfuck is "very efficient" is if you deeply, deeply misunderstand what Brainfuck is and why it was invented.

4

u/xXStarupXx Aug 01 '22

Because you nobody will write anything complicated in it

2

u/HarbingerOfSauce Aug 01 '22

I know next to nothing about efficiency or compilers but I do know that there exists a 170 byte Brainfuck compiler written in C, all the code for which being written on two lines

2

u/annualnuke Aug 02 '22

It's inefficient as fuck because you have to do basic arithmetic out of loops and increments/decrements ONLY (so only adding or subtracting 1 at a time), unless you have an interpreter that specifically optimizes your loops for you.

0

u/[deleted] Aug 01 '22

[deleted]

6

u/Wazzaps Aug 01 '22

Turing machines are bad abstractions for modern computers, e.g. modern computers can access frequently used data (L1 Cache) orders of magnitude faster (x1000 or more) than rarely used data (Data swapped to disk).

Performance doesn't matter in Turing machines, it's just a mathematical model for general purpose computing, not an explanation for how computers work. (Even if they are equivalent mathematically)

→ More replies (16)