h3rald / min

A small but practical concatenative programming language and shell
https://min-lang.org
MIT License
310 stars 23 forks source link

Exited abruptly from repl #138

Closed ghost closed 3 years ago

ghost commented 3 years ago

0 (dup 5 >) (dup puts) () 'succ linrec putting this in repl did a sudden exit in repl.

I found a NEW(New Exit Way) in min repl. Could you explain why the sudden exit happens?

h3rald commented 3 years ago

Erhm... that my friend is called infinite recursion 😬

At present there are no checks, but Nim seem to limit to 2000 or something then throws an exception (in dev mode). I will see if I can trap it...

ghost commented 3 years ago

Erhm... that my friend is called infinite recursion 😬

What is wrong with my code? I don't understand why recursion that it makes.

h3rald commented 3 years ago

OK I tried handling the segfault but in this case Nim doesn't really help: I cannot trap anything in case of a stack overflow. I would have to somehow limit recursion but I'd rather not do it... (and it would be quite tricky to do it properly).

What's wrong with your code? Right well, have a look at the docs about linrec. Basically what happens is the following.

You have this:

0 (dup 5 >) (dup puts) () 'succ 

So:

You get the picture...

ghost commented 3 years ago

5 (dup 0 ==) 'succ (dup pred) '* linrec -> 5 4 3 2 1 1 * * * * *

I'm a bit confused about linrec usage. Could you explain how it works?

h3rald commented 3 years ago

Sure... Although for better theory I recommend the Introduction to Joy, and specifically the part about recursive combinators. Joy is the concatenative language that inspired min btw.

So linrec implements linear recursion and it takes 4 quotations, which can be identified as follows:

So the algorithm goes like this:

  1. if "if-part" returns true
    1. then "then-part" is executed and the execution ends.
  2. otherwise "else1-part" is executed, then:
    1. recursion occurs, go back to 1.
    2. "else2-part" is executed.

It is quite a cool way to generalize linear recursion, just it takes a bit to get used to it. 😊

ghost commented 3 years ago

Sure... Although for better theory I recommend the Introduction to Joy, and specifically the part about recursive combinators. Joy is the concatenative language that inspired min btw.

So linrec implements linear recursion and it takes 4 quotations, which can be identified as follows:

  • "if-part"
  • "then-part"
  • "else1-part"
  • "else2-part"

So the algorithm goes like this:

  1. if "if-part" returns true

    1. then "then-part" is executed and the execution ends.
  2. otherwise "else1-part" is executed, then:

    1. recursion occurs, go back to 1.
    2. "else2-part" is executed.

It is quite a cool way to generalize linear recursion, just it takes a bit to get used to it. 😊

Oh... i thought which else2-part as finally stage that runs after every conditional control:

Evaluates quot1.
If quot1 evaluates to true, then it evaluates quot2.
Otherwises it executes quot3 and recurses using the same four quotations.
**Finally**, it executes quot4.
ghost commented 3 years ago

That was helpful!!

ghost commented 3 years ago

Introduction to Joy

I wonder what differs min from Joy? I looked and didn't see any difference surprisingly. Or... this is a Nim wraption of Joy?

But i wrote so many things with min and i like min more always.

h3rald commented 3 years ago

Well, min was inspired by Joy. The syntax is more or less the same, the most notable difference being parenthesis instead of square brackets for quotations. Also most of the operators in the stack modules were implemented based on Joy combinators and other combinator birds.

I was always fascinated by Joy. However, Joy is not practical, it is an academic language prototype to illustrate concatenative programming. Unlike Joy, min:

ghost commented 3 years ago

I understood the differencies, thanks!

ghost commented 3 years ago

Look at #139