2011-12-03 65 views
0

我正在學習sml並試圖製作一個簡化邏輯公式的程序。當我嘗試運行這段代碼時,我總是得到同樣的錯誤,這是我無法弄清楚的。它始終是「錯誤:語法錯誤:刪除BAR ID DARROW」。我已經把它貼下面的代碼:在sml代碼中的語法錯誤

- 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); 
+0

請記住添加所有代碼,也包括您使用的數據結構。從解釋器複製粘貼並不是向您顯示代碼的好方法。考慮包括你的源文件,然後再考慮你得到的錯誤。 –

回答

3

你需要有括號圍繞嵌套case語句,也使用它們,並具有多種功能的語句時,因爲無法區分管道(|)屬於有趣的條款或案例條款。

你也缺少在Simplify (Or (x, y))第二種情況下,「Y」參數simplify,和你的一些巢式病例表現有一個起始管,它不應該:

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 
+0

我已經添加了整個代碼。你能否給我舉一個關於「嵌套case語句的括號」的小例子。謝謝。 – user1047517

+0

非常感謝它現在完美運作。 – user1047517