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?