201 lines
9.9 KiB
HTML
201 lines
9.9 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Thun</title>
|
|
<link rel="stylesheet" href="css/font/fonts.css">
|
|
<link rel="stylesheet" href="css/site.css">
|
|
<script src="Joy.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="joy_interpreter"></div>
|
|
<h1>Thun</h1>
|
|
<p>A Dialect of Joy.</p>
|
|
<p>Version 0.5.0</p>
|
|
<blockquote>
|
|
<p>Simple pleasures are the best.</p>
|
|
</blockquote>
|
|
<p><a href="https://en.wikipedia.org/wiki/Joy_%28programming_language%29">Joy</a> is a
|
|
programming language created by Manfred von Thun that is easy to use and
|
|
understand and has many other nice properties. <strong>Thun</strong> is a dialect of
|
|
Joy that attempts to stay very close to the spirit of Joy but does not
|
|
precisely match the behaviour of the original version written in C. It
|
|
started as a Python project called "Joypy", but after someone claimed
|
|
that name on PyPI before me I renamed it to Thun in honor of Manfred Von
|
|
Thun. Now there are interpreters implemented in several additional
|
|
languages (C, Elm, Nim, OCaml, Prolog, and Scheme).</p>
|
|
<p>Joy is:</p>
|
|
<ul>
|
|
<li><a href="https://en.wikipedia.org/wiki/Purely_functional_programming">Purely Functional</a></li>
|
|
<li><a href="https://en.wikipedia.org/wiki/Stack-oriented_programming_language">Stack-based</a></li>
|
|
<li><a href="https://en.wikipedia.org/wiki/Concatenative_programming_language">Concatinative</a>
|
|
(See also <a href="http://www.concatenative.org/wiki/view/Concatenative%20language">concatenative.org</a>)</li>
|
|
<li><a href="notebooks/Categorical.html">Categorical</a></li>
|
|
</ul>
|
|
<p>The best source for learning about Joy is the information made available
|
|
at the <a href="http://www.latrobe.edu.au/humanities/research/research-projects/past-projects/joy-programming-language">website of La Trobe University</a>
|
|
| <a href="https://www.kevinalbrecht.com/code/joy-mirror/">(mirror)</a> which
|
|
contains source code for the original C interpreter, Joy language source
|
|
code for various functions, and a great deal of fascinating material
|
|
mostly written by Von Thun on Joy and its deeper facets as well as how to
|
|
program in it and several interesting aspects. It's quite a treasure
|
|
trove.</p>
|
|
<ul>
|
|
<li><a href="https://en.wikipedia.org/wiki/Joy_%28programming_language%29">Wikipedia entry for Joy</a></li>
|
|
<li><a href="http://www.latrobe.edu.au/humanities/research/research-projects/past-projects/joy-programming-language">Homepage at La Trobe University</a>
|
|
<a href="https://www.kevinalbrecht.com/code/joy-mirror/">(Kevin Albrecht's mirror)</a></li>
|
|
<li><a href="https://web.archive.org/web/20220411010035/https://joypy.osdn.io/">The original Thun/Joypy site</a></li>
|
|
</ul>
|
|
<h2>Example Code</h2>
|
|
<p>Here is an example of Joy code. This function <code>square_spiral</code> accepts
|
|
two integers and increments or decrements one of them such that the new
|
|
pair of numbers is the next coordinate pair in a square spiral (like the
|
|
kind used to construct an <a href="https://en.wikipedia.org/wiki/Ulam_spiral">Ulam Spiral</a>).
|
|
For more information see <a href="notebooks/Square_Spiral.html">Square Spiral Example Joy Code</a>.</p>
|
|
<pre><code>square_spiral [p] [then] [else] ifte
|
|
|
|
p [p0] [p1] and
|
|
p0 [abs] ii <=
|
|
p1 [<>] [pop !-] or
|
|
|
|
then [ !-] [[++]] [[--]] ifte dip
|
|
else [pop !-] [--] [++] ifte
|
|
</code></pre>
|
|
<p>It might seem unreadable but with familiarity it becomes as legible as
|
|
any other notation.</p>
|
|
<h2>Project Hosted on <a href="https://ariadne.systems/gogs/sforman/Thun">Ariadne Systems</a></h2>
|
|
<ul>
|
|
<li><a href="https://ariadne.systems/gogs/sforman/Thun">Source Repository</a>
|
|
(<a href="https://github.com/calroc/Thun">mirror</a>)</li>
|
|
<li><a href="https://ariadne.systems/gogs/sforman/Thun/issues">Bug tracker</a>
|
|
(<a href="https://osdn.net/projects/joypy/ticket/">old tracker</a>)</li>
|
|
<li><a href="https://osdn.net/projects/joypy/forums/">Forums</a></li>
|
|
<li><a href="https://osdn.net/projects/joypy/lists/">Mailing list</a></li>
|
|
</ul>
|
|
<h2>Documentation</h2>
|
|
<p>The <a href="Thun.html">Thun specification</a> document describes the Thun dialect,
|
|
however most of the
|
|
documentation is in the form of <a href="notebooks/index.html">Jupyter Notebooks</a>
|
|
that go into more detail.</p>
|
|
<p><strong><a href="notebooks/index.html">Jupyter Notebooks</a></strong></p>
|
|
<p>There's also a <a href="FuncRef.html">Function Reference</a> that lists each
|
|
function and combinator by name and gives a brief description. (It's
|
|
usually out of date, I'm working on it.)</p>
|
|
<p><strong><a href="FuncRef.html">Function Reference</a></strong></p>
|
|
<h2>Basics of Joy</h2>
|
|
<p>The original Joy has several datatypes (such as strings and sets)
|
|
but the Thun dialect currently only uses four:</p>
|
|
<ul>
|
|
<li>Integers, signed and unbounded by machine word length (they are
|
|
<a href="https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic">bignums</a>.)</li>
|
|
<li>Boolean values <code>true</code> and <code>false</code>.</li>
|
|
<li>Lists quoted in <code>[</code> and <code>]</code> brackets.</li>
|
|
<li>Symbols (names).</li>
|
|
</ul>
|
|
<p>Joy is built around three things: a <strong>stack</strong> of data items, an <strong>expression</strong>
|
|
representing a program to evaluate, and a <strong>dictionary</strong> of named functions.</p>
|
|
<h3>Stack</h3>
|
|
<p>Joy is <a href="https://en.wikipedia.org/wiki/Stack-oriented_programming_language">stack-based</a>.
|
|
There is a single main <strong>stack</strong> that holds data items, which can be integers, bools,
|
|
symbols (names), or sequences of data items enclosed in square brackets (<code>[</code> or <code>]</code>).</p>
|
|
<p>We use the terms "stack", "quote", "sequence",
|
|
"list", and others to mean the same thing: a simple linear datatype that
|
|
permits certain operations such as iterating and pushing and popping
|
|
values from (at least) one end.</p>
|
|
<blockquote>
|
|
<p>In describing Joy I have used the term quotation to describe all of the
|
|
above, because I needed a word to describe the arguments to combinators
|
|
which fulfill the same role in Joy as lambda abstractions (with
|
|
variables) fulfill in the more familiar functional languages. I use the
|
|
term list for those quotations whose members are what I call literals:
|
|
numbers, characters, truth values, sets, strings and other quotations.
|
|
All these I call literals because their occurrence in code results in
|
|
them being pushed onto the stack. But I also call [London Paris] a list.
|
|
So, [dup *] is a quotation but not a list.</p>
|
|
</blockquote>
|
|
<p>From <a href="http://archive.vector.org.uk/art10000350">"A Conversation with Manfred von Thun" w/ Stevan Apter</a></p>
|
|
<h3>Expression</h3>
|
|
<p>A Joy <strong>expression</strong> is just a sequence or list of items. Sequences
|
|
intended as programs are called "quoted programs". Evaluation proceeds
|
|
by iterating through the terms in an expression putting all literals
|
|
(integers, bools, or lists) onto the main stack and executing functions
|
|
named by symbols as they are encountered. Functions receive the current
|
|
stack, expression, and dictionary and return the next stack, expression,
|
|
and dictionary.</p>
|
|
<h3>Dictionary</h3>
|
|
<p>The <strong>dictionary</strong> associates symbols (names) with Joy expressions that
|
|
define the available functions of the Joy system. Together the stack,
|
|
expression, and dictionary are the entire state of the Joy interpreter.</p>
|
|
<h3>Interpreter</h3>
|
|
<p>The Joy interpreter is extrememly simple. It accepts a stack, an
|
|
expression, and a dictionary, and it iterates through the expression
|
|
putting values onto the stack and delegating execution to functions which
|
|
it looks up in the dictionary.</p>
|
|
<p><img alt="Joy Interpreter Flowchart" src="/joy_interpreter_flowchart.svg"></p>
|
|
<p>All control flow works by
|
|
<a href="https://en.wikipedia.org/wiki/Continuation-passing_style">Continuation Passing Style</a>.
|
|
<strong>Combinators</strong> (see below) alter control flow by prepending quoted programs to the pending
|
|
expression (aka "continuation".)</p>
|
|
<hr>
|
|
<p>From here it kinda falls apart...</p>
|
|
<h3>Literals and Simple Functions</h3>
|
|
<p>TODO</p>
|
|
<h3>Combinators</h3>
|
|
<p>The main loop is very simple as most of the action happens through what
|
|
are called <strong>combinators</strong>. These are functions which accept quoted programs on the
|
|
stack and run them in various ways. These combinators reify specific
|
|
control-flow patterns (such as <code>ifte</code> which is like <code>if.. then.. else..</code> in other
|
|
languages.) Combinators receive the current
|
|
expession in addition to the stack and return the next expression. They
|
|
work by changing the pending expression the interpreter is about to
|
|
execute. (The combinators could work by making recursive calls to the
|
|
interpreter and all intermediate state would be held in the call stack of
|
|
the implementation language, in this joy implementation they work instead
|
|
by changing the pending expression and intermediate state is put there.)</p>
|
|
<pre><code>joy? 23 [0 >] [dup --] while
|
|
23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
|
|
</code></pre>
|
|
<h3>Core Words</h3>
|
|
<p>This is the <em>basis</em> set of functions, the rest of functions in the Thun
|
|
dialect of Joy are defined in terms of these:</p>
|
|
<pre><code>branch
|
|
dip
|
|
i
|
|
loop
|
|
|
|
clear
|
|
concat
|
|
cons
|
|
dup
|
|
first
|
|
pop
|
|
rest
|
|
stack
|
|
swaack
|
|
swap
|
|
truthy
|
|
inscribe
|
|
|
|
+ - * / %
|
|
|
|
< > >= <= != <> =
|
|
|
|
lshift rshift
|
|
</code></pre>
|
|
<hr>
|
|
<p>Copyright © 2014 - 2023 Simon Forman</p>
|
|
<p>This file is part of Thun</p>
|
|
<p>Thun is free software: you can redistribute it and/or modify it under the
|
|
terms of the GNU General Public License as published by the Free Software
|
|
Foundation, either version 3 of the License, or (at your option) any
|
|
later version.</p>
|
|
<p>Thun is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
details.</p>
|
|
<p>You should have received a copy of the GNU General Public License along
|
|
with Thun. If not see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.</p>
|
|
<script>var joy_interpreter = Elm.Main.init({node: document.getElementById('joy_interpreter')});</script>
|
|
</body>
|
|
</html>
|