私有数据构造函数上的模式匹配

fir*_*iku 7 haskell algebraic-data-types pattern-synonyms

我正在为网格轴编写一个简单的ADT.在我的应用程序中,网格可以是常规的(在坐标之间有恒定的步长),也可以是不规则的(否则).当然,常规网格只是不规则网格的一种特殊情况,但在某些情况下可能需要区分它们(例如,执行一些优化).所以,我声明我的ADT如下:

data GridAxis = RegularAxis (Float, Float) Float -- (min, max) delta
              | IrregularAxis [Float]            -- [xs]
Run Code Online (Sandbox Code Playgroud)

但我不希望用户使用max < min或使用无序xs列表创建格式错误的轴.因此,我添加了"更智能"的构造函数,它们执行一些基本检查:

regularAxis :: (Float, Float) -> Float -> GridAxis
regularAxis (a, b) dx = RegularAxis (min a b, max a b) (abs dx)

irregularAxis :: [Float] -> GridAxis
irregularAxis xs = IrregularAxis (sort xs)
Run Code Online (Sandbox Code Playgroud)

我不希望用户直接创建网格,因此我不会将GridAxis数据构造函数添加到模块导出列表中:

module GridAxis (
    GridAxis,
    regularAxis,
    irregularAxis,
    ) where
Run Code Online (Sandbox Code Playgroud)

但事实证明,完成此操作后,我再也无法使用模式匹配GridAxis了.试着用它

import qualified GridAxis as GA

test :: GA.GridAxis -> Bool
test axis = case axis of
              GA.RegularAxis -> True
              GA.IrregularAxis -> False  
Run Code Online (Sandbox Code Playgroud)

给出以下编译器错误:

src/Physics/ImplicitEMC.hs:7:15:
    Not in scope: data constructor `GA.RegularAxis'

src/Physics/ImplicitEMC.hs:8:15:
    Not in scope: data constructor `GA.IrregularAxis'
Run Code Online (Sandbox Code Playgroud)

这有什么可以解决的吗?

And*_*ács 9

您可以定义构造函数模式同义词.这使您可以使用相同的名称进行智能构造和"哑"模式匹配.

{-# LANGUAGE PatternSynonyms #-}

module GridAxis (GridAxis, pattern RegularAxis, pattern IrregularAxis) where
import Data.List

data GridAxis = RegularAxis_ (Float, Float) Float -- (min, max) delta
              | IrregularAxis_ [Float]            -- [xs]

-- The line with "<-" defines the matching behavior
-- The line with "=" defines the constructor behavior
pattern RegularAxis minmax delta <- RegularAxis_ minmax delta where
  RegularAxis (a, b) dx = RegularAxis_ (min a b, max a b) (abs dx)

pattern IrregularAxis xs <- IrregularAxis_ xs where
  IrregularAxis xs = IrregularAxis_ (sort xs)
Run Code Online (Sandbox Code Playgroud)

现在你可以这样做:

module Foo
import GridAxis

foo :: GridAxis -> a
foo (RegularAxis (a, b) d) = ...
foo (IrregularAxis xs) = ...
Run Code Online (Sandbox Code Playgroud)

并且还使用RegularAxisIrregularAxis作为智能构造函数.


chi*_*chi 5

这看起来是模式同义词的一个用例。

基本上你不导出真正的构造函数,而只是导出一个“智能”构造函数

{-# LANGUAGE PatternSynonyms #-}
module M(T(), SmartCons, smartCons) where

data T = RealCons Int

-- the users will construct T using this
smartCons :: Int -> T
smartCons n = if even n then RealCons n else error "wrong!"

-- ... and destruct T using this
pattern SmartCons n <- RealCons n
Run Code Online (Sandbox Code Playgroud)

M然后可以使用另一个模块导入

case someTvalue of
   SmartCons n -> use n
Run Code Online (Sandbox Code Playgroud)

和例如

let value = smartCons 23 in ...
Run Code Online (Sandbox Code Playgroud)

但不能RealCons直接使用。


如果你更喜欢留在基本的 Haskell 中,不带扩展,你可以使用“视图类型”

module M(T(), smartCons, Tview(..), toView) where
data T = RealCons Int
-- the users will construct T using this
smartCons :: Int -> T
smartCons n = if even n then RealCons n else error "wrong!"

-- ... and destruct T using this
data Tview = Tview Int
toView :: T -> Tview
toView (RealCons n) = Tview n
Run Code Online (Sandbox Code Playgroud)

在这里,用户可以完全访问视图类型,可以自由地构造/销毁该视图类型,但对于实际类型只有一个受限制的启动构造函数TT通过移动到视图类型可以破坏实际类型

case toView someTvalue of
  Tview n -> use n
Run Code Online (Sandbox Code Playgroud)

对于嵌套模式,事情会变得更加麻烦,除非您启用其他扩展,例如ViewPatterns.