module S06 (
safeTail, safeInit, safeLast,
root, isWHNF, cbn,
equals,
) where
import BTree (BTree(..))
import qualified BTree
import Term
import Data.List
import Set
safeTail, safeInit :: [a] -> Maybe [a]
safeTail [] = Nothing
safeTail (_:xs) = Just xs
safeInit [] = Nothing
safeInit (x:xs) = case safeInit xs of
Nothing -> Just []
Just ys -> Just $ x:ys
safeLast :: [a] -> Maybe a
safeLast [] = Nothing
safeLast (x:xs) = case safeLast xs of
Nothing -> Just x
y -> y
root :: Term -> Maybe Term
root (App (Abs x t) u) = Just $ subst x u t
root _ = Nothing
isWHNF :: Term -> Bool
isWHNF (Var _) = True
isWHNF (Abs _ _) = True
isWHNF (App (Abs _ _) _) = False
isWHNF (App t _) = isWHNF t
cbn :: Term -> Maybe Term
cbn t =
if isWHNF t then Nothing
else case root t of
Nothing -> leftmost t
u -> u
where
leftmost (App t u) = case cbn t of
Just t' -> Just $ App t' u
Nothing -> case cbn u of
Just u' -> Just $ App t u'
Nothing -> Nothing
equals :: Ord a => Set a -> Set a -> Bool
equals s t = toList s == toList t
instance Ord a => Eq (Set a) where
(==) = equals