Skip to content

Commit

Permalink
Replace true with T and false with F
Browse files Browse the repository at this point in the history
Per discussion in hylang#908.

True and False are still allowed. Allowing users to use these as variable names is probably more trouble than it's worth, and I figure this helps to smooth over human translation from Python code to Hy. It might be a good idea to use T and F consistently in Hy's own code, not to mention the documentation. (This preference could be stated in the style guide.)

I added a bit to the documentation about the correspondences between True, False, None, and their synonyms because this didn't seem to be documented at all.
  • Loading branch information
Kodiologist committed Nov 3, 2016
1 parent f60ed24 commit 868e782
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 112 deletions.
11 changes: 9 additions & 2 deletions docs/language/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ languages.
Notes on Syntax
===============

logical values
--------------

``True``, ``False``, and ``None`` have the same meaning as in Python.
In addition, ``True`` may be written as ``T``, ``False`` as ``F``, and
``None`` as ``nil``.

integers
--------

Expand Down Expand Up @@ -392,7 +399,7 @@ Some example usage:

.. code-block:: clj
=> (if true
=> (if T
... (do (print "Side effects rock!")
... (print "Yeah, really!")))
Side effects rock!
Expand Down Expand Up @@ -589,7 +596,7 @@ Parameters may have the following keywords in front of them:
=> (apply compare ["lisp" "python"]
... {"keyfn" (fn [x y]
... (reduce - (map (fn [s] (ord (first s))) [x y])))
... "reverse" true})
... "reverse" T})
4
.. code-block:: python
Expand Down
8 changes: 4 additions & 4 deletions docs/language/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ is ``True``, the function prints Python code instead.
body=[
Expr(value=Call(func=Name(id='print'), args=[Str(s='Hello World!')], keywords=[], starargs=None, kwargs=None))])
=> (disassemble '(print "Hello World!") true)
=> (disassemble '(print "Hello World!") T)
print('Hello World!')
Expand Down Expand Up @@ -898,12 +898,12 @@ as an example of how to use some of these functions.
(defn fib []
(setv a 0)
(setv b 1)
(while true
(while T
(yield a)
(setv (, a b) (, b (+ a b)))))
Note the ``(while true ...)`` loop. If we run this in the REPL,
Note the ``(while T ...)`` loop. If we run this in the REPL,

.. code-block:: hy
Expand Down Expand Up @@ -1140,7 +1140,7 @@ if *from-file* ends before a complete expression can be parsed.
=> ; (print "hyfriends!")
=> (with [f (open "example.hy")]
... (try
... (while true
... (while T
... (let [exp (read f)]
... (do
... (print "OHY" exp)
Expand Down
6 changes: 3 additions & 3 deletions docs/language/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Second Stage Expression-Dispatch

The only special case is the ``HyExpression``, since we need to create different
AST depending on the special form in question. For instance, when we hit an
``(if true true false)``, we need to generate a ``ast.If``, and properly
``(if T T F)``, we need to generate a ``ast.If``, and properly
compile the sub-nodes. This is where the ``@builds()`` with a String as an
argument comes in.

Expand Down Expand Up @@ -321,7 +321,7 @@ In Python, doing something like:
features, such as ``if``, ``for``, or ``while`` are statements.

Since they have no "value" to Python, this makes working in Hy hard, since
doing something like ``(print (if true true false))`` is not just common, it's
doing something like ``(print (if T T F))`` is not just common, it's
expected.

As a result, we auto-mangle things using a ``Result`` object, where we offer
Expand All @@ -331,7 +331,7 @@ assignment to things while running.

As example, the Hy::

(print (if true true false))
(print (if T T F))

Will turn into::

Expand Down
4 changes: 2 additions & 2 deletions docs/style-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ Coding Style
(def *limit* 400000)
(defn fibs [a b]
(while true
(while T
(yield a)
(setv (, a b) (, b (+ a b)))))
;; Bad (and not preferred)
(defn fibs [a b]
(while true
(while T
(yield a)
(def (, a b) (, b (+ a b)))))
Expand Down
7 changes: 4 additions & 3 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,16 @@ In Hy, you would do:
(print "That variable is too big!")]
[(< somevar 10)
(print "That variable is too small!")]
[true
[T
(print "That variable is jussssst right!")])
What you'll notice is that ``cond`` switches off between a statement
that is executed and checked conditionally for true or falseness, and
then a bit of code to execute if it turns out to be true. You'll also
notice that the ``else`` is implemented at the end simply by checking
for ``true`` -- that's because ``true`` will always be true, so if we get
this far, we'll always run that one!
for ``T`` -- that's because ``T`` will always be true, so if we get
this far, we'll always run that one! (``T`` is a synonym for ``True``,
``F`` for ``False``, and ``nil`` for ``None``.)

You might notice above that if you have code like:

Expand Down
2 changes: 1 addition & 1 deletion hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def load_stdlib():
# keywords in Python 3.*
def _is_hy_builtin(name, module_name):
extras = ['True', 'False', 'None',
'true', 'false', 'nil']
'T', 'F', 'nil']
if name in extras or keyword.iskeyword(name):
return True
# for non-Hy modules, check for pre-existing name in
Expand Down
2 changes: 1 addition & 1 deletion hy/contrib/walk.hy
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
((type form) (outer (HyExpression (map inner form))))]
[(coll? form)
(walk inner outer (list form))]
[true (outer form)]))
[T (outer form)]))

(defn postwalk [f form]
"Performs depth-first, post-order traversal of form. Calls f on each
Expand Down
12 changes: 6 additions & 6 deletions hy/core/language.hy
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"Decrement n by 1"
(- n 1))

(defn disassemble [tree &optional [codegen false]]
(defn disassemble [tree &optional [codegen F]]
"Return the python AST for a quoted Hy tree as a string.
If the second argument is true, generate python code instead."
(import astor)
Expand Down Expand Up @@ -250,8 +250,8 @@
"Return True if char `x` parses as an integer"
(try
(integer? (int x))
(except [e ValueError] False)
(except [e TypeError] False)))
(except [ValueError] F)
(except [TypeError] F)))

(defn interleave [&rest seqs]
"Return an iterable of the first item in each of seqs, then the second etc."
Expand All @@ -267,7 +267,7 @@

(defn iterate [f x]
(setv val x)
(while true
(while T
(yield val)
(setv val (f val))))

Expand Down Expand Up @@ -363,7 +363,7 @@

(defn repeatedly [func]
"Yield result of running func repeatedly"
(while true
(while T
(yield (func))))

(defn second [coll]
Expand Down Expand Up @@ -412,7 +412,7 @@
"Read from input and returns a tokenized string.
Can take a given input buffer to read from"
(def buff "")
(while true
(while T
(def inn (str (.readline from-file)))
(if (= inn eof)
(raise (EOFError "Reached end of file" )))
Expand Down
4 changes: 2 additions & 2 deletions hy/core/macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
(macro-error None "`for' requires a body to evaluate")]
[(empty? args) `(do ~@body ~@belse)]
[(= (len args) 2) `(for* [~@args] (do ~@body) ~@belse)]
[true
[T
(let [alist (cut args 0 nil 2)]
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] (do ~@body) ~@belse))]))

Expand Down Expand Up @@ -228,7 +228,7 @@
(setv ~g!iter (iter ~expr))
(setv ~g!return nil)
(setv ~g!message nil)
(while true
(while T
(try (if (isinstance ~g!iter types.GeneratorType)
(setv ~g!message (yield (.send ~g!iter ~g!message)))
(setv ~g!message (yield (next ~g!iter))))
Expand Down
4 changes: 2 additions & 2 deletions hy/lex/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ def t_identifier(p):
pass

table = {
"true": "True",
"false": "False",
"T": "True",
"F": "False",
"nil": "None",
}

Expand Down
2 changes: 1 addition & 1 deletion tests/compilers/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_ast_invalid_unary_op():
def test_ast_bad_while():
"Make sure AST can't compile invalid while"
cant_compile("(while)")
cant_compile("(while (true))")
cant_compile("(while (T))")


def test_ast_good_do():
Expand Down
10 changes: 5 additions & 5 deletions tests/native_tests/contrib/anaphoric.hy
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@

(defn test-ap-if []
"NATIVE: testing anaphoric if"
(ap-if true (assert-true it))
(ap-if false true (assert-false it))
(try (macroexpand '(ap-if true))
(except [HyMacroExpansionError] true)
(else (assert false))))
(ap-if T (assert-true it))
(ap-if F T (assert-false it))
(try (macroexpand '(ap-if T))
(except [HyMacroExpansionError] T)
(else (assert F))))

(defn test-ap-each []
"NATIVE: testing anaphoric each"
Expand Down
10 changes: 5 additions & 5 deletions tests/native_tests/contrib/loop.hy
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
(try
(setv n (non-tco-sum 100 10000))
(except [e RuntimeError]
(assert true))
(assert T))
(else
(assert false)))
(assert F)))

;; tco-sum should not fail
(try
(setv n (tco-sum 100 10000))
(except [e RuntimeError]
(assert false))
(assert F))
(else
(assert (= n 10100)))))

Expand All @@ -41,6 +41,6 @@
(try
(bad-recur 3)
(except [e TypeError]
(assert true))
(assert T))
(else
(assert false))))
(assert F))))
4 changes: 2 additions & 2 deletions tests/native_tests/core.hy
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,10 @@
(setv res (list (take-nth 3 [1 2 3 None 5 6])))
(assert-equal res [1 None])
;; using 0 should raise ValueError
(let [passed false]
(let [passed F]
(try
(setv res (list (take-nth 0 [1 2 3 4 5 6 7])))
(except [ValueError] (setv passed true)))
(except [ValueError] (setv passed T)))
(assert passed)))

(defn test-take-while []
Expand Down
6 changes: 3 additions & 3 deletions tests/native_tests/defclass.hy
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@

(defn test-defclass-dynamic-inheritance []
"NATIVE: test defclass with dynamic inheritance"
(defclass A [((fn [] (if true list dict)))]
(defclass A [((fn [] (if T list dict)))]
[x 42])
(assert (isinstance (A) list))
(defclass A [((fn [] (if false list dict)))]
(defclass A [((fn [] (if F list dict)))]
[x 42])
(assert (isinstance (A) dict)))

Expand All @@ -58,7 +58,7 @@
(try
(do
(x)
(assert false))
(assert F))
(except [NameError])))

(defn test-defclass-docstring []
Expand Down
Loading

0 comments on commit 868e782

Please sign in to comment.