polymorphic-variants

Polymorphic variants and let%bind type error

早过忘川 提交于 2020-06-29 06:39:28
问题 I'm trying to use the technique in Composable Error Handling in OCaml (Result type with polymorphic variants for errors) for some code I've written. The types of the functions I'm trying to use look like this: val parse : parser -> token list -> (Nominal.term, [> `ParseError of string ]) Result.t val lex : lexer -> string -> (token list, [> `LexError of string ]) Result.t My attempt at composing them is this: let lex_and_parse : parser -> lexer -> string -> (Nominal.term, [> `ParseError of

Polymorphic variants and type signatures

前提是你 提交于 2020-06-16 19:15:30
问题 (This is an extension / distillation of Polymorphic variants and let%bind type error) Consider the following code: Version 1: let x : [> `Error1 ] = (`Error1 : [> `Error1 ]) let y : [> `Error1 | `Error2 ] = x Version 2: let x : [> `Error1 ] = (`Error1 : [ `Error1 ]) let y : [> `Error1 | `Error2 ] = x Version 1 typechecks, but version 2 fails (I'm compiling with 4.09.0): File "test.ml", line 2, characters 33-34: 2 | let y : [> `Error1 | `Error2 ] = x ^ Error: This expression has type [ `Error1

Wildcard pattern overriding subtype constraint on polymorphic variant

强颜欢笑 提交于 2019-12-10 04:35:20
问题 Given these types type a = [ `A ] type b = [ a | `B | `C ] and this function let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | `C -> "C" applying a value of type a works without issue, as expected: let a: a = `A let _ = pp a However, if the function is modified to include a wildcard pattern let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | _ -> "?" even though everything else remains the same, it now yields the following error (on let _ = pp a ): This expression has type

Wildcard pattern overriding subtype constraint on polymorphic variant

允我心安 提交于 2019-12-05 07:20:13
Given these types type a = [ `A ] type b = [ a | `B | `C ] and this function let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | `C -> "C" applying a value of type a works without issue, as expected: let a: a = `A let _ = pp a However, if the function is modified to include a wildcard pattern let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | _ -> "?" even though everything else remains the same, it now yields the following error (on let _ = pp a ): This expression has type b -> string but an expression was expected of type a -> 'a Type b = [ `A | `B ] is not compatible with