# GADT and OR pattern matching

**URL:** <https://discuss.ocaml.org/t/gadt-and-or-pattern-matching/4154>\
**Category:** Learning\
**Tags:** gadt\
**Created:** [July 26, 2019, 8:49pm UTC](https://discuss.ocaml.org/t/gadt-and-or-pattern-matching/4154 "2019-07-26T20:49:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cloudyhug](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cloudyhug/32/2399_2.png) [@cloudyhug](https://discuss.ocaml.org/u/cloudyhug)\
**Post date:** [July 26, 2019, 8:49pm UTC](https://discuss.ocaml.org/t/gadt-and-or-pattern-matching/4154/1 "2019-07-26T20:49:45Z")

</div>

Hi everyone. I have noticed a very strange behaviour for GADTs when using pattern matching. Consider the following type and function :

```ocaml
type _ t =
  | A : int t
  | B : int t
  | C : char t

let f : type a. a t -> (a -> string) = function
  | A -> string_of_int
  | B -> string_of_int
  | C -> String.make 1

```

Then the function works and it compiles. However, when combining the match cases of the same type for a syntactic sugar purpose…

```ocaml
let g : type a. a t -> (a -> string) = function
  | A | B -> string_of_int
  | C -> String.make 1

```

… I’m getting this error :

```nohighlight
Error: This pattern matches values of type int t
       but a pattern was expected which matches values of type a t
       Type int is not compatible with type a 

```

I’m using OCaml 4.07.0. Is that a known bug for this OCaml version, or is it something normal coming from the GADT type system that is a bit different from the classic one ? Thanks for explanations.

---

<div class="post-metadata">

**Author:** ![Levi\_Roth](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/levi_roth/32/2268_2.png) [@Levi\_Roth](https://discuss.ocaml.org/u/Levi_Roth)\
**Post date:** [July 26, 2019, 9:30pm UTC](https://discuss.ocaml.org/t/gadt-and-or-pattern-matching/4154/2 "2019-07-26T21:30:17Z")

</div>

In OCaml 4.07, you can’t use GADTs in or-patterns.

In OCaml 4.08 [you can use GADTs in some or-patterns](https://github.com/ocaml/ocaml/pull/2110), but you can’t make use of type equations introduced by or-patterns (in your case, the equation `a = int`), so unfortunately your specific example still won’t compile.

---

<div class="post-metadata">

**Author:** ![YueLiPicasso](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yuelipicasso/32/3396_2.png) [@YueLiPicasso](https://discuss.ocaml.org/u/YueLiPicasso)\
**Post date:** [February 12, 2022, 7:36pm UTC](https://discuss.ocaml.org/t/gadt-and-or-pattern-matching/4154/3 "2022-02-12T19:36:07Z")

</div>

I find [a similar problem](https://discuss.ocaml.org/t/locally-abstract-types-in-an-or-pattern-with-a-gadt-scrutinee-clash-during-refinement/9315) with or-pattern and GADT …

---

<div class="post-metadata">

**Author:** ![glyh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/glyh/32/4668_2.png) [@glyh](https://discuss.ocaml.org/u/glyh)\
**Post date:** [September 4, 2023, 5:33am UTC](https://discuss.ocaml.org/t/gadt-and-or-pattern-matching/4154/4 "2023-09-04T05:33:36Z")

</div>

Is there a way to workaround it?

---

<div class="post-metadata">

**Author:** ![lukstafi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lukstafi/32/3442_2.png) [@lukstafi](https://discuss.ocaml.org/u/lukstafi)\
**Post date:** [September 4, 2023, 8:47am UTC](https://discuss.ocaml.org/t/gadt-and-or-pattern-matching/4154/5 "2023-09-04T08:47:05Z")

</div>

Maybe someone has a PPX to expand OR branches in `match with`? IIRC there is an optimization step to merge case branches with the same code, so this is only affecting the power and time of type inference.
