# Map GADT type specification to function arguments

**URL:** https://discuss.ocaml.org/t/map-gadt-type-specification-to-function-arguments/946
**Category:** Learning
**Tags:** gadt
**Created:** [October 11, 2017, 2:20pm UTC](https://discuss.ocaml.org/t/map-gadt-type-specification-to-function-arguments/946 "2017-10-11T14:20:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)
#### Post date: [October 11, 2017, 2:20pm UTC](https://discuss.ocaml.org/t/map-gadt-type-specification-to-function-arguments/946/1 "2017-10-11T14:20:47Z")

</div>

Hi,

You probably know how many of the commandline parsers in OCaml work: you construct a “specification” which then leads to a function which has the same signature as the specification implied. For example Cmdliner.

I have created such a specification type [in this gist](https://gist.github.com/Leonidas-from-XIV/5fc4a34a0edcee615a526e2ed2c4943c) with some help, and I seems to be able to compose `String` and `Int` to get the proper `String -> Int` etc. signatures. But I am struggling on how to write the function which handles the decoding. The `decode_elem` function does not type for obvious reasons and I am unsure how to tackle that to begin with.

Needless to say, I’m a GADT newbie. I was told that [Farfadet](https://github.com/oklm-wsh/Farfadet) does something similar, but it also does a lot more than that and it is tricky for me to condense it to a smaller example.

Any help is appreciated!

---

<div class="post-metadata">

### Author: ![hcarty](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hcarty/32/1824_2.png) [@hcarty](https://discuss.ocaml.org/u/hcarty)
#### Post date: [October 11, 2017, 2:36pm UTC](https://discuss.ocaml.org/t/map-gadt-type-specification-to-function-arguments/946/2 "2017-10-11T14:36:47Z")

</div>

I haven’t had a chance to look at the rest, but for `decode_elem` you need some annotation:

```ocaml
let decode_elem : type a. a elem -> (string -> a) = function
  | String -> fun x -> x
  | Int -> int_of_string

```

---

<div class="post-metadata">

### Author: ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)
#### Post date: [October 11, 2017, 3:00pm UTC](https://discuss.ocaml.org/t/map-gadt-type-specification-to-function-arguments/946/3 "2017-10-11T15:00:41Z")

</div>

Oh wow, that works indeed! I wasn’t aware that the type system even allows that.

---

<div class="post-metadata">

### Author: ![kantian](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kantian](https://discuss.ocaml.org/u/kantian)
#### Post date: [October 11, 2017, 4:34pm UTC](https://discuss.ocaml.org/t/map-gadt-type-specification-to-function-arguments/946/4 "2017-10-11T16:34:36Z")

</div>

> [@Leonidas](#):
>
> I wasn’t aware that the type system even allows that.

It is explained [in the OCaml documentation](http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec238).
