I implemented Goto in OCaml

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024

Комментарии • 146

  • @FastNotSave
    @FastNotSave Год назад +180

    This is even better because your labels are runtime values, so you can compute goto destinations at runtime via string interpolation or such, making it even more readable.

    • @FastNotSave
      @FastNotSave Год назад +22

      Also, you can have multiple labels with the same name, meaning the same goto call will jump to different labels depending in which block the goto is (since you do a linear search through the list), increasing maintainability.

    • @rogo7330
      @rogo7330 Год назад +1

      GNU C extensions have runtime-evaluated labels. But GNU C is mostly heresy (despite being actual better version of C, not like C++), so be carefull and put ifdef __GNUC__ everywhere.

    • @samgiz
      @samgiz Год назад +8

      @@FastNotSave A goto call should always jump to the same instance of a label (the first one in the list) as it iterates through all blocks from the beginning, not from where it was called.

    • @FastNotSave
      @FastNotSave Год назад +16

      @@samgiz that is correct, my bad. The code was so readable that I got confused.

    • @johangamb
      @johangamb Год назад +4

      @@samgiz it would be a trivial change to make the iteration looking for next label start from the previous block being executed (and loop back to start again if not found on rest of list), in which case you really could have multiple identical labels, as the guy with headphones wrote, drastically increasing maintainability and code scanability

  • @pasc4le
    @pasc4le Год назад +90

    I started to doubt it some weeks ago, but now I'm sure. His villain arc has started.

  • @starup4960
    @starup4960 Год назад +54

    Compsoy: recursion
    Compchad: recursion in a try catch
    10/10 video

    • @monad_tcp
      @monad_tcp 9 месяцев назад

      I though he was going to implement full blown call/cc , or import setjmp/longjmp , but a try/catch is essentially that, setjmp/longjmp. Use this knowledge with care.

    • @angelcaru
      @angelcaru 6 месяцев назад +2

      10/10 comment, i managed to segfault in Python

    • @angelcaru
      @angelcaru 6 месяцев назад +1

      wtf?? its not letting me reply to another comment

  • @mosomiliardaru
    @mosomiliardaru Год назад +181

    dude you've reached pure programming god tier by now

    • @ribosomerocker
      @ribosomerocker Год назад +12

      ocaml isn't pure

    • @__gadonk__
      @__gadonk__ Год назад +23

      @@ribosomerocker 🤓

    • @ribosomerocker
      @ribosomerocker Год назад +2

      ​@@__gadonk__ 🤓🤓🤓🤓🤓🤓🤓🤓🤓

    • @K5RTO
      @K5RTO Год назад +13

      @@ribosomerocker what man has made filthy, a god makes pure

    • @miran248
      @miran248 Год назад +1

      He got so pure he overflowed.

  • @anon_y_mousse
    @anon_y_mousse Год назад +19

    I recently installed OCaml to get some project that someone else wrote to compile so I could use it. I learned a little bit of ML like 20 years ago and knew Caml and OCaml existed, but never used them. Your video hasn't changed my mind on goto, but it has changed my mind about even getting into OCaml.

  • @AlgoFodder
    @AlgoFodder Год назад +10

    Senior Dev strats: "Let's hallucinate like ChadGPT".

  • @replikvltyoutube3727
    @replikvltyoutube3727 Год назад +10

    Tsoding has ascended into implementing goto, as recursion syntactic sugar

    • @monad_tcp
      @monad_tcp 9 месяцев назад +1

      everything is syntactic sugar

  • @fille.imgnry
    @fille.imgnry Год назад +3

    Holy moly! I last watched your videos 2 years ago, and there were so much ”more” of you then. Nice! Looking good. Your content is still top notch as usual.

  • @januszkszczotek8587
    @januszkszczotek8587 Год назад +28

    19:10 You have optional arguments with default values. E.g. `let goto_block ?(name = None) ...` and call it `goto_block ~name:(Some "foo")`. Mutual recursion is possible by chaining the function definitions with `let rec foo = ... and bar = ...`.

  • @paoloose
    @paoloose Год назад +8

    beautiful control flow, I didn't ever wonder before if goto could be implemented in any language
    teachers only resort to saying that goto is garbage but you come up with this beauty of state machine

  • @R3AktoRMacedonia
    @R3AktoRMacedonia Год назад +12

    Bro you look like stereotypical "Mad Scientist" on the thumbnail :)))))

  • @frechjo
    @frechjo Год назад +7

    I think that function used as an intermediate step, to control where to "jump" execution, is usually called a "trampoline".
    I've seen it used similarly to implement things like tail call optimization in languages that don't have it, like Javascript XD.
    Which seems like a completely different thing at first sight, but it¡s actually related. Tail calls behave as a jump or goto, and that's why they can be eliminated in the first place.

  • @matrixnorm6672
    @matrixnorm6672 7 месяцев назад +3

    it's over for gotocels

  • @javajava7867
    @javajava7867 Год назад +6

    Oh , first time a project shorter than an hour, you're becoming a youtuber🎉🎉

  • @two-spikes
    @two-spikes Год назад +3

    best preview ever made on RUclips
    now implement gotos in C like they're not yet implemented

  • @freesoftwareextremist8119
    @freesoftwareextremist8119 Год назад +4

    What CompSoy programmers have to do to mimic a fraction of the power of Common Lisp.

  • @teej_dv
    @teej_dv Год назад +9

    great video :)

  • @neshkeev
    @neshkeev Год назад +18

    Why don't you implement a continuation monad with the call/cc function in OCaml? It will allow you implement the `goto` thing more efficiently

  • @dulr10
    @dulr10 Год назад +16

    You either die a hero, or live long enough to see yourself become the villain.
    That was the comment I would put while watching the first part of the video. I changed my mind when the irony and meming ended.
    I gotta say that this is the best video you uploaded in a while in my books. The former Tsoding (the real chad imo) would be proud of you.

  • @l.piekha100
    @l.piekha100 Год назад +10

    Siberian Chud

  • @necuz
    @necuz Год назад +15

    Your approach of skipping blocks that shouldn't be executed reminded me of the same approach being used to implement loops in "M/o/Vfuscator", a C compiler that generates only mov instructions.

  • @Phillip3223
    @Phillip3223 Год назад +6

    Actually I have that whole "lable" vs "label" spelling issue too

    • @necuz
      @necuz Год назад +2

      theater theatre 🤷🏻‍♂

  • @ben__01
    @ben__01 Год назад +5

    millions must ocaml

  • @parseemizuhasi9338
    @parseemizuhasi9338 3 месяца назад

    The best CPS(Continue Passing Style) vedio.

  • @warko4
    @warko4 Год назад +3

    GOTO reminded me of Basic but also MacroB in cnc programming, I'm using it a lot with G-CODE on Swiss lathes..

  • @Lars-ce4rd
    @Lars-ce4rd 5 месяцев назад +1

    Beautiful and useful. I love DRY code. Also thanks for the gist so I can just copy-paste that into my projects. Sincerely, thank you. See you in hell.

  • @johetajava
    @johetajava 9 месяцев назад

    I love your presentation style :'D
    Keep up with your divine mission!

  • @davidrudpedersen5622
    @davidrudpedersen5622 Год назад +3

    Very gigachad

  • @felix.5507
    @felix.5507 Год назад +1

    I see one possible improvement and two problems.
    If you change line 16 to also [] -> throw Goto entry than you have even some kind of interfunction jumps.
    The problem that arises from that is what happens if you try to jump a Label that is not defined and what if the user catches or throws Gotos themselves.
    But the bigger problem I see with this approach is to have the jumps being calculated at runtime. That kind of violates the advantage of goto in my eyes because of the huge Overhead beeing generated.
    But I am no programmer and don't have any education in this field!
    But I love your unique apoaches to everything you do. Keep on comming up with such interesting ideas.
    I hope you can keep making content and wish you the best of luck.

  • @MrJCraft
    @MrJCraft Год назад +3

    This is true art!

  • @HelloThere-xs8ss
    @HelloThere-xs8ss Год назад +2

    What a villain to admire

  • @hooxenv9692
    @hooxenv9692 Год назад +1

    chad goto and compsoy LOL will be borrowing those for sure!

  • @JeremyGluckStuff
    @JeremyGluckStuff Год назад +5

    CompSoy

  • @000dr0g
    @000dr0g Год назад +2

    as an F# hacker, I approve this message.

  • @Novascrub
    @Novascrub Год назад +2

    Somebody read the Lambda papers.

  • @alurma
    @alurma Год назад +1

    Algebraic effects are awesome

  • @Q__Q
    @Q__Q Год назад

    this is one of the only videos that i actually understood what he was talking about

  • @Scouarn
    @Scouarn Год назад +2

    I tried to do it myself before watching. I defined a binding operator that does the try-with, but you can't jump forward this way (I'm still hacking it I have some ideas).
    Also I think it can be done by defining labels as mutually recursive functions that tail-call each other.
    My half working solution looks like that :
    let* loop = make_label () in
    goto loop;
    goto exit; (* Doesn't work since exit is not defined *)
    let* exit = make_label () in
    ...

  • @realdebil.
    @realdebil. Год назад +2

    wait, it's all a state machine? always has been~

  • @angelcaru
    @angelcaru 6 месяцев назад +1

    badass intro

  • @primosoma
    @primosoma Год назад +4

    i like compsoy even if i don't know what it means

  • @dupdrop
    @dupdrop Год назад +6

    I learned some lua for configuring neovim recently and was surprised to discover that in lua 5.2 they actually added goto to the language. Does that make lua the chad-est dynamic language now? Kind of ironic since they don't even know the only true correct number to start counting from, but then again, would a true chad care about that?

    • @replikvltyoutube3727
      @replikvltyoutube3727 Год назад

      The tables start at 1

    • @dupdrop
      @dupdrop Год назад +2

      @@replikvltyoutube3727 I know. Real programmers count up from zero ;)

    • @yjlom
      @yjlom Год назад +5

      @@dupdrop real programmers count down

    • @vnshngpnt
      @vnshngpnt Год назад

      Based Lua

    • @safariknight3239
      @safariknight3239 Год назад

      ​@@yjlom so would the first element be index -1 or 0?

  • @alexanderkovalev391
    @alexanderkovalev391 11 месяцев назад

    hmm, looks like an interesting challenge for me to sneaky commit something like that into our production erlang code

  • @Tekay37
    @Tekay37 Год назад +1

    I'm not sure if you bullied Exceptions, functional languages or both here. Either way, I like it.

  • @luvincste
    @luvincste Год назад +2

    coming from c#, isn't there a conditional catch? you could divide the blocks in multiple catches each one for a label, and then throwing you owuld go in that 'case', like a sort of switch

    • @monad_tcp
      @monad_tcp 9 месяцев назад

      no, we don't do that here, we do match, no label jumps from machine code

  • @monad_tcp
    @monad_tcp 9 месяцев назад

    20:06 shot out to F#'s computational expressions (not to use the compsoy "monadic")

  • @NuclearShadowYT
    @NuclearShadowYT Год назад +1

    one way to introduce new syntax in a language is to make a preprocessor which converts your new syntax to an implementation in the language

  • @caio_c
    @caio_c Год назад +1

    I wouldn't be able to figure out something like that, tsoding is slowly ascending to programmer mage levels

  • @realdebil.
    @realdebil. Год назад +1

    i love you mr. tsoding

  • @michalbotor
    @michalbotor Год назад +2

    todaY wE arE goinG tO enD thE worlD
    so lets start with implementing 'hello world'

  • @adrient8288
    @adrient8288 9 месяцев назад

    How is this so close to your old channel type of video and simultaneously the polar opposite of it

  • @ihalseide
    @ihalseide Год назад

    This reminds me of the special operator LABELS in Lisp

  • @martinstrnad3243
    @martinstrnad3243 Месяц назад

    Very similar to tagbody in common lisp. 👍

  • @arcane.wizard
    @arcane.wizard Год назад +6

    I love your content mate. This is one of the few channels that provides based HQ content for C chads. Nowadays, the majority of YT tech. content creators 're making garbage for JS soydevs:
    ""x Is A gAmE cHaNgEr!"
    "sToP uSiNg y! uSe ThIs wheelReinvented.js nOdE pAcKaGe InStEaD!"

    • @CPSPD
      @CPSPD Год назад

      @@andrewdunbar828I literally cannot stand Acerola but the video content is always so good. Recommended definitely.

  • @Vulto166
    @Vulto166 Год назад

    That was fun!
    Thank you Tsoding!

  • @ismaelvc3728
    @ismaelvc3728 4 месяца назад

    Instead of an unconditional "goto loop", it could have been "if !i >= 10 then goto loop", no need for the "out" label, it would be a do-while though.

  • @pstrusi
    @pstrusi Год назад +1

    I've noticed that you always use Emacs for your workflow, btw superfast.
    My question: do you use it cause is really highly efficient in comparison to others modern IDEor text editors. I'm aware that this is a controversial area, but as an amateur programmer, is very confusing and I'd like to know professional opinions. Many, nowadays, are commenting that VScode is the replacement of Emacs. Do you agree? Emacs is obviously more powerful but young generations are not sticking to Emacs community. Thanks for any insight

  • @WilderPoo
    @WilderPoo Год назад +1

    State of the art

  • @mellowyellow7523
    @mellowyellow7523 Год назад +2

    SOMEONE TELL TSODING TO STOP SAYING RIGHT EVERY 5 SECONDS I CANT LISTEN TO IT

  • @underageh4x0r
    @underageh4x0r Год назад +2

    pure evil

  • @valovanonym
    @valovanonym Год назад +1

    What a madman

  • @el_chivo99
    @el_chivo99 Год назад

    conditional jumping would also be easy under this approach!!

  • @ebriussenex9850
    @ebriussenex9850 5 месяцев назад

    Сверхчеловек сверхразум

  • @dirrelito
    @dirrelito Год назад

    I think the same thing in Haskell would have been much more beautiful. Goto statements should be implemented in a pure way.

    • @dirrelito
      @dirrelito Год назад

      callCC seems like magic--- but your implementation in this stream seems like essentially CPS

  • @abdelrahmandwedar
    @abdelrahmandwedar 11 месяцев назад

    Structured programming: NOOOOOOOOOOOO!!!!

  • @cynocephalusw
    @cynocephalusw 5 месяцев назад

    Tom Harding implemented GoTo by using circular reasoning.

  • @liquidsnake6879
    @liquidsnake6879 Год назад

    "Today i woke up and chose the violence" and that's the story of how i subscribed and that was before the "double dick operator" which is a beautiful name and an excellent way to ensure nobody ever forgets it lol

  • @kuyajj68
    @kuyajj68 Год назад

    The thumbnail forced me to watch this 😂

  • @monad_tcp
    @monad_tcp 9 месяцев назад

    28:51 can your C even do that !
    If it compiles, it works !

  • @rodelias9378
    @rodelias9378 Год назад

    That was great!!

  • @ilovepeaceandplaying8917
    @ilovepeaceandplaying8917 Год назад +1

    pog

  • @csori1075
    @csori1075 Год назад +2

    CompSoy lmao

  • @beachneosculpordwarf
    @beachneosculpordwarf Год назад +1

    I am very afraid of mutations, what do I do

  • @renato360a
    @renato360a Год назад

    this isn't really goto (fully) because you can't have multiple gotos inside one "block". Unless you make redundant copies of chunks of blocks.

  • @timwhite1783
    @timwhite1783 Год назад

    You get a like just for that thumbnail 👍

  • @youtubelisk
    @youtubelisk Год назад +2

    I have a hard time understanding tsoding's sarcasm. Most things just whoosh by me.
    Are people scared of for loops?
    Do students use recursive?
    What is soycaml?
    ~$ man tsoding-jokes

    • @ea_naseer
      @ea_naseer Год назад

      zig doesn't have a for loop.

    • @anon_y_mousse
      @anon_y_mousse Год назад +1

      @@ea_naseer Yeah it does. It's a weird syntax, but it's there.

  • @straight2heaven_
    @straight2heaven_ Год назад +3

    нифига у тебя лицо злое на превью.

    • @TsodingDaily
      @TsodingDaily  Год назад +4

      Мизицем ноги об стол ударился специально для превью.

    • @spidorak254
      @spidorak254 Год назад

      @@TsodingDaily Мизинцем* bro

  • @IBelieveInCode
    @IBelieveInCode Год назад +2

    "I implemented Goto in OCaml"
    Yes, but what for ?

    • @diegorocha2186
      @diegorocha2186 Год назад +8

      To show you that he can and you cannot!

    • @vitalyl1327
      @vitalyl1327 Год назад

      Language which does not have a goto is a crappy compilation target - and, therefore, a crappy metalanguage. You cannot compile parsers efficiently, for example, if you don't have a decently fast goto.

  • @minzmango1915
    @minzmango1915 Год назад

    i love you

  • @motorbreath69
    @motorbreath69 4 дня назад

    If you install utop you can run OCaml REPL inside Emacs using C-c C-e to eval function and C-c C-b to eval buffer just like emacs lisp. Makes developing much easier.

  • @ErikBongers
    @ErikBongers 6 месяцев назад

    I wouldn't trust a language that hasn't goto. And that hasn't nullptr.

  • @evlogiy
    @evlogiy Год назад

    Wait. So Rust is basically a low level ML language!? I just started with Rust and never tried OCaml, but this looks very familiar for some reason...

    • @SimonBuchanNz
      @SimonBuchanNz Год назад

      Rust was initially implemented in ocaml, and it takes several details from ocaml including a lot of it's naming, but it deliberately draws from a lot of different types of languages.

  • @trejohnson7677
    @trejohnson7677 Год назад

    yes.

  • @user-pe7gf9rv4m
    @user-pe7gf9rv4m 8 месяцев назад

    this reminds me of single-store/assign in LLVM IR type stuff

  • @ikilledthemoon
    @ikilledthemoon Год назад

    Why are you scowling like judge dredd

  • @jorios550
    @jorios550 Год назад

    I wonder how it would look lime in a language that supports macros

  • @pookiepats
    @pookiepats 4 месяца назад

    Ocaml soy? Lmfao

  • @user-gj2xx4ns1w
    @user-gj2xx4ns1w Год назад

    здарова, будут туториалы для начинающих?

  • @idk24lol
    @idk24lol Год назад

    something

  • @alurma
    @alurma Год назад +1

    Good but using if is soy. Computed goto better

    • @alurma
      @alurma Год назад +1

      Not chad enough Kappa

  • @postmodernist1848
    @postmodernist1848 Год назад

    akshually, functions have namespacing and labels don't so this "goto implementation" has some limitations
    P. S. Don't call me 🤓

  • @Waldganger64
    @Waldganger64 Год назад

    HOW DARE YOU.

  • @nothappyz
    @nothappyz Год назад +1

    First try

    • @nothappyz
      @nothappyz Год назад

      Also maybe doing a video trying to hack it into the compiler might be cool?

  • @user-tk2jy8xr8b
    @user-tk2jy8xr8b Год назад

    Try using CPS for that purpose next time ;)

  • @suryaprakash-sh5bf
    @suryaprakash-sh5bf Год назад

    Sir can I have your neovim config

  • @anhladkyi
    @anhladkyi Год назад

    When AI videos again??

  • @CherishedMe
    @CherishedMe 4 месяца назад

    so basic...

  • @bruisedbug
    @bruisedbug Год назад

    im sorry, but you look like "the west has fallen" wojak

  • @SimGunther
    @SimGunther Год назад +4

    Congrats, you rediscovered call/cc

    • @alurma
      @alurma Год назад +1

      But typed!

    • @frechjo
      @frechjo Год назад

      call/cc preserves the execution context, it saves the state of the stack, no?
      This is a trampoline, it behaves similarly, but without preserving the state of the program, it only jumps to where it's told.

  • @varshneydevansh
    @varshneydevansh Год назад +2

    master