Aliases for Value Constructors

2019-06-26 02:43发布

问题:

Is it possible to define aliases for value constructors? The context is this: I am writing a program, that implements primitive recursive functions as Turing machines. For that I am working with unary integer encoding. My Turing machine type is defined like this:

-- definition of a Turing machine
data TuringMachine a = TuringMachine
                     State      -- q0
                     (Set State) -- F
                     (Set State) -- Q
                     (Set (Symbol a)) -- Gamma
                     (Set (Symbol a)) -- Sigma
                     (Map (State, Symbol a) (State, Symbol a, Instruction)) -- delta
                     deriving (Show)

where my symbol type is defined like this:

data Symbol a = Symbol a | Blank | Delim | Final | One deriving (Eq, Ord)

I have One as a convenience in there, so I don't need to write Symbol 1 everywhere. But that is a little untidy. I would like to define an alias outside of the type Symbol, like

alias One = Symbol 1

and

alias Zero = Blank

Is something like that possible?

回答1:

{-# Language PatternSynonyms #-}

pattern Zero :: Symbol a
pattern Zero = Blank

pattern One :: (Eq a, Num a) => Symbol a
pattern One = Symbol 1

works (bidirectional pattern synonym)