# (anonymous?) polymorphic records

**URL:** <https://discuss.ocaml.org/t/anonymous-polymorphic-records/9256>\
**Category:** Learning\
**Created:** [January 31, 2022, 1:47pm UTC](https://discuss.ocaml.org/t/anonymous-polymorphic-records/9256 "2022-01-31T13:47:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrolland](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@nrolland](https://discuss.ocaml.org/u/nrolland)\
**Post date:** [January 31, 2022, 1:47pm UTC](https://discuss.ocaml.org/t/anonymous-polymorphic-records/9256/1 "2022-01-31T13:47:13Z")

</div>

Is there a way to avoid to create records only to preserve polymorphism ?

Say, for this, in haskell style

```haskell
h :: (forall r. (r -> a) -> (f r -> f b)) -> f a -> f b
h malg = malg id

```

---

<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:** [January 31, 2022, 3:13pm UTC](https://discuss.ocaml.org/t/anonymous-polymorphic-records/9256/2 "2022-01-31T15:13:56Z")

</div>

You can use objects, they can have polymorphic methods:

```ocaml
let f (id:<f:'a. 'a -> 'a>) = id#f 0, id#f "zero"

```

---

<div class="post-metadata">

**Author:** ![Maelan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/maelan/32/1184_2.png) [@Maelan](https://discuss.ocaml.org/u/Maelan)\
**Post date:** [January 31, 2022, 4:30pm UTC](https://discuss.ocaml.org/t/anonymous-polymorphic-records/9256/3 "2022-01-31T16:30:48Z")

</div>

The following doesn’t help reducing the syntactic noise, but note that when using a record for non-prenex polymorphism like this, your record has only one field and is immutable, so (with a recent enough OCaml) you can unbox it and get rid of the runtime overhead:

```ocaml
type ('a, 'b) fwrap = { f : 'r. ('r -> 'a) -> 'r list -> 'b list } [@@unboxed]

let apply_id : type a b. (a, b) fwrap -> a list -> b list =
  fun w xs -> w.f Fun.id xs
(* is compiled the same as just: *)
let apply_id_magic : type a b. (a, b) fwrap -> a list -> b list =
  fun w xs -> (Obj.magic w) Fun.id xs

let mwrap : type a. (a, a) fwrap = { f = List.map }
(* is compiled to nothing at all (alias of List.map). *)

```

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [January 31, 2022, 8:22pm UTC](https://discuss.ocaml.org/t/anonymous-polymorphic-records/9256/4 "2022-01-31T20:22:10Z")

</div>

> [@Maelan](#):
>
> ```auto
> (* is compiled the same as just: *)
> let apply_id_magic : type a b. (a, b) fwrap -> a list -> b list =
> fun w xs -> (Obj.magic w) Fun.id xs
> 
> ```

Not sure if I understood the gist of this comment, but, as written, this will almost certainly cause a segfault if executed.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [January 31, 2022, 8:23pm UTC](https://discuss.ocaml.org/t/anonymous-polymorphic-records/9256/5 "2022-01-31T20:23:59Z")

</div>

> [@nojb](#):
>
> Not sure if I understood the gist of this comment, but, as written, this will almost certainly cause a segfault if executed.

Ah, I understood it now, it is illustrating the `unboxed` attribute, makes sense. Sorry for the noise.

Cheers,  
Nicolas
