价值建构者的别名 [英] Aliases for Value Constructors

查看:90
本文介绍了价值建构者的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为值构造函数定义别名?上下文是这样的:我正在编写一个程序,该程序实现图灵机的原始递归功能。为此,我正在使用一元整数编码。我的图灵机类型定义如下:

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)

我有一个一个为方便起见,所以我不需要到处写符号1 。但这有点不整洁。我想在 Symbol 类型之外定义别名,例如

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

alias Zero = Blank

是否可能这样?

推荐答案

{-# 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)

这篇关于价值建构者的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