sml代码中的语法错误

时间:2011-12-03 00:33:17

标签: sml smlnj

我正在学习sml并尝试制作一个简化逻辑公式的程序。当我尝试运行此代码时,我总是得到相同的错误,这是我无法弄清楚的。它总是“错误:语法错误:删除BAR ID DARROW”。我附上了以下代码:

- fun Simplify (Or(True, _)) = True
= | Simplify (Or(_, True)) = True
= | Simplify (Or(False, False)) = False
= | Simplify (Or(x, False)) = (Simplify x)
= | Simplify (Or(False, x)) = (Simplify x)
= | Simplify (Or (Var (x), Var (y))) = Or (Var (x), Var (y))
= | Simplify (Or (x, y)) = case Simplify x of
=                                       True => True
=                                       | False => Simplify y
=                                       | x' => case Simplify
=                                       | True => True
GC #0.0.0.0.2.85:   (2 ms)
=                                       | False => x'
stdIn:50.6-50.15 Error: syntax error: deleting  BAR ID DARROW
-                                       | y' => Or(x', y')
= (*And*)
= | Simplify (And(_, False)) = False
stdIn:2.1-2.8 Error: syntax error: deleting  BAR ID DARROW
stdIn:54.1-54.11 Error: syntax error: deleting  BAR ID
- | Simplify (And(False, _)) = False
stdIn:1.1-2.6 Error: syntax error: deleting  BAR ID
- | Simplify (And(True, True)) = True
= | Simplify (And(True, x)) = (Simplify x)
stdIn:1.1-2.6 Error: syntax error: deleting  BAR ID
- | Simplify (And(x, True)) = (Simplify x)
= | Simplify (And(Var (x), Var(y))) = And (Var (x), Var (y))
stdIn:1.1-2.6 Error: syntax error: deleting  BAR ID
- | Simplify (And (x, y)) = case Simplify x of
stdIn:1.1-2.6 Error: syntax error: deleting  BAR ID
stdIn:53.3-57.4 Error: syntax error: deleting  CASE ID
-                               False => False
=                               | True => Simplify y
stdIn:2.6-62.6 Error: syntax error: deleting  DARROW ID BAR
-                               | x' => case Simplify y of
=                               | False => False
stdIn:1.5-2.7 Error: syntax error: deleting  BAR ID DARROW
-                               | True => x'
=                               | y' => And(x', y')
stdIn:1.5-2.9 Error: syntax error: deleting  BAR ID DARROW
- (*Not*)
- | Simplify (Not(Not(x))) = (Simplify x)
= | Simplify (Not(True)) = False
stdIn:68.1-68.11 Error: syntax error: deleting  BAR ID
- | Simplify (Not(False)) = True
= | Simplify (Not(Var (x))) = (Not (Var x))
stdIn:1.1-68.3 Error: syntax error: deleting  BAR ID
GC #0.0.0.0.3.201:   (1 ms)
- | Simplify (Not x) = case Simplify x of
stdIn:1.1-68.3 Error: syntax error: deleting  BAR ID
stdIn:68.14-71.4 Error: syntax error: deleting  CASE ID
-                                True => False
=                               | False => True
stdIn:68.3-74.6 Error: syntax error: deleting  DARROW ID BAR
-                               | x' => Not x'
= (*general*)
= | Simplify True = True
stdIn:1.5-68.4 Error: syntax error: deleting  BAR ID DARROW
- | Simplify False = False
= | Simplify (Var(x)) = Var(x);

我添加了整个代码:

datatype formula =
True
| False
| Var of string 
| Not of formula
| And of formula * formula
| Or of formula * formula;

fun Simplify (Or(True, _)) = True
| Simplify (Or(_, True)) = True
| Simplify (Or(False, False)) = False
| Simplify (Or(x, False)) = (Simplify x)
| Simplify (Or(False, x)) = (Simplify x)
| Simplify (Or (Var (x), Var (y))) = Or (Var (x), Var (y))
| Simplify (Or (x, y)) = case Simplify x of
                    True => True
                    | False => Simplify y
                    | x' => case Simplify y of
                    | True => True
                    | False => x'
                    | y' => Or(x', y')
(*And*)
| Simplify (And(_, False)) = False
| Simplify (And(False, _)) = False
| Simplify (And(True, True)) = True
| Simplify (And(True, x)) = (Simplify x)
| Simplify (And(x, True)) = (Simplify x)
| Simplify (And(Var (x), Var(y))) = And (Var (x), Var (y))
| Simplify (And (x, y)) = case Simplify x of
                False => False
                | True => Simplify y
                | x' => case Simplify y of
                | False => False
                | True => x'
                | y' => And(x', y')
(*Not*)
| Simplify (Not(Not(x))) = (Simplify x)
| Simplify (Not(True)) = False
| Simplify (Not(False)) = True
| Simplify (Not(Var (x))) = (Not (Var x))
| Simplify (Not x) = case Simplify x of
                 True => False
                | False => True
                | x' => Not x'
(*general*)
| Simplify True = True
| Simplify False = False
| Simplify (Var(x)) = Var(x);

1 个答案:

答案 0 :(得分:3)

您需要在嵌套的case语句周围使用括号,当使用它们并具有多个函数子句时,因为无法将管道(|)与属于fun子句或case子句区分开来。

simplify的第二种情况下,你错过了Simplify (Or (x, y))的“y”参数,并且你的一些嵌套的case表达式有一个起始管道,它不应该在:

fun Simplify (Or (True, _)) = True
  | Simplify (Or (_, True)) = True
  | Simplify (Or (False, False)) = False
  | Simplify (Or (x, False)) = Simplify x
  | Simplify (Or (False, x)) = Simplify x
  | Simplify (Or (Var x, Var y)) = Or (Var x, Var y)
  | Simplify (Or (x, y)) = (case Simplify x of
                              True => True
                            | False => Simplify y
                            | x' => (case Simplify y of
                                       True => True
                                     | False => x'
                                     | y' => Or(x', y')))
  (*And*)
  | Simplify (And (_, False)) = False
  | Simplify (And (False, _)) = False
  | Simplify (And (True, True)) = True
  | Simplify (And (True, x)) = (Simplify x)
  | Simplify (And (x, True)) = (Simplify x)
  | Simplify (And (Var x, Var y)) = And (Var x, Var y)
  | Simplify (And (x, y)) = (case Simplify x of
                               False => False
                             | True => Simplify y
                             | x' => (case Simplify y of
                                        False => False
                                      | True => x'
                                      | y' => And(x', y')))
  (*Not*)
  | Simplify (Not (Not x)) = (Simplify x)
  | Simplify (Not True) = False
  | Simplify (Not False) = True
  | Simplify (Not (Var x)) = Not (Var x)
  | Simplify (Not x) = (case Simplify x of
                          True => False
                        | False => True
                        | x' => Not x')
  (*general*)
  | Simplify True = True
  | Simplify False = False
  | Simplify (Var x) = Var x

Atleast用这个相当错误的数据类型编译:

datatype Expr = Or  of Expr *  Expr
              | And of Expr * Expr
              | Not of Expr
              | Var of Expr
              | True
              | False