{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
module Language.REST.RuntimeTerm
( RuntimeTerm(..)
, ToRuntimeTerm(..)
, subTerms
, contains
)
where
import Data.Hashable
import GHC.Generics (Generic)
import Text.Printf
import qualified Data.List as L
import Language.REST.Op
data RuntimeTerm = App Op [RuntimeTerm] deriving (RuntimeTerm -> RuntimeTerm -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RuntimeTerm -> RuntimeTerm -> Bool
$c/= :: RuntimeTerm -> RuntimeTerm -> Bool
== :: RuntimeTerm -> RuntimeTerm -> Bool
$c== :: RuntimeTerm -> RuntimeTerm -> Bool
Eq, Eq RuntimeTerm
RuntimeTerm -> RuntimeTerm -> Bool
RuntimeTerm -> RuntimeTerm -> Ordering
RuntimeTerm -> RuntimeTerm -> RuntimeTerm
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 :: RuntimeTerm -> RuntimeTerm -> RuntimeTerm
$cmin :: RuntimeTerm -> RuntimeTerm -> RuntimeTerm
max :: RuntimeTerm -> RuntimeTerm -> RuntimeTerm
$cmax :: RuntimeTerm -> RuntimeTerm -> RuntimeTerm
>= :: RuntimeTerm -> RuntimeTerm -> Bool
$c>= :: RuntimeTerm -> RuntimeTerm -> Bool
> :: RuntimeTerm -> RuntimeTerm -> Bool
$c> :: RuntimeTerm -> RuntimeTerm -> Bool
<= :: RuntimeTerm -> RuntimeTerm -> Bool
$c<= :: RuntimeTerm -> RuntimeTerm -> Bool
< :: RuntimeTerm -> RuntimeTerm -> Bool
$c< :: RuntimeTerm -> RuntimeTerm -> Bool
compare :: RuntimeTerm -> RuntimeTerm -> Ordering
$ccompare :: RuntimeTerm -> RuntimeTerm -> Ordering
Ord, forall x. Rep RuntimeTerm x -> RuntimeTerm
forall x. RuntimeTerm -> Rep RuntimeTerm x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RuntimeTerm x -> RuntimeTerm
$cfrom :: forall x. RuntimeTerm -> Rep RuntimeTerm x
Generic, Eq RuntimeTerm
Int -> RuntimeTerm -> Int
RuntimeTerm -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: RuntimeTerm -> Int
$chash :: RuntimeTerm -> Int
hashWithSalt :: Int -> RuntimeTerm -> Int
$chashWithSalt :: Int -> RuntimeTerm -> Int
Hashable)
instance Show RuntimeTerm where
show :: RuntimeTerm -> String
show (App Op
op []) = forall a. Show a => a -> String
show Op
op
show (App Op
op [RuntimeTerm]
ts) = forall r. PrintfType r => String -> r
printf String
"%s(%s)" (forall a. Show a => a -> String
show Op
op) forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [[a]] -> [a]
L.intercalate String
", " (forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show [RuntimeTerm]
ts)
class ToRuntimeTerm a where
toRuntimeTerm :: a -> RuntimeTerm
instance ToRuntimeTerm Op where
toRuntimeTerm :: Op -> RuntimeTerm
toRuntimeTerm Op
op = Op -> [RuntimeTerm] -> RuntimeTerm
App Op
op []
instance ToRuntimeTerm RuntimeTerm where
toRuntimeTerm :: RuntimeTerm -> RuntimeTerm
toRuntimeTerm = forall a. a -> a
id
subTerms :: RuntimeTerm -> [(RuntimeTerm, RuntimeTerm -> RuntimeTerm)]
subTerms :: RuntimeTerm -> [(RuntimeTerm, RuntimeTerm -> RuntimeTerm)]
subTerms t :: RuntimeTerm
t@(App Op
f [RuntimeTerm]
ts) = (RuntimeTerm
t, forall a. a -> a
id) forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Int -> [(RuntimeTerm, RuntimeTerm -> RuntimeTerm)]
st [Int
0..forall (t :: * -> *) a. Foldable t => t a -> Int
length [RuntimeTerm]
ts forall a. Num a => a -> a -> a
- Int
1]
where
st :: Int -> [(RuntimeTerm, RuntimeTerm -> RuntimeTerm)]
st :: Int -> [(RuntimeTerm, RuntimeTerm -> RuntimeTerm)]
st Int
i =
let
ti :: RuntimeTerm
ti = [RuntimeTerm]
ts forall a. [a] -> Int -> a
!! Int
i
go :: RuntimeTerm -> RuntimeTerm
go RuntimeTerm
t' =
Op -> [RuntimeTerm] -> RuntimeTerm
App Op
f forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
take Int
i [RuntimeTerm]
ts forall a. [a] -> [a] -> [a]
++ [RuntimeTerm
t'] forall a. [a] -> [a] -> [a]
++ forall a. Int -> [a] -> [a]
drop (Int
i forall a. Num a => a -> a -> a
+ Int
1) [RuntimeTerm]
ts
go2 :: (a, a -> RuntimeTerm) -> (a, a -> RuntimeTerm)
go2 (a
srt, a -> RuntimeTerm
toFull) = (a
srt, RuntimeTerm -> RuntimeTerm
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> RuntimeTerm
toFull)
in
forall a b. (a -> b) -> [a] -> [b]
map forall {a} {a}. (a, a -> RuntimeTerm) -> (a, a -> RuntimeTerm)
go2 (RuntimeTerm -> [(RuntimeTerm, RuntimeTerm -> RuntimeTerm)]
subTerms RuntimeTerm
ti)
contains :: RuntimeTerm -> RuntimeTerm -> Bool
contains :: RuntimeTerm -> RuntimeTerm -> Bool
contains RuntimeTerm
t1 RuntimeTerm
t2 | RuntimeTerm
t1 forall a. Eq a => a -> a -> Bool
== RuntimeTerm
t2 = Bool
True
contains (App Op
_ [RuntimeTerm]
ts) RuntimeTerm
t = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (RuntimeTerm -> RuntimeTerm -> Bool
contains RuntimeTerm
t) [RuntimeTerm]
ts