elvecent

First Order Haskell Chaos

Feb 25th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --ghc 7.10
  2.  
  3. data Term = Var String | Const String | Func String [Term] deriving Eq
  4.  
  5. data Formula = Tr | Fa | Rel String [Term] |
  6.                And Formula Formula | Or Formula Formula |
  7.                To Formula Formula | Not Formula |
  8.                Forall String Formula | Exists String Formula
  9.                
  10. data Expr = Term Term | Formula Formula deriving Show
  11.  
  12. instance Show Term where
  13.     show (Var s) = s
  14.     show (Const c) = c
  15.     show (Func f ts) = f ++ (show ts)
  16.    
  17. instance Show Formula where
  18.     show Tr = "T"
  19.     show Fa = "_|_"
  20.     show (Rel r ts) = r ++ (show ts)
  21.     show (And f1 f2) = "(" ++ (show f1) ++ "^" ++ (show f2) ++ ")"
  22.     show (Or f1 f2) = "(" ++ (show f1) ++ "v" ++ (show f2) ++ ")"
  23.     show (To f1 f2) = "(" ++ (show f1) ++ "->" ++ (show f2) ++ ")"
  24.     show (Not f) = "-" ++ (show f)
  25.     show (Forall v f) = "(" ++ "П" ++ v ++ ": " ++ (show f) ++ ")"
  26.     show (Exists v f) = "(" ++ "E" ++ v ++ ": " ++ (show f) ++ ")"
  27.  
  28. subformulas (And f1 f2) = [f1,f2] ++ (subformulas f1) ++ (subformulas f2)
  29. subformulas (Or f1 f2) = subformulas (And f1 f2)
  30. subformulas (To f1 f2) = subformulas (And f1 f2)
  31. subformulas (Not f1) = [f1] ++ (subformulas f1)
  32. subformulas (Forall _ f1) = subformulas (Not f1)
  33. subformulas (Exists _ f1) = subformulas (Not f1)
  34. subformulas _ = []
  35.  
  36. termSubstitute x t tt@(Var _) = if tt == x then t else tt
  37. termSubstitute _ _ tt@(Const _) = tt
  38. termSubstitute x t tt@(Func f ts) = Func f $ map (\z -> termSubstitute x t z) ts
  39.  
  40. -- where possible
  41. formSubstitute x t (Rel r ts) = Rel r $ map (\z -> termSubstitute x t z) ts
  42. formSubstitute x t (And f1 f2) = And (formSubstitute x t f1) (formSubstitute x t f2)
  43. formSubstitute x t (Or f1 f2) = Or (formSubstitute x t f1) (formSubstitute x t f2)
  44. formSubstitute x t (To f1 f2) = To (formSubstitute x t f1) (formSubstitute x t f2)
  45. formSubstitute x t (Not f1) = Not (formSubstitute x t f1)
  46. formSubstitute x t ff@(Forall v f) = if ((Var v) `elem` (freeVars $ Term t)) || ((Var v) == x) then ff else Forall v $ formSubstitute x t f
  47. formSubstitute x t ff@(Exists v f) = if ((Var v) `elem` (freeVars $ Term t)) || ((Var v) == x) then ff else Exists v $ formSubstitute x t f
  48. formSubstitute _ _ whatever = whatever
  49.  
  50. -- duplicates are allowed!
  51. freeVars (Term (Var s)) = [Var s]
  52. freeVars (Term (Func _ ts)) = concat $ map (\x -> freeVars (Term x)) ts
  53. freeVars (Formula (Rel _ ts)) = freeVars $ Term $ Func "" ts
  54. freeVars (Formula (And f1 f2)) = freeVars (Formula f1) ++ (freeVars $ Formula f2)
  55. freeVars (Formula (Or f1 f2)) = freeVars (Formula f1) ++ (freeVars $ Formula f2)
  56. freeVars (Formula (To f1 f2)) = freeVars (Formula f1) ++ (freeVars $ Formula f2)
  57. freeVars (Formula (Not f1)) = freeVars (Formula f1)
  58. freeVars (Formula (Forall v f)) = remove (Var v) $ freeVars $ Formula f
  59. freeVars (Formula (Exists v f)) = remove (Var v) $ freeVars $ Formula f
  60.  
  61. remove _ [] = []
  62. remove y (x:xs) = if x == y then remove y xs else x : (remove y xs)
  63.  
  64. main = do
  65.        print f
  66.        print $ formSubstitute (Var "x") (Func "f" [Var "z",Var "y"]) f
  67.        where f = Exists "y" $ And (Rel "R2" [Var "x"]) (Not $ Forall "x" $ Or (Rel "R1" [Var "x"]) (Rel "R1" [Var "y"]))
Advertisement
Add Comment
Please, Sign In to add comment