Guest User

foh

a guest
Feb 25th, 2017
33
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.  
  29. unterm (Term t) = t
  30.  
  31. unformula (Formula f) = f
  32.  
  33. subformulas (And f1 f2) = [f1,f2] ++ (subformulas f1) ++ (subformulas f2)
  34. subformulas (Or f1 f2) = subformulas (And f1 f2)
  35. subformulas (To f1 f2) = subformulas (And f1 f2)
  36. subformulas (Not f1) = [f1] ++ (subformulas f1)
  37. subformulas (Forall _ f1) = subformulas (Not f1)
  38. subformulas (Exists _ f1) = subformulas (Not f1)
  39. subformulas _ = []
  40.  
  41. termSubstitute x t tt@(Var _) = if tt == x then t else tt
  42. termSubstitute _ _ tt@(Const _) = tt
  43. termSubstitute x t tt@(Func f ts) = Func f $ map (\z -> termSubstitute x t z) ts
  44.  
  45. formSubstitute x t (Rel r ts) = Rel r $ map (\z -> termSubstitute x t z) ts
  46. formSubstitute x t (And f1 f2) = And (formSubstitute x t f1) (formSubstitute x t f2)
  47. formSubstitute x t (Or f1 f2) = Or (formSubstitute x t f1) (formSubstitute x t f2)
  48. formSubstitute x t (To f1 f2) = To (formSubstitute x t f1) (formSubstitute x t f2)
  49. formSubstitute x t (Not f1) = Not (formSubstitute x t f1)
  50. formSubstitute x t ff@(Forall v f) = if (Var v) `elem` (freeVars $ Term t)
  51.                                      then undefined
  52.                                      else if (Var v) == x
  53.                                      then ff else Forall v $ formSubstitute x t f
  54. formSubstitute x t ff@(Exists v f) = if (Var v) `elem` (freeVars $ Term t)
  55.                                      then undefined
  56.                                      else if (Var v) == x
  57.                                      then ff else Exists v $ formSubstitute x t f
  58. formSubstitute _ _ whatever = whatever
  59.  
  60. -- duplicates are allowed!
  61. freeVars (Term (Var s)) = [Var s]
  62. freeVars (Term (Func _ ts)) = concat $ map (\x -> freeVars (Term x)) ts
  63. freeVars (Formula (Rel _ ts)) = freeVars $ Term $ Func "" ts
  64. freeVars (Formula (And f1 f2)) = freeVars (Formula f1) ++ (freeVars $ Formula f2)
  65. freeVars (Formula (Or f1 f2)) = freeVars (Formula f1) ++ (freeVars $ Formula f2)
  66. freeVars (Formula (To f1 f2)) = freeVars (Formula f1) ++ (freeVars $ Formula f2)
  67. freeVars (Formula (Not f1)) = freeVars (Formula f1)
  68. freeVars (Formula (Forall v f)) = remove (Var v) $ freeVars $ Formula f
  69. freeVars (Formula (Exists v f)) = remove (Var v) $ freeVars $ Formula f
  70.  
  71. remove _ [] = []
  72. remove y (x:xs) = if x == y then remove y xs else x : (remove y xs)
  73.  
  74. main = do
  75.        print f
  76.        print $ formSubstitute (Var "x") (Var "z") f
  77.        where f = And (Rel "R2" [Var "x"]) (Not $ Forall "x" $ Or (Rel "R1" [Var "x"]) (Rel "R1" [Var "y"]))
Add Comment
Please, Sign In to add comment