Anonymous Top Secret
Anonymous Top Secret

Reputation: 11

Haskell: Algebraic data type from List?

Assume I have a list [A, B, C]. Is there any way I can declare an algebraic data type based on that list with the following semantics:

data V = A | B | C

Thanks!

Upvotes: 1

Views: 614

Answers (1)

justkris
justkris

Reputation: 810

What you want is abstract data types generated dynamically, which sounds fairly non-standard in any language with ADTs.

What you could do is have a data type with a single constructor, and then a so-called smart-constructor, which imposes some logic on what values can be passed to the constructor.

data PermList a = PermList ([a] -> Bool) [a]

permList :: ([a] -> Bool) -> [a] -> PermList a
permList f xs | f xs      = PermList f xs
              | otherwise = undefined

Upvotes: 4

Related Questions