Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 529

(Lazy) Haskell undefined/bottom in OCaml

$
0
0

Haskell has a really swell undefined value, which lazily raises an exception (upon evaluation). Ocaml is of course strict, so as far as I can tell there is no equivalent of Haskell's undefined. This is unfortunate though, because it means there is no bottom type for values. Let's say I wanted an

val a : int

I could of course do

let a = failwith "undefined"let () =  print_string "something unrelated\n"

And this happily compiles. Unfortunately, upon running it we get the undefined exception (which is expected).

What I want is to let a be a bottom/undefined value without changing its type (so things like Lazy won't work). Is this possible?

extra details:So what I'm asking for probably sounds fairly silly. To curtail any commentary on why I shouldn't be doing this, allow me to briefly describe my use case. I'm writing a script that modifies the AST of an mli file to produce a "empty"ml file which matches its signatures. For the general case, it's possible to have val a : int in your mli so I need a way to generically synthesize a bottom type. failwith "undefined" works if all I needed was for compilation to succeed. But unfortunately, I also need to link this ml file against an OUnit test suite and run it (obviously the suite will fail, but the purpose is to be able to run it with -list-test so I can programmatically get a list of all tests).

more details:I recognize that the proper way to solve this (probably) is to write a function which can generate the bottom type for any generic type. For the builtin primitives (and list, option, etc.) this is simple enough (just verbose). This becomes more complicated with records (perhaps defined in the stdlib, but also may be defined in the same file or a different package). To handle this my AST transformer then needs to have a complete understanding of the OCaml type system and file importing strategies for packages, which is a lot more logic than I want to/should include in it.


Viewing all articles
Browse latest Browse all 529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>