haskellocamlderivingvia# deriving implementation in OCaml

The best code is code that does not exist, and in that regard, Haskell has great support for deriving implementation (that became even better with `deriving via`

).

```
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE KindSignatures, PolyKinds#-}
import Data.Kind (Type)
data NTree (a :: Type) =
NLeaf a
| NNode (NTree (a,a))
deriving (Eq, Ord, Read, Show, Functor, Foldable, Traversable)
```

As far as I can tell, the same in OCaml requires a bit of manual plumbing

```
type 'a n_tree = NLeaf of 'a | NNode of ('a * 'a) n_tree (* [@@deriving map] fails *)
let rec map_ntree : 'a 'b. 'a n_tree -> ('a -> 'b) -> 'b n_tree =
fun t f ->
match t with
| NLeaf x -> NLeaf (f x)
| NNode p -> NNode (map_ntree p (fun (l, r) -> (f l, f r)))
```

What's the status of these derivations in OCaml?

Is there a better way to supply automatically the corresponding proof trees as of now?

Would it be hard to make some similar more powerful `deriving`

extension?

Solution

There are a few ppx derivers available in opam, try `opam search ppx`

. As an example, you can use ppx_deriving, e.g., in the OCaml top-level,

```
# #use "topfind";;
# #require "ppx_deriving.std";;
# type 'a n_tree = NLeaf of 'a | NNode of 'a * 'a n_tree
[@@deriving show, eq, ord, iter, fold, map];;
type 'a n_tree = NLeaf of 'a | NNode of 'a * 'a n_tree
val pp_n_tree :
(Ppx_deriving_runtime.Format.formatter -> 'a -> Ppx_deriving_runtime.unit) ->
Ppx_deriving_runtime.Format.formatter ->
'a n_tree -> Ppx_deriving_runtime.unit = <fun>
val show_n_tree :
(Ppx_deriving_runtime.Format.formatter -> 'a -> Ppx_deriving_runtime.unit) ->
'a n_tree -> Ppx_deriving_runtime.string = <fun>
val equal_n_tree :
('a -> 'a -> Ppx_deriving_runtime.bool) ->
'a n_tree -> 'a n_tree -> Ppx_deriving_runtime.bool = <fun>
val compare_n_tree :
('a -> 'a -> Ppx_deriving_runtime.int) ->
'a n_tree -> 'a n_tree -> Ppx_deriving_runtime.int = <fun>
val iter_n_tree : ('a -> unit) -> 'a n_tree -> unit = <fun>
val fold_n_tree : ('a -> 'b -> 'a) -> 'a -> 'b n_tree -> 'a = <fun>
val map_n_tree : ('a -> 'b) -> 'a n_tree -> 'b n_tree = <fun>
```

- Why do I get "Unexpected reply type" from notify-send when using this Haskell notification server?
- Haskell fails to infer the return type of a monad after using the sequence operator
- Don't understand notation of morphisms in Monoid definition
- Foldln in haskell
- Is this property of a functor stronger than a monad?
- How to Instantiate a Custom Data Type with Record Syntax and Multiple Constructors
- How do I make a minimal working example for the a DBus server?
- Is it safe to downgrade Haskell stack version?
- Haskell, list of natural number
- unfamiliar syntax in Haskell / Clash function type signature
- foldM with monad State does not type check
- Why does my Runge-Kutta implementation oscillate to 0?
- How do I get the desired behavior in my TCP server?
- Why does the Haskell PVP describe new functions as non-breaking?
- How do I correctly use toLower in Haskell?
- Every Lens' is a Traversal'... how?
- How do I crate a value of type a{sv} for a call to org.freedesktop.Notifications.Notify via DBus?
- Web Scraping With Haskell
- Double exclamation marks in Haskell
- Haskell Servant POST FormUrlEncoded for (Vector String) field
- Confusion about list types in Haskell
- Idiomatic way to define new vs persisted types in Haskell
- Why does Cabal, unlike GHC, not automatically enable GeneralizedNewtypeDeriving if I explicitly enabled DerivingStrategies?
- What is the proper way of wrapping an Int (not a general type) in another type if type safety is the only motive?
- Parsing inside `between` with Megaparsec
- takeWhile implementation in JavaScript - Looking for better ideas
- How to setup MINGW environment variables for Haskell language server in vscode?
- mysql-haskell no invoke of Right case of try function
- Run cleanup function in multiple Haskell child threads when POSIX Signal sent (SIGTERM etc)
- Gloss animations jerky and hope to add `-O2` to GHCi