单身人士的单身人士(在Haskell中模拟复杂的pi类型)

时间:2018-09-19 05:55:20

标签: haskell types ghc idris dependent-type

我在Idris中有一个简单的概念证明,它使用依赖类型来实施一些不太复杂的业务逻辑。为了保护不是那么天真的人,已经更改了一些名称,但是其思想是我们希望按顺序收集“行”。每行都属于一个特定的部分,但是只有一个(EconProduction)具有我们所关心的内容。通常,行有一个特定于部分的关键字和一个表达式,其形式/类型可能取决于所使用的关键字。

对于此特定部分,每行要么描述一个“阶段”(Prod)的一些数字,要么延续最后一个命名的“阶段”(Continue)。

在Idris,我们可以这样做:

data EconSection
  = EconGeneral
  | EconProduction

data EconPhase
  = Oil
  | Water
  | NumPhase Nat

data ContState
  = ContNone
  | ContProd EconPhase

data Keyword : EconSection -> ContState -> ContState -> Type where
  Prod : (p : EconPhase) -> Keyword EconProduction c (ContProd p)
  Continue : Keyword s c c

data Expression : (s : EconSection) ->
                  (d : ContState) ->
                  Keyword s c d ->
                  Type where
  ExProc : Double -> Double -> Expression EconProduction (ContProd p) k

data Line : EconSection -> ContState -> ContState -> Type where
  L : (k : Keyword s c d) -> Expression s d k -> Line s c d

data Lines : EconSection -> ContState -> Type where
  First : Line s ContNone d -> Lines s d
  Then : Lines s c -> Line s c d -> Lines s d

infixl 0 `Then`

good : Lines EconProduction (ContProd (NumPhase 1))
good = First (L (Prod Oil) (ExProc 23.2 70.1))
      `Then` (L (Continue) (ExProc 27.9 1.2))
      `Then` (L (Prod (NumPhase 1)) (ExProc 91.2 7014.1))
      `Then` (L (Continue) (ExProc 91.2 7014.1))

到目前为止一切顺利!通常依赖的typestate业务。出于非常实际的业务原因,我们希望在GHC Haskell中实际实现此逻辑。我已经用单例构建了它(只是出于概念上的简短证明,按需滚动自己的包,而不是使用singletons包):

{-# LANGUAGE GADTs, KindSignatures, DataKinds #-}
{-# LANGUAGE RankNTypes, TypeInType, TypeOperators #-}
{-# LANGUAGE TypeFamilies, TypeFamilyDependencies, MultiParamTypeClasses #-}

import Data.Kind (Type)

data Nat
  = Z
  | S Nat

data SNat :: Nat -> Type where
  SZ :: SNat 'Z
  SS :: SNat n -> SNat ('S n)

data SSNat :: forall (n :: Nat) . SNat n -> Type where
  SSZ :: SSNat 'SZ
  SSS :: SSNat n -> SSNat ('SS n)

type family SingNat (n :: Nat) :: SNat n where
  SingNat 'Z = 'SZ
  SingNat ('S n) = 'SS (SingNat n)

data EconSection
  = EconGeneral
  | EconProduction

data SEconSection :: EconSection -> Type where
  SEconGeneral :: SEconSection 'EconGeneral
  SEconProduction :: SEconSection 'EconProduction

type family SingSection (s :: EconSection) :: SEconSection s where
  SingSection 'EconGeneral = 'SEconGeneral
  SingSection 'EconProduction = 'SEconProduction 

data EconPhase
  = Oil
  | Water
  | NumPhase Nat

data SEconPhase :: EconPhase -> Type where
  SOil :: SEconPhase 'Oil
  SWater :: SEconPhase 'Water
  SNumPhase :: SNat n -> SEconPhase ('NumPhase n)

data SSEconPhase :: forall (p :: EconPhase) . SEconPhase p -> Type where
  SSOil :: SSEconPhase 'SOil
  SSWater :: SSEconPhase 'SWater
  SSNumPhase :: SSNat n -> SSEconPhase ('SNumPhase n)

type family SingEconPhase (p :: EconPhase) :: SEconPhase p where
  SingEconPhase 'Oil = 'SOil
  SingEconPhase 'Water = 'SWater
  SingEconPhase ('NumPhase n) = 'SNumPhase (SingNat n)

data ContState
  = ContNone
  | ContProd EconPhase

data SContState :: ContState -> Type where
  SContNone :: SContState 'ContNone
  SContProd :: SEconPhase p -> SContState ('ContProd p)

type family SingContState (c :: ContState) :: SContState c where
  SingContState 'ContNone = 'SContNone
  SingContState (ContProd p) = 'SContProd (SingEconPhase p)

data Keyword :: EconSection -> ContState -> ContState -> Type where
  Prod :: SEconPhase p -> Keyword 'EconProduction c ('ContProd p)
  Continue :: Keyword s c c

data SKeyword :: forall (s :: EconSection) (c :: ContState) (d :: ContState) .
                 Keyword s c d -> Type where
  SProd :: SSEconPhase p -> SKeyword ('Prod p)
  SContinue :: SKeyword 'Continue

data Expression :: forall (s :: EconSection) (c :: ContState) (d :: ContState) .
                   SEconSection s -> SContState d -> Keyword s c d -> Type where
  ExProc :: Double -> Double -> Expression SEconProduction (SContProd p) k

type family KWSection k where
  KWSection (Keyword s _ _) = s

type family KWFrom k where
  KWFrom (Keyword _ c _) = c

type family KWTo k where
  KWTo (Keyword _ _ d) = d

data Line :: EconSection -> ContState -> ContState -> Type where
  L :: SKeyword (k :: Keyword s c d)
    -> Expression (SingSection s) (SingContState d) k
    -> Line s c d

data Lines :: EconSection -> ContState -> Type where
  First :: Line s 'ContNone d -> Lines s d
  Then :: Lines s c -> Line s c d -> Lines s d

infixl 0 `Then`

good :: Lines 'EconProduction ('ContProd ('NumPhase ('S 'Z)))
good = First (L (SProd SSOil) (ExProc 23.2 70.1))
      `Then` (L (SContinue) (ExProc 27.9 1.2))
      `Then` (L (SProd (SSNumPhase (SSS SSZ))) (ExProc 91.2 7014.1))
      `Then` (L (SContinue) (ExProc 91.2 7014.1))

这是我的问题。有什么办法可以避免“单身人士的单身”?我根本不喜欢SSNat之类的外观,但这是通过将每个pi类型转换为额外的单例处理层而得到的。我无法使任何更简单的方法起作用,并且在singletons软件包中没有看到任何巧妙的想法可以简化此过程,尽管我可能很容易错过了所有模板Haskell下方的内容。

1 个答案:

答案 0 :(得分:8)

是的。

考虑到,根据单例类型的定义,单例中的类型信息与该单例的单例一样多,因为它们都有唯一的实例。

在您的代码中

考虑到以上几点,我们可以从您的代码中删除SSNatSSEconPhase声明。然后,在SProd构造函数中

SProd :: SSEconPhase p - > SKeyword ('Prod p)

我们知道SEconPhase足以决定p,因此我们可以将其重写为

SProd :: SEconPhase p - > SKeyword ('Prod p)

哪个会产生种类错误-我们需要的是像这样的类型转换

SomeType :: (p :: EconPhase) -> SEconPhase p

您已经在代码中将其定义为SingEconPhase。结果是

SProd :: SEconPhase p - > SKeyword ('Prod (SingEconPhase p))

一般

您永远不必编写单例的单例-如果您需要将类型参数“提升”为单例类型,那么正确的选择是像完成操作一样编写类型族。