# Deconstructing polymorphic variant type

**URL:** <https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984>\
**Category:** Learning\
**Tags:** type-system, polymorphic-variants\
**Created:** [December 17, 2022, 12:42pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984 "2022-12-17T12:42:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![orbitz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/orbitz/32/110_2.png) [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Post date:** [December 17, 2022, 12:42pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984/1 "2022-12-17T12:42:13Z")

</div>

Is it possible to deconstruct a polymorphic variant type such that this does what you would intuit it should?

```ocaml
let foo = function
  | `OK a -> Ok a
  | otherwise -> Error otherwise

```

And the pseudo code for the type would be:

```ocaml
val foo : [> `OK of 'a | 'b] -> ('a, 'b) result

```

The closest thing I’ve been able to do includes the `OK` variable on on the `Error` side of the result.

I tried something like:

```ocaml
type ('a, 'b) foo = [> `OK of 'a | 'b] -> ('a, 'b) result

```

But this fails because `'b` can be anything, so I would need a way to constrain it to just polymorphic variants

---

<div class="post-metadata">

**Author:** ![Tim-ats-d](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/tim-ats-d/32/3268_2.png) [@Tim-ats-d](https://discuss.ocaml.org/u/Tim-ats-d)\
**Post date:** [December 17, 2022, 2:52pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984/3 "2022-12-17T14:52:16Z")

</div>

> [@orbitz](#):
>
> ```auto
> let foo = function
> | `OK a -> Ok a
> | otherwise -> Error otherwise
> 
> ```

Do you have tried to input this snippet in a toplevel?

It gives me the following:

```auto
val foo : ([> `OK of 'b] as 'a) -> ('b, 'a) result = <fun>

```

---

<div class="post-metadata">

**Author:** ![orbitz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/orbitz/32/110_2.png) [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Post date:** [December 17, 2022, 4:51pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984/4 "2022-12-17T16:51:48Z")

</div>

That includes the ``OK` constructor on the `Error` side as well, which is what I’m trying to get around.

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [December 17, 2022, 6:05pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984/5 "2022-12-17T18:05:06Z")

</div>

This is indeed not possible (and you would need to constraint `'b` to a polymorphic variant that does not contain ` `Ok` rather than a constraint-free polymorphic variants).

---

<div class="post-metadata">

**Author:** ![orbitz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/orbitz/32/110_2.png) [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Post date:** [December 17, 2022, 6:14pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984/6 "2022-12-17T18:14:24Z")

</div>

Thank you @octachron .

I don’t know much about type systems: is what I’m asking theoretically not possible or is it just a feature that Ocaml hasn’t implemented?

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [December 17, 2022, 7:22pm UTC](https://discuss.ocaml.org/t/deconstructing-polymorphic-variant-type/10984/7 "2022-12-17T19:22:44Z")

</div>

This is a deliberate limitation of the OCaml type system compared to the underlying theory. When objects and polymorphic variants were added to OCaml, the type system was deliberately designed to ensure that the complicated row type variables were never visible to the users.
