Haifu
Note: all information on this article comes from here, so there may be some mistakes.
Overview
Haifu is an esoteric programming language designed by David Morgan-Mar which draws upon Eastern philosophy.
It has four design principles:
- The programming language should respect nature and be mindful of its beauty
- The language will be based on the five classical elements of Asian thought, rather than the limiting Western Aristotelian notion of four elements. The five elements are: Earth, Fire, Water, Wood, and Metal
- The language should have artistic merit. To this end, all valid code must be in the form of haiku
- Because David Morgan-Mar only speaks English, code will take the form of English haiku, with the classic 5-7-5 syllable structure based on English words. He realises this is a fundamental limitation on the purity of the language, but any Chinese or Japanese speakers out there are welcomed to port Haifu to versions in those languages
The following concepts have been derived from these principles.
Language concepts
Haiku structure
Haifu code consists of normal English words, arranged into haiku of three lines each. The first line of each haiku contains 5 syllables, the second line 7 syllables, and the third line 5 syllables. Any other arrangement is a syntax error. A program may contain any number of haiku, arranged sequentially. Words may not be broken across lines or haiku by hyphenation, unless the word is normally hyphenated.
Tao-ian values
The restrictive Western notions of true and false are not used. Instead, Haifu programmers must be aware of Yin and Yang. The programmer must concern him or herself with balancing Yin and Yang values throughout the program to achieve balance, since without balance the program will not run. Of course, if the programmer is one with the Tao balance will come effortlessly.
Even numbers have Yin quality, while odd numbers have Yang quality. Floating point numbers have the quality of the number they are "reaching for" (i.e. they are treated as if rounded away from zero; 1.2 is Yin, -2.3 is Yang).
The five elements
Data values are associated with one of the five elements: Earth, Fire, Water, Wood, and Metal. Each element relates to the other four in different ways:
- Creation: Earth creates Metal, Metal creates Water, Water creates Wood, Wood creates Fire, Fire creates Earth
- Destruction: Earth destroys Water, Water destroys Fire, Fire destroys Metal, Metal destroys Wood, Wood destroys Earth
- Love: Earth loves Fire, Fire loves Wood, Wood loves Water, Water loves Metal, Metal loves Earth
- Fear: Earth fears Wood, Wood fears Metal, Metal fears Fire, Fire fears Water, Water fears Earth
Creation and destruction are physical relationships, while love and fear are emotional relationships.
Assension to the celestial bureaucracy
The Celestial Bureaucracy is a strictly ordered hierarchy of positions into which data values, variables and operators may be placed. These items are placed on the bottom-most rung of the Bureaucracy, and may only approach Heaven by the placing of further items below them. Items may also be removed from the Bureaucracy, from any rung. When items are removed, all items above them lose influence and thus descend away from Heaven (Unenlightened Western programmers may see some similarities to a stack structure, but such observations are narrow-minded and misleading, as the Celestial Bureaucracy operates in the spiritual realm, unlike materialistic Western data structures).
When a Haifu program is executed, all non-comment words and punctuation marks are placed in order into the Celestial Bureaucracy. Hyphenated words are treated as a single word. When all the haiku have been thus processed, the Bureaucracy contains all words of the code, with the first word nearest Heaven and the last word furthest away. The Celestial Bureaucrat (language interpreter) then processes the words beginning at the bottom rung, ascending one rung at a time, until the Bureaucrat reaches Heaven (i.e. is "above" all the words in the Bureaucracy), at which point the program terminates. The Celestial Bureaucrat does not occupy positions in the Bureaucracy, but merely looks at the words.
The Celestial Bureaucrat has a Delegate to help him. The Delegate also begins looking at the bottom rung of the Bureaucracy, but only ascends or descends on the direct orders of the Celestial Bureaucrat. Additionally, no matter what orders are given to the Delegate, he may never rise to a position higher than the Celestial Bureaucrat - if such an order is given, the Delegate merely rises to the same level as the Celestial Bureaucrat.
Syntax elements
Haifu is not case-sensitive. Words may be capitalised as desired for literary effect.
Data values
Haifu supports only numerical data, though in I/O they may be interpreted in different ways. In particular, character data are interpreted as Unicode values (to allow operations on Asian characters, though this also works for Western characters). Both integers and floating point numbers are supported, although there is no distinction between them.
Integer literals may be specified by writing the number in English, in either cardinal (one, two, three) or ordinal (first, second, third) form. Additionally, common synonyms for numbers are accepted: "no", "none" for zero, "a", "an" for one, "couple" for two, "dozen" for twelve, "century" for one-hundred, etc. A literal which forms a single number must be hyphenated together, otherwise it will be treated as two numbers: "two hundred" is 2 followed by 100, "two-hundred" is 200. Numbers, especially long ones, may be hyphenated across lines and/or haiku to maintain proper haiku structure. The size of integers specifiable is governed by the artistic problems inherent in filling an entire haiku with just a number.
Random numbers may be generated using words such as "some", "few", "many", etc - which evaluate to suitable random integers whenever encountered by the Celestial Bureaucrat or Delegate.
Floating point literals may not be specified and can only be generated by operating on integers. If a floating point number is required to be interpreted as an integer (for Unicode output, for example), it is treated as the number it strives for (see the Yin/Yang section).
All number literals are reserved words in Haifu. All number literals are initially of Earth elemental quality.
If the Celestial Bureaucrat encounters a numeric literal, it is beneath his dignity and he ignores it and ascends to the next rung of the Bureaucracy.
Variables
Any non-reserved word which possesses the fundamental qualities of one of the five elements and is not a vulgar word may be used as a variable name. Vulgarities are forbidden because they should not be used in delicate artistry such as Haifu code. The variable maintains the qualities of that element until changed by an operator. Examples of variable names in each element:
- Wood: tree, grass, cherry, oak
- Fire: flame, ash, smoke, embers
- Earth: soil, mountain, rock, plain
- Metal: sword, iron, plough, knife
- Water: rain, snow, river, ice
Variable names may be referred to by both singular and plural forms - "tree" and "trees" refer to the same variable. Any word which is not clearly associated with a particular element is considered to have Earth quality, though it is considered bad programming practice to rely on this.
Variables may contain a sequence of orders remembered by the Celestial Bureaucrat. The remembering of orders is discussed under "Punctuation" below.
Note that when a variable name is placed into the Bureaucracy it is the variable itself, not just its value. A variable may fill multiple roles in the Bureaucracy, and thus appear in it many times at different levels of enlightenment. If the value or elemental nature of the variable changes, the change applies to all roles in which the variable finds itself. This may confuse unenlightened Western programmers, but those in harmony with the Tao realise there can be no other way and bend as the willow to the requirements.
If the Celestial Bureaucrat encounters a variable, it performs the sequence of orders it has remembered as the definition of that variable. If the variable has not yet been defined, the Bureaucrat does nothing and ascends to the next rung of the Bureaucracy.
Punctuation
Punctuation marks enable the definition of commands. When the Celestial Bureaucrat encounters a punctuation mark it treats the next word as a variable name and the following sequence of orders as a definition of that variable. As it ascends through the orders it does not process them, but merely remembers the sequence, until it reaches either another copy of the command name, or any punctuation mark. That completes the definition of the variable, and the Celestial Bureaucrat then continues ascending the Bureaucracy, processing orders as normal. Variable names may be redefined later.
Operators
The following reserved words are operators in Haifu. When the Celestial Bureaucrat encounters them, they cause certain operations to be performed, often by the Delegate. Words which perform the same operation are listed separated by /. Note that tense, derivations, conjugations, etc. are treated as the same word, so "destroys", "destroying", "destroyed" and "destruction" are identical to "destroy". If any order specifies an operation which makes no sense, the Bureaucrat ignores it and ascends to the next rung of the Celestial Bureaucracy.
Celestial bureaucrat control
- heaven/nirvana/enlightenment/harmony: This causes the Celestial Bureaucrat to ascend to Heaven immediately (thus terminating program execution).
- promote/more/increase/wax: This causes the Celestial Bureaucrat to ascend a number of rungs equal to the number or numeric variable at the Delegate's rung. If the Delegate's item is not a number or variable containing a number, this operation does nothing.
- demote/less/reduce/wane: This causes the Celestial Bureaucrat to descend a number of rungs equal to the number or numeric variable at the Delegate's rung. If the Delegate's item is not a number or variable containing a number, this operation does nothing. If the Celestial Bureaucrat descends below the Delegate, the Delegate also descends to the same level as the Bureaucrat.
- blossom/flower/petal: This causes the Delegate to examine the Yin/Yang value of the item at his rung. If it is Yin, the Celestial Bureaucrat descends a number of rungs equal to the numeric vaue of the Delegate's item. If it is Yang, the Celestial Bureaucrat ascends that number of rungs. If the item does not have a numeric value (and thus is neither Yin nor Yang), this operation does nothing.
Delegate control
- rise/float/ascend/up: This causes the Delegate to move up a number of rungs equal to the number or numeric value of a variable in the rung immediately below the Celestial Bureaucrat. If the rung below the Bureaucrat does not contain such a number, the Delegate moves up one rung. In either case, the Delegate may only rise as high as the same rung as the Bureaucrat.
- fall/drop/descend/down: This causes the Delegate to move down a number of rungs equal to the number or numeric value of a variable in the rung immediately below the Celestial Bureaucrat. If the rung below the Bureaucrat does not contain such a number, the Delegate moves down one rung. If the number is greater than the number of rungs below the Delegate, he falls to the very bottom of the Celestial Bureaucracy.
I/O control
- listen/hear/see: This is an input command which causes a number or unicode character on the standard input stream to be read and inserted as the bottom-most value on the Celestial Bureaucracy. If no input awaits, this command takes the word in the rung above the Celestial Bureaucrat and "reads" it by removing it from that position and putting it at the bottom of the Bureaucracy.
- speak/say/draw: This is an output command which causes the Delegate to output the number or numeric value of a variable at his rung to the standard output stream as a Unicode character. If the number is outside the legal range for Unicode, or the Delegate's item is not a number or variable containing a number, this operation does nothing.
- count/number/age: This is an output command which causes the Delegate to output the number or numeric value of a variable at his rung to the standard output stream as a number. If the Delegate's item is not a number or variable containing a number, this operation does nothing.
Elemental control
- create/produce/build: This causes the Delegate to change the elemental quality of the number or numeric-valued variable at his rung to the quality of the element that its current quality creates.
- destroy/damage/kill: This causes the Delegate to change the elemental quality of the number or numeric-valued variable at his rung to the quality of the element that its current quality destroys.
- fear/hate/doubt: This causes the Delegate to change the elemental quality of the number or numeric-valued variable at his rung to the quality of the element that its current quality fears.
- love/desire/regard: This causes the Delegate to change the elemental quality of the number or numeric-valued variable at his rung to the quality of the element that its current quality loves.
Numeric control
- become/reach/achieve: This causes the Delegate to change the value of any variable or number literal in his rung of the Bureaucracy to become the number it strives to be. This will round a floating point value to the next integer further away from zero, increment a positive integer, and decrement a negative integer. Because a zero value strives for nothing, it becomes enlightened and the variable or literal is transformed to the word "nirvana". This operation also changes the elemental quality of a variable to the element created by its current quality, but does not assign an elemental quality to a literal. If the word to be operated on is not a variable or number literal, this operation has no effect.
- like/as/is/resemble: This causes the variable in the rung immediately below the Celestial Bureaucrat to take the value of the first variable or number encountered by the Delegate as the Delegate searches from his current rung downwards. The Delegate then moves to the location of the variable or number found. If no variable or number is found, the Delegate remains at his current rung and assigns the value of zero to the variable.
- tomorrow/today/yesterday is left blank in the source, likely by accident
- negative/not/deny: This causes the Delegate to change the sign of the number or numeric-valued variable at his rung.
- operate/examine/study: This causes the Delegate to perform a mathematical operation on the values of the two numbers or variables in his rung and the one just above it. The operation is defined as follows, where A is the value one rung above the Delegate, and B is the value of the Delegate's item:
Elemental relationship | Operation |
---|---|
B creates A | A+B |
B destroys A | A-B |
B fears A | A/B |
B loves A | A*B |
B is the same element as A | Yang if both A and B are Yang, Yin otherwise |
Yin and Yang results are integers of the appropriate type, but the exact values are implementation-dependent. The result replaces the value in the Delegate's rung, either as the value of the variable there, otherwise as a literal. The elemental quality of the item remain unchanged. If either operand has no numeric value, the operation is ignored.
Comments
Comments may be inserted between any words in the code, and are delimited by matching pairs of commas. Words in comments count towards syllables on each line. A comment at the end of a program, after which there is no more code, does not need a terminating comma delimiter. The words of a comment and the commas are not placed into the Celestial Bureaucracy.
As in all good programming practice, comments are used to elucidate sections of code, provide extra syllables for syntactic integrity, and make poignant observations on the nature of life and the ephemeral qualities of beauty. Comments may span haiku, and may be several haiku long if the programmer has something particularly insightful to say about the code or the universe in general.