mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 19:42:50 -06:00
GF.Grammar.Macros: simplify composOp and composSafeOp
This commit is contained in:
@@ -27,7 +27,9 @@ import GF.Grammar.Values
|
|||||||
import GF.Grammar.Predef
|
import GF.Grammar.Predef
|
||||||
import GF.Grammar.Printer
|
import GF.Grammar.Printer
|
||||||
|
|
||||||
import Control.Monad (liftM, liftM2)
|
import Control.Monad.Identity(Identity(..))
|
||||||
|
import qualified Data.Traversable as T(mapM)
|
||||||
|
import Control.Monad (liftM, liftM2, liftM3)
|
||||||
import Data.Char (isDigit)
|
import Data.Char (isDigit)
|
||||||
import Data.List (sortBy,nub)
|
import Data.List (sortBy,nub)
|
||||||
import Text.PrettyPrint
|
import Text.PrettyPrint
|
||||||
@@ -453,98 +455,34 @@ stringFromTerm = err id (ifNull "" (sstr . head)) . strsFromTerm
|
|||||||
|
|
||||||
-- | to define compositional term functions
|
-- | to define compositional term functions
|
||||||
composSafeOp :: (Term -> Term) -> Term -> Term
|
composSafeOp :: (Term -> Term) -> Term -> Term
|
||||||
composSafeOp op trm = case composOp (mkMonadic op) trm of
|
composSafeOp op = runIdentity . composOp (return . op)
|
||||||
Ok t -> t
|
|
||||||
_ -> error "the operation is safe isn't it ?"
|
|
||||||
where
|
|
||||||
mkMonadic f = return . f
|
|
||||||
|
|
||||||
-- | to define compositional term functions
|
-- | to define compositional term functions
|
||||||
composOp :: Monad m => (Term -> m Term) -> Term -> m Term
|
composOp :: Monad m => (Term -> m Term) -> Term -> m Term
|
||||||
composOp co trm =
|
composOp co trm =
|
||||||
case trm of
|
case trm of
|
||||||
App c a ->
|
App c a -> liftM2 App (co c) (co a)
|
||||||
do c' <- co c
|
Abs b x t -> liftM (Abs b x) (co t)
|
||||||
a' <- co a
|
Prod b x a t -> liftM2 (Prod b x) (co a) (co t)
|
||||||
return (App c' a')
|
S c a -> liftM2 S (co c) (co a)
|
||||||
Abs b x t ->
|
Table a c -> liftM2 Table (co a) (co c)
|
||||||
do t' <- co t
|
R r -> liftM R (mapAssignM co r)
|
||||||
return (Abs b x t')
|
RecType r -> liftM RecType (mapPairsM co r)
|
||||||
Prod b x a t ->
|
P t i -> liftM2 P (co t) (return i)
|
||||||
do a' <- co a
|
ExtR a c -> liftM2 ExtR (co a) (co c)
|
||||||
t' <- co t
|
T i cc -> liftM2 (flip T) (mapPairsM co cc) (changeTableType co i)
|
||||||
return (Prod b x a' t')
|
V ty vs -> liftM2 V (co ty) (mapM co vs)
|
||||||
S c a ->
|
Let (x,(mt,a)) b -> liftM3 let' (co a) (T.mapM co mt) (co b)
|
||||||
do c' <- co c
|
where let' a' mt' b' = Let (x,(mt',a')) b'
|
||||||
a' <- co a
|
C s1 s2 -> liftM2 C (co s1) (co s2)
|
||||||
return (S c' a')
|
Glue s1 s2 -> liftM2 Glue (co s1) (co s2)
|
||||||
Table a c ->
|
Alts t aa -> liftM2 Alts (co t) (mapM (pairM co) aa)
|
||||||
do a' <- co a
|
FV ts -> liftM FV (mapM co ts)
|
||||||
c' <- co c
|
Strs tt -> liftM Strs (mapM co tt)
|
||||||
return (Table a' c')
|
EPattType ty -> liftM EPattType (co ty)
|
||||||
R r ->
|
ELincat c ty -> liftM (ELincat c) (co ty)
|
||||||
do r' <- mapAssignM co r
|
ELin c ty -> liftM (ELin c) (co ty)
|
||||||
return (R r')
|
ImplArg t -> liftM ImplArg (co t)
|
||||||
RecType r ->
|
|
||||||
do r' <- mapPairListM (co . snd) r
|
|
||||||
return (RecType r')
|
|
||||||
P t i ->
|
|
||||||
do t' <- co t
|
|
||||||
return (P t' i)
|
|
||||||
ExtR a c ->
|
|
||||||
do a' <- co a
|
|
||||||
c' <- co c
|
|
||||||
return (ExtR a' c')
|
|
||||||
|
|
||||||
T i cc ->
|
|
||||||
do cc' <- mapPairListM (co . snd) cc
|
|
||||||
i' <- changeTableType co i
|
|
||||||
return (T i' cc')
|
|
||||||
|
|
||||||
V ty vs ->
|
|
||||||
do ty' <- co ty
|
|
||||||
vs' <- mapM co vs
|
|
||||||
return (V ty' vs')
|
|
||||||
|
|
||||||
Let (x,(mt,a)) b ->
|
|
||||||
do a' <- co a
|
|
||||||
mt' <- case mt of
|
|
||||||
Just t -> co t >>= (return . Just)
|
|
||||||
_ -> return mt
|
|
||||||
b' <- co b
|
|
||||||
return (Let (x,(mt',a')) b')
|
|
||||||
|
|
||||||
C s1 s2 ->
|
|
||||||
do v1 <- co s1
|
|
||||||
v2 <- co s2
|
|
||||||
return (C v1 v2)
|
|
||||||
Glue s1 s2 ->
|
|
||||||
do v1 <- co s1
|
|
||||||
v2 <- co s2
|
|
||||||
return (Glue v1 v2)
|
|
||||||
Alts t aa ->
|
|
||||||
do t' <- co t
|
|
||||||
aa' <- mapM (pairM co) aa
|
|
||||||
return (Alts t' aa')
|
|
||||||
FV ts -> mapM co ts >>= return . FV
|
|
||||||
Strs tt -> mapM co tt >>= return . Strs
|
|
||||||
|
|
||||||
EPattType ty ->
|
|
||||||
do ty' <- co ty
|
|
||||||
return (EPattType ty')
|
|
||||||
|
|
||||||
ELincat c ty ->
|
|
||||||
do ty' <- co ty
|
|
||||||
return (ELincat c ty')
|
|
||||||
|
|
||||||
ELin c ty ->
|
|
||||||
do ty' <- co ty
|
|
||||||
return (ELin c ty')
|
|
||||||
|
|
||||||
ImplArg t ->
|
|
||||||
do t' <- co t
|
|
||||||
return (ImplArg t')
|
|
||||||
|
|
||||||
_ -> return trm -- covers K, Vr, Cn, Sort, EPatt
|
_ -> return trm -- covers K, Vr, Cn, Sort, EPatt
|
||||||
|
|
||||||
getTableType :: TInfo -> Err Type
|
getTableType :: TInfo -> Err Type
|
||||||
|
|||||||
Reference in New Issue
Block a user