Limited value types in Haskell

Is it possible to define restricted types in Haskell, i.e. I would like to be able to express

Prelude> let legalCharacters = ' ':['A'..'Z']
Prelude> legalCharacters
" ABCDEFGHIJKLMNOPQRSTUVWXYZ"

as a type, if possible.

+4
source share
1 answer

It can be done in modern GHC (> = 7.10, possibly already 7.8).

{-# LANGUAGE KindSignatures, DataKinds, MonoLocalBinds #-}
import GHC.TypeLits

newtype LegalChar (legalSet :: Symbol)
   = LegalChar {getLegalChar :: Char}
  deriving (Show)

fromChar :: KnownSymbol legal => Char -> Maybe (LegalChar legal)
fromChar c
   | c`elem`symbolVal r = Just r
   | otherwise          = Nothing
 where r = LegalChar c

Then

*Main> fromChar 'a' :: Maybe (LegalChar "abc")
Just (LegalChar {getLegalChar = 'a'})
*Main> fromChar 'x' :: Maybe (LegalChar "abc")
Nothing

I think that in GHC-8 you can even give a legalSetlook Stringand end the restriction KnownSymbol, you don’t know how this will work.

+13
source

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


All Articles