I’m learning sml and trying to make a program that simplifies logic formulas. When I try to run this code, I always get the same error, which I cannot figure out. It’s always “Error: syntax error: deleting BAR ID DARROW”. I’ve attached the code below:
- 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);
I’ve added the whole code:
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);
You need to have parenthesis around nested case statements, also when using them and having multiple function clauses, as it is not possible to differentiate the pipe (|) from belonging to the fun clauses or the case clauses.
Also you are missing the “y” argument to
simplifyin the second case ofSimplify (Or (x, y)), and some of your nested case expressions have a starting pipe, where it shouldn’t:Atleast this compiles with this rather wrongish datatype: