需要使用标准库data Ordering = LT | EQ | GT, 以及 compare :: Ord => a -> a -> Ordering。
1 2 3 4 5 6 7 8 9
dataTree = LeafInt | NodeTreeIntTree tr :: Tree tr = Node (Node (Leaf1) 3 (Leaf4)) 5 (Node (Leaf6) 7 (Leaf9)) occurs :: Int -> Tree -> Bool occurs m (Leaf n) = m == n occurs m (Node l n r) = case compare m n of LT -> occurs m l EQ -> True GT -> occurs m r
eval :: Subst -> Prop -> Bool eval _ (Const b) = b eval s (Var x) = find x s eval s (Not p) = not (eval s p) eval s (And p q) = eval s p && eval s q eval s (Disj p q) = eval s p || eval s q eval s (Equi p q) = eval s p == eval s q eval s (Imply p q) = eval s p <= eval s q
vars :: Prop -> String vars (Const _) = [] vars (Var x) = [x] vars (Not p) = vars p vars (And p q) = vars p ++ vars q vars (Disj p q) = vars p ++ vars q vars (Equi p q) = vars p ++ vars q vars (Imply p q) = vars p ++ vars q
bools :: Int -> [[Bool]] bools0 = [[]] bools n = map (False:) bss ++ map (True:) bss where bss = bools (n - 1) rmdups :: Eq a => [a] -> [a] rmdups [] = [] rmdups (x : xs) = x : rmdups (filter (/= x) xs)
substs :: Prop -> [Subst] substs p = map (zip us) (bools (length us)) where us = rmdups (vars p) isTaut :: Prop -> Bool isTaut p = and [eval s p | s <- substs p]
-- 7 abstract machine dataExpr = ValInt | AddExprExpr | MulExprExpr -- value :: Expr -> Int -- value (Val n) = n -- value (Add x y) = value x + value y
eval :: Expr -> Cont -> Int eval (Val n) c = exec c n eval (Add x y) c = eval x (EVALADD y : c) eval (Mul x y) c = eval x (EVALMUL y : c)
exec :: Cont -> Int -> Int exec [] n = n exec (EVALADD y : c) n = eval y (ADD n : c) exec (EVALMUL y : c) n = eval y (MUL n : c) exec (ADD n : c) m = exec c (n + m) exec (MUL n : c) m = exec c (n * m)