GADT enumeration in Haskell

Could you tell me if there are any extensions to the Haskell output mechanism for the Enum class? I mean, there are many reasonable situations besides the case of "constructors with odd constructors." Is there any work on this topic?

+6
source share
1 answer

Do you really need a gadt? Or do you just want to remove the restriction on a simple enumeration type using only null constructors? If the latter, then there are options. One of them is to use the GHC Generic mechanism along with the implementation of a suitable general enumeration class. This is available in the generic-deriving package. Here is an example:

 {-# LANGUAGE DeriveGeneric #-} import Generics.Deriving data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Show, Generic) instance GEnum Bool instance GEnum a => GEnum (Tree a) test :: [Tree Bool] test = take 10 genum 

Now test is the following list:

 [ Leaf False , Node (Leaf False) (Leaf False) , Leaf True , Node (Leaf False) (Node (Leaf False) (Leaf False)) , Node (Node (Leaf False) (Leaf False)) (Leaf False) , Node (Leaf False) (Leaf True) , Node (Node (Leaf False) (Leaf False)) (Node (Leaf False) (Leaf False)) , Node (Leaf True) (Leaf False),Node (Leaf False) (Node (Leaf False) (Node (Leaf False) (Leaf False))) , Node (Node (Leaf False) (Leaf False)) (Leaf True) ] 

This genum implementation uses diagonalization to combine products. This ensures that each value does appear somewhere in the list, but can lead to an unexpected order.

+4
source

Source: https://habr.com/ru/post/943561/


All Articles