Re: Operator overloading?

Liste des Groupes 
Sujet : Re: Operator overloading?
De : anton (at) *nospam* mips.complang.tuwien.ac.at (Anton Ertl)
Groupes : comp.lang.forth
Date : 27. Jul 2024, 16:35:55
Autres entêtes
Organisation : Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID : <2024Jul27.173555@mips.complang.tuwien.ac.at>
References : 1 2 3 4 5
User-Agent : xrn 10.11
minforth@gmx.net (minforth) writes:
On Thu, 25 Jul 2024 12:08:58 +0000, Anton Ertl wrote:
I am thinking on a much smaller scale i.e. unification of
operators for xVALUEs and xLOCALs. ANS Forth already has
overloaded TO but stops there.
>
+TO is a common extension.
>
Certainly. I have some array operations in mind, but they
are not relevant here. The principle is like
>
: INIT { a }
 [[: a ;]] \ read counter
 [[: 1 +to a ;]] ; \ increment counter
DEFER count IS count
DEFER read IS read
5 INIT
COUNT COUNT READ -> should give 7

My guess is that three lines here should be

DEFER count
DEFER read
5 INIT IS count IS read

[[: ;]] define closures, but unlike gforth's more flexible
flat closures, they capture simply all upvalues (here local a).

Gforth's flag closures can be considered lower-level (they come out of
implementation ideas in the Scheme community) and are easier to
implement, but emulating closures that capture outer locals is more
cumbersome.  One other aspect is that in Gforth's closures the
programmer decides whether closures are allocated in the dictionary,
on the locals stack, on the heap, or elsewhere.  In the present case
the dictionary seems to be a good place, and one can write this
example as:

: init ( a -- )
  align here >r , \ allocate a on the heap
  r@ [n:d @ ;]         \ read counter
  r> [n:d 1 swap +! ;] \ increment counter
;
DEFER count
DEFER read
5 INIT IS count IS read
count count read . \ prints "7 "

INIT uses pure-stack closures [n:d consumes a value (in this exampl,
the address of the cell containing A) from the stack at closure
creation time and pushes it on the stack at closure run-time.  Look,
Ma, no locals:-).

However, this does not use value-flavoured stuff, because we have to
pass the address of A around, and then it's easier to use the
variable-flavoured words.  However, if you prefer the value-flavoured
words, value-flavoured fields were recently added to Gforth and can be
used to do that:

begin-structure counter
  value: counter-val
end-structure
: init ( a -- )
  align here >r counter allot
  r@ to counter-val
  r@ [n:d counter-val ;]
  r> [n:d 1 swap +to counter-val ;]
;
DEFER count
DEFER read
5 INIT IS count IS read
count count read . \ prints "7 "

However, you still have to deal with the address explicitly, which
becomes especially obvious in the counter closure.

This raises the question of why you want to use closures for this
task.  Why not use one of the object-oriented Forth packages, some of
which support value-flavoured fields (Mini-OOF2 among them AFAIK).

The code compiles unmodified for different types of a.
Incidentally, it compiles now with +TO, but realistically you
can't declare new 'op'TOs for too many different 'op' operators.

This statement can be read in two different ways.  I think there can
be too many, but Stephen Pelc may think otherwise:-).

In any case, if the reason you want value-flavoured stuff is that you
want to be able to change the types, then yes, value-flavoured fields
(in objects or plain structures) are the way to go.

One caveat that we have here is that TO (and, AFAIK in existing
implementations, +TO) have different stack effects for VALUEs,
2VALUEs, and FVALUEs, which means that you usually cannot change the
types between the types that deal with one cell on the stack (VALUE:
CVALUE: WVALUE: LVALUE: etc.) and those that take two cells or a float
value.

- anton
--
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: https://forth-standard.org/
   EuroForth 2024: https://euro.theforth.net

