Sign.hs revision 27bdba808fa9637ef10b739233fde57c77245f5d
bbae6e6ca0de7f2ffbb44d2c8da179f2b717237fChristian MaederModule : $Header$
81d182b21020b815887e9057959228546cf61b6bChristian MaederDescription : Definition of signatures for the Edinburgh
62ecb1e7f8fd9573eea8369657de12c7bf9f4f25Christian Maeder Logical Framework
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederCopyright : (c) Kristina Sojakova, DFKI Bremen 2009
bbae6e6ca0de7f2ffbb44d2c8da179f2b717237fChristian MaederLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
bbae6e6ca0de7f2ffbb44d2c8da179f2b717237fChristian MaederMaintainer : k.sojakova@jacobs-university.de
fbb66ee3e170624835b99f7aa91980753cb5b472Christian MaederStability : experimental
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederPortability : portable
0ea85310d2beb8aa03cac481ad2a6564e6b8ddbcChristian Maeder , Symbol (..)
bbae6e6ca0de7f2ffbb44d2c8da179f2b717237fChristian Maeder , getDeclaredSyms
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder , getDefinedSyms
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian Maeder , getLocalSyms
bbae6e6ca0de7f2ffbb44d2c8da179f2b717237fChristian Maeder , isDeclaredSym
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian Maeder , isDefinedSym
e774ab5733a1d673b123b0e63b14dd533e6fd4fcChristian Maeder , getSymValue
6e39bfd041946fce4982ac89834be73fd1bfb39aChristian Maeder , printSymbol
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maederimport qualified Data.List as List
b645cf3dc1e449038ed291bbd11fcc6e02b2fc7fChristian Maederimport qualified Data.Set as Set
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maederimport qualified Data.Map as Map
42c01284bba8d7c8d995c8dfb96ace57d28ed1bcTill Mossakowskitype Var = String
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maedertype NAME = String
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maedertype MODULE = String
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maedertype BASE = String
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maederdata Symbol = Symbol
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder { symBase :: BASE
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder , symModule :: MODULE
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder , symName :: NAME
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder } deriving (Ord, Show)
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maederdata EXP = Type
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder | Const Symbol
0ea85310d2beb8aa03cac481ad2a6564e6b8ddbcChristian Maeder | Appl EXP [EXP]
0ea85310d2beb8aa03cac481ad2a6564e6b8ddbcChristian Maeder | Func [EXP] EXP
ccf3de3d66b521a260e5c22d335c64a48e3f0195Christian Maeder | Pi [DECL] EXP
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder | Lamb [DECL] EXP
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder deriving (Ord, Show)
c44c23429c72f3a709e22a18f2ed6f05fc8cc765Christian Maedertype DECL = (Var,EXP)
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maederdata DEF = Def
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maeder { getSym :: Symbol
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maeder , getType :: EXP
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maeder , getValue :: Maybe EXP
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder } deriving (Eq, Ord, Show)
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maederdata Sign = Sign
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maeder { sigBase :: BASE
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maeder , sigModule :: MODULE
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maeder , getDefs :: [DEF]
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder deriving (Show, Ord)
0ea85310d2beb8aa03cac481ad2a6564e6b8ddbcChristian MaederemptySig :: Sign
0ea85310d2beb8aa03cac481ad2a6564e6b8ddbcChristian MaederemptySig = Sign "" "" []
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederaddDef :: DEF -> Sign -> Sign
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederaddDef d (Sign b m ds) = Sign b m $ ds ++ [d]
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder-- get the set of all symbols
715ffaf874309df081d1e1cd8e05073fc1227729Christian MaedergetAllSyms :: Sign -> Set.Set Symbol
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaedergetAllSyms (Sign _ _ ds) = Set.fromList $ map getSym ds
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder-- checks if the symbol is defined or declared in the signature
0ea85310d2beb8aa03cac481ad2a6564e6b8ddbcChristian MaederisConstant :: Symbol -> Sign -> Bool
62ecb1e7f8fd9573eea8369657de12c7bf9f4f25Christian MaederisConstant s sig = Set.member s $ getAllSyms sig
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder-- get the set of declared symbols
f454c20b6c126bea7d31d400cc8824b9ee8cc6eaChristian MaedergetDeclaredSyms :: Sign -> Set.Set Symbol
715ffaf874309df081d1e1cd8e05073fc1227729Christian MaedergetDeclaredSyms (Sign _ _ ds) =
0ea85310d2beb8aa03cac481ad2a6564e6b8ddbcChristian Maeder Set.fromList $ map getSym $ filter (\ d -> isNothing $ getValue d) ds
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder-- checks if the symbol is declared in the signature
36c6cc568751e4235502cfee00ba7b597dae78dcChristian MaederisDeclaredSym :: Symbol -> Sign -> Bool
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederisDeclaredSym s sig = Set.member s $ getDeclaredSyms sig
836e72a3c413366ba9801726f3b249c7791cb9caChristian Maeder-- get the set of declared symbols
836e72a3c413366ba9801726f3b249c7791cb9caChristian MaedergetDefinedSyms :: Sign -> Set.Set Symbol
89dc77946055c0e4cb4671c4a74c3dcd55ed41a1Christian MaedergetDefinedSyms (Sign _ _ ds) =
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder Set.fromList $ map getSym $ filter (\ d -> isJust $ getValue d) ds
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder-- checks if the symbol is defined in the signature
149e43c4a2705a86a0e5fa301ba849fdf19db32eChristian MaederisDefinedSym :: Symbol -> Sign -> Bool
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederisDefinedSym s sig = Set.member s $ getDefinedSyms sig
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian Maeder-- get the set of symbols not included from other signatures
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaedergetLocalSyms :: Sign -> Set.Set Symbol
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian MaedergetLocalSyms sig = Set.filter (\ s -> isLocalSym s sig) $ getAllSyms sig
bbae6e6ca0de7f2ffbb44d2c8da179f2b717237fChristian Maeder-- checks if the symbol is local to the signature
89dc77946055c0e4cb4671c4a74c3dcd55ed41a1Christian MaederisLocalSym :: Symbol -> Sign -> Bool
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederisLocalSym s sig =
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder and [sigBase sig == symBase s, sigModule sig == symModule s]
d48085f765fca838c1d972d2123601997174583dChristian Maeder-- returns the type/kind for the given symbol
797f811e57952d59e73b8cd03b667eef276db972Christian MaedergetSymType :: Symbol -> Sign -> Maybe EXP
89dc77946055c0e4cb4671c4a74c3dcd55ed41a1Christian MaedergetSymType sym sig =
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder let res = List.find (\ d -> getSym d == sym) $ getDefs sig
3daa82a175c7cfabf22455aa77c4beda327404e4Christian Maeder in case res of
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder Nothing -> Nothing
3daa82a175c7cfabf22455aa77c4beda327404e4Christian Maeder Just (Def _ t _) -> Just t
3daa82a175c7cfabf22455aa77c4beda327404e4Christian Maeder-- returns the value for the given symbol, if it exists
3daa82a175c7cfabf22455aa77c4beda327404e4Christian MaedergetSymValue :: Symbol -> Sign -> Maybe EXP
36c6cc568751e4235502cfee00ba7b597dae78dcChristian MaedergetSymValue sym sig =
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder let res = List.find (\ d -> getSym d == sym) $ getDefs sig
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian Maeder in case res of
bbae6e6ca0de7f2ffbb44d2c8da179f2b717237fChristian Maeder Nothing -> Nothing
89dc77946055c0e4cb4671c4a74c3dcd55ed41a1Christian Maeder Just (Def _ _ v) -> v
149e43c4a2705a86a0e5fa301ba849fdf19db32eChristian Maeder-- pretty printing
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maederinstance Pretty Sign where
1738d16957389457347bee85075d3d33d002158fChristian Maeder pretty = printSig
6e39bfd041946fce4982ac89834be73fd1bfb39aChristian MaederprintSig :: Sign -> Doc
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederprintSig sig = vcat $ map (printDef sig) $ getDefs sig
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederprintDef :: Sign -> DEF -> Doc
304c84f22dd78f7979efd81b8fc38c8d2197ed39Christian MaederprintDef sig (Def s t v) =
304c84f22dd78f7979efd81b8fc38c8d2197ed39Christian Maeder [ printSymbol sig s
304c84f22dd78f7979efd81b8fc38c8d2197ed39Christian Maeder , text "::" <+> printExp sig t
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder Nothing -> empty
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder Just val -> text "=" <+> printExp sig val
e215ca0377dc79c9bcfb105157ecc4b958bec67bChristian MaederprintSymbol :: Sign -> Symbol -> Doc
e215ca0377dc79c9bcfb105157ecc4b958bec67bChristian MaederprintSymbol sig s =
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder if (isLocalSym s sig)
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian Maeder then text $ symName s
2bf209888545860dc77b9c3f2198d00eeab30d20Christian Maeder else text (symModule s) <> text sigDelimS <> text (symName s)
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian MaederprintExp :: Sign -> EXP -> Doc
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian MaederprintExp _ Type = text "type"
2bf209888545860dc77b9c3f2198d00eeab30d20Christian MaederprintExp sig (Const s) = printSymbol sig s
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian MaederprintExp _ (Var n) = text n
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian MaederprintExp sig (Appl e es) =
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder let f = printExpWithPrec sig (precAppl + 1) e
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder as = map (printExpWithPrec sig precAppl) es
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder in hsep (f:as)
getNewName :: Var -> Set.Set Var -> Var
getNewNameH :: Var -> Set.Set Var -> Var -> Int -> Var
if (Set.notMember var names)
getFreeVars :: EXP -> Set.Set Var
getFreeVarsH :: EXP -> Set.Set Var
getFreeVarsH Type = Set.empty
getFreeVarsH (Const _) = Set.empty
getFreeVarsH (Var x) = Set.singleton x
Set.union (getFreeVarsH f) (getFreeVarsH a)
Set.union (getFreeVarsH t) (getFreeVarsH v)
getFreeVarsH _ = Set.empty
renameH m _ (Var n) = Var $ Map.findWithDefault n n m
let vars = Set.delete n1 $ getFreeVars s1
vars1 = Set.delete n2 $ getFreeVars s2
let vars = Set.delete n1 $ getFreeVars s1
vars1 = Set.delete n2 $ getFreeVars s2