"as" keyword in type expression

7.4 Type expressions of the Inria manual shows that the “as” keyword can be used as part of a type expression. Initially I thought it might be used as some sort of alias for part of the type expression so it can be used in later part of expression. However all my attempts to use it are syntax error, and I can’t find any single example on google and grepping in core and a few other git repo only shows “as” usage in pattern matching.

Can anyone show an example of proper use of the “as” keyword in type expression? Thanks in advance.

1 Like

You were probably missing parentheses:

let f: ( 'a -> 'a as 'b ) -> 'b = fun x -> x
(* or even *)
let g: ((('a*'a as 'b) * 'b) as 'c) -> 'c = fun x -> x

From the standard library, the type probably can’t be represented without the use of as.

Can the keyword be used with recursive type with parametric arguments similar to the following?

type 'a node = | A of 'a node | B
(* following line has syntax error *)
type 'a node as 'an = | A of 'an | B

Not exactly like that, but there are quite close possible uses:

type 'a node = | A of ('a node as 'an) | B of 'an | C of 'a;;
type 'a node' = [`A of 'an | `B of 'a] as 'an;;

Or

type 'a t = Node of 'at | B
  constraint 'at = 'a t;;