Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Never make any mistaeks. -- Anonymous, in a mail discussion about to a kernel bug report


devel / comp.lang.lisp / IMMEDIATE and def_macro

SubjectAuthor
* IMMEDIATE and def_macronone
`* Re: IMMEDIATE and def_macroKaz Kylheku
 `- Re: IMMEDIATE and def_macronone

1
IMMEDIATE and def_macro

<nnd$274ec76a$1e14a220@7c962486245f4d15>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17429&group=comp.lang.lisp#17429

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: IMMEDIATE and def_macro
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$274ec76a$1e14a220@7c962486245f4d15>
Organization: KPN B.V.
Date: Fri, 07 Jul 2023 14:15:27 +0200
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe006.abavia.com!abp003.abavia.com!news.kpn.nl!not-for-mail
Lines: 45
Injection-Date: Fri, 07 Jul 2023 14:15:27 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 2521
 by: none - Fri, 7 Jul 2023 12:15 UTC

I'm trying to implement mal on ciforth
https://github.com/kanaka/mal
Amounts to implementing a dialect of lisp using a dialect of Forth.

In step 8 I encounter the following:

-Add a new attribute is_macro to mal function types. This should
default to false.

Am I mistaken or is this the familiar immediate flag added to
Forth definitions?

- Add a new special form defmacro!. This is very similar to the def!
form, but before the evaluated value (mal function) is set in the
environment, the is_macro attribute should be set to true.

Much like colon definitions, later to be modified by IMMEDIATE to add
that behaviour.

- Add a macroexpand function: This function takes arguments ast and env.
It calls is_macro_call with ast and env and loops while that condition
is true. Inside the loop, the first element of the ast list (a
symbol), is looked up in the environment to get the macro function.
This macro function is then called/applied with the rest of the ast
elements (2nd through the last) as arguments. The return value of the
macro call becomes the new value of ast. When the loop completes
because ast no longer represents a macro call, the current value of
ast is returned.

How is this different from executing an immediate definition in
the middle of deferring an action? The only difference seems
to be that Forth does this much cleaner and with less caveats.
First and foremost it is far easier to keep track of arguments.
They reside on the stack.

Am I the first to notice this? Chuck Moore was a student of McCarthy.
Perhaps he invented Forth as a simpler version of lisp.

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: IMMEDIATE and def_macro

<20230707102938.23@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17430&group=comp.lang.lisp#17430

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: IMMEDIATE and def_macro
Date: Fri, 7 Jul 2023 17:57:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 99
Message-ID: <20230707102938.23@kylheku.com>
References: <nnd$274ec76a$1e14a220@7c962486245f4d15>
Injection-Date: Fri, 7 Jul 2023 17:57:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f779c27224260df751517bdd9ccf5abe";
logging-data="1483085"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19x7gwo5scIU0El3ZYROmti0XsLzezCU+w="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:8Nlm8JEem3PpK63mw38uuj2xz5M=
 by: Kaz Kylheku - Fri, 7 Jul 2023 17:57 UTC

On 2023-07-07, albert@cherry.(none) (albert) <albert@cherry> wrote:
> I'm trying to implement mal on ciforth
> https://github.com/kanaka/mal
> Amounts to implementing a dialect of lisp using a dialect of Forth.
>
> In step 8 I encounter the following:
>
> -Add a new attribute is_macro to mal function types. This should
> default to false.
>
> Am I mistaken or is this the familiar immediate flag added to
> Forth definitions?

It may be similar. Macros are invoked during expansion.
Code can be expanded without being executed immediately. Or
at all. For instance

;; function is only defined, not called; but macro is called
(defun foo ()
(macro ..))

;; macro likely called in spite of being dead code.
;; (unless expander handles trivial dead code elimination cases).
(if nil
(macro ...))

> - Add a new special form defmacro!. This is very similar to the def!
> form, but before the evaluated value (mal function) is set in the
> environment, the is_macro attribute should be set to true.
>
> Much like colon definitions, later to be modified by IMMEDIATE to add
> that behaviour.

There amy be some similarities, but that doesn't necessarily mean
you can solve the MAL task by mapping macros directly to IMMEDIATE
words.
>
> - Add a macroexpand function: This function takes arguments ast and env.
> It calls is_macro_call with ast and env and loops while that condition
> is true.
>
> How is this different from executing an immediate definition in
> the middle of deferring an action? The only difference seems
> to be that Forth does this much cleaner and with less caveats.
> First and foremost it is far easier to keep track of arguments.
> They reside on the stack.

The user of the macro system doesn't have any problem with argument
tracking; arguments nicely appear as lexically scoped identifiers,
bound on entry into the function.

If you're writing code in Forth to make that work, then of course
that is your problem.

If you're making a Forth from scratch, you have to implement
several stacks yourself.

If you're making a Lisp from scratch, you have to implement
environments.

The simplest possible implementation of environments is stack-like.
The environment is the head of an association list, which looks like
this: ((b . 1) (a . 2)) for an environment which contains two
variables a and b bound to values 1 and 2.

A new binding is created with cons:

(cons (cons 'c 3) previous-env)

In some Lisps like Common Lisp, there is an acons for this:

(acons 'c 3 previous-env)

a new env is returned which looks like ((c . 3) (b . 1) (a . 2)).

The new environment doesn't clobber the old one; the environment
is a local variable in a recursive interpreter, passed around through
its recursion.

That's a reference model for a lexically scoped Lisp that came
into the Lisp culture mainly via the Scheme influence.

A lexically scoped Lisp doesn't have to reveal to the programs
the detailed representation of environments. Unless a special
escape hatch is provided for it, programs don't see the hidden
"env" variable. This is a good thing because it allows programs
to be compiled. Compiled code uses a radically different
representation of the environment.

> Am I the first to notice this? Chuck Moore was a student of McCarthy.
> Perhaps he invented Forth as a simpler version of lisp.

Even assembly languages have actions that are done now, while
assembling the code, and not at run-time.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: IMMEDIATE and def_macro

<nnd$7e70b6b4$022046df@684c07a62c9b2601>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17431&group=comp.lang.lisp#17431

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$274ec76a$1e14a220@7c962486245f4d15> <20230707102938.23@kylheku.com>
Subject: Re: IMMEDIATE and def_macro
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$7e70b6b4$022046df@684c07a62c9b2601>
Organization: KPN B.V.
Date: Sat, 08 Jul 2023 10:05:06 +0200
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe005.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 88
Injection-Date: Sat, 08 Jul 2023 10:05:06 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 3891
 by: none - Sat, 8 Jul 2023 08:05 UTC

In article <20230707102938.23@kylheku.com>,
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>On 2023-07-07, albert@cherry.(none) (albert) <albert@cherry> wrote:
>> I'm trying to implement mal on ciforth
>> https://github.com/kanaka/mal
>> Amounts to implementing a dialect of lisp using a dialect of Forth.
>>
<SNIP>
(much appreciated insight)

>
>If you're writing code in Forth to make that work, then of course
>that is your problem.
>
>If you're making a Forth from scratch, you have to implement
>several stacks yourself.
>
>If you're making a Lisp from scratch, you have to implement
>environments.
>
>The simplest possible implementation of environments is stack-like.
>The environment is the head of an association list, which looks like
>this: ((b . 1) (a . 2)) for an environment which contains two
>variables a and b bound to values 1 and 2.

That is available in Forth under the name wordlist.
There is a structure name VOCABULARY that have names and
traditionally are linked among themselves, that can be used
to point to outer environments.

>
>A new binding is created with cons:
>
> (cons (cons 'c 3) previous-env)

In my forth this is implemented as
[``set'' and ``get'' is the name prescribed by mal.]
\ Add symbol value to the current.
: set >R $, R@ >NFA ! R> CURRENT @ LINK ;
Note that each of these components are available in the Forth
core.
Finding a name through a recursive environments is done by
\ For sc env-wid , return item . or throw. Follow outer links.
: get
BEGIN DUP >R (FIND) DUP 0= WHILE
DROP R> WID>VFA @ DUP 0= 8010 ?ERROR
>WID REPEAT
NIP NIP RDROP ;
Likewise each of the components are present in the Forth core.
Not necessarily ISO standard words, but language components
needed to implement Forth that are repurposed.

>
>In some Lisps like Common Lisp, there is an acons for this:
>
> (acons 'c 3 previous-env)
>
>a new env is returned which looks like ((c . 3) (b . 1) (a . 2)).
>
>The new environment doesn't clobber the old one; the environment
>is a local variable in a recursive interpreter, passed around through
>its recursion.
>
>That's a reference model for a lexically scoped Lisp that came
>into the Lisp culture mainly via the Scheme influence.
>
>A lexically scoped Lisp doesn't have to reveal to the programs
>the detailed representation of environments. Unless a special
>escape hatch is provided for it, programs don't see the hidden
>"env" variable. This is a good thing because it allows programs
>to be compiled. Compiled code uses a radically different
>representation of the environment.

This recursive fibonacci function works
(def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 \
(+ (fib (- N 1)) (fib (- N 2)))))))

So the idea that Forth dictionary entries can serve as lisp "objects"
and that wordlists can serve as hashes has worked so far.

>Kas
Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor