最好的代码是不存在的代码,在这方面,Haskell 对派生实现有很好的支持(使用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)
据我所知,OCaml 中的相同需要一些手动管道
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)))
这些推导在 OCaml 中的状态如何?
到目前为止,有没有更好的方法来自动提供相应的证明树?
deriving
做一些类似的更强大的扩展会很难吗?