module Logic.Judge.Formula.Datastructure (
Formula(..)
, Marked(..)
, Term(..)
, Ambiguous(..)
, Proposition
, Predicate
, FormulaML
, FormulaJL
, Classical
, Quantifier(..)
, Modality(..)
, Justification(..)
, simplify
, asTerm
, isFormula
, isExtension
, isMarkedFormula
, Subterm(..)
, HasVariables(..)
) where
import "base" Data.List (nub)
data Formula ext
= Variable String
| Constant Bool
| Extend ext (Formula ext)
| Negation (Formula ext)
| Conjunction (Formula ext) (Formula ext)
| Disjunction (Formula ext) (Formula ext)
| XDisjunction (Formula ext) (Formula ext)
| Implication (Formula ext) (Formula ext)
| BiImplication (Formula ext) (Formula ext)
deriving (Eq, Ord)
type Proposition = Formula Classical
type Predicate = Formula Quantifier
type FormulaML = Formula Modality
type FormulaJL = Formula Justification
type Classical = ()
data Quantifier
= Universal String
| Existential String
deriving (Eq)
data Modality
= Necessary
| Possible
deriving (Eq, Ord)
data Justification
= ProofVariable String
| ProofConstant String
| ProofChecker Justification
| Application Justification Justification
| Sum Justification Justification
deriving (Eq, Ord)
data Marked formula = Marked
{ marks :: [String]
, unmarked :: formula
} deriving (Eq, Ord)
instance Functor Marked where
fmap f (Marked marks x) = Marked marks (f x)
simplify :: Formula ext -> Formula ext
simplify formula = case formula of
Variable v -> Variable v
Constant False -> Constant False
Constant True -> Implication (Constant False) (Constant False)
Extend e f -> Extend e (simplify f)
Implication f1 f2 -> Implication (simplify f1) (simplify f2)
Negation f -> Implication (simplify f) (Constant False)
Disjunction f1 f2 -> simplify $ Implication (Negation f1) f2
Conjunction f1 f2 -> simplify $ Negation (Implication f1 (Negation f2))
XDisjunction f1 f2 -> simplify $ Conjunction (Implication (Negation f1) f2) (Implication (Negation f2) f1)
BiImplication f1 f2 -> simplify $ Conjunction (Implication f1 f2) (Implication f2 f1)
data Term ext
= Formula (Formula ext)
| Extension ext
| MarkedFormula (Marked (Formula ext))
deriving (Eq, Ord)
isFormula :: Term ext -> Bool
isFormula (Formula f) = True
isFormula _ = False
isExtension :: Term ext -> Bool
isExtension (Extension e) = True
isExtension _ = False
isMarkedFormula :: Term ext -> Bool
isMarkedFormula (MarkedFormula _) = True
isMarkedFormula _ = False
asTerm :: Marked (Formula ext) -> [Term ext]
asTerm φ = [MarkedFormula φ, Formula . unmarked $ φ]
newtype Ambiguous term = Ambiguous [term]
class (Subterm ext) term where
subterms :: term -> [Term ext]
instance Subterm ext ext => Subterm ext (Term ext) where
subterms (Formula f) = subterms f
subterms (Extension e) = subterms e
subterms (MarkedFormula f) = subterms f
instance Subterm ext ext => Subterm ext (Marked (Formula ext)) where
subterms t@(Marked _ f) = (MarkedFormula t) : subterms f
instance Subterm ext ext => Subterm ext (Ambiguous (Term ext)) where
subterms (Ambiguous terms) = terms >>= subterms
instance Subterm ext ext => Subterm ext (Formula ext) where
subterms term = case term of
t@(Variable var) -> [Formula t]
t@(Constant a) -> [Formula t]
t@(Implication a b) -> Formula t:subterms a ++ subterms b
t@(Extend e a) -> Formula t:subterms e ++ subterms a
instance Subterm Justification Justification where
subterms term = case term of
t@(ProofVariable var) -> [Extension t]
t@(ProofConstant c) -> [Extension t]
t@(ProofChecker s) -> Extension t:subterms s
t@(Application s u) -> Extension t:subterms s ++ subterms u
t@(Sum s u) -> Extension t:subterms s ++ subterms u
class HasVariables term where
variables :: term -> [String]
isVariable :: term -> Bool
isConstant :: term -> Bool
isAtomary :: term -> Bool
isAtomary t = isConstant t || isVariable t
size :: term -> Int
instance HasVariables ext => HasVariables (Term ext) where
variables (Formula f) = variables f
variables (Extension e) = variables e
variables (MarkedFormula f) = variables f
isVariable (Formula f) = isVariable f
isVariable (Extension e) = isVariable e
isVariable (MarkedFormula f) = False
isConstant (Formula f) = isConstant f
isConstant (Extension e) = isConstant e
isConstant (MarkedFormula f) = False
size (Formula f) = size f
size (Extension e) = size e
size (MarkedFormula f) = size f
instance HasVariables ext => HasVariables (Ambiguous (Term ext)) where
variables (Ambiguous terms) = terms >>= variables
isVariable (Ambiguous terms) = any isConstant terms
isConstant (Ambiguous terms) = any isConstant terms
size (Ambiguous []) = 0
size (Ambiguous (t:_)) = size t
instance HasVariables term => HasVariables (Marked term) where
variables (Marked _ f) = variables f
isVariable (Marked _ f) = isVariable f
isConstant (Marked _ f) = isConstant f
size (Marked m f) = length m + size f
instance HasVariables ext => HasVariables (Formula ext) where
variables term = case term of
Variable var -> [var]
Constant a -> []
Implication a b -> variables a ++ variables b
Extend e a -> variables e ++ variables a
isVariable (Variable _) = True
isVariable _ = False
isConstant (Constant _) = True
isConstant _ = False
size term = case term of
Implication a b -> 1 + size a + size b
Extend a b -> 1 + size a + size b
_ -> 0
instance HasVariables Justification where
variables term = case term of
ProofVariable var -> [var]
ProofConstant c -> []
ProofChecker s -> variables s
Application s u -> variables s ++ variables u
Sum s u -> variables s ++ variables u
isVariable (ProofVariable _) = True
isVariable _ = True
isConstant (ProofConstant _) = True
isConstant _ = False
size term = case term of
ProofChecker s -> 1 + size s
Application a b -> 1 + size a + size b
Sum a b -> 1 + size a + size b
_ -> 0