esolang

Lazy K

Pure combinatory logic that can still do I/O. A program is one function: it takes all of the input as a lazy list and returns all of the output.

Ben Rudiak-Gould, 2002 · Turing complete: yes · ●●● hard · esolangs.org

Tested with 2 examples and 2 known programs · Test method

Run it

Example

Expected output

cat

Output


The idea

Unlambda cheats a little. It has .x for printing, which is a side effect, and d and c to control when things happen. Ben Rudiak-Gould wanted the same game with no cheating: only S, K and I, no side effects, and lazy evaluation so that order does not matter.

Read the story

The trick is to make input and output data. The whole input is handed to the program as one list of numbers, and the program's value is the output, another list. Because evaluation is lazy, the interpreter can print the first character of the output before the program has looked at the second character of the input. An interactive program is just a function on infinite lists.

Lazy K accepts four syntaxes and lets you mix them: plain combinators (S K I with parentheses), Unlambda style (backticks), Iota and Jot.

The machine

PartWhat it is
The programA combinator expression built from S, K and I
A numberA Church numeral: n = λf.λx. f(f(…f(x))) with n fs
A listPairs: cons a b = λf. f a b. The head is list K, the tail is list (K I)
InputOne numeral per byte, then 256 forever as the end marker
OutputRead from the program's result until the first numeral of 256 or more

The reference interpreter also uses that last numeral (minus 256) as the exit code.

Example

The identity is cat.

The identity is cat. Every byte of input is copied to the output, and the 256 at the end of the input ends the output too:

Expected: prints: hello

The tail of a list is list (K I), so the program λl. l (K I) drops the first character. Written without the λ, that is S I (K (K I)):

Expected: prints: hello
Expected: the same program in Unlambda style

Check it: S I (K (K I)) l → I l (K (K I) l) → l (K I), the tail.

Writing anything that prints a constant is far harder: 72 (H) has to be a Church numeral, built from S and K. Real Lazy K programs are normally compiled from lambda terms, not written by hand.

This implementation handles the combinator and Unlambda syntaxes only, not Iota or Jot.

Checked examples

Example

Expected output

cat

Output


Quirks

  • A run stops after 3,000,000 steps.

Try this

  1. Write a program that drops the first two characters of its input.
  2. Mix the two syntaxes: write the same program as S I (K (K I)) with one part in backtick style, and check that it still drops the first character.
  3. Hard: the numeral 2 is S (S (K S) K) I. Use it to write a program that prints every character of its input twice. You will need a fixed-point combinator.

Patterns it teaches

  • Combinators: S, K and I and nothing else.
  • Lambda encoding: characters are Church numerals, strings are lists of pairs, and end of file is the number 256.
  • Turing tarpit: universal, and every program longer than a few characters needs a compiler.
  • Code as data: the input arrives as a function, and the program uses it by applying it.

Further

Functional paradigm

Write your own in Run Convert a program from Lazy K

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run lazyk -e 'I' --input 'cat'
shib manual lazyk

Source

Module
langs/dialects/ports.js
Interpreter
langs/dialects/functional.js

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

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