我想编写一个haskell程序,以基于其指称语义来实现一种简单的命令式语言。 我在Windows上使用GHCi版本8.4.2。 在实现下面描述的函数和过程抽象时遇到了一些问题。
让我形容它。我称它为IMP。 首先,我定义抽象语法。 IMP仅接受整数。并且标识符将是字符串。
type Numeral = Int
type Ident = String
data Command =
Skip
| Assign (Ident, Expression)
| Letin (Declaration, Command )
| Cmdcmd (Command, Command )
| Ifthen (Expression, Command, Command)
| Whiledo (Expression, Command )
| IdentifierC ( ActualParameter )
data Expression =
Num Numeral
| False_
| True_
| Notexp Expression
| Id Ident
| Sumof (Expression, Expression)
| Subof (Expression, Expression)
| Prodof (Expression, Expression)
| Less (Expression, Expression)
| Leten (Declaration, Expression)
| IdentifierE ( ActualParameter )
deriving Show
type ActualParameter = Expression
data FormalParameter = Constfp Identifier
data Declaration =
Constdef (Ident, Expression)
| Vardef (Ident, TypeDef )
| Func Identifier ( FormalParameter ) ~ Expression
| Proce Identifier ( FormalParameter ) ~ Command
deriving Show
data TypeDef =
Bool | Int
deriving Show
简要说明:
在命令,跳过不做任何事情。
分配将把表达式的值提供给标识符。
Letin将在命令中声明一些变量。
Cmdcmd将顺序运行2个命令。
如果是条件命令,则如果第一个表达式的值为真,则运行第一个命令,否则运行第二个命令。
Whiledo是循环。
IdentifierC(ActualParameter):IdentifierC表示本地环境中的某些功能。这个ActualParameter是具有某些值的某些表达式。该命令强制在指定值上调用此函数。
在表达式中,从上到下分别是: 数字 假 布尔真 表达否定 从当地环境中获得Ident的价值。 求和2个表达式 减去2个表达式 2的乘积 < 在表达式中声明一些变量 IdentifierE(ActualParameter)IdentifierE表示本地环境中的某些功能。这个ActualParameter是具有某些值的某些表达式。此命令强制在指定值上调用此函数。终于有了这个表达式的结果。
然后是语义域
type Integer = Int
type Boolean = Bool
type Location = Int
type Function = Argument -> Store -> Value
type Procedure = Argument -> Store -> Store
-- store would be snapshot of the memory.
data Value = IntValue Int
| TruthValue Bool
deriving (Eq, Show)
-- first class value only are int and bool
type Storable = Value
data Bindable = Const Value
| Variable Location
| Function Func
| Procedure Proce
deriving (Eq, Show)
data Denotable = Unbound | Bound Bindable
deriving (Eq, Show)
type Argument = Value
data Sval = Stored Storable | Undef | Unused
-- The actual storage in a Store
type DataStore = Location -> Sval
-- --bot--- --top--- --data---
data Store = Store (Location, Location, DataStore)
type Environ = Ident -> Denotable
-- ---------- Semantic Functions -------------- --
valuation :: Int -> Value
evaluate :: Expression -> Environ -> Store -> Value
elaborate :: Declaration -> Environ -> Store -> (Environ,Store)
execute :: Command -> Environ -> Store -> Store
-- the main goal is to define these semantic functions
-- I give some examples in my source code below.
我的代码是在这里:https://github.com/sanyuwen/IMP/blob/master/DSemImp.hs。 我的测试代码在这里:https://github.com/sanyuwen/IMP/blob/master/ImpTest.hs
当我使用GHC导入DSemImp模块时,遇到很多错误。
DSemImp.hs:52:32: error:
Not in scope: type constructor or class ‘Identifier’
|
52 | data FormalParameter = Constfp Identifier | ^^^^^^^^^^
It said data FormalParameter = Constfp Identifier is not legal.
without this how can I define formal parameter ??
答案 0 :(得分:3)
您没有定义Identifier
。尽管您定义了Ident
,但这是您的意思吗?