Hacker Newsnew | past | comments | ask | show | jobs | submit | mads_hartmann's commentslogin

The following implementation comes closer to the original semantics of the ADT implementation.

  type _ value' =
    | GBool : bool -> bool value'
    | GInt : int -> int value'
  
  type _ expr' =
    | GValue : 'a value' -> 'a expr'
    | GIf : bool expr' * 'a expr' * 'a expr' -> 'a expr'
    | GEq : 'a expr' * 'a expr' -> bool expr'
    | GLt : int expr' * int expr' -> bool expr'
  
  let rec eval' : type a. a expr' -> a = function
    | GValue (GBool b) -> b
    | GValue (GInt i) -> i
    | GIf (b, l, r) -> if eval' b then eval' l else eval' r
    | GEq (a, b) -> (eval' a) = (eval' b)
    | GLt (a,b) -> a < b ;;
  
  eval' (GIf ((GEq ((GValue (GInt 2)), (GValue (GInt 2)))),
        (GValue (GInt 42)),
        (GValue (GInt 12))));;
  
  eval' (GIf ((GEq ((GValue (GInt 2)), (GValue (GInt 2)))),
        (GValue (GBool true)),
        (GValue (GBool false))));;
It does not support having separate return types for the branches on an if-statement but I consider that to be a good thing ;) I should probably update the ADT example to dis-allow that.


It should still be possible to simplify eval' by merging the two GValue cases, right?


As long as you're okay with your DSL subsiding all OCaml types you can do

    GLift : a -> a expr'


I don't think you can do that since the wrapped values have different types.


Well spotted, you're absolutely right. My hunch is that it's doable to maintain the semantics but I need to play around with the example a bit to know for sure.


The grey boxes are the input to the REPL. You should be able to just copy paste the contents of any of the boxes. The green boxes are the output of the REPL.


OCaml has an extremely predictable memory usage. Part III of Real World OCaml covers this is great detail[1]

GC pauses haven't hurt us really. We usually just spawn a ton of processes so if one process pauses a bit to GC we won't notice.

The module system is awesome. It's something i plan to cover next time I find the time to extend the article.

[1] https://realworldocaml.org/v1/en/html/pt03.html


Yeah, most of the services that I work on in my team at issuu are written in OCaml :)


Great to know people using such an interesting language (and also being based in Europe)!


Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: