How to model hierarchical data types in Haskell?

前端 未结 2 1095
感情败类
感情败类 2021-01-06 11:00

I have a bunch of types where their hierarchy stores some useful information. I\'m trying to avoid having to bake in knowledge of the hierarchy of the types into the functio

2条回答
  •  长发绾君心
    2021-01-06 11:51

    Use typeclasses.

    First define each of the concrete types as a seperate data declaration.

    Then for each type with subtypes declare a typeclass with a constraint on its parent. An example of this sort of relationship is Functor => Applicative => Monad structure in prelude.

    So to define the example structure:

    class Root where
        ...
    
    class Root => Dep where
        ...
    
    class Dep => Aux where
        ...
    
    class Aux => Auxpass where
        ...
    
    class Aux => Cop where
        ...
    
    class Dep => Arg where
        ...
    
    class Arg => Agent where
        ...
    

提交回复
热议问题