discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Functions literals / higher order functions

MM
Michael Marx
Tue, Nov 26, 2019 12:51 AM

I get

Compiling design (CSG Tree generation)...

ECHO: g3_x = function(x) g((x + a))

ERROR: Recursion detected calling function 'g' in file fn literals parkinbot.scad, line 4

TRACE: called by 'g', in file fn literals parkinbot.scad, line 4.

TRACE: called by 'g3', in file fn literals parkinbot.scad, line 13.

TRACE: called by 'echo', in file fn literals parkinbot.scad, line 13.

Compiling design (CSG Products generation)...

When I changed it to:

function move(a, g) =

function(x)

//echo(a,g) 

  g(x+a) ;

g1 = function(x) x;

g2 = move(0,g1);

g3 = move(1,g2);

echo(g3_x=g3);

echo(g3_2=g3(2));

removing the comments causes a seeming infinite recursion echoing to the console, had to quit the
gui, then it crashed.


From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Ronaldo Persiano
Sent: Tue, 26 Nov 2019 11:08
To: OpenSCAD general discussion
Subject: Re: [OpenSCAD] Functions literals / higher order functions

I can't see why the following generates a stack overflow:

function move(a, g) =

function(x) g(x+a) ;

g1 = function(x) x;

g2 = move(0,g1);

g3 = move(0,g2);

echo(g3_x=g3);

echo(g3_2=g3(2));

If g3 is defined as g2 no error is reported.

I get Compiling design (CSG Tree generation)... ECHO: g3_x = function(x) g((x + a)) ERROR: Recursion detected calling function 'g' in file fn literals parkinbot.scad, line 4 TRACE: called by 'g', in file fn literals parkinbot.scad, line 4. TRACE: called by 'g3', in file fn literals parkinbot.scad, line 13. TRACE: called by 'echo', in file fn literals parkinbot.scad, line 13. Compiling design (CSG Products generation)... When I changed it to: function move(a, g) = function(x) //echo(a,g) g(x+a) ; g1 = function(x) x; g2 = move(0,g1); g3 = move(1,g2); echo(g3_x=g3); echo(g3_2=g3(2)); removing the comments causes a seeming infinite recursion echoing to the console, had to quit the gui, then it crashed. _____ From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Ronaldo Persiano Sent: Tue, 26 Nov 2019 11:08 To: OpenSCAD general discussion Subject: Re: [OpenSCAD] Functions literals / higher order functions I can't see why the following generates a stack overflow: function move(a, g) = function(x) g(x+a) ; g1 = function(x) x; g2 = move(0,g1); g3 = move(0,g2); echo(g3_x=g3); echo(g3_2=g3(2)); If g3 is defined as g2 no error is reported.
MM
Michael Marx
Tue, Nov 26, 2019 1:07 AM

I expect it is a scope issue with the 'move' function, I'll mention it on GitHub.


From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Michael Marx
Sent: Tue, 26 Nov 2019 11:51
To: 'OpenSCAD general discussion'
Subject: Re: [OpenSCAD] Functions literals / higher order functions

I get

Compiling design (CSG Tree generation)...

ECHO: g3_x = function(x) g((x + a))

ERROR: Recursion detected calling function 'g' in file fn literals parkinbot.scad, line 4

TRACE: called by 'g', in file fn literals parkinbot.scad, line 4.

TRACE: called by 'g3', in file fn literals parkinbot.scad, line 13.

TRACE: called by 'echo', in file fn literals parkinbot.scad, line 13.

Compiling design (CSG Products generation)...

When I changed it to:

function move(a, g) =

function(x)

//echo(a,g) 

  g(x+a) ;

g1 = function(x) x;

g2 = move(0,g1);

g3 = move(1,g2);

echo(g3_x=g3);

echo(g3_2=g3(2));

removing the comments causes a seeming infinite recursion echoing to the console, had to quit the
gui, then it crashed.


From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Ronaldo Persiano
Sent: Tue, 26 Nov 2019 11:08
To: OpenSCAD general discussion
Subject: Re: [OpenSCAD] Functions literals / higher order functions

I can't see why the following generates a stack overflow:

