{-# LANGUAGE TypeFamilies #-}
module AutoProof.Internal.AST
(
AST (Root, root, children, height, size, metadata),
ASTMetadata (ASTMetadata, getHeight, getSize),
subtrees,
properSubtrees,
atomicASTConstructor,
unaryASTConstructor,
binaryASTConstructor,
unaryRootedASTConstructor,
binaryRootedASTConstructor,
ternaryRootedASTConstructor,
)
where
import Data.Set (Set)
import qualified Data.Set as Set
data ASTMetadata = ASTMetadata
{
ASTMetadata -> Int
getHeight :: !Int,
ASTMetadata -> Int
getSize :: !Int
}
deriving (ASTMetadata -> ASTMetadata -> Bool
(ASTMetadata -> ASTMetadata -> Bool)
-> (ASTMetadata -> ASTMetadata -> Bool) -> Eq ASTMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ASTMetadata -> ASTMetadata -> Bool
$c/= :: ASTMetadata -> ASTMetadata -> Bool
== :: ASTMetadata -> ASTMetadata -> Bool
$c== :: ASTMetadata -> ASTMetadata -> Bool
Eq, Eq ASTMetadata
Eq ASTMetadata =>
(ASTMetadata -> ASTMetadata -> Ordering)
-> (ASTMetadata -> ASTMetadata -> Bool)
-> (ASTMetadata -> ASTMetadata -> Bool)
-> (ASTMetadata -> ASTMetadata -> Bool)
-> (ASTMetadata -> ASTMetadata -> Bool)
-> (ASTMetadata -> ASTMetadata -> ASTMetadata)
-> (ASTMetadata -> ASTMetadata -> ASTMetadata)
-> Ord ASTMetadata
ASTMetadata -> ASTMetadata -> Bool
ASTMetadata -> ASTMetadata -> Ordering
ASTMetadata -> ASTMetadata -> ASTMetadata
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ASTMetadata -> ASTMetadata -> ASTMetadata
$cmin :: ASTMetadata -> ASTMetadata -> ASTMetadata
max :: ASTMetadata -> ASTMetadata -> ASTMetadata
$cmax :: ASTMetadata -> ASTMetadata -> ASTMetadata
>= :: ASTMetadata -> ASTMetadata -> Bool
$c>= :: ASTMetadata -> ASTMetadata -> Bool
> :: ASTMetadata -> ASTMetadata -> Bool
$c> :: ASTMetadata -> ASTMetadata -> Bool
<= :: ASTMetadata -> ASTMetadata -> Bool
$c<= :: ASTMetadata -> ASTMetadata -> Bool
< :: ASTMetadata -> ASTMetadata -> Bool
$c< :: ASTMetadata -> ASTMetadata -> Bool
compare :: ASTMetadata -> ASTMetadata -> Ordering
$ccompare :: ASTMetadata -> ASTMetadata -> Ordering
$cp1Ord :: Eq ASTMetadata
Ord)
class AST t where
type Root t
root :: t -> Root t
children :: t -> [t]
metadata :: t -> ASTMetadata
height :: t -> Int
height = ASTMetadata -> Int
getHeight (ASTMetadata -> Int) -> (t -> ASTMetadata) -> t -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> ASTMetadata
forall t. AST t => t -> ASTMetadata
metadata
size :: t -> Int
size = ASTMetadata -> Int
getSize (ASTMetadata -> Int) -> (t -> ASTMetadata) -> t -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> ASTMetadata
forall t. AST t => t -> ASTMetadata
metadata
atomicASTConstructor :: (ASTMetadata -> a -> t) -> a -> t
atomicASTConstructor :: (ASTMetadata -> a -> t) -> a -> t
atomicASTConstructor g :: ASTMetadata -> a -> t
g = ASTMetadata -> a -> t
g ASTMetadata
atomicMetadata
unaryASTConstructor :: AST t => (ASTMetadata -> t -> t) -> t -> t
unaryASTConstructor :: (ASTMetadata -> t -> t) -> t -> t
unaryASTConstructor g :: ASTMetadata -> t -> t
g t :: t
t = ASTMetadata -> t -> t
g (t -> ASTMetadata
forall t. AST t => t -> ASTMetadata
unaryMetadata t
t) t
t
binaryASTConstructor :: AST t => (ASTMetadata -> t -> t -> t) -> t -> t -> t
binaryASTConstructor :: (ASTMetadata -> t -> t -> t) -> t -> t -> t
binaryASTConstructor g :: ASTMetadata -> t -> t -> t
g t :: t
t u :: t
u = ASTMetadata -> t -> t -> t
g (t -> t -> ASTMetadata
forall t. AST t => t -> t -> ASTMetadata
binaryMetadata t
t t
u) t
t t
u
unaryRootedASTConstructor :: AST t => (ASTMetadata -> a -> t -> t) -> a -> t -> t
unaryRootedASTConstructor :: (ASTMetadata -> a -> t -> t) -> a -> t -> t
unaryRootedASTConstructor g :: ASTMetadata -> a -> t -> t
g a :: a
a t :: t
t = ASTMetadata -> a -> t -> t
g (t -> ASTMetadata
forall t. AST t => t -> ASTMetadata
unaryMetadata t
t) a
a t
t
binaryRootedASTConstructor :: AST t => (ASTMetadata -> a -> t -> t -> t) -> a -> t -> t -> t
binaryRootedASTConstructor :: (ASTMetadata -> a -> t -> t -> t) -> a -> t -> t -> t
binaryRootedASTConstructor g :: ASTMetadata -> a -> t -> t -> t
g a :: a
a t :: t
t u :: t
u = ASTMetadata -> a -> t -> t -> t
g (t -> t -> ASTMetadata
forall t. AST t => t -> t -> ASTMetadata
binaryMetadata t
t t
u) a
a t
t t
u
ternaryRootedASTConstructor :: AST t => (ASTMetadata -> a -> t -> t -> t -> t) -> a -> t -> t -> t -> t
ternaryRootedASTConstructor :: (ASTMetadata -> a -> t -> t -> t -> t) -> a -> t -> t -> t -> t
ternaryRootedASTConstructor g :: ASTMetadata -> a -> t -> t -> t -> t
g a :: a
a t :: t
t u :: t
u v :: t
v = ASTMetadata -> a -> t -> t -> t -> t
g (t -> t -> t -> ASTMetadata
forall t. AST t => t -> t -> t -> ASTMetadata
ternaryMetadata t
t t
u t
v) a
a t
t t
u t
v
atomicMetadata :: ASTMetadata
atomicMetadata :: ASTMetadata
atomicMetadata = $WASTMetadata :: Int -> Int -> ASTMetadata
ASTMetadata {getHeight :: Int
getHeight = 0, getSize :: Int
getSize = 1}
unaryMetadata :: AST t => t -> ASTMetadata
unaryMetadata :: t -> ASTMetadata
unaryMetadata t :: t
t = $WASTMetadata :: Int -> Int -> ASTMetadata
ASTMetadata {getHeight :: Int
getHeight = 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ t -> Int
forall t. AST t => t -> Int
height t
t, getSize :: Int
getSize = 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ t -> Int
forall t. AST t => t -> Int
size t
t}
binaryMetadata :: AST t => t -> t -> ASTMetadata
binaryMetadata :: t -> t -> ASTMetadata
binaryMetadata t :: t
t u :: t
u =
$WASTMetadata :: Int -> Int -> ASTMetadata
ASTMetadata
{ getHeight :: Int
getHeight = 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (t -> Int
forall t. AST t => t -> Int
height t
t) (t -> Int
forall t. AST t => t -> Int
height t
u),
getSize :: Int
getSize = 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ t -> Int
forall t. AST t => t -> Int
size t
t Int -> Int -> Int
forall a. Num a => a -> a -> a
+ t -> Int
forall t. AST t => t -> Int
size t
u
}
ternaryMetadata :: AST t => t -> t -> t -> ASTMetadata
ternaryMetadata :: t -> t -> t -> ASTMetadata
ternaryMetadata t :: t
t u :: t
u v :: t
v =
$WASTMetadata :: Int -> Int -> ASTMetadata
ASTMetadata
{ getHeight :: Int
getHeight = 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (t -> Int
forall t. AST t => t -> Int
height t
t) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (t -> Int
forall t. AST t => t -> Int
height t
u) (t -> Int
forall t. AST t => t -> Int
height t
v)),
getSize :: Int
getSize = 1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ t -> Int
forall t. AST t => t -> Int
size t
t Int -> Int -> Int
forall a. Num a => a -> a -> a
+ t -> Int
forall t. AST t => t -> Int
size t
u Int -> Int -> Int
forall a. Num a => a -> a -> a
+ t -> Int
forall t. AST t => t -> Int
size t
v
}
subtrees :: (AST t, Ord t) => t -> Set t
subtrees :: t -> Set t
subtrees t :: t
t = (t -> Set t -> Set t) -> Set t -> [t] -> Set t
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Set t -> Set t -> Set t
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Set t -> Set t -> Set t) -> (t -> Set t) -> t -> Set t -> Set t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Set t
forall t. (AST t, Ord t) => t -> Set t
subtrees) (t -> Set t
forall a. a -> Set a
Set.singleton t
t) (t -> [t]
forall t. AST t => t -> [t]
children t
t)
properSubtrees :: (AST t, Ord t) => t -> Set t
properSubtrees :: t -> Set t
properSubtrees t :: t
t = (t -> Set t -> Set t) -> Set t -> [t] -> Set t
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Set t -> Set t -> Set t
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Set t -> Set t -> Set t) -> (t -> Set t) -> t -> Set t -> Set t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> Set t
forall t. (AST t, Ord t) => t -> Set t
subtrees) Set t
forall a. Set a
Set.empty (t -> [t]
forall t. AST t => t -> [t]
children t
t)