我正在尝试进行以下编译(record syntax on a GADT-style):
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
module Gsd.CLI.Steps where
import System.Console.Byline
import Data.Text
import Gsd.Clients
import Gsd.Read.Workspace
import Gsd.Read.Goal
type ErrorDescription = String
type WorkOnWorkspacesStepHandle = Clients -> Byline IO ()
type WorkOnAWorkspaceStepHandle = Clients -> Workspace -> WorkOnWorkspacesStepHandle -> Byline IO ()
type WorkOnAGoalStepHandle = Clients -> Workspace -> Goal -> WorkOnAWorkspaceStepHandle -> WorkOnWorkspacesStepHandle -> Byline IO ()
data StepName = WorkOnWorkspaces | WorkOnAWorkspace | WorkOnAGoal
data Step a where
WorkOnWorkspacesStep { workOnWorkspaces:: WorkOnWorkspacesStepHandle, clients::Clients } :: Step WorkOnWorkspaces
WorkOnAWorkspaceStep { workOnWorkspace:: WorkOnAWorkspaceStepHandle, clients::Clients, workspace::Workspace , workOnWorkspaces:: WorkOnWorkspacesStepHandle} :: Step WorkOnAWorkspace
WorkOnAGoalStep { workOnAGoal :: WorkOnAGoalStepHandle, clients::Clients, workspace::Workspace, goal::Goal , workOnWorkspace:: WorkOnAWorkspaceStepHandle, workOnWorkspaces:: WorkOnWorkspacesStepHandle} :: Step WorkOnAGoal
编译器不喜欢我使用的data Step a
语法
/Users/nhenin/dev/gsdFlow/src/Gsd/CLI/Steps.hs:25:25: error: parse error on input ‘{’
|
25 | WorkOnWorkspacesStep { workOnWorkspaces:: WorkOnWorkspacesStepHandle, clients::Clients } :: Step WorkOnWorkspaces
| ^
^
我正在关注以下示例:
data Term a where
Lit { val :: Int } :: Term Int
Succ { num :: Term Int } :: Term Int
Pred { num :: Term Int } :: Term Int
IsZero { arg :: Term Int } :: Term Bool
Pair { arg1 :: Term a
, arg2 :: Term b
} :: Term (a,b)
If { cnd :: Term Bool
, tru :: Term a
, fls :: Term a
} :: Term a
哪个编译得不太好...
答案 0 :(得分:1)
正确的语法是(疯狂):
data Foo a where
Bar :: { fd1 :: T1, fd2 :: T2 } -> Foo b
--- etc.
因此,在这种情况下,您可能需要:
data Step a where
WorkOnWorkspacesStep ::
{ workOnWorkspaces:: WorkOnWorkspacesStepHandle
, clients::Clients } ->
Step WorkOnWorkspaces
WorkOnAWorkspaceStep ::
{ workOnWorkspace:: WorkOnAWorkspaceStepHandle
, clients::Clients
, workspace::Workspace
, workOnWorkspaces:: WorkOnWorkspacesStepHandle } ->
Step WorkOnAWorkspace
WorkOnAGoalStep ::
{ workOnAGoal :: WorkOnAGoalStepHandle
, clients::Clients
, workspace::Workspace
, goal::Goal
, workOnWorkspace:: WorkOnAWorkspaceStepHandle
, workOnWorkspaces:: WorkOnWorkspacesStepHandle } ->
Step WorkOnAGoal
作为参考,请参见GHC user guide(在链接部分的底部附近)。
编辑:不幸的是,这似乎并不合法
但是,对于GADT,还存在以下附加约束:每个具有字段f的构造函数都必须具有相同的结果类型(模Alpha转换)