Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml - What data type is some and none?

Tags:

types

ocaml

If I am using Some and None combo in a list what would be the data type of the list? Is it always 'a? Or is there some sort of a type for Some/None?

let listVar : (* type here *) list = [Some 4; Some 3; None; Some 2];;

If I put int it gives me error:

This expression has type int option * int option * 'a option * int option but is here used with type int

When I put 'a it compiles fine but the basic OCaml tutorial says (I made references to other languages to explain my question better):

It won't be clear yet why polymorphic functions are useful, but they are very useful and very common, and so we'll discuss them later on. (Hint: polymorphism is kind of like templates in C++ or generics in Java 1.5).

I thought this was like reference/pointer in other languages, which actually made sense. But now, I really don't understand what type is None. Same goes with Some.

Also, I know I should ask two questions in one question, but this one has a strong relation to the previous question. What is the point of Some? I usually see it used when None is used. If I implement the above list without Some, it still compiles but the list structure doesn't have the "option" flag, which I am guessing means optional (I cant seem to find anything on the internet regarding this). Can someone provide me with one case this is useful?

like image 710
Gasim Avatar asked Feb 01 '14 13:02

Gasim


People also ask

What is none in OCaml?

Options are an Ocaml standard type that can be either None (undefined) or Some x where x can be any value. Options are widely used in Ocaml to represent undefined values (a little like NULL in C, but in a type and memory safe way). This module adds some functions for working with options.

What is type unit in OCaml?

unit — The Unit Value The built-in printing functions return type unit, for example; the value of a while or for loop (both of which are expressions, not "statements") is also of type unit. OCaml uses this convention to help catch more type errors.

What does and mean in OCaml?

The and keyword is used either to avoid multiple let (first example, I never use it for this but why not) or for mutually recursive definitions of types, functions, modules... As you can see in your second example : let rec debug stack env (r, ty) = ...

What is Val OCaml?

Well, val is a keyword in OCaml with several different uses. The cases you mention are both, in essence, that val is used in a module signature to specify values that appear in the module. Values are things like functions and expressions.


3 Answers

It is option type, indicating whether it has some value, or not.

If you come from Java or C# or other imperative programming, whenever you want to return a null in a method of Java, in OCaml, you should consider return a None

like image 103
Jackson Tale Avatar answered Nov 11 '22 04:11

Jackson Tale


What you have written here is a value of type (int option * int option * 'a option * int option) list, i.e. a list of quadruplets in which the first two component are optional integer, the next one is polymorphic so far (the Some case is undefined yet) and the last one is an optional integer. This is because , is the tuple separator. The list separator is ; so I guess you wanted to write

let listVar = [Some 4; Some 3; None; Some 2];;

Which has type (int option) list. Whenever you use Some or None with an arbitrary value of type 'a you get a value of type 'a option.

like image 45
Daniel Bünzli Avatar answered Nov 11 '22 04:11

Daniel Bünzli


Caml has a predefined `a option type (the `a means it can be an option of anything, in the same way that an `a list can be an int list, a float list...). It's actually a very simple type :

type `a option = Some of `a | None

Basically, a value with type `a option is either None or a value of type `a put inside a box : it is not the same type as `a (which is why you get an error when you tell the compiler to treat it as an int).

You should probably think of it as an explicit way of dealing with errors (more explicit than exceptions). Consider those (non tail recursive, poorly written) functions :

let rec max_exn l = 
  match l with
    | [] -> failwith "Empty list"
    | [x] -> x
    | x::tl -> max x (max_exn tl)

let rec max_option l = 
  match l with
    | [] -> None
    | [x] -> Some x
    | x::tl -> max x (max_option tl) (* max does work on options *)

The first one has type `a list -> `a, which makes it possible for the caller to forget about the empty list case. The second one has type `a list -> `a option, meaning that the error case will always be dealt with by the caller.

To put it another way : None is somewhat similar to null, except you can always tell from the type of a function whether or not it might return None (and if it might, the compiler forces you to deal with it).

like image 32
Jbeuh Avatar answered Nov 11 '22 05:11

Jbeuh