Skip to content

Locally abstract types #20

@mimoo

Description

@mimoo
(* an example with type variables that produces a bug *)
let map (x: 'a * 'a) (f:'a -> 'b) : ('b * 'b) =
	let x1, x2 = x in	
	(f x1, x2)

gives

val map : 'b * 'b -> ('b -> 'b) -> 'b * 'b = <fun>

whereas it should be (f x1, f x2)

keeping type abstracts prevents unification, which would have had the compiler yell at us:

let map (type a b) (x: a * a) (f:a -> b) : (b * b) =
	let x1, x2 = x in	
	(f x1, x2)

gives us: Error: This expression has type a but an expression was expected of type b

Also, it's useful for GADTs (see GADT section)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions