b0VIM 7.2MImmvierachecha~mviera/Doctorado/svn/staff.doaitse.mviera/syntaxmacros/SyntaxMacros2/src/Language/Grammars/Grammar.hsutf-8 3210#"! Utp>Gad4PPWV1zyqgfeT, 7 P O x * )   g M  m 4   qVU#U"c?yxw^EDCzyJ=< s=yw=<; (PP f) <*> (PP g) = PP [ Star f' g' | f' <- f, g' <- g ] pure f = PP [ Pure f ]instance Applicative (ListProd l env) where fmap f (PP p) = PP [ Star (Pure f) p' | p' <- p ]instance Functor (ListProd l env) wherefixPrd p = PP [ (Fix . prod) p ]fixPrd :: ListProd (FL a) env a -> ListProd l env avarPrd = PP [ Var ]varPrd :: ListProd (FL a) env atr s = PP [ Sym $ Term s ]tr :: String -> ListProd l env DTerm ntPrd s = id <$> nt sntPrd :: Symbol a TNonT env -> ListProd l env ant s = sym snt :: Symbol a TNonT env -> ListProd l env asym s = PP [ Sym $ s ]sym :: Symbol a t env -> ListProd l env aprod (PP ps) = PS psprod :: ListProd l env a -> Productions l a envnewtype ListProd l env a = PP { unPP :: [ Prod l a env ] }-- APPLICATIVE INTERFACE------------------------op = TermOpcon = TermConidvar = TermVaridchar = TermCharint = TermIntop :: Symbol String TAttT envcon :: Symbol String TAttT envvar :: Symbol String TAttT envchar :: Symbol Char TAttT envint :: Symbol Int TAttT envmatchSym _ _ = NothingmatchSym TermOp TermOp = Just EqmatchSym TermConid TermConid = Just EqmatchSym TermVarid TermVarid = Just EqmatchSym TermInt TermInt = Just EqmatchSym (Term x) (Term y) | x == y = Just EqmatchSym (Nont x) (Nont y) = pairEq $ match x y -> Maybe (Equal (a,t1) (b,t2))matchSym :: Symbol a t1 env -> Symbol b t2 env pairEq Nothing = NothingpairEq (Just Eq) = Just EqpairEq :: Maybe (Equal a b) -> Maybe (Equal (a,t) (b,t))getRefNT (Nont ref) = refgetRefNT :: Symbol a TNonT env -> Ref a env --- TODO: the rest of EnumValToken TermOp :: Symbol String TAttT env TermConid :: Symbol String TAttT env TermVarid :: Symbol String TAttT env TermChar :: Symbol Char TAttT env TermInt :: Symbol Int TAttT env -- attributed terminals Nont :: Ref a env -> Symbol a TNonT env Term :: String -> Symbol DTerm TTerm envdata Symbol a t env wheredata TAttTdata TNonTdata TTermtype DTerm = String-- Fix :: (forall s. Prod a s -> Productions a s) -> Prod a env -- I tried with HOAS, but it is more restrictive Var :: Prod (FL a) a env Fix :: Productions (FL a) a env -> Prod l a env Pure :: a -> Prod l a env Sym :: Symbol a t env -> Prod l a env FlipStar :: Prod l a env -> Prod l (a->b) env -> Prod l b env Star :: Prod l (a->b) env -> Prod l a env -> Prod l b envdata Prod l a env where = PS {unPS :: [Prod l a env]}newtype Productions l a env type GramEnv = Env (Productions TL) (GramEnv env env) = forall env . Grammar (Ref a env) data Grammar a data FL adata TL--------------------------------------------- GRAMMAR REPRESENTATION-------------------------------------------import Unsafe.Coerceimport Cont MultiParamTypeClasses, FunctionalDependencies, FlexibleInstanc MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, FlexibleContexts, UndecidableInstances #-}{-# LANGUAGE ExistentialQuantification, GADTs, EmptyDataDecls,ad>\-,+ ih: q N { Q P  _ Q P  E , L    ~}|FnGFEDZYrMBnmH-oZ$# show (Suc r) = show $ (1::Int) + ((read . show) r) show Zero = "0"instance Show (Ref a env) where show (TermOp) = "op" show (TermConid) = "con" show (TermVarid) = "var" show (TermChar) = "char" show (TermInt) = "int" show (Nont r) = show r show (Term s) = show sinstance Show (Symbol a t env) where show Var = "var" show (Fix f) = "fix " ++ show f show (Pure _) = "pure" show (Sym s) = show s show (FlipStar pa pf) = "(" ++ show pa ++ "<**>" ++ show pf ++ ")" show (Star pf pa) = "(" ++ show pf ++ "<*>" ++ show pa ++ ")"instance Show (Prod l a env) where show (PS prods) = show prodsinstance Show (Productions l a env) where showEnv n (Ext nts nont) = show n ++ "->" ++ show nont ++ "\n" ++ showEnv (n+1) nts showEnv _ (Empty) = "\n"instance ShowEnv (Env (Productions l) env env') where showEnv :: Int -> a -> Stringclass ShowEnv a where show env = showEnv 0 envinstance ShowEnv (Env (Productions l) env env') => Show (Env (Productions l) env env') where show (Grammar r prods) = show r ++ "\n" ++ show prodsinstance Show (Grammar a) where-- Just for debugging purposes-- Show instances for the Grammars------------------------------------------------------------------------------- l <=> v = (l .=.) <$> (PP [ Sym v ])instance LabelSymbol TTerm v v where -- only to have all the instances l <=> v = (l .=.) <$> (PP [ Sym v ])instance LabelSymbol TNonT v v where l <=> v = (\x -> l .=. (\(Record HNil) -> x)) <$> (PP [ Sym v ])instance LabelSym idiomatic isf str = idiomatic (isf <* (tr str)) idiomatic isf str = idiomatic (isf <* (tr str)) => Idiomatic l env f (String -> g) whereinstance (Idiomatic l env f g) idiomatic isf f = idiomatic (isf <*> (pure f))instance Idiomatic l env f g => Idiomatic l env ((a -> b) -> f) ((a -> b) -> g) where idiomatic isf is = idiomatic (isf <*> is)instance Idiomatic l env f g => Idiomatic l env (a -> f) (ListProd l env a -> g) where idiomatic ix Ii = ixinstance Idiomatic l env x (Ii -> ListProd l env x) where idiomatic :: ListProd l env f -> gclass Idiomatic l env f g | g -> f l env whereiI = idiomatic (pure id)iI ::Idiomatic l env (a -> a) g => g-- | The function `iI` is to be pronounced as @start@data Ii = Ii -- | The `Ii` is to be pronounced as @stop@-- IDIOMS------------------------ more = (:) <$> p <*> varPrd where none = pure [] pMany p = fixPrd (none <|> more)pMany :: ListProd (FL [a]) env a -> ListProd l env [a] more = (:) <$> p <*> varPrd where one = (:[]) <$> p pSome p = fixPrd (one <|> more)pSome :: ListProd (FL [a]) env a -> ListProd l env [a]prodToFL Var = unsafeCoerce Var -- the other option is to add an error hereprodToFL (Fix f) = Fix fprodToFL (Pure a) = Pure aprodToFL (Sym s) = Sym sprodToFL (FlipStar f g) = FlipStar (prodToFL f) (prodToFL g)prodToFL (Star f g) = Star (prodToFL f) (prodToFL g)prodToFL :: Prod l a env -> Prod (FL b) a envtoFL (PP p) = PP $ map prodToFL ptoFL :: ListProd l env a -> ListProd (FL b) env a more = (:) <$> toFL p <*> varPrd where none = pure [] many p = fixPrd (none <|> more) more = (:) <$> toFL p <*> varPrd where one = (:[]) <$> toFL p some p = fixPrd (one <|> more) (PP f) <|> (PP g) = PP (f ++ g) empty = PP []instance Alternative (ListProd l env) whereadGed=#"RQP B     e . - } | F ! ~ } Z  b B A   }dcC. s show (Suc r) = show $ (1::Int) + ((read . show) r) show Zero = "0"instance Show (Ref a env) where show (TermOp) = "op" show (TermConid) = "con" show (TermVarid) = "var" show (TermChar) = "char" show (TermInt) = "int" show (Nont r) = show r show (Term s) = show sinstance Show (Symbol a t env) where show Var = "var" show (Fix f) = "fix " ++ show f show (Pure _) = "pure" show (Sym s) = show s show (FlipStar pa pf) = "(" ++ show pa ++ "<**>" ++ show pf ++ ")" show (Star pf pa) = "(" ++ show pf ++ "<*>" ++ show pa ++ ")"instance Show (Prod l a env) where show (PS prods) = show prodsinstance Show (Productions l a env) where showEnv n (Ext nts nont) = show n ++ "->" ++ show nont ++ "\n" ++ showEnv (n+1) nts showEnv _ (Empty) = "\n"instance ShowEnv (Env (Productions l) env env') where showEnv :: Int -> a -> Stringclass ShowEnv a where show env = showEnv 0 envinstance ShowEnv (Env (Productions l) env env') => Show (Env (Productions l) env env') where show (Grammar r prods) = show r ++ "\n" ++ show prodsinstance Show (Grammar a) where-- Just for debugging purposes-- Show instances for the Grammars------------------------------------------------------------------------------- l <=> v = (l .=.) <$> (PP [ Sym v ])instance LabelSymbol TTerm v v where -- only to have all the instances l <=> v = (l .=.) <$> (PP [ Sym v ])instance LabelSymbol TNonT v v where l <=> v = (\x -> l .=. (\(Record HNil) -> x)) <$> (PP [ Sym v ])instance LabelSymbol TAttT v (Record HNil -> v) where (<=>) :: label -> Symbol v t env -> ListProd l env (LVPair label v')class LabelSymbol t v v' | t v -> v' where infixr 6 <=>(^=) _ = LSPair(^=) :: nt -> Symbol a t env -> LSPair nt a t envinfixr 6 ^= labelLSPair _ = undefinedlabelLSPair :: LSPair nt a t env -> ntnewtype LSPair nt a t env = LSPair { symLSPair :: (Symbol a t env) } -------------------------------------------------------------------------------adonmA'\[> k  ~ + *  t s Y ! ^ & b H G F z< p=  xTSB0 WBAtrqK0/. r?> (PP f) <*> (PP g) = PP [ Star f' g' | f' <- f, g' <- g ] pure f = PP [ Pure f ]instance Applicative (ListProd l env) where fmap f (PP p) = PP [ Star (Pure f) p' | p' <- p ]instance Functor (ListProd l env) wherefixPrd p = PP [ (Fix . prod) p ]fixPrd :: ListProd (FL a) env a -> ListProd l env avarPrd = PP [ Var ]varPrd :: ListProd (FL a) env atr s = PP [ Sym $ Term s ]tr :: String -> ListProd l env DTerm ntPrd s = id <$> nt sntPrd :: Symbol a TNonT env -> ListProd l env ant s = sym snt :: Symbol a TNonT env -> ListProd l env asym s = PP [ Sym $ s ]sym :: Symbol a t env -> ListProd l env aprod (PP ps) = PS psprod :: ListProd l env a -> Productions l a envnewtype ListProd l env a = PP { unPP :: [ Prod l a env ] }-- APPLICATIVE INTERFACE------------------------op = TermOpcon = TermConidvar = TermVaridchar = TermCharint = TermIntop :: Symbol String TAttT envcon :: Symbol String TAttT envvar :: Symbol String TAttT envchar :: Symbol Char TAttT envint :: Symbol Int TAttT envmatchSym _ _ = NothingmatchSym TermOp TermOp = Just EqmatchSym TermConid TermConid = Just EqmatchSym TermVarid TermVarid = Just EqmatchSym TermInt TermInt = Just EqmatchSym (Term x) (Term y) | x == y = Just EqmatchSym (Nont x) (Nont y) = pairEq $ match x y -> Maybe (Equal (a,t1) (b,t2))matchSym :: Symbol a t1 env -> Symbol b t2 env pairEq Nothing = NothingpairEq (Just Eq) = Just EqpairEq :: Maybe (Equal a b) -> Maybe (Equal (a,t) (b,t))getRefNT (Nont ref) = refgetRefNT :: Symbol a TNonT env -> Ref a env --- TODO: the rest of EnumValToken TermOp :: Symbol String TAttT env TermConid :: Symbol String TAttT env TermVarid :: Symbol String TAttT env TermChar :: Symbol Char TAttT env TermInt :: Symbol Int TAttT env -- attributed terminals Nont :: Ref a env -> Symbol a TNonT env Term :: String -> Symbol DTerm TTerm envdata Symbol a t env wheredata TAttTdata TNonTdata TTermtype DTerm = String-- Fix :: (forall s. Prod a s -> Productions a s) -> Prod a env -- I tried with HOAS, but it is more restrictive Var :: Prod (FL a) a env Fix :: Productions (FL a) a env -> Prod l a env Pure :: a -> Prod l a env Sym :: Symbol a t env -> Prod l a env FlipStar :: Prod l a env -> Prod l (a->b) env -> Prod l b env Star :: Prod l (a->b) env -> Prod l a env -> Prod l b envdata Prod l a env where = PS {unPS :: [Prod l a env]}newtype Productions l a env type GramEnv = Env (Productions TL) (GramEnv env env) = forall env . Grammar (Ref a env) data Grammar a data FL adata TL--------------------------------------------- GRAMMAR REPRESENTATION-------------------------------------------import Unsafe.Coerceimport Control.Applicativeimport Data.HListimport Language.AbstractSyntax.TTTASmodule Language.Grammars.Grammar where