diff --git a/2019/lynn/.gitignore b/2019/lynn/.gitignore index 305346bc9..89da4fb6d 100644 --- a/2019/lynn/.gitignore +++ b/2019/lynn/.gitignore @@ -3,6 +3,9 @@ *.dSYM fib fib.c +ghcfib +ghcfib.hi +ghcfib.o hint hint.c indent diff --git a/2019/lynn/README.md b/2019/lynn/README.md index 82f3b4467..fcce9c662 100644 --- a/2019/lynn/README.md +++ b/2019/lynn/README.md @@ -5,6 +5,17 @@ make ``` +### Bugs and (Mis)features: + +The current status of this entry is: + +``` +STATUS: INABIAF - please **DO NOT** fix +``` + +For more detailed information see [2019 lynn in bugs.md](/bugs.md#2019-lynn). + + ## To use: ```sh @@ -15,15 +26,14 @@ make ### Try: ```sh -(./prog < fib.hs; cat prog.c) > fib.c -cc fib.c -o fib -./fib +./try.sh ``` ## Judges' remarks: -A fully functional compiler. The example prints out the 30th Fibonacci number. +A fully functional compiler. The example prints out the 30th [Fibonacci +number](https://en.wikipedia.org/wiki/Fibonacci_sequence). ## Author's remarks: @@ -39,7 +49,7 @@ system? The solution is simple: no type checking. It also means no typeclasses. Just pass dictionaries explicitly. * `ZeroPauseGarbageCollection`: Instead of disruptive stop-the-world garbage - collection, we only tidy up when the world stops of its own accord, that is, + collection, we only tidy up when the world stops on its own accord, that is, on program termination. * `OneHundredPercentPure`: Gone is the catch-all lawless IO monad. And no @@ -47,6 +57,7 @@ typeclasses. Just pass dictionaries explicitly. * `SyntaxForTheMasses`: See below. + ### Building: Build the compiler: @@ -59,7 +70,7 @@ cc -o prog prog.c #### Fibonacci numbers: -Test the compiler on `fib.hs`: +Test the compiler on [fib.hs](fib.hs): ```sh (./prog < fib.hs ; cat prog.c) > fib.c @@ -67,19 +78,20 @@ Test the compiler on `fib.hs`: Compiling the output produces a binary that prints the 30th Fibonacci number. -The file `ghcfib.hs` includes `fib.hs` with some glue code, and shows GHC also -accepts our subset of Haskell: +The file [ghcfib.hs](ghcfib.hs) includes [fib.hs](fib.hs) with some glue code, +and shows GHC also accepts our subset of Haskell: ```sh - ghc ghcfib.hs +ghc ghcfib.hs ``` + #### Self-hosting compiler: To avoid spoiling this entry by revealing the original Haskell source, we -instead provide `hint.hs`, the output of a certain stage of the compiler when -run on itself. This intermediate output is hopefully difficult to understand, -yet is accepted by our compiler: +instead provide [hint.hs](hint.hs), the output of a certain stage of the +compiler when run on itself. This intermediate output is hopefully difficult to +understand, yet is accepted by our compiler: ```sh (./prog < hint.hs ; cat prog.c) > hint.c @@ -90,13 +102,15 @@ make hint The output program behaves like the compiler itself. Unlike the original source from which it is derived, GHC fails to compile -`hint.hs`. This is because values have been replaced with their Scott encodings +[hint.hs](hint.hs). This is because values have been replaced with their [Scott +encodings](https://en.wikipedia.org/wiki/Mogensen-Scott_encoding) by this stage, which messes up type-checks; we'd need `equirecursive` types for it to work. + #### Regexes: -The file `lol.hs` contains an adaptation of [Doug McIlroy's elegant code from +The file [lol.hs](lol.hs) contains an adaptation of [Doug McIlroy's elegant code from "Enumerating the strings of regular languages"](https://www.cs.dartmouth.edu/~doug/nfa.pdf). We exercise it by showing the first entries of the length-ordered list of all strings consisting @@ -114,10 +128,12 @@ A GHC wrapper is provided: ghc ghclol.hs ``` + ### Strongly-connected components: -See `scc.hs` (and its GHC wrapper `ghcscc.hs`) for an elegant way to print the -strongly-connected components of a graph in reverse topological order. +See [scc.hs](scc.hs) (and its GHC wrapper [ghcscc.hs](ghcscc.hs)) for an elegant +way to print the strongly-connected components of a graph in reverse topological +order. ```sh (./prog < scc.hs ; cat prog.c) > scc.c @@ -128,15 +144,16 @@ It expects the input to be in a similar format as a previous entry ```sh make scc -./scc < ../../2018/vokes/example-1.txt -./scc < ../../2018/vokes/example-2.txt +./scc < example-1.txt +./scc < example-2.txt ``` The output should agree, though our program omits line numbers and does not sort entries within a line. (Also, our program only treats spaces as whitespace, and supports any non-space character in a vertex name.) -### Syntax ### + +### Syntax Some claim Haskell syntax is frightening because braces and semicolons are optional. Some complain about a zoo of twisty little operators, all alike. Our @@ -144,11 +161,13 @@ compiler addresses such concerns by making braces and semicolons compulsory, disabling layout-based rules, making every operator left-associative with same precedence, and only defining 6 primitive functions: - : + - * / <= +``` +: + - * / <= +``` -The arithmetic operations behave as they do in C on unsigned ints, and `(:)` is -Haskell's cons function. Like Haskell and unlike C, `(<=)` returns a Bool -(which are Church booleans behind the scenes) and not an Int. +The arithmetic operations behave as they do in C on `unsigned int`s, and `(:)` is +Haskell's `cons` function. Like Haskell and unlike C, `(<=)` returns a `Bool` +(which are Church booleans behind the scenes) and not an `Int`. No ifs, ands, or buts. Define them yourself. No tuples. Who needs them when algebraic data types delivers a better product? There is no do. No where. No @@ -160,11 +179,12 @@ Within global scope or a let expression, each definition can only refer to itself or previous definitions. This implies we can only achieve mutual recursion by having one function pass itself to others. -### Caveats ### + +### Caveats The only primitive type is `Int`. In particular, they represent characters. To interoperate with GHC, our compiler treats any undefined functions as the -identity function, so that we may freely use `ord` and `chr` (or `fromEnum` and +`identity` function, so that we may freely use `ord` and `chr` (or `fromEnum` and `toEnum`) to make our code acceptable to both compilers. The main function is the last function to successfully parse. It should have @@ -172,7 +192,7 @@ type `[Int] -> [Int]`, and our compiler treats this like the function passed into `Prelude.interact`, that is, the entire standard input is passed to this function, and the result is printed to standard output. -Bool must be defined as: +`Bool` must be defined as: ``` data Bool = True | False; @@ -181,14 +201,14 @@ data Bool = True | False; so that it matches the Scott-encoded booleans internally used by the primitive function `(<=)`. -The alternatives in a case expressions must list every data constructor in +The alternatives in a `case` expressions must list every data constructor in the order they are defined. For example, if we have: ``` data Foo a b = Bar a | Baz | Qux Int [b] ``` -then a case expression that examines a term of this type must have the form: +then a `case` expression that examines a term of this type must have the form: ``` case x of @@ -203,9 +223,10 @@ semicolons as separators, not terminators. The effect of erroneous input is undefined. It may be best to develop with GHC, but even then, be mindful of changes needed because of issues caused by the -uniform operator precedence and the touchy format of case alternatives. +uniform operator precedence and the touchy format of `case` alternatives. -### Why? ### + +### Why? One-letter variable names abound in IOCCC entries, and for good reason. These tiny pieces of confetti are hard to read, and leave room for more code. Then @@ -221,6 +242,7 @@ or rather, a compiler that accepts a subset of Haskell sufficiently large to self-host. You might say I wrote a tool for this contest, then ran it on itself to make an entry for it. + ### Obfuscation techniques: Even with Kiselyov's algorithm and some term rewriting, the compiler only fit @@ -236,7 +258,7 @@ ternary operators, and so on. * Huffman coding. * Base-85 because high bits are frowned upon. * Mixed radix encoding to game iocccsize. From a past winner -([2018/bellard](/2018/bellard/prog.c)), it seems 9 11 12 32 are the only +([2018/bellard](/2018/bellard/README.md)), it seems 9 11 12 32 are the only whitespace octets that may appear verbatim in string literals. * Choosing what to encode in Huffman/base-85 and what to encode in mixed radix was a delicate balancing act. In the end, I only had a few bytes to spare, which @@ -244,16 +266,16 @@ I spent on gratuitous confusion. * The effects of some functions depend on the order their arguments are evaluated, yet the program works either way. Why? * Ugly macros for the runtime system's jump table for lazy reduction. A previous -winner ([2013/endoh1](/2013/endoh1/endoh1.c)) has a cuter solution, which I +winner ([2013/endoh1](/2013/endoh1/README.md)) has a cuter solution, which I avoid because of originality concerns and also because my combinators are compressed. * Relies on the inability of C comments to nest. -* Cheaper and more complicated to print comma-separated ints (and header and +* Cheaper and more complicated to print comma-separated `int`s (and header and footer) in C. * Primitive functions use a trick described in depth by [Naylor and Runciman, "The Reduceron reconfigured and re-evaluated"](https://www.cs.york.ac.uk/fp/reduceron/jfp-reduceron.pdf). We -represent the integer n with a term equivalent to `Y(BT)n`; it works because +represent the integer `n` with a term equivalent to `Y(BT)n`; it works because `Y(BT)ne = e(Y(BT)n)`. * A sentinel in the heap often confused me, so ought to confuse others. @@ -263,19 +285,20 @@ compiler. See [hint.hs](hint.hs). * Mercilessly point-free. Everything is a combinator. * Scott encoding. Everything is a combinator. * Sum types are oddly ordered and may even contain unused data constructors to - reduce code duplication. (In C terms, unions may have extra fields, and they're + reduce code duplication. (In C terms, `union`s may have extra fields, and they're ordered in such a way so we can reuse code to access certain fields.) * The `undefined` function compiles to the `(.)` function. * Replaced the only `(&&)` with `(||)` and `not` using De Morgan's law, which makes some comparisons less comprehensible. * [Kiselyov only published "λ to SKI, Semantically" last year](http://okmij.org/ftp/tagless-final/ski.pdf). - * Recursion via the fixpoint (Y) combinator, which is represented by the + * Recursion via the fixpoint (`Y`) combinator, which is represented by the variable `""` during one stage of compilation. * Involves theory that may be less familiar to C programmers: maps, folds, parser combinators, lambda calculus, bracket abstraction, denotational semantics, etc. + ### Warnings: On my system, it compiles cleanly with `-Wall` with older standards, e.g. @@ -284,6 +307,7 @@ On my system, it compiles cleanly with `-Wall` with older standards, e.g. Compiling the compiler output with `-Wall` triggers a warning about a strange-looking comment. + ### Behind-the-scenes commentary: [My website reveals how this compiler diff --git a/2019/lynn/example-1.txt b/2019/lynn/example-1.txt new file mode 100644 index 000000000..e0e4bb068 --- /dev/null +++ b/2019/lynn/example-1.txt @@ -0,0 +1,8 @@ +0 4 8 +1 0 +2 1 3 +3 2 +4 1 +5 4 6 +6 5 2 +7 3 6 7 diff --git a/2019/lynn/example-2.txt b/2019/lynn/example-2.txt new file mode 100644 index 000000000..b90152aff --- /dev/null +++ b/2019/lynn/example-2.txt @@ -0,0 +1,8 @@ +0 1 +1 2 4 5 +2 3 6 +3 2 7 +4 0 5 +5 6 5 +6 5 +7 3 6 diff --git a/2019/lynn/try.sh b/2019/lynn/try.sh new file mode 100755 index 000000000..fce906ce4 --- /dev/null +++ b/2019/lynn/try.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# +# try.sh - demonstrate IOCCC winner 2019/lynn +# + +# make sure CC is set so that when we do make CC="$CC" it isn't empty. Doing it +# this way allows us to have the user specify a different compiler in an easy +# way. +if [[ -z "$CC" ]]; then + CC="cc" +fi + +make CC="$CC" all >/dev/null || exit 1 + +# clear screen after compilation so that only the entry is shown +clear + +echo "$ (./prog < fib.hs; cat prog.c) > fib.c" 1>&2 +(./prog < fib.hs; cat prog.c) > fib.c +echo "$ cc -Wno-everything fib.c -o fib" 1>&2 +cc -Wno-everything fib.c -o fib +echo "$ ./fib" 1>&2 +./fib +echo 1>&2 +read -r -n 1 -p "Press any key to continue: " +echo 1>&2 + +echo "$ (./prog < lol.hs ; cat prog.c) > lol.c" 1>&2 +(./prog < lol.hs ; cat prog.c) > lol.c +echo "$ make lol" 1>&2 +make lol +read -r -n 1 -p "Press any key to run: ./lol: " +echo 1>&2 +./lol +echo 1>&2 + +read -r -n 1 -p "Press any key to continue: " +echo 1>&2 +echo "$ (./prog < scc.hs ; cat prog.c) > scc.c" 1>&2 +(./prog < scc.hs ; cat prog.c) > scc.c +echo "$ make scc" 1>&2 +make scc +read -r -n 1 -p "Press any key to run: ./scc < example-1.txt: " +echo 1>&2 +./scc < example-1.txt +read -r -n 1 -p "Press any key to run: ./scc < example-2.txt: " +echo 1>&2 +./scc < example-2.txt + + +exit 0 + diff --git a/bugs.md b/bugs.md index 9a291d015..b6d7430d3 100644 --- a/bugs.md +++ b/bugs.md @@ -3492,6 +3492,18 @@ not encountered this myself). - The program contains some unused code and data. +## 2019 lynn + +### STATUS: INABIAF - please **DO NOT** fix +### Source code: [2019/lynn/prog.c](2019/lynn/prog.c) +### Information: [2019/lynn/README.md](2019/lynn/README.md) + +The author wrote that there are a number of differences from what one might +expect. Rather than duplicate the information we refer you to the author's +remarks in the sections [Syntax](/2019/lynn/README.md#syntax) and +[Caveats](/2019/lynn/README.md#caveats). + + ## 2019 poikola ### STATUS: INABIAF - please **DO NOT** fix diff --git a/thanks-for-help.md b/thanks-for-help.md index b9dd4193f..b740d1731 100644 --- a/thanks-for-help.md +++ b/thanks-for-help.md @@ -4233,6 +4233,16 @@ He also added the script [try.sh](/2019/karns/try.sh) to showcase the entry a bit more easily. +## [2019/lynn](/2019/lynn/prog.c) ([README.md](/2019/lynn/README.md])) + +[Cody](#cody) added the [try.sh](/2019/lynn/try.sh) script. + +Cody also added the [example-1.txt](/2019/lynn/example-1.txt) and +[example-2.txt](/2019/lynn/example-2.txt) text files from +[2018/vokes](/2018/vokes/README.md) so that the entry does not rely on any other +entry existing. + + ## [2019/yang](/2019/yang/prog.c) ([README.md](/2019/yang/README.md])) [Cody](#cody) added the [try.sh](/2019/yang/try.sh) script which also involved