{-# LANGUAGE FlexibleContexts #-}
module AutoProof.Internal.Parser
(
parseFormula,
parseJudgement,
unsafeParseFormula,
unsafeParseJudgement,
)
where
import AutoProof.Internal.Formula (Formula (And, Iff, Imp, Lit, Not, Or, Var))
import AutoProof.Internal.Judgement
( Judgement,
(|-),
)
import AutoProof.Internal.Utils.Parser
( Parser,
between,
chainl1,
chainr1,
char,
eof,
notFollowedBy,
oneOf,
parse,
sepBy,
spaces,
string,
)
import Control.Applicative (Alternative (empty, many, (<|>)))
import Data.Maybe (fromMaybe)
parseFormula :: String -> Maybe (Formula String)
parseFormula :: String -> Maybe (Formula String)
parseFormula = Parser (Formula String) -> String -> Maybe (Formula String)
forall a. Parser a -> String -> Maybe a
parse (Parser (Formula String)
formula Parser (Formula String) -> Parser () -> Parser (Formula String)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
eof)
parseJudgement :: String -> Maybe (Judgement String)
parseJudgement :: String -> Maybe (Judgement String)
parseJudgement = Parser (Judgement String) -> String -> Maybe (Judgement String)
forall a. Parser a -> String -> Maybe a
parse (Parser (Judgement String)
judgement Parser (Judgement String) -> Parser () -> Parser (Judgement String)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
eof)
unsafeParseFormula :: String -> Formula String
unsafeParseFormula :: String -> Formula String
unsafeParseFormula = Formula String -> Maybe (Formula String) -> Formula String
forall a. a -> Maybe a -> a
fromMaybe (String -> Formula String
forall a. HasCallStack => String -> a
error "Parse error") (Maybe (Formula String) -> Formula String)
-> (String -> Maybe (Formula String)) -> String -> Formula String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe (Formula String)
parseFormula
unsafeParseJudgement :: String -> Judgement String
unsafeParseJudgement :: String -> Judgement String
unsafeParseJudgement = Judgement String -> Maybe (Judgement String) -> Judgement String
forall a. a -> Maybe a -> a
fromMaybe (String -> Judgement String
forall a. HasCallStack => String -> a
error "Parse error") (Maybe (Judgement String) -> Judgement String)
-> (String -> Maybe (Judgement String))
-> String
-> Judgement String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe (Judgement String)
parseJudgement
formula :: Parser (Formula String)
formula :: Parser (Formula String)
formula = Parser (Formula String) -> Parser (Formula String)
forall a. Parser a -> Parser a
padded Parser (Formula String)
implication
context :: Parser [Formula String]
context :: Parser [Formula String]
context = Parser (Formula String)
formula Parser (Formula String) -> Parser Char -> Parser [Formula String]
forall a b. Parser a -> Parser b -> Parser [a]
`sepBy` Char -> Parser Char
char ','
judgement :: Parser (Judgement String)
judgement :: Parser (Judgement String)
judgement = [Formula String] -> Formula String -> Judgement String
forall a (f :: * -> *).
(Ord a, Foldable f) =>
f (Formula a) -> Formula a -> Judgement a
(|-) ([Formula String] -> Formula String -> Judgement String)
-> Parser [Formula String]
-> Parser (Formula String -> Judgement String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Formula String]
context Parser (Formula String -> Judgement String)
-> Parser (Formula String) -> Parser (Judgement String)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Parser String
spaces Parser String -> Parser String -> Parser String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser String
turnstileS Parser String -> Parser (Formula String) -> Parser (Formula String)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser (Formula String)
formula)
symbol :: [String] -> Parser String
symbol :: [String] -> Parser String
symbol [] = Parser String
forall (f :: * -> *) a. Alternative f => f a
empty
symbol (c :: String
c : cs :: [String]
cs)
| String
c String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "|" = (String -> Parser String
string "|" Parser String -> Parser () -> Parser String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser Char -> Parser ()
forall a. Parser a -> Parser ()
notFollowedBy (Char -> Parser Char
char '-')) Parser String -> Parser String -> Parser String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [String] -> Parser String
symbol [String]
cs
| Bool
otherwise = String -> Parser String
string String
c Parser String -> Parser String -> Parser String
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [String] -> Parser String
symbol [String]
cs
impS :: Parser String
impS :: Parser String
impS = [String] -> Parser String
symbol ["->", "=>", "→", "⇾", "⇒", "⊃"]
iffS :: Parser String
iffS :: Parser String
iffS = [String] -> Parser String
symbol ["<->", "↔", "⇿", "⇔"]
andS :: Parser String
andS :: Parser String
andS = [String] -> Parser String
symbol ["&", "^", "∧", "/\\", "*"]
orS :: Parser String
orS :: Parser String
orS = [String] -> Parser String
symbol ["|", "∨", "\\/", "+"]
notS :: Parser String
notS :: Parser String
notS = [String] -> Parser String
symbol ["~", "¬"]
falseS :: Parser String
falseS :: Parser String
falseS = [String] -> Parser String
symbol ["⊥", "false"]
trueS :: Parser String
trueS :: Parser String
trueS = [String] -> Parser String
symbol ["⊤", "true"]
turnstileS :: Parser String
turnstileS :: Parser String
turnstileS = [String] -> Parser String
symbol ["|-", "⊢"]
implication :: Parser (Formula String)
implication :: Parser (Formula String)
implication = Parser (Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String)
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainr1 Parser (Formula String)
equivalence (Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
forall a. Parser a -> Parser a
padded (Formula String -> Formula String -> Formula String
forall a. Formula a -> Formula a -> Formula a
Imp (Formula String -> Formula String -> Formula String)
-> Parser String
-> Parser (Formula String -> Formula String -> Formula String)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser String
impS))
equivalence :: Parser (Formula String)
equivalence :: Parser (Formula String)
equivalence = Parser (Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String)
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 Parser (Formula String)
disjunction (Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
forall a. Parser a -> Parser a
padded (Formula String -> Formula String -> Formula String
forall a. Formula a -> Formula a -> Formula a
Iff (Formula String -> Formula String -> Formula String)
-> Parser String
-> Parser (Formula String -> Formula String -> Formula String)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser String
iffS))
disjunction :: Parser (Formula String)
disjunction :: Parser (Formula String)
disjunction = Parser (Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String)
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 Parser (Formula String)
conjunction (Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
forall a. Parser a -> Parser a
padded (Formula String -> Formula String -> Formula String
forall a. Formula a -> Formula a -> Formula a
Or (Formula String -> Formula String -> Formula String)
-> Parser String
-> Parser (Formula String -> Formula String -> Formula String)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser String
orS))
conjunction :: Parser (Formula String)
conjunction :: Parser (Formula String)
conjunction = Parser (Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String)
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 Parser (Formula String)
negation (Parser (Formula String -> Formula String -> Formula String)
-> Parser (Formula String -> Formula String -> Formula String)
forall a. Parser a -> Parser a
padded (Formula String -> Formula String -> Formula String
forall a. Formula a -> Formula a -> Formula a
And (Formula String -> Formula String -> Formula String)
-> Parser String
-> Parser (Formula String -> Formula String -> Formula String)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser String
andS))
negation :: Parser (Formula String)
negation :: Parser (Formula String)
negation = Parser String
spaces Parser String -> Parser (Formula String) -> Parser (Formula String)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (((Formula String -> Formula String)
-> (Formula String -> Formula String)
-> Formula String
-> Formula String)
-> (Formula String -> Formula String)
-> [Formula String -> Formula String]
-> Formula String
-> Formula String
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (Formula String -> Formula String)
-> (Formula String -> Formula String)
-> Formula String
-> Formula String
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) Formula String -> Formula String
forall a. a -> a
id ([Formula String -> Formula String]
-> Formula String -> Formula String)
-> Parser [Formula String -> Formula String]
-> Parser (Formula String -> Formula String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (Formula String -> Formula String)
-> Parser [Formula String -> Formula String]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Formula String -> Formula String
forall a. Formula a -> Formula a
Not (Formula String -> Formula String)
-> Parser String -> Parser (Formula String -> Formula String)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser String
notS) Parser (Formula String -> Formula String)
-> Parser (Formula String) -> Parser (Formula String)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Formula String)
atom)
atom :: Parser (Formula String)
atom :: Parser (Formula String)
atom = Parser (Formula String) -> Parser (Formula String)
forall a. Parser a -> Parser a
enclosed Parser (Formula String)
formula Parser (Formula String)
-> Parser (Formula String) -> Parser (Formula String)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser (Formula String)
false Parser (Formula String)
-> Parser (Formula String) -> Parser (Formula String)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser (Formula String)
true Parser (Formula String)
-> Parser (Formula String) -> Parser (Formula String)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser (Formula String)
variable
false :: Parser (Formula String)
false :: Parser (Formula String)
false = Bool -> Formula String
forall a. Bool -> Formula a
Lit Bool
False Formula String -> Parser String -> Parser (Formula String)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser String
falseS
true :: Parser (Formula String)
true :: Parser (Formula String)
true = Bool -> Formula String
forall a. Bool -> Formula a
Lit Bool
True Formula String -> Parser String -> Parser (Formula String)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Parser String
trueS
variable :: Parser (Formula String)
variable :: Parser (Formula String)
variable = String -> Formula String
forall a. a -> Formula a
Var (String -> Formula String)
-> Parser String -> Parser (Formula String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser String
name
where
name :: Parser String
name = (:) (Char -> String -> String)
-> Parser Char -> Parser (String -> String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Char
nameStart Parser (String -> String) -> Parser String -> Parser String
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Char -> Parser String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many Parser Char
nameChar
nameStart :: Parser Char
nameStart = String -> Parser Char
oneOf (String -> Parser Char) -> String -> Parser Char
forall a b. (a -> b) -> a -> b
$ '_' Char -> String -> String
forall a. a -> [a] -> [a]
: ['a' .. 'z'] String -> String -> String
forall a. [a] -> [a] -> [a]
++ ['A' .. 'Z']
nameChar :: Parser Char
nameChar = Parser Char
nameStart Parser Char -> Parser Char -> Parser Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> Parser Char
oneOf ('\'' Char -> String -> String
forall a. a -> [a] -> [a]
: ['0' .. '9'])
padded :: Parser a -> Parser a
padded :: Parser a -> Parser a
padded = Parser String -> Parser String -> Parser a -> Parser a
forall b c a. Parser b -> Parser c -> Parser a -> Parser a
between Parser String
spaces Parser String
spaces
enclosed :: Parser a -> Parser a
enclosed :: Parser a -> Parser a
enclosed = Parser Char -> Parser Char -> Parser a -> Parser a
forall b c a. Parser b -> Parser c -> Parser a -> Parser a
between (Char -> Parser Char
char '(') (Char -> Parser Char
char ')')