function move(a, g) =

function(x) g(x+a) ;

g1 = function(x) x;

g2 = move(0,g1);

g3 = move(0,g2);

echo(g3_x=g3);

echo(g3_2=g3(2));

If g3 is defined as g2 no error is reported.

I expect it is a scope issue with the 'move' function, I'll mention it on GitHub. _____ From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Michael Marx Sent: Tue, 26 Nov 2019 11:51 To: 'OpenSCAD general discussion' Subject: Re: [OpenSCAD] Functions literals / higher order functions I get Compiling design (CSG Tree generation)... ECHO: g3_x = function(x) g((x + a)) ERROR: Recursion detected calling function 'g' in file fn literals parkinbot.scad, line 4 TRACE: called by 'g', in file fn literals parkinbot.scad, line 4. TRACE: called by 'g3', in file fn literals parkinbot.scad, line 13. TRACE: called by 'echo', in file fn literals parkinbot.scad, line 13. Compiling design (CSG Products generation)... When I changed it to: function move(a, g) = function(x) //echo(a,g) g(x+a) ; g1 = function(x) x; g2 = move(0,g1); g3 = move(1,g2); echo(g3_x=g3); echo(g3_2=g3(2)); removing the comments causes a seeming infinite recursion echoing to the console, had to quit the gui, then it crashed. _____ From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Ronaldo Persiano Sent: Tue, 26 Nov 2019 11:08 To: OpenSCAD general discussion Subject: Re: [OpenSCAD] Functions literals / higher order functions I can't see why the following generates a stack overflow: function move(a, g) = function(x) g(x+a) ; g1 = function(x) x; g2 = move(0,g1); g3 = move(0,g2); echo(g3_x=g3); echo(g3_2=g3(2)); If g3 is defined as g2 no error is reported.
M
MichaelAtOz
Tue, Nov 26, 2019 1:49 AM

As I mentioned  On GitHub here
https://github.com/openscad/openscad/pull/3077#issuecomment-558417555  ,
this stops the infinite recursion, don't know why, need caffeine...

function move(a, g) = echo(move=a,g)
function(x)
echo(g_in_move=a,x)
(x+a) ;

g1 = function(x)

      x;

g2 = move(1,g1);
g3 = move(3,g2);

echo(g1_=g1);
echo(g1_1=g1(1));
echo(g2_=g2);
echo(g2_7=g2(7));
echo(g3_x=g3);
echo(g3_5=g3(5));

Gets:

Compiling design (CSG Tree generation)...
ECHO: move = 1, function(x) x
ECHO: move = 3, function(x) echo(g_in_move = a, x) (x + a)
ECHO: g1_ = function(x) x
ECHO: g1_1 = 1
ECHO: g2_ = function(x) echo(g_in_move = a, x) (x + a)
ECHO: g_in_moveg = 1, 7
ECHO: g2_7 = 8
ECHO: g3_x = function(x) echo(g_in_move = a, x) (x + a)
ECHO: g_in_moveg = 3, 5
ECHO: g3_5 = 8
Compiling design (CSG Products generation)...
Geometries in cache: 0


Admin - email* me if you need anything,  or if I've done something stupid...

  • click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

--
Sent from: http://forum.openscad.org/

As I mentioned On GitHub here <https://github.com/openscad/openscad/pull/3077#issuecomment-558417555> , this stops the infinite recursion, don't know why, need caffeine... function move(a, g) = echo(move=a,g) function(x) echo(g_in_move=a,x) (x+a) ; g1 = function(x) x; g2 = move(1,g1); g3 = move(3,g2); echo(g1_=g1); echo(g1_1=g1(1)); echo(g2_=g2); echo(g2_7=g2(7)); echo(g3_x=g3); echo(g3_5=g3(5)); Gets: Compiling design (CSG Tree generation)... ECHO: move = 1, function(x) x ECHO: move = 3, function(x) echo(g_in_move = a, x) (x + a) ECHO: g1_ = function(x) x ECHO: g1_1 = 1 ECHO: g2_ = function(x) echo(g_in_move = a, x) (x + a) ECHO: g_in_moveg = 1, 7 ECHO: g2_7 = 8 ECHO: g3_x = function(x) echo(g_in_move = a, x) (x + a) ECHO: g_in_moveg = 3, 5 ECHO: g3_5 = 8 Compiling design (CSG Products generation)... Geometries in cache: 0 ----- Admin - email* me if you need anything, or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. -- Sent from: http://forum.openscad.org/
RP
Ronaldo Persiano
Tue, Nov 26, 2019 2:08 AM

Michael,

You may need another coffee after confirming that the following runs
without error:

function move(a, g) =
function(x) g(x+a) ;

g1 = function(x) x;
g2 = move(0,g1);
//g0 = g2;
g0 = function(x) g2(x) ;
g3 = move(0,g0);

echo(g3_x=g3);
echo(g3_2=g3(2));

unless the commented line substitute the g0 definition.

Compiling design (CSG Tree generation)...

ECHO: g3_x = function(x) g((x + a))

ECHO: g3_2 = 2

Compiling design (CSG Products generation)...

Geometries in cache: 5

Geometry cache size in bytes: 1774120

CGAL Polyhedrons in cache: 0

CGAL cache size in bytes: 0

Compiling design (CSG Products normalization)...

Normalized CSG tree has 0 elements

Compile and preview finished.
Total rendering time: 0 hours, 0 minutes, 0 seconds

Michael, You may need another coffee after confirming that the following runs without error: function move(a, g) = function(x) g(x+a) ; g1 = function(x) x; g2 = move(0,g1); //g0 = g2; g0 = function(x) g2(x) ; g3 = move(0,g0); echo(g3_x=g3); echo(g3_2=g3(2)); unless the commented line substitute the g0 definition. Compiling design (CSG Tree generation)... ECHO: g3_x = function(x) g((x + a)) ECHO: g3_2 = 2 Compiling design (CSG Products generation)... Geometries in cache: 5 Geometry cache size in bytes: 1774120 CGAL Polyhedrons in cache: 0 CGAL cache size in bytes: 0 Compiling design (CSG Products normalization)... Normalized CSG tree has 0 elements Compile and preview finished. Total rendering time: 0 hours, 0 minutes, 0 seconds >
RP
Ronaldo Persiano
Tue, Nov 26, 2019 2:23 AM

Inserting an indirection also works fine:

function move(a, g) =
function(x) identity(x+a) ;

function  identity(x) = x;

g1 = function(x) x;
g2 = move(0,g1);
g3 = move(0,g2);

echo(g3_x=g3);
echo(g3_2=g3(2));

Inserting an indirection also works fine: function move(a, g) = function(x) identity(x+a) ; function identity(x) = x; g1 = function(x) x; g2 = move(0,g1); g3 = move(0,g2); echo(g3_x=g3); echo(g3_2=g3(2)); > >
M
MichaelAtOz
Tue, Nov 26, 2019 2:29 AM

Oops, at line 4, dropped the g() in my edits...


Admin - email* me if you need anything,  or if I've done something stupid...

  • click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

--
Sent from: http://forum.openscad.org/

Oops, at line 4, dropped the g() in my edits... ----- Admin - email* me if you need anything, or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. -- Sent from: http://forum.openscad.org/
RP
Ronaldo Persiano
Tue, Nov 26, 2019 2:58 AM

I followed Michael and also dropped the g. But anyway, the last code still
works fine with

function move(a, g) =
function(x) identity(g(x+a)) ;

Em ter., 26 de nov. de 2019 às 02:23, Ronaldo Persiano <
rcmpersiano@gmail.com> escreveu:

Inserting an indirection also works fine:

function move(a, g) =
function(x) identity(x+a) ;

function  identity(x) = x;

g1 = function(x) x;
g2 = move(0,g1);
g3 = move(0,g2);

echo(g3_x=g3);
echo(g3_2=g3(2));

I followed Michael and also dropped the g. But anyway, the last code still works fine with function move(a, g) = function(x) identity(g(x+a)) ; Em ter., 26 de nov. de 2019 às 02:23, Ronaldo Persiano < rcmpersiano@gmail.com> escreveu: > Inserting an indirection also works fine: > > > function move(a, g) = > function(x) identity(x+a) ; > > function identity(x) = x; > > g1 = function(x) x; > g2 = move(0,g1); > g3 = move(0,g2); > > echo(g3_x=g3); > echo(g3_2=g3(2)); > > > >> >>
M
MichaelAtOz
Tue, Nov 26, 2019 3:45 AM

See https://github.com/openscad/openscad/pull/3077#issuecomment-558442998

assert() helps, feels like a scope problem, but I need paper and pencil, my
head is filling with virtual functions...I don't like function literals...


Admin - email* me if you need anything,  or if I've done something stupid...

  • click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

--
Sent from: http://forum.openscad.org/

See https://github.com/openscad/openscad/pull/3077#issuecomment-558442998 assert() helps, feels like a scope problem, but I need paper and pencil, my head is filling with virtual functions...I don't like function literals... ----- Admin - email* me if you need anything, or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. -- Sent from: http://forum.openscad.org/
RP
Ronaldo Persiano
Tue, Nov 26, 2019 11:58 AM

Yet another hack that works:

function move(a, g) =
function(x)
let( v = g(x+a) ) v;

g1 = function(x) x;
g2 = move(2, g1);
g3 = move(3, g2);

echo(g3_x=g3);
echo(g3_2=g3(2));

Compiling design (CSG Tree generation)...

ECHO: g3_x = function(x) let(v = g((x + a))) v

ECHO: g3_2 = 7

Compiling design (CSG Products generation)...

Geometries in cache: 1

Geometry cache size in bytes: 270008

CGAL Polyhedrons in cache: 0

CGAL cache size in bytes: 0

Compiling design (CSG Products normalization)...

Normalized CSG tree has 0 elements
Compile and preview finished.

I can't see how scope may matter here.

Yet another hack that works: function move(a, g) = function(x) let( v = g(x+a) ) v; g1 = function(x) x; g2 = move(2, g1); g3 = move(3, g2); echo(g3_x=g3); echo(g3_2=g3(2)); Compiling design (CSG Tree generation)... ECHO: g3_x = function(x) let(v = g((x + a))) v ECHO: g3_2 = 7 Compiling design (CSG Products generation)... Geometries in cache: 1 Geometry cache size in bytes: 270008 CGAL Polyhedrons in cache: 0 CGAL cache size in bytes: 0 Compiling design (CSG Products normalization)... Normalized CSG tree has 0 elements Compile and preview finished. I can't see how scope may matter here.
MM
Michael Marx
Tue, Nov 26, 2019 12:08 PM

Not normal scope, a bug in how it works.

function move(a, g) = echo(move=a,g)

function(m)

  echo(m_in_move=a,m,g)

  assert(m<12)

    g(echo(str("call g(",m+a,")[g=",g,"]")) m+a) ;

g1 = function(x)

   echo(fx=x)

      x;

g2 = move(2,g1);

g3 = move(3,g2);

echo(g2_=g2);

echo(g3_x=g3);

echo(g3_5=g3(5));

Produces

Compiling design (CSG Tree generation)...

ECHO: move = 2, function(x) echo(fx = x) x

ECHO: move = 3, function(m) echo(m_in_move = a, m, g) assert((m

ECHO: g2_ = function(m) echo(m_in_move = a, m, g) assert((m

ECHO: g3_x = function(m) echo(m_in_move = a, m, g) assert((m

ECHO: m_in_move = 3, 5, function(m) echo(m_in_move = a, m, g) assert((m

ECHO: "call g(8)[g=function(m) echo(m_in_move = a, m, g) assert((m

ECHO: "call g(8)[g=function(m) echo(m_in_move = a, m, g) assert((m

ECHO: m_in_move = 2, 8, function(x) echo(fx = x) x

ECHO: "call g(10)[g=function(x) echo(fx = x) x]"

ECHO: m_in_move = 2, 10, function(x) echo(fx = x) x

ECHO: "call g(12)[g=function(x) echo(fx = x) x]"

ECHO: m_in_move = 2, 12, function(x) echo(fx = x) x

ERROR: Assertion '(m < 12)' failed in file fn literals ronaldo.scad, line 4

TRACE: called by 'g', in file fn literals ronaldo.scad, line 5.

TRACE: called by 'g3', in file fn literals ronaldo.scad, line 15.

TRACE: called by 'echo', in file fn literals ronaldo.scad, line 15.

Compiling design (CSG Products generation)...

Geometries in cache: 0

You can see it is saying it wants to call g1, but is actually calling g2 recursively.


From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Ronaldo Persiano
Sent: Tue, 26 Nov 2019 22:58
To: OpenSCAD general discussion
Subject: Re: [OpenSCAD] Functions literals / higher order functions

Yet another hack that works:

function move(a, g) =
function(x)
let( v = g(x+a) ) v;

g1 = function(x) x;
g2 = move(2, g1);
g3 = move(3, g2);

echo(g3_x=g3);

echo(g3_2=g3(2));

Compiling design (CSG Tree generation)...

ECHO: g3_x = function(x) let(v = g((x + a))) v

ECHO: g3_2 = 7

Compiling design (CSG Products generation)...

Geometries in cache: 1

Geometry cache size in bytes: 270008

CGAL Polyhedrons in cache: 0

CGAL cache size in bytes: 0

Compiling design (CSG Products normalization)...

Normalized CSG tree has 0 elements

Compile and preview finished.

I can't see how scope may matter here.

Not normal scope, a bug in how it works. function move(a, g) = echo(move=a,g) function(m) echo(m_in_move=a,m,g) assert(m<12) g(echo(str("call g(",m+a,")[g=",g,"]")) m+a) ; g1 = function(x) echo(fx=x) x; g2 = move(2,g1); g3 = move(3,g2); echo(g2_=g2); echo(g3_x=g3); echo(g3_5=g3(5)); Produces Compiling design (CSG Tree generation)... ECHO: move = 2, function(x) echo(fx = x) x ECHO: move = 3, function(m) echo(m_in_move = a, m, g) assert((m ECHO: g2_ = function(m) echo(m_in_move = a, m, g) assert((m ECHO: g3_x = function(m) echo(m_in_move = a, m, g) assert((m ECHO: m_in_move = 3, 5, function(m) echo(m_in_move = a, m, g) assert((m ECHO: "call g(8)[g=function(m) echo(m_in_move = a, m, g) assert((m ECHO: "call g(8)[g=function(m) echo(m_in_move = a, m, g) assert((m ECHO: m_in_move = 2, 8, function(x) echo(fx = x) x ECHO: "call g(10)[g=function(x) echo(fx = x) x]" ECHO: m_in_move = 2, 10, function(x) echo(fx = x) x ECHO: "call g(12)[g=function(x) echo(fx = x) x]" ECHO: m_in_move = 2, 12, function(x) echo(fx = x) x ERROR: Assertion '(m < 12)' failed in file fn literals ronaldo.scad, line 4 TRACE: called by 'g', in file fn literals ronaldo.scad, line 5. TRACE: called by 'g3', in file fn literals ronaldo.scad, line 15. TRACE: called by 'echo', in file fn literals ronaldo.scad, line 15. Compiling design (CSG Products generation)... Geometries in cache: 0 You can see it is saying it wants to call g1, but is actually calling g2 recursively. _____ From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Ronaldo Persiano Sent: Tue, 26 Nov 2019 22:58 To: OpenSCAD general discussion Subject: Re: [OpenSCAD] Functions literals / higher order functions Yet another hack that works: function move(a, g) = function(x) let( v = g(x+a) ) v; g1 = function(x) x; g2 = move(2, g1); g3 = move(3, g2); echo(g3_x=g3); echo(g3_2=g3(2)); Compiling design (CSG Tree generation)... ECHO: g3_x = function(x) let(v = g((x + a))) v ECHO: g3_2 = 7 Compiling design (CSG Products generation)... Geometries in cache: 1 Geometry cache size in bytes: 270008 CGAL Polyhedrons in cache: 0 CGAL cache size in bytes: 0 Compiling design (CSG Products normalization)... Normalized CSG tree has 0 elements Compile and preview finished. I can't see how scope may matter here.