This question is really moot, I think I must have hit a bug in my program or something. If you are still looking for PicoLisp and onOff behaviour, look here.
is this supposed to happen?
: (show NIL)
NIL NIL
-> NIL
: (onOff)
-> T
: (show NIL)
T T
-> T
: (=T NIL)
-> T
:
(onOff sym ..) -> flg
Logical negates the VAL's of all argument symbols sym. Returns the
new value of the last symbol.
Should not the symbol names be passed explicitly?
Why does it return value of the last symbol?
It's a bug in the PicoLisp implementation with the onOff macro.
The parameters of the onOff function are: (onOff var ..) -> flg
It takes a var and many more variables and logically negates them (true becomes false, false becomes true). I'm willing to bet that the onOff macro takes a single list of arguments. Which means that if NO arguments are given to it, that list of arguments is empty which means the symbol given to the function is NIL.
Macro is bolded because that's where the problem lies. Using a macro makes it so you can pass the symbols to it without quoting. So the onOff macro is generating incorrect code.
This was a red herring, there was no bug. Sorry, HN wrong call. Also, it is now under the MIT (X11) license, the most liberal of open source licenses.
Related
I'm learning Ltac2 and reading the official documentation of coq 8.13.2.
I do not get what role Control.refine play in the evaluation inside of quotations, as there doesn't seem to be a lot of explanations to this function.
For example, using variables in tactic expressions inside a constr quotation should be done by: constr:(... $x ...), where $x is the syntactic sugar of ltac2:(Control.refine (fun () => x)).
Why does, say simply, ltac2:(x) not work? (and it indeed doesn't, as coq gives an error of Error: Cannot infer an existential variable of type ..., at the position where the constr represented by x should be inserted).
So my questions are:
What does Control.refine do in general?
It seems to be an idiom to sometimes do this: constr:( ... ltac2:(Control.refine (fun () => ...) ...), in what situation where such anti-quotation idiom should (or shouldn't) be used?
Thanks.
The ltac2:(...) in-term quotation expects an Ltac2 term of type unit, not of type constr. Actually, your example should trigger a warning because of this.
Since the content of the quotation has type unit, it means that it can only act through side-effects. The warning tells you that the value returned by the quotation will be discarded. In particular, it cannot be used to fill the hole.
Rather, the semantics of the quotation is to evaluate its content inside a goal Γ ⊢ A where Γ is the current list of hypotheses, and A the inferred type of the hole. It is expected that doing this will solve the goal, and the resulting partial proof will be used as the Coq term filler for that hole. This is precisely the rôle of Control.refine : (unit -> constr) -> unit. It takes a (for technical reasons, thunked) term as an argument and uses it to solve the goal(s) under focus.
What happens in your example is that you provide a term that is just ignored, the goal is left untouched and the quotation rightfully complains that it was not solved.
Regarding your second point, I cannot really tell. It depends on what you want do do. But in general, if you stick to antiquotations $x I would consider this more readable. It is not always possible to do so, though, e.g. if the term being built depends on a context introduced in the constr:(...) quotation.
In Elisp, I've encountered different APIs for modeling boolean values.
I was under the impression that t and nil were the idiomatic ways of representing true and false respectively. However, I've also seen 1 and -1 used to model the same thing.
What confuses me is that I have come across APIs that won't work if nil is supplied but will work if -1 is used.
Can someone help me understand which is in fact the preferred way. And if the answer is t and nil, I welcome any theories on why some developers use 1 and -1 for their APIs...
sds and sepp2k have covered the main misconception, but in answer to:
why some developers use 1 and -1 for their APIs...
The reason that Emacs minor modes use positive and negative numbers (or rather non-positive numbers, including zero) to mean "enable" and "disable", rather than using t and nil, is that the argument is optional, and when an optional argument is not supplied its value will be nil. Consequently it would not be possible to distinguish between passing an argument of nil explicitly, and not passing an argument at all.
Historically, passing no argument (i.e. an argument of nil) meant that the mode would be toggled.
These days the mode is only toggled when calling it interactively, and an argument of nil means "enable". This change was made so that the likes of (add-hook 'prog-mode-hook 'some-minor-mode) -- which will result in some-minor-mode being called with no arguments -- is guaranteed to enable that mode.
Lisp
In lisp (both Emacs Lisp and Common Lisp) the only false value is nil.
Everything else is true, including 0, -1 &c &c.
Emacs
Emacs uses negative arguments (including -1) to indicate "turn off this mode". E.g., C-h f toggle-truncate-lines
With prefix argument ARG, truncate long lines if ARG is positive,
otherwise fold them.
I'm curious about the return value of define in Scheme. So I wrote the following lines in Racket
#lang r5rs
(display (define a 3))
And get the error
define: not allowed in an expression context in: (define a 3)
I have 2 questions about this:
Does it mean that define has no return value?
According to R5RS, define is not an expression. It's a program structure. Is it true that only expressions have return values, and other forms don't?
"If a tree falls in a forest and no one is around to hear it, does it make a sound?"
It's not valid to use define in any context where a return value could meaningfully be obtained. So it's moot whether it has a return value or not; you'll never be able to observe it.
In Scheme, define can only be used in two places:
At the top level, or
At the very beginning of a "body".
In neither of those places is a "return value" relevant.
This will print data, but I want it to print show. I want to print the value, not the expression, how would I do it?
(defun display (x)
(list x))
(setq temp 'data)
(set temp 'show)
(display 'data)
what if u dont know if the variable is bound or not? i have to write a function that take a key and a value, if key does not exist, then i have to do setq key value, if the key already exist, then i would add the value to the key. In this case, if i do (storedata key value), if value have not been bounded, i get an unbound error, how would i handle this case?
for example, if there is no mydata and i do (storedata value mydata) then mydata would become (value), now if i do (storedata value2 mydata) then mydata becomes (value value2).
Quoting a list or a symbol in Lisp with ' is exactly equivalent to using the special form (quote ...). It's specifically for making the quoted thing not get evaluated. 'data in Lisp code or typed into the REPL is the same thing as (quote data), and evaluates to the symbol data.
data without the quote evaluates to the value of the variable data in the current scope. So, at the REPL:
[1]> (setq data 14)
14
[2]> data
14
The first expression also evaluates to 14 because setq returns the value of the bound variable (in this respect acting like the assignment operator = in C).
What you've done in the above code is to set the variable named temp to contain the symbol data, then, by using set (without the setq), set the variable named data to the symbol show. This is a bit similar to using soft references in Perl (for example), but I don't think it's particularly widely used or advisable as a Lisp technique.
By the way, your display procedure is probably not doing what you think either: it returns a single element list of whatever you pass to it. The fact that the value gets printed when you type it into the REPL is just because the value of any expression gets printed at the REPL. To display a value in a program you might use print or maybe format. (I'm assuming you're using Common Lisp, since it's obviously not Scheme, but maybe it's some other Lisp variety, in which case that link won't help.)
You are quoting data. If you want it to be evaluated you should just call
(display data)
I'm new to Picolisp.
I tried this, and obtained a segfault:
: ('(1 2) 6)
Segmentation fault
But, if i try:
: ('(a b c) 6)
-> NIL
I mostly understand why, but it was a surprise that PicoLisp responded with a segfault instead of an error. Does this means that Picolisp doesn't check if a number is a function but it does when it is a symbol?
(Lifted from picolisp mailing list.)
Yes, this is the expected behavior.
PicoLisp evaluates the CAR of a list, perhaps repeatedly, until it hits
upon a function. A function is either a list (then it is a Lisp-level
function) or a short number (then it is a built-in function, written in
either asm or C). If that number does not point to executable code
(which is difficult to check at runtime), a crash happens.
I would consider such a crash an "extended error message": Why not let
the hardware (MMU) do the runtime check?
In general, it is not possible to have the interpreter catch any
possible error (think of infinite loops, for example), so PicoLisp takes
the stand of giving some responsibility to the programmer.
In practice, an error like the above one will be detected at the first
test run of your program.
BTW, an exception to the above rule is only a list that directly has a
number in its CAR. Such a list auto-evaluates:
: (1 2 3)
-> (1 2 3)
Is just a convenient feature, not having to quote such constant lists.
: ('(a b c) 6)
-> NIL
I mostly understand why, but it was a
surprise that Picolisp responded with
a segfault instead of an error. Does
this means that Picolisp doesn't check
if a number is a function but it does
when it is a symbol?
In that case, (a b c) is in fact a legal function definition: It is a
function with a single symbolic parameter 'a' (so that function does not
evaluate its arguments), and a body of two symbols. This is equivalent
to
: (de foo a
b
c )
-> foo
When this function is executed, it binds the argument list (3) to that
symbol 'a', then executes 'b' and 'c'. This function returns the value
of 'c', which was NIL in your example.
When you do:
: (de foo H H)
: (foo 1 2 3)
-> (1 2 3)
: foo
-> (H H)
so you can also do:
: ('(H H) 1 2 3)
-> (1 2 3)
Correct.
I bet you know what happens here; you
are trying to use a number as a
variable, which is illegal -> crash
(besides it does not make sense
anyway)
It is correct what you said. The interpreter hits upon '1' in the place
of an expected function parameter.
: (setq 7 5)
!? (setq 7 5)
7 -- Variable expected
?