2025-02-04 07:34:45 pgimeno I think I have found a way to optimize recursion into a loop even if it's not a tail call 2025-02-04 07:34:57 I have to try it yet 2025-02-04 07:37:09 instead of duplicating the same code on the return stack or whatever I use, the code will not change so I increment a counter and when the recursion ends I evaluate the remaining code that number of times 2025-02-04 08:37:03 : oh 1- dup 0= 'exit if oh 1+ 1+ ; 2025-02-04 08:37:14 that's the version in my toy lang 2025-02-04 08:37:34 returns 12 like gforth did 2025-02-04 08:38:36 with the remaining (1+ 1+) I save that and every time I recurse I increment a counter 2025-02-04 08:39:18 once recursion ends I just evaluate that (1+ 1+) the number of times of the counter 2025-02-04 08:40:59 so no matter how much it recurses there is no call stack, but an array that only has the size of two elements (1+ 1+) and will not grow during recursion 2025-02-04 08:49:04 in gforth 2025-02-04 08:49:07 : test 1- dup 0= if exit then recurse 1+ 1+ ; 2025-02-04 08:49:22 300000 test . cr => return stack overflow 2025-02-04 08:50:53 in my lang returns 599998 2025-02-04 08:50:53 but I'm not sure if it's working fine or just the return stack of perl is bigger 2025-02-04 09:12:46 1000000 oh => 1999998 2025-02-04 09:18:45 I cannot find a way to have multiple branches though 2025-02-04 09:26:22 That's interesting 2025-02-04 09:26:39 Multiple branches? 2025-02-04 09:26:56 No, recursive -> loop optimisation 2025-02-04 09:27:20 Sorry, I missed the start of this :P 2025-02-04 09:27:43 No problemo 2025-02-04 09:30:56 Multiple branches is interesting though, the reason you can't is because I think you'd need a stack to track what post-effects to apply 2025-02-04 09:30:56 I.E. there's no benefit to looping 2025-02-04 09:31:15 But if there's only one post-effect you can track how many times with a counter and just loop, so there's less memory usage 2025-02-04 09:32:49 If the post-effects were commutative you could have two counters for two branches 2025-02-04 09:32:56 etc 2025-02-04 10:27:15 yeah I can only give id to the branches and store a sequence of those id 2025-02-04 10:27:26 then use compression mechanisms 2025-02-04 10:28:10 GeDaMo use the irc log 2025-02-04 10:28:24 This channel is logged? 2025-02-04 10:28:27 https://forth.chat/logs/libera/forth/2025-02-04 2025-02-04 10:29:07 That should really be mentioned in the /topic 2025-02-04 10:29:19 it is 2025-02-04 10:29:37 | http://forth.chat for more information, related channels, public logs of the channel, and notes on the bots 2025-02-04 10:29:44 Oh yeah :P 2025-02-04 10:43:16 the one branch can be unlimited since you can use an additional counter for when the counter wraps when reaches it's maximum value 2025-02-04 10:44:07 to count how many times it wraps and make a nested loop 2025-02-04 10:44:47 and if the wrap counter also wraps you make another counter and so on xd 2025-02-04 10:45:07 well eventually will die 2025-02-04 10:46:18 for the multiple branches I was thinking that if you detect patterns like 123123123 you can use a hash table or alike to save that as x => 123123123 and store the x in the sequence instead, then decode when evaluating 2025-02-04 12:09:20 GeDaMo: in addition to the mention in the channel title, there is a channel join message that mentions the channel being publicly logged 2025-02-04 12:09:48 I don't think I get that one 2025-02-04 12:12:08 probably depends on the client, some don't make it easy to see channel welcome messages. (e.g., irccloud dumps these into the main server messages listing) 2025-02-04 13:03:44 some very small CPUs used to use LFSRs for their program counters, and for example the Atari 2600 used one for its X-coordinate pixel counter 2025-02-04 13:04:34 a fun thing I realized the other day about LFSR program counters is that they allow you to have conditional branch instructions that don't specify a destination 2025-02-04 13:05:28 I don't think any architecture has ever done this 2025-02-04 13:07:17 because almost any trivial modification you make to the program counter, such as toggling the LSB, will put you on an almost arbitrarily far away part of the LFSR sequence 2025-02-04 15:08:35 vms14: sorry to be the one to burst your bubble again, but anything that doesn't use a return stack is broken 2025-02-04 15:10:53 you may have solved my example, which was a simple example designed to show how using a jump doesn't cut it, but for every other hack I can give you an example that breaks it. In this case, visiting every node in a tree would break it, and so would two mutually recursive functions. 2025-02-04 15:11:07 Come to think about it, can you do mutual recursion in Forth? 2025-02-04 15:14:21 Ah, apparently there's a word DEFER which is kind of a forward declaration. 2025-02-04 15:45:26 pgimeno don't worry, I appreciate your feedback 2025-02-04 15:45:38 and I'm not afraid of making mistakes, that's how I learn 2025-02-04 15:45:45 but yeah you are right 2025-02-04 15:46:00 is a very limited thing and cannot handle mutual recursion 2025-02-04 15:47:25 or two recursive calls in the same word 2025-02-04 15:50:52 xd yeah 2025-02-04 15:52:49 it seems that I cannot avoid having a global return stack for this 2025-02-04 15:53:18 saw some light but it was a little hole 2025-02-04 15:53:58 still I value that you help me realize 2025-02-04 15:54:10 so thanks again pgimeno :D 2025-02-04 15:55:55 no prob, I am now writing a binary tree visiting example so you have something to test it on 2025-02-04 16:01:09 Yeah it's only taken you years to accept this lol :P 2025-02-04 16:01:24 :D 2025-02-04 16:01:34 Better late than never 2025-02-04 16:16:06 http://www.formauri.es/personal/pgimeno/pastes/tree.4th 2025-02-04 16:28:34 (just updated to emphasize it's a binary tree) 2025-02-04 16:29:03 pgimeno ty for the code, I will try to see if I can make it run somehow 2025-02-04 16:29:27 although it would be different because this version of the interpreter is not trying to be forth like the other did 2025-02-04 16:30:55 I will play with it later and come here to cry when does not work 2025-02-04 16:31:25 ok ^.^ 2025-02-04 16:38:04 pgimeno are you spanish? 2025-02-04 16:38:52 I'm from barcelona, asking cause the link has .es 2025-02-04 16:40:00 is that site your bussiness? 2025-02-04 16:40:36 oh yes I am, Pedro Gimeno 2025-02-04 16:40:53 it was at some point, now the business parts are obsolete 2025-02-04 16:42:16 I wanted to send a resume :/ 2025-02-04 16:42:51 oh, sorry 2025-02-04 20:27:49 vms14: being from Barcelona, would you say you were Spanish or not? 2025-02-04 20:58:51 yes 2025-02-04 20:59:25 catalonia independence was only a joke 2025-02-04 20:59:37 we are not going anywhere 2025-02-04 21:56:57 pgimeno I can't 2025-02-04 21:57:07 at least not like I'm trying 2025-02-04 21:57:16 I can evaluate only one branch 2025-02-04 21:57:22 but serves me well 2025-02-04 21:57:38 I will only optimize tail calls and learn the lesson 2025-02-04 21:58:00 and no mutual recursion 2025-02-04 22:03:22 are you sure about the memory leak, btw? Python is reference counted but it has a different kind of garbage collector for the cases of circular references 2025-02-04 22:03:53 the memory leak in Perl when you make a self-referencing closure, I mean 2025-02-04 22:06:22 in perl one is probably supposed to use weaken somewhere 2025-02-04 22:10:01 yeah if a closure retains a variable of itself perl cannot reclaim it 2025-02-04 22:10:53 as thrig mentions you can weaken a reference so it should work 2025-02-04 22:10:53 and you can also store it somewhere 2025-02-04 22:11:41 I guess I can manage to provide something refining what I was doing 2025-02-04 22:12:19 but I have no idea how to fix it and it won't provide much more than what perl already provides 2025-02-04 22:12:56 so I'll just add tco when I see is a tail call and let the perl return stack do it's job when not 2025-02-04 22:13:59 the cool thing is that I could use compression mechanisms to make the return stack more compact, with very bad performance since it will have to decode it 2025-02-04 22:14:31 but at the end it will have a limit so it's a lot of effort to get at the same place I was already 2025-02-04 22:14:49 tco is the only thing that makes sense to implement 2025-02-04 22:15:08 was cool to play with the concept though 2025-02-04 22:15:21 so I thank you again for that code 2025-02-04 22:15:41 :-) 2025-02-04 22:24:50 I like the fact that you can make in the interpreter the dept limit a variable and let the user choose its own recursion depth limit