ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* supp-ran.lua makes code unable to work in ConTeXt
@ 2020-05-01  4:12 Jairo A. del Rio
  2020-05-01 13:21 ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Jairo A. del Rio @ 2020-05-01  4:12 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2177 bytes --]

I've adapted the following code to make mazes in LuaLaTeX and ConTeXt:
https://www.rosettacode.org/wiki/Maze_generation#Lua

I defined a modified version to make content visible to TeX and I saved in
a file named maze.lua:

function make_maze_tex(w, h, m)
  w = w or 16
  h = h or 8

  local map = initialize_grid(w*2+1, h*2+1)

  function walk(x, y)
    map[y][x] = false

    local d = { 1, 2, 3, 4 }
    shuffle(d)
    for i, dirnum in ipairs(d) do
      local xx = x + dirs[dirnum].x
      local yy = y + dirs[dirnum].y
      if map[yy] and map[yy][xx] then
        map[avg(y, yy)][avg(x, xx)] = false
        walk(xx, yy)
      end
    end
  end

  walk(math.random(1, w)*2, math.random(1, h)*2)

 tex.print([[\bgroup]])
 tex.print([[\baselineskip ]]..m)
  local s = {}
  for i = 1, h*2+1 do
    --table.insert(s, [[\hbox{]])
    tex.print([[\hbox{]])
    for j = 1, w*2+1 do
     -- table.insert(s, [[\hbox{]])
      if (j == 1 and i == 2) or (j == 2*w + 1 and i == 2*h) then
        tex.print([[\hskip ]]..m)
      elseif map[i][j] then
        --table.insert(s, [[\vrule width ]]..m..[[ height ]]..m)
        tex.print([[\vrule width ]]..m..[[ height ]]..m)
      else
        --table.insert(s, [[\hskip ]]..m)
        tex.print([[\hskip ]]..m)
      end
    --  table.insert(s, [[}]].."\n")
    end
    --table.insert(s, [[}]])
  tex.print([[}]])
  end
 --tex.print(table.concat(s))
 tex.print([[\egroup]])
end

And I executed the folowing in LuaLaTeX and ConTeXt:

%\documentclass{article}
%\usepackage{luacode}
%\begin{document}
\starttext
%\begin{luacode*}
\startluacode
mz = dofile("maze.lua")
local count = 0
for i=10,109 do
count = count + 1
tex.print([[\subject{Laberinto ]]..count..[[}]])
mz.make_maze_tex(i, i, [[\dimexpr\textwidth/]]..(2*i+1)..[[\relax]])
tex.print([[\pagebreak]])
end
%\end{luacode*}
\stopluacode
%\end{document}
\stoptext

Whereas it works very fast in LuaLaTeX it crashes on ConTeXt and outputs
the following:

token call, execute:
...ext/tex/texmf-context/tex/context/base/mkiv/supp-ran.lua:30: C stack
overflow


Why does it happen? I guess it has something to do with math.randomseed,
but I don't know how to avoid this error message.

[-- Attachment #1.2: Type: text/html, Size: 3020 bytes --]

[-- Attachment #2: maze.lua --]
[-- Type: text/x-lua, Size: 2737 bytes --]

local mazegen = {}

math.randomseed( os.time() )
 
-- Fisher-Yates shuffle from http://santos.nfshost.com/shuffling.html
local function shuffle(t)
  for i = 1, #t - 1 do
    local r = math.random(i, #t)
    t[i], t[r] = t[r], t[i]
  end
end

mazegen.shuffle = shuffle 
-- builds a width-by-height grid of trues
local function initialize_grid(w, h)
  local a = {}
  for i = 1, h do
    table.insert(a, {})
    for j = 1, w do
      table.insert(a[i], true)
    end
  end
  return a
end
 
mazegen.initialize_grid = initialize_grid
-- average of a and b
local function avg(a, b)
  return (a + b) / 2
end
 
 
local dirs = {
  {x = 0, y = -2}, -- north
  {x = 2, y = 0}, -- east
  {x = -2, y = 0}, -- west
  {x = 0, y = 2}, -- south
}
 
local function make_maze(w, h)
  w = w or 16
  h = h or 8
 
  local map = initialize_grid(w*2+1, h*2+1)
 
  function walk(x, y)
    map[y][x] = false
 
    local d = { 1, 2, 3, 4 }
    shuffle(d)
    for i, dirnum in ipairs(d) do
      local xx = x + dirs[dirnum].x
      local yy = y + dirs[dirnum].y
      if map[yy] and map[yy][xx] then
        map[avg(y, yy)][avg(x, xx)] = false
        walk(xx, yy)
      end
    end
  end
 
  walk(math.random(1, w)*2, math.random(1, h)*2)
 
  local s = {}
  for i = 1, h*2+1 do
    for j = 1, w*2+1 do
      if map[i][j] then
        table.insert(s, '#')
      else
        table.insert(s, ' ')
      end
    end
    table.insert(s, '\n')
  end
  return table.concat(s)
end

mazegen.make_maze = make_maze

function make_maze_tex(w, h, m)
  w = w or 16
  h = h or 8
 
  local map = initialize_grid(w*2+1, h*2+1)
 
  function walk(x, y)
    map[y][x] = false
 
    local d = { 1, 2, 3, 4 }
    shuffle(d)
    for i, dirnum in ipairs(d) do
      local xx = x + dirs[dirnum].x
      local yy = y + dirs[dirnum].y
      if map[yy] and map[yy][xx] then
        map[avg(y, yy)][avg(x, xx)] = false
        walk(xx, yy)
      end
    end
  end
 
  walk(math.random(1, w)*2, math.random(1, h)*2)
 
 tex.print([[\bgroup]])
 tex.print([[\baselineskip ]]..m)
  local s = {}
  for i = 1, h*2+1 do
    --table.insert(s, [[\hbox{]])
    tex.print([[\hbox{]])
    for j = 1, w*2+1 do
     -- table.insert(s, [[\hbox{]])
      if (j == 1 and i == 2) or (j == 2*w + 1 and i == 2*h) then 
        tex.print([[\hskip ]]..m)
      elseif map[i][j] then
        --table.insert(s, [[\vrule width ]]..m..[[ height ]]..m)
        tex.print([[\vrule width ]]..m..[[ height ]]..m)
      else
        --table.insert(s, [[\hskip ]]..m)
        tex.print([[\hskip ]]..m)
      end
    --  table.insert(s, [[}]].."\n")
    end
    --table.insert(s, [[}]])
  tex.print([[}]])
  end
 --tex.print(table.concat(s))
 tex.print([[\egroup]])
end

mazegen.make_maze_tex = make_maze_tex

return mazegen
 

[-- Attachment #3: mazecontext.tex --]
[-- Type: text/x-tex, Size: 273 bytes --]

\starttext
\startluacode
mz = dofile("maze.lua")
local count = 0
for i=10,109 do
count = count + 1
tex.print([[\subject{Laberinto ]]..count..[[}]])
mz.make_maze_tex(i, i, [[\dimexpr\textwidth/]]..(2*i+1)..[[\relax]])
tex.print([[\pagebreak]])
end 
\stopluacode  
\stoptext 

[-- Attachment #4: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: supp-ran.lua makes code unable to work in ConTeXt
  2020-05-01  4:12 supp-ran.lua makes code unable to work in ConTeXt Jairo A. del Rio
@ 2020-05-01 13:21 ` Hans Hagen
  2020-05-01 21:38   ` Aditya Mahajan
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2020-05-01 13:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Jairo A. del Rio

[-- Attachment #1: Type: text/plain, Size: 3891 bytes --]

On 5/1/2020 6:12 AM, Jairo A. del Rio wrote:
> I've adapted the following code to make mazes in LuaLaTeX and ConTeXt: 
> https://www.rosettacode.org/wiki/Maze_generation#Lua
> 
> I defined a modified version to make content visible to TeX and I saved 
> in a file named maze.lua:
> 
> function make_maze_tex(w, h, m)
>    w = w or 16
>    h = h or 8
> 
>    local map = initialize_grid(w*2+1, h*2+1)
> 
>    function walk(x, y)
>      map[y][x] = false
> 
>      local d = { 1, 2, 3, 4 }
>      shuffle(d)
>      for i, dirnum in ipairs(d) do
>        local xx = x + dirs[dirnum].x
>        local yy = y + dirs[dirnum].y
>        if map[yy] and map[yy][xx] then
>          map[avg(y, yy)][avg(x, xx)] = false
>          walk(xx, yy)
>        end
>      end
>    end
> 
>    walk(math.random(1, w)*2, math.random(1, h)*2)
> 
>   tex.print([[\bgroup]])
>   tex.print([[\baselineskip ]]..m)
>    local s = {}
>    for i = 1, h*2+1 do
>      --table.insert(s, [[\hbox{]])
>      tex.print([[\hbox{]])
>      for j = 1, w*2+1 do
>       -- table.insert(s, [[\hbox{]])
>        if (j == 1 and i == 2) or (j == 2*w + 1 and i == 2*h) then
>          tex.print([[\hskip ]]..m)
>        elseif map[i][j] then
>          --table.insert(s, [[\vrule width ]]..m..[[ height ]]..m)
>          tex.print([[\vrule width ]]..m..[[ height ]]..m)
>        else
>          --table.insert(s, [[\hskip ]]..m)
>          tex.print([[\hskip ]]..m)
>        end
>      --  table.insert(s, [[}]].."\n")
>      end
>      --table.insert(s, [[}]])
>    tex.print([[}]])
>    end
>   --tex.print(table.concat(s))
>   tex.print([[\egroup]])
> end
> 
> And I executed the folowing in LuaLaTeX and ConTeXt:
> 
> %\documentclass{article}
> %\usepackage{luacode}
> %\begin{document}
> \starttext
> %\begin{luacode*}
> \startluacode
> mz = dofile("maze.lua")
> local count = 0
> for i=10,109 do
> count = count + 1
> tex.print([[\subject{Laberinto ]]..count..[[}]])
> mz.make_maze_tex(i, i, [[\dimexpr\textwidth/]]..(2*i+1)..[[\relax]])
> tex.print([[\pagebreak]])
> end
> %\end{luacode*}
> \stopluacode
> %\end{document}
> \stoptext
> 
> Whereas it works very fast in LuaLaTeX it crashes on ConTeXt and outputs 
> the following:

btw, fast is relative ... no problem making the code twice as fast

it is actually a nice example for the cld manual ... attached some 
output from a first 'improvement' (that version runs some 50-60% faster 
so you have a challenge ... actually, it could be a nice challenge for 
others on this list: how to make this one contextish and so, maybe we 
should have a challenge every few weeks)

> token call, execute: 
> ...ext/tex/texmf-context/tex/context/base/mkiv/supp-ran.lua:30: C stack 
> overflow 
> 
> Why does it happen? I guess it has something to do with math.randomseed, 
> but I don't know how to avoid this error message.
no, it's just lua running out of stack space so it depends on the binary 
(and als the initial random value)

luatex 1.13 uses lua 5.3 which has a large stack but depending on the 
compiler/linker settings and platform (it's 1000000 but depending on the 
situatation you get a message or crash when that doesn't work out)

luajittex has a limited stack of 8000 so it will also crash

luametatex uses lua 5.4 which uses a bit different stack model and can't 
go that high (it has a default of 2000 but i will bnump that to 6000 
which still seems to work ok, that way i get upto a 120 maze)

(new lmtx upload later this weekend)

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------

[-- Attachment #2: mazecontext.pdf --]
[-- Type: application/pdf, Size: 9334 bytes --]

[-- Attachment #3: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: supp-ran.lua makes code unable to work in ConTeXt
  2020-05-01 13:21 ` Hans Hagen
@ 2020-05-01 21:38   ` Aditya Mahajan
  2020-05-01 22:25     ` Hans Hagen
  0 siblings, 1 reply; 6+ messages in thread
From: Aditya Mahajan @ 2020-05-01 21:38 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 1470 bytes --]

On Fri, 1 May 2020, Hans Hagen wrote:

> On 5/1/2020 6:12 AM, Jairo A. del Rio wrote:
>> I've adapted the following code to make mazes in LuaLaTeX and ConTeXt: 
>> https://www.rosettacode.org/wiki/Maze_generation#Lua
>> 
>> I defined a modified version to make content visible to TeX and I saved in 
>> a file named maze.lua:
>> 
>> function make_maze_tex(w, h, m)
>>    w = w or 16
>>    h = h or 8
>>
>>    local map = initialize_grid(w*2+1, h*2+1)
>>
>>    function walk(x, y)
>>      map[y][x] = false
>>
>>      local d = { 1, 2, 3, 4 }
>>      shuffle(d)
>>      for i, dirnum in ipairs(d) do
>>        local xx = x + dirs[dirnum].x
>>        local yy = y + dirs[dirnum].y
>>        if map[yy] and map[yy][xx] then
>>          map[avg(y, yy)][avg(x, xx)] = false
>>          walk(xx, yy)
>>        end
>>      end
>>    end
>>
>>    walk(math.random(1, w)*2, math.random(1, h)*2)
>> 
>> ...ext/tex/texmf-context/tex/context/base/mkiv/supp-ran.lua:30: C stack 
>> overflow 
>> Why does it happen? I guess it has something to do with math.randomseed, 
>> but I don't know how to avoid this error message.
> no, it's just lua running out of stack space so it depends on the binary 
> (and als the initial random value)

But why is it running out of stack? Isn't the walk function tail recursive, in which case it should not cause a stack overflow (https://www.lua.org/pil/6.3.html).

Aditya

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: supp-ran.lua makes code unable to work in ConTeXt
  2020-05-01 21:38   ` Aditya Mahajan
@ 2020-05-01 22:25     ` Hans Hagen
  2020-05-02  3:05       ` Aditya Mahajan
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2020-05-01 22:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Aditya Mahajan

On 5/1/2020 11:38 PM, Aditya Mahajan wrote:
> On Fri, 1 May 2020, Hans Hagen wrote:
> 
>> On 5/1/2020 6:12 AM, Jairo A. del Rio wrote:
>>> I've adapted the following code to make mazes in LuaLaTeX and 
>>> ConTeXt: https://www.rosettacode.org/wiki/Maze_generation#Lua
>>>
>>> I defined a modified version to make content visible to TeX and I 
>>> saved in a file named maze.lua:
>>>
>>> function make_maze_tex(w, h, m)
>>>    w = w or 16
>>>    h = h or 8
>>>
>>>    local map = initialize_grid(w*2+1, h*2+1)
>>>
>>>    function walk(x, y)
>>>      map[y][x] = false
>>>
>>>      local d = { 1, 2, 3, 4 }
>>>      shuffle(d)
>>>      for i, dirnum in ipairs(d) do
>>>        local xx = x + dirs[dirnum].x
>>>        local yy = y + dirs[dirnum].y
>>>        if map[yy] and map[yy][xx] then
>>>          map[avg(y, yy)][avg(x, xx)] = false
>>>          walk(xx, yy)
>>>        end
>>>      end
>>>    end
>>>
>>>    walk(math.random(1, w)*2, math.random(1, h)*2)
>>>
>>> ...ext/tex/texmf-context/tex/context/base/mkiv/supp-ran.lua:30: C 
>>> stack overflow Why does it happen? I guess it has something to do 
>>> with math.randomseed, but I don't know how to avoid this error message.
>> no, it's just lua running out of stack space so it depends on the 
>> binary (and als the initial random value)
> 
> But why is it running out of stack? Isn't the walk function tail 
> recursive, in which case it should not cause a stack overflow 
> (https://www.lua.org/pil/6.3.html).
not really tail recursive: that is only true when it's the last action 
and return is used and it nests very deep

function foo(...)
   ...
   return foo(...)
end

versus

function foo(...)
   ...
   if something then
      foo(...)
   end
end

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: supp-ran.lua makes code unable to work in ConTeXt
  2020-05-01 22:25     ` Hans Hagen
@ 2020-05-02  3:05       ` Aditya Mahajan
  2020-05-02  3:17         ` Aditya Mahajan
  0 siblings, 1 reply; 6+ messages in thread
From: Aditya Mahajan @ 2020-05-02  3:05 UTC (permalink / raw)
  To: Hans Hagen; +Cc: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 2211 bytes --]

On Sat, 2 May 2020, Hans Hagen wrote:

> On 5/1/2020 11:38 PM, Aditya Mahajan wrote:
>> On Fri, 1 May 2020, Hans Hagen wrote:
>> 
>>> On 5/1/2020 6:12 AM, Jairo A. del Rio wrote:
>>>> I've adapted the following code to make mazes in LuaLaTeX and ConTeXt: 
>>>> https://www.rosettacode.org/wiki/Maze_generation#Lua
>>>> 
>>>> I defined a modified version to make content visible to TeX and I saved 
>>>> in a file named maze.lua:
>>>> 
>>>> function make_maze_tex(w, h, m)
>>>>    w = w or 16
>>>>    h = h or 8
>>>> 
>>>>    local map = initialize_grid(w*2+1, h*2+1)
>>>> 
>>>>    function walk(x, y)
>>>>      map[y][x] = false
>>>> 
>>>>      local d = { 1, 2, 3, 4 }
>>>>      shuffle(d)
>>>>      for i, dirnum in ipairs(d) do
>>>>        local xx = x + dirs[dirnum].x
>>>>        local yy = y + dirs[dirnum].y
>>>>        if map[yy] and map[yy][xx] then
>>>>          map[avg(y, yy)][avg(x, xx)] = false
>>>>          walk(xx, yy)
>>>>        end
>>>>      end
>>>>    end
>>>> 
>>>>    walk(math.random(1, w)*2, math.random(1, h)*2)
>>>> 
>>>> ...ext/tex/texmf-context/tex/context/base/mkiv/supp-ran.lua:30: C stack 
>>>> overflow Why does it happen? I guess it has something to do with 
>>>> math.randomseed, but I don't know how to avoid this error message.
>>> no, it's just lua running out of stack space so it depends on the binary 
>>> (and als the initial random value)
>> 
>> But why is it running out of stack? Isn't the walk function tail 
>> recursive, in which case it should not cause a stack overflow 
>> (https://www.lua.org/pil/6.3.html).
> not really tail recursive: that is only true when it's the last action and 
> return is used and it nests very deep
>
> function foo(...)
>  ...
>  return foo(...)
> end
>
> versus
>
> function foo(...)
>  ...
>  if something then
>     foo(...)
>  end
> end

Thanks for the clarification.

If you or anyone else is going to play around with generating mazes,
definitely take a look at http://www.mazesforprogrammers.com/

and the live demos here: 
https://weblog.jamisbuck.org/2011/2/7/maze-generation-algorithm-recap

Aditya

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: supp-ran.lua makes code unable to work in ConTeXt
  2020-05-02  3:05       ` Aditya Mahajan
@ 2020-05-02  3:17         ` Aditya Mahajan
  0 siblings, 0 replies; 6+ messages in thread
From: Aditya Mahajan @ 2020-05-02  3:17 UTC (permalink / raw)
  To: mailing list for ConTeXt users

> If you or anyone else is going to play around with generating mazes,
> definitely take a look at http://www.mazesforprogrammers.com/
>
> and the live demos here: 
> https://weblog.jamisbuck.org/2011/2/7/maze-generation-algorithm-recap

A nice overview of the relationship with spanning tress (by the same author) http://www.jamisbuck.org/presentations/rubyconf2011/index.html

And another set of visualizations
https://bost.ocks.org/mike/algorithms/#maze-generation

Aditya


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2020-05-02  3:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-01  4:12 supp-ran.lua makes code unable to work in ConTeXt Jairo A. del Rio
2020-05-01 13:21 ` Hans Hagen
2020-05-01 21:38   ` Aditya Mahajan
2020-05-01 22:25     ` Hans Hagen
2020-05-02  3:05       ` Aditya Mahajan
2020-05-02  3:17         ` Aditya Mahajan

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 NNTP newsgroup(s).