# How to use ppx\_show

**URL:** <https://discuss.ocaml.org/t/how-to-use-ppx-show/11405>\
**Category:** Learning\
**Tags:** ppx, ocaml\
**Created:** [February 12, 2023, 4:20pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405 "2023-02-12T16:20:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![RuRu](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@RuRu](https://discuss.ocaml.org/u/RuRu)\
**Post date:** [February 12, 2023, 4:20pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/1 "2023-02-12T16:20:31Z")

</div>

Hey, I am new to Ocaml, and trying to understand how the language works.  
My current struggle is understand how to add ppx\_show on types and classes.

Coming form Java or Rust its very simple to print objects. Either have a .toString generated on the objects, or add a Debug Trait for rust.

Now in Ocaml I found ppx\_show, but material on how to use it is much to be desired.

Here is my case and my dune config.

(

```auto
executable
 (public_name order_manager)
 (name main)
 (preprocess(pps ppx_deriving.show ppx_deriving.ord))
 (libraries orderbook lwt sexplib ppx_show.runtime)
 (modules main))

```

As for the code:

```auto

module OrderEntry = struct
  include Showable

  type t = {securityId: string ; price: int ; qty: int ; } [@@deriving show]
end 

module PriceBook(S: SideType) = 
  struct

    type t = { id: string ; price: int ; side: string ; mutable price_map: OrderEntry.t list } [@@deriving show] ;;

    let init_book p = { id = "0001" ; price = p ; side = "BID" ; price_map = [] }
end;;

module SellPriceBook = PriceBook(struct let side = Sell end)

let pb = SellPriceBook.init_book

let () = show pb 

File "bin/main.ml", line 8, characters 9-13:
8 | let () = show pb 
             ^^^^
Error: Unbound value show

```

What am missing to be able to print the content of the types ?

---

<div class="post-metadata">

**Author:** ![art-w](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/art-w/32/4728_2.png) [@art-w](https://discuss.ocaml.org/u/art-w)\
**Post date:** [February 12, 2023, 5:36pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/2 "2023-02-12T17:36:44Z")

</div>

Hello! Can you try:

```ocaml
let pb = SellPriceBook.init_book 42 (* was missing a price *)

let str = SellPriceBook.show pb (* was missing the module *)

let () = print_endline str (* show returns a string, not unit *)

```

To clarify, the PPX automatically defines the `show` function for your type next to it, in the module `SellPriceBook`. The `show` function is not defined globally which is why you got the unbound error 🙂

You should also be able to drop the `ppx_show.runtime` from your dune file (as you are actually using `ppx_deriving.show` and not `ppx_show`)

---

<div class="post-metadata">

**Author:** ![RuRu](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@RuRu](https://discuss.ocaml.org/u/RuRu)\
**Post date:** [February 12, 2023, 6:45pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/3 "2023-02-12T18:45:06Z")

</div>

Thanks for the comment and pointing out my mistakes.

I have tried the suggestion, and it is still complaining.

```auto
File "bin/main.ml", line 8, characters 10-28:
8 | let str = SellPriceBook.show pb (* was missing the module *)
              ^^^^^^^^^^^^^^^^^^
Error: Unbound value SellPriceBook.show

```

It would make sense that the function is generated under the module where the derived type is. But for some reason its not binding to the module ?

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [February 12, 2023, 7:45pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/4 "2023-02-12T19:45:56Z")

</div>

If you use VsCode + right extension, you can mouse hover over module name, to see if stringification functions appeared.

In general, it would be much easier, if you upload a demo.

---

<div class="post-metadata">

**Author:** ![art-w](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/art-w/32/4728_2.png) [@art-w](https://discuss.ocaml.org/u/art-w)\
**Post date:** [February 12, 2023, 8:34pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/5 "2023-02-12T20:34:39Z")

</div>

Do you have a signature on the functor `PriceBook` in the real code? (or an `.mli` file?) The `show` function must be exported if you want to use it from the outside 🙂 (you can also add the `[@@deriving show]` in the signature/mli near the `type t`)

(Ah, and in case your code is split between a library and an executable, you also need the `(preprocess (pps ppx_deriving.show etc))` in the library dune file, not only on the final executable)

---

<div class="post-metadata">

**Author:** ![RuRu](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@RuRu](https://discuss.ocaml.org/u/RuRu)\
**Post date:** [February 13, 2023, 9:55am UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/6 "2023-02-13T09:55:09Z")

</div>

Thats right it in its own lib folder.

Here is some details, probably should of been included from the start 🙂

```auto

(library
  (preprocess (pps ppx_deriving.show ppx_deriving.ord))
  (flags (:standard -w -66))
  (libraries fmlib_std core base sexplib)
  (name orderbook)
  (modules orderbook))

(executable
 (public_name order_service)
 (name main)
 (preprocess (pps ppx_deriving.show ppx_deriving.ord))
 (libraries orderbook lwt sexplib)
  (modules main))

```

```auto

Main.ml

open! Base
open Orderbook

module SellPriceBook = PriceBook(struct let side = Sell end)

let pb = SellPriceBook.init_book 42 (* was missing a price *)

let str = SellPriceBook.show pb (* was missing the module *)

let () = print_endline str (* show returns a string, not unit *)

```

orderbook.ml

```auto

open Base
open Core

type side = Buy | Sell [@@deriving show]
module type SideType = sig
    val side : side 
end

module OrderEntry = struct
  type t = {securityId: string ; price: int ; qty: int ; } [@@deriving show]
end 

module PriceBook(S: SideType) = 
  struct

    type price_book_level = { id: string ; price: int ; side: string ; mutable price_map: OrderEntry.t list } [@@deriving show] ;;

    let init_book p = { id = "0001" ; price = p ; side = "BID" ; price_map = [] }

    let to_string pb = Sexp.to_string pb

end;;

```

Finding it very strange why it doesnt want to bind ☹

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [February 13, 2023, 10:58am UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/7 "2023-02-13T10:58:50Z")

</div>

When you add `[@@deriving show]` to a type declaration, it creates functions which are based on the name of the type you’re declaring.

So in your last message, the generated `show` function in `PriceBook` is called `show_price_book_level`, not `show`.

More precisely the rule for `ppx_deriving` is that if the type is named `t`, the function is `show`, otherwise for a type named `x`, the function is `show_x`.

---

<div class="post-metadata">

**Author:** ![RuRu](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@RuRu](https://discuss.ocaml.org/u/RuRu)\
**Post date:** [February 13, 2023, 1:07pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/8 "2023-02-13T13:07:20Z")

</div>

That has worked thanks ! It prints as it should. Thank you 🙂

Now, does `ppx_deriving` works for classes/objects as well or only ADT ?

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [February 13, 2023, 3:14pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/9 "2023-02-13T15:14:57Z")

</div>

You might be interested in this: [How to print anything in OCaml - DEV Community 👩‍💻👨‍💻](https://dev.to/yawaramin/how-to-print-anything-in-ocaml-1hkl)

---

<div class="post-metadata">

**Author:** ![RuRu](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@RuRu](https://discuss.ocaml.org/u/RuRu)\
**Post date:** [February 14, 2023, 4:11pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/10 "2023-02-14T16:11:22Z")

</div>

thanks for the material. Will prove useful.

But it doesnt answer whether it works for classes in Ocaml? On another hand, is there any incentive to use classes/objects in Ocaml or its better to stick to fully functional approach?

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [February 14, 2023, 4:31pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/11 "2023-02-14T16:31:43Z")

</div>

Sorry, I realize that my post isn’t directly talking about printing in OCaml programs. I’ll update it. The context here is that once you’ve defined a pretty-printer, you can use the `Format` module to print it e.g.

```auto
let pp_obj = ...
let obj = object ... end
Format.printf "this is the object: %a\n" pp_obj obj

```

My post shows an example of defining a pretty-printer for an object.

---

<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:** [February 14, 2023, 7:19pm UTC](https://discuss.ocaml.org/t/how-to-use-ppx-show/11405/12 "2023-02-14T19:19:55Z")

</div>

(Side note: objects in OCaml can be fully functional. For better or worse, OCaml offers lots of non-redundant ways to structure programs and implement abstractions, that all fit together subdividing the design space. What differentiates objects from records is subtyping: one object type will be a special case of another object type if it offers more methods. Not so for records: when one record has strictly more fields than another, it cannot be used in place of the other.)