Date Sujet#  Auteur
25 Jul 24 * Operator overloading?78minforth
25 Jul 24 +- Re: Operator overloading?1dxf
25 Jul 24 +* Re: Operator overloading?18Michael Raitza
25 Jul 24 i`* Re: Operator overloading?17minforth
25 Jul 24 i +* Re: Operator overloading?5mhx
25 Jul 24 i i`* Re: Operator overloading?4minforth
26 Jul 24 i i `* Re: Operator overloading?3minforth
26 Jul 24 i i  `* Re: Operator overloading?2mhx
26 Jul 24 i i   `- Re: Operator overloading?1minforth
25 Jul 24 i `* Re: Operator overloading?11Anton Ertl
25 Jul 24 i  +* Re: Operator overloading?2minforth
30 Jul 24 i  i`- Re: Operator overloading?1Ruvim
27 Jul 24 i  `* Re: Operator overloading?8minforth
27 Jul 24 i   `* Re: Operator overloading?7Anton Ertl
27 Jul 24 i    +* Re: Operator overloading?4minforth
30 Jul 24 i    i+- quotations and closures (was: Operator overloading?)1Ruvim
1 Aug 24 i    i`* Re: Operator overloading?2Anton Ertl
1 Aug 24 i    i `- Re: Operator overloading?1minforth
27 Jul 24 i    `* Re: Operator overloading?2albert
29 Jul 24 i     `- Re: Operator overloading?1minforth
26 Jul 24 +- Re: Operator overloading?1Jan Coombs
30 Jul 24 `* Re: Operator overloading?57Stephen Pelc
30 Jul 24  +* Re: Operator overloading?3minforth
30 Jul 24  i+- Re: Operator overloading?1Stephen Pelc
1 Aug 24  i`- Re: Operator overloading?1Anton Ertl
31 Jul 24  `* Re: Operator overloading?53albert
31 Jul 24   `* Re: Operator overloading?52Gerry Jackson
31 Jul 24    +* Re: Operator overloading?50mhx
31 Jul 24    i+* Re: Operator overloading?3minforth
31 Jul 24    ii`* Re: Operator overloading?2mhx
31 Jul 24    ii `- Re: Operator overloading?1mhx
31 Jul 24    i+* VALUE and TO implementation (was: Operator overloading?)45Ruvim
31 Jul 24    ii+* Re: VALUE and TO implementation (was: Operator overloading?)41Anton Ertl
1 Aug 24    iii`* Re: VALUE and TO implementation40Ruvim
1 Aug 24    iii `* Re: VALUE and TO implementation39Anton Ertl
2 Aug 24    iii  `* Re: VALUE and TO implementation38Ruvim
3 Aug 24    iii   +* Re: VALUE and TO implementation35minforth
3 Aug 24    iii   i+* Re: VALUE and TO implementation33Ruvim
3 Aug 24    iii   ii`* Re: VALUE and TO implementation32minforth
4 Aug 24    iii   ii `* Re: VALUE and TO implementation31Ruvim
4 Aug 24    iii   ii  `* Re: VALUE and TO implementation30mhx
4 Aug 24    iii   ii   +* Re: VALUE and TO implementation4Paul Rubin
4 Aug 24    iii   ii   i+* Re: VALUE and TO implementation2mhx
4 Aug 24    iii   ii   ii`- Re: VALUE and TO implementation1Ruvim
4 Aug 24    iii   ii   i`- Re: VALUE and TO implementation1Ruvim
4 Aug 24    iii   ii   +- Re: VALUE and TO implementation1Anton Ertl
4 Aug 24    iii   ii   +* Alternative for long parsing words (was: VALUE and TO implementation)9Ruvim
5 Aug 24    iii   ii   i`* Re: Alternative for long parsing words (was: VALUE and TO implementation)8albert
5 Aug 24    iii   ii   i `* Re: Alternative for long parsing words7Ruvim
5 Aug 24    iii   ii   i  +* Re: Alternative for long parsing words5Anton Ertl
6 Aug 24    iii   ii   i  i+* Re: Alternative for long parsing words2Ruvim
6 Aug 24    iii   ii   i  ii`- Re: Alternative for long parsing words1mhx
6 Aug 24    iii   ii   i  i`* Re: Alternative for long parsing words2Ruvim
9 Aug 24    iii   ii   i  i `- Re: Alternative for long parsing words1Ruvim
6 Aug 24    iii   ii   i  `- Re: Alternative for long parsing words1albert
5 Aug 24    iii   ii   +- Re: VALUE and TO implementation1Stephen Pelc
5 Aug 24    iii   ii   `* Re: VALUE and TO implementation14dxf
5 Aug 24    iii   ii    `* Re: VALUE and TO implementation13mhx
5 Aug 24    iii   ii     +* Re: VALUE and TO implementation10dxf
5 Aug 24    iii   ii     i`* Re: VALUE and TO implementation9Ruvim
6 Aug 24    iii   ii     i `* Re: VALUE and TO implementation8dxf
7 Aug 24    iii   ii     i  `* Re: VALUE and TO implementation7dxf
7 Aug 24    iii   ii     i   +* Re: VALUE and TO implementation3Anton Ertl
7 Aug 24    iii   ii     i   i+- Re: VALUE and TO implementation1dxf
7 Aug 24    iii   ii     i   i`- Interpretation semantics (was: VALUE and TO implementation)1Ruvim
7 Aug 24    iii   ii     i   `* Re: VALUE and TO implementation3albert
7 Aug 24    iii   ii     i    `* Re: VALUE and TO implementation2Paul Rubin
8 Aug 24    iii   ii     i     `- Re: VALUE and TO implementation1albert
5 Aug 24    iii   ii     `* Re: VALUE and TO implementation2Anton Ertl
6 Aug 24    iii   ii      `- Re: VALUE and TO implementation1albert
3 Aug 24    iii   i`- Re: VALUE and TO implementation1Anton Ertl
3 Aug 24    iii   `* Re: VALUE and TO implementation2mhx
4 Aug 24    iii    `- Re: VALUE and TO implementation1Ruvim
1 Aug 24    ii+* Re: VALUE and TO implementation2minforth
2 Aug 24    iii`- Re: VALUE and TO implementation1albert
1 Aug 24    ii`- Re: VALUE and TO implementation1Ruvim
31 Jul 24    i`- Re: Operator overloading?1Gerry Jackson
31 Jul 24    `- Re: Operator overloading?1Anton Ertl

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal