unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
@ 2013-12-24  9:13 Tassilo Horn
  2013-12-24  9:37 ` Tassilo Horn
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Tassilo Horn @ 2013-12-24  9:13 UTC (permalink / raw)
  To: 16238


I'm just reading through the `pcase' docs in the info manual.  The
simple expression language example has a small bug.  The `env' parameter
is missing at the location marked below:

--8<---------------cut here---------------start------------->8---
(defun evaluate (exp env)
  (pcase exp
    (`(add ,x ,y)         (+ (evaluate x env) (evaluate y env)))
    (`(call ,fun ,arg)    (funcall (evaluate fun) (evaluate arg env)))
                                                `--- HERE!
    (`(fn ,arg ,body)     (lambda (val)
                            (evaluate body (cons (cons arg val) env))))
    ((pred numberp)       exp)
    ((pred symbolp)       (cdr (assq exp env)))
    (_                    (error "Unknown expression %S" exp))))
--8<---------------cut here---------------end--------------->8---

But even then, I can't get `fn' forms working:

ELISP> (evaluate '(fn x (add 1 x)) nil)
(lambda
  (val)
  (evaluate body
            (cons
             (cons arg val)
             env)))

But shouldn't `arg' be substituted with 'x and `body' with '(add 1 x)?
Now when I call the function, I get a void-variable error whereas I
should get 3:

ELISP> (evaluate '(call (fn x (add 1 x)) 2) nil)
*** Eval error ***  Symbol's value as variable is void: body

Strange enough, the `call' pattern which uses the same pattern syntax
seems to work, though:

ELISP> (evaluate '(call foo 2) '((foo . (lambda (x) (+ 1 x)))))
3 (#o3, #x3, ?\C-c)

In any case, it would be good if the docs would contain a sample program
defined in the simple expression language.  Getting the syntax right
just from the patterns is not enough.  For example, it's not obvious
that `fn' forms take only one symbol as `arg' whereas you'd usually
expect an argument list (which might be restricted to length 1).



In GNU Emacs 24.3.50.2 (x86_64-pc-linux-gnu, GTK+ Version 3.8.7)
 of 2013-12-22 on thinkpad
Bzr revision: eliz@gnu.org-20131222183138-zlzn9zzpz1hwd110
Windowing system distributor `The X.Org Foundation', version 11.0.11499904
System Description:     NAME=Gentoo

Configured using:
 `configure --prefix=/usr --build=x86_64-pc-linux-gnu
 --host=x86_64-pc-linux-gnu --mandir=/usr/share/man
 --infodir=/usr/share/info --datadir=/usr/share --sysconfdir=/etc
 --localstatedir=/var/lib --libdir=/usr/lib64 --disable-silent-rules
 --disable-dependency-tracking --program-suffix=-emacs-24-vcs
 --infodir=/usr/share/info/emacs-24-vcs
 --enable-locallisppath=/etc/emacs:/usr/share/emacs/site-lisp
 --with-gameuser=games --without-compress-info
 --with-file-notification=gfile --disable-acl --with-dbus --with-gnutls
 --with-gpm --without-hesiod --without-kerberos --without-kerberos5
 --with-xml2 --without-selinux --without-wide-int --with-zlib
 --with-sound=alsa --with-x --without-ns --without-gconf
 --with-gsettings --with-toolkit-scroll-bars --with-gif --with-jpeg
 --with-png --with-rsvg --with-tiff --with-xpm --with-imagemagick
 --with-xft --with-libotf --with-m17n-flt --with-x-toolkit=gtk3
 GENTOO_PACKAGE=app-editors/emacs-vcs-24.3.9999 EBZR_BRANCH=trunk
 EBZR_REVNO=115696 'CFLAGS=-march=native -pipe -g3 -ggdb' CPPFLAGS=
 'LDFLAGS=-Wl,-O1 -Wl,--as-needed''

Important settings:
  value of $LC_COLLATE: C
  value of $LC_MONETARY: de_DE.utf8
  value of $LC_NUMERIC: de_DE.utf8
  value of $LC_TIME: de_DE.utf8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  helm-match-plugin-mode: t
  helm-occur-match-plugin-mode: t
  highlight-parentheses-mode: t
  global-company-mode: t
  company-mode: t
  eldoc-mode: t
  shell-dirtrack-mode: t
  paredit-mode: t
  highlight-symbol-mode: t
  global-edit-server-edit-mode: t
  outline-minor-mode: t
  recentf-mode: t
  global-subword-mode: t
  subword-mode: t
  savehist-mode: t
  show-paren-mode: t
  icomplete-mode: t
  minibuffer-depth-indicate-mode: t
  tooltip-mode: t
  electric-pair-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
  2013-12-24  9:13 bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong Tassilo Horn
@ 2013-12-24  9:37 ` Tassilo Horn
  2013-12-24  9:55 ` Michael Heerdegen
  2013-12-24 14:53 ` Stefan Monnier
  2 siblings, 0 replies; 8+ messages in thread
From: Tassilo Horn @ 2013-12-24  9:37 UTC (permalink / raw)
  To: 16238

Tassilo Horn <tsdh@gnu.org> writes:

> ELISP> (evaluate '(fn x (add 1 x)) nil)
> (lambda
>   (val)
>   (evaluate body
>             (cons
>              (cons arg val)
>              env)))
>
> But shouldn't `arg' be substituted with 'x and `body' with '(add 1 x)?

It seems the `lambda' is the problem that prevents substitution of arg
and body.  By doing some kinda strange quoting I can get it right, but
IMHO that shouldn't be needed:

--8<---------------cut here---------------start------------->8---
(defun evaluate (exp env)
  (pcase exp
    (`(add ,x ,y)         (+ (evaluate x env) (evaluate y env)))
    (`(call ,fun ,arg)    (funcall (evaluate fun env) (evaluate arg env)))
    (`(fn ,arg ,body)     `(lambda (val)
			     (evaluate ',body (cons (cons ',arg val) env))))
    ((pred numberp)       exp)
    ((pred symbolp)       (cdr (assq exp env)))
    (_                    (error "Unknown expression %S" exp))))
--8<---------------cut here---------------end--------------->8---

ELISP> (evaluate '(fn x (add 1 x)) nil)
(lambda
  (val)
  (evaluate
   '(add 1 x)
   (cons
    (cons 'x val)
    env)))

ELISP> (evaluate '(call (fn x (add 1 x)) 3) nil)
4 (#o4, #x4, ?\C-d)

Bye,
Tassilo





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
  2013-12-24  9:13 bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong Tassilo Horn
  2013-12-24  9:37 ` Tassilo Horn
@ 2013-12-24  9:55 ` Michael Heerdegen
  2013-12-24 14:53 ` Stefan Monnier
  2 siblings, 0 replies; 8+ messages in thread
From: Michael Heerdegen @ 2013-12-24  9:55 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: 16238

Tassilo Horn <tsdh@gnu.org> writes:

> But shouldn't `arg' be substituted with 'x and `body' with '(add 1 x)?
> Now when I call the function, I get a void-variable error whereas I
> should get 3:
>
> ELISP> (evaluate '(call (fn x (add 1 x)) 2) nil)
> *** Eval error ***  Symbol's value as variable is void: body

I think the example is designed for lexical-binding.  That should be
mentioned.  It is not so nice anyway, since people will likely C-x C-e
the example and then it won't work.

Michael.





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
  2013-12-24  9:13 bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong Tassilo Horn
  2013-12-24  9:37 ` Tassilo Horn
  2013-12-24  9:55 ` Michael Heerdegen
@ 2013-12-24 14:53 ` Stefan Monnier
  2013-12-24 15:34   ` Tassilo Horn
  2 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2013-12-24 14:53 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: 16238

> (defun evaluate (exp env)
>   (pcase exp
>     (`(add ,x ,y)         (+ (evaluate x env) (evaluate y env)))
>     (`(call ,fun ,arg)    (funcall (evaluate fun) (evaluate arg env)))
>                                                 `--- HERE!

Indeed, feel free to fix it.

> But shouldn't `arg' be substituted with 'x and `body' with '(add 1 x)?

No, the code is meant for lexical-binding.


        Stefan





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
  2013-12-24 14:53 ` Stefan Monnier
@ 2013-12-24 15:34   ` Tassilo Horn
  2013-12-24 20:13     ` Glenn Morris
  0 siblings, 1 reply; 8+ messages in thread
From: Tassilo Horn @ 2013-12-24 15:34 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Michael Heerdegen, 16238

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

>> (defun evaluate (exp env)
>>   (pcase exp
>>     (`(add ,x ,y)         (+ (evaluate x env) (evaluate y env)))
>>     (`(call ,fun ,arg)    (funcall (evaluate fun) (evaluate arg env)))
>>                                                 `--- HERE!
>
> Indeed, feel free to fix it.

Done.

>> But shouldn't `arg' be substituted with 'x and `body' with '(add 1
>> x)?
>
> No, the code is meant for lexical-binding.

Ok, I see.  I've also mentioned that in the docs, though I guess you can
refine that comment to be more specific.

Bye,
Tassilo





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
  2013-12-24 15:34   ` Tassilo Horn
@ 2013-12-24 20:13     ` Glenn Morris
  2013-12-25  9:15       ` Tassilo Horn
  0 siblings, 1 reply; 8+ messages in thread
From: Glenn Morris @ 2013-12-24 20:13 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: Michael Heerdegen, 16238

Tassilo Horn wrote:

>> No, the code is meant for lexical-binding.
>
> Ok, I see.  I've also mentioned that in the docs, though I guess you can
> refine that comment to be more specific.

That _example_ assumes lexical binding. _Pcase itself_ does not require
lexical binding. (You've written in the manual that it does.)





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
  2013-12-24 20:13     ` Glenn Morris
@ 2013-12-25  9:15       ` Tassilo Horn
  2013-12-25 20:04         ` Tassilo Horn
  0 siblings, 1 reply; 8+ messages in thread
From: Tassilo Horn @ 2013-12-25  9:15 UTC (permalink / raw)
  To: Glenn Morris; +Cc: Michael Heerdegen, 16238

Glenn Morris <rgm@gnu.org> writes:

>>> No, the code is meant for lexical-binding.
>>
>> Ok, I see.  I've also mentioned that in the docs, though I guess you
>> can refine that comment to be more specific.
>
> That _example_ assumes lexical binding. _Pcase itself_ does not
> require lexical binding. (You've written in the manual that it does.)

Ah, yes.  I'm so used to lexical binding that I can't see a closure when
it's immediately in front of me.  I've corrected the sentence and moved
it directly after the `evaluate' code.

Bye,
Tassilo





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong
  2013-12-25  9:15       ` Tassilo Horn
@ 2013-12-25 20:04         ` Tassilo Horn
  0 siblings, 0 replies; 8+ messages in thread
From: Tassilo Horn @ 2013-12-25 20:04 UTC (permalink / raw)
  To: 16238-done

Closing.





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-12-25 20:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-24  9:13 bug#16238: 24.3.50; pcase docs (and possibly pcase) wrong Tassilo Horn
2013-12-24  9:37 ` Tassilo Horn
2013-12-24  9:55 ` Michael Heerdegen
2013-12-24 14:53 ` Stefan Monnier
2013-12-24 15:34   ` Tassilo Horn
2013-12-24 20:13     ` Glenn Morris
2013-12-25  9:15       ` Tassilo Horn
2013-12-25 20:04         ` Tassilo Horn

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).