Morphism.hs revision c7e03d0708369f944b6f235057b39142a21599f2
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens ElknerModule : $Header$
c7e03d0708369f944b6f235057b39142a21599f2Mihai CodescuDescription : Symbols and signature morphisms for the CASL logic
c7e03d0708369f944b6f235057b39142a21599f2Mihai CodescuCopyright : (c) Christian Maeder, Till Mossakowski and Uni Bremen 2002-2004
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu ProdescuLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
c7e03d0708369f944b6f235057b39142a21599f2Mihai CodescuMaintainer : Christian.Maeder@dfki.de
402ffc909b645ddb2d5c76343bf74a6cb33c6205Christian MaederStability : provisional
c7e03d0708369f944b6f235057b39142a21599f2Mihai CodescuPortability : portable
c7e03d0708369f944b6f235057b39142a21599f2Mihai CodescuSymbols and signature morphisms for the CASL logic
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescuissue warning for symbols lists like __ * __, __ + __: Elem * Elem -> Elem
c7e03d0708369f944b6f235057b39142a21599f2Mihai Codescuthe qualification only applies to __+__ !
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroederpossibly reuse SYMB_KIND for Kind
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowskiimport qualified Data.Map as Map
c7e03d0708369f944b6f235057b39142a21599f2Mihai Codescuimport qualified Data.Set as Set
5418aa59492005c2ca40436ab84c4029cd2922a5Christian Maederimport qualified Common.Lib.Rel as Rel
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescutype SymbolSet = Set.Set Symbol
7b5ba6a2f3990cf508f90e55e1d59068e6aaa6a3Mihai Codescutype SymbolMap = Map.Map Symbol Symbol
7b5ba6a2f3990cf508f90e55e1d59068e6aaa6a3Mihai Codescudata RawSymbol = ASymbol Symbol | AnID Id | AKindedId Kind Id
7b5ba6a2f3990cf508f90e55e1d59068e6aaa6a3Mihai Codescu deriving (Show, Eq, Ord)
7b5ba6a2f3990cf508f90e55e1d59068e6aaa6a3Mihai Codescuinstance PosItem RawSymbol where
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu getRange rs =
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu ASymbol s -> getRange s
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu AnID i -> getRange i
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder AKindedId _ i -> getRange i
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescutype RawSymbolSet = Set.Set RawSymbol
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescutype RawSymbolMap = Map.Map RawSymbol RawSymbol
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescudata Kind = SortKind | FunKind | PredKind
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu deriving (Show, Eq, Ord)
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescutype Sort_map = Map.Map SORT SORT
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu-- always use the partial profile as key!
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescutype Fun_map = Map.Map (Id,OpType) (Id, FunKind)
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescutype Pred_map = Map.Map (Id,PredType) Id
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescudata Morphism f e m = Morphism
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu { msource :: Sign f e
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu , mtarget :: Sign f e
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu , sort_map :: Sort_map
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu , fun_map :: Fun_map
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu , pred_map :: Pred_map
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu , extended_map :: m
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu } deriving (Eq, Show)
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapSort :: Sort_map -> SORT -> SORT
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapSort sorts s = Map.findWithDefault s s sorts
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroedermapOpType :: Sort_map -> OpType -> OpType
272a5a82588ab4115dac5eb81d43d74929e99ad7Till MossakowskimapOpType sorts t = if Map.null sorts then t else
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu t { opArgs = map (mapSort sorts) $ opArgs t
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu , opRes = mapSort sorts $ opRes t }
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapOpTypeK :: Sort_map -> FunKind -> OpType -> OpType
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapOpTypeK sorts k t = makeTotal k $ mapOpType sorts t
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumakeTotal :: FunKind -> OpType -> OpType
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumakeTotal Total t = t { opKind = Total }
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumakeTotal _ t = t
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapOpSym :: Sort_map -> Fun_map -> (Id, OpType) -> (Id, OpType)
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapOpSym sMap fMap (i, ot) =
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu let mot = mapOpType sMap ot in
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu case Map.lookup (i, ot {opKind = Partial} ) fMap of
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Nothing -> (i, mot)
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu Just (j, k) -> (j, makeTotal k mot)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder-- | Check if two OpTypes are equal modulo totality or partiality
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescucompatibleOpTypes :: OpType -> OpType -> Bool
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroedercompatibleOpTypes ot1 ot2 = opArgs ot1 == opArgs ot2 && opRes ot1 == opRes ot2
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapPredType :: Sort_map -> PredType -> PredType
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroedermapPredType sorts t = if Map.null sorts then t else
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu t { predArgs = map (mapSort sorts) $ predArgs t }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroedermapPredSym :: Sort_map -> Pred_map -> (Id, PredType) -> (Id, PredType)
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescumapPredSym sMap fMap (i, pt) =
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder (Map.findWithDefault i (i, pt) fMap, mapPredType sMap pt)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroedertype Ext f e m = Sign f e -> Sign f e -> m
142cf75c6377873ccc72450202fe7f0df94a6dffMihai CodescuembedMorphism :: Ext f e m -> Sign f e -> Sign f e -> Morphism f e m
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuembedMorphism extEm a b = Morphism
96d8cf9817eeb0d26cba09ca192fc5a33e27bc09mcodescu { msource = a
142cf75c6377873ccc72450202fe7f0df94a6dffMihai Codescu , mtarget = b
80875f917d741946a39d0ec0b5721e46ba609823Till Mossakowski , extended_map = extEm a b
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuidToSortSymbol :: Id -> Symbol
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuidToSortSymbol idt = Symbol idt SortAsItemType
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuidToOpSymbol :: Id -> OpType -> Symbol
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroederidToOpSymbol idt typ = Symbol idt (OpAsItemType typ)
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuidToPredSymbol :: Id -> PredType -> Symbol
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuidToPredSymbol idt typ = Symbol idt (PredAsItemType typ)
803425dfe6a5da41b9cea480788980fa104545adMihai CodescusymbTypeToKind :: SymbType -> Kind
803425dfe6a5da41b9cea480788980fa104545adMihai CodescusymbTypeToKind (OpAsItemType _) = FunKind
803425dfe6a5da41b9cea480788980fa104545adMihai CodescusymbTypeToKind (PredAsItemType _) = PredKind
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroedersymbTypeToKind SortAsItemType = SortKind
b7f625188b6bf3d708b2a1b64a6cea7880708adbChristian MaedersymbolToRaw :: Symbol -> RawSymbol
803425dfe6a5da41b9cea480788980fa104545adMihai CodescusymbolToRaw sym = ASymbol sym
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuidToRaw :: Id -> RawSymbol
803425dfe6a5da41b9cea480788980fa104545adMihai CodescuidToRaw x = AnID x
803425dfe6a5da41b9cea480788980fa104545adMihai CodescurawSymName :: RawSymbol -> Id
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von SchroederrawSymName (ASymbol sym) = symName sym
96d8cf9817eeb0d26cba09ca192fc5a33e27bc09mcodescurawSymName (AnID i) = i
96d8cf9817eeb0d26cba09ca192fc5a33e27bc09mcodescurawSymName (AKindedId _ i) = i
803425dfe6a5da41b9cea480788980fa104545adMihai CodescusymOf :: Sign f e -> SymbolSet
96d8cf9817eeb0d26cba09ca192fc5a33e27bc09mcodescusymOf sigma =
b7f625188b6bf3d708b2a1b64a6cea7880708adbChristian Maeder let sorts = Set.map idToSortSymbol $ sortSet sigma
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder concatMap (\ (i, ts) -> map ( \ t -> idToOpSymbol i t)
803425dfe6a5da41b9cea480788980fa104545adMihai Codescu concatMap (\ (i, ts) -> map ( \ t -> idToPredSymbol i t)
803425dfe6a5da41b9cea480788980fa104545adMihai Codescu in Set.unions [sorts, ops, preds]
foldl insertRsys (return Map.empty) (concat ls)
case Map.lookup rsy1 m1 of
Nothing -> return $ Map.insert rsy1 rsy2 m1
case Map.lookup (Symbol {symName = s, symbType = SortAsItemType}) smap
Map.insert s t m
mapFun i ots m = Set.fold (insFun i) m ots
case Map.lookup (Symbol {symName = i, symbType = OpAsItemType ot}) smap
else Map.insert (i, ot {opKind = Partial}) (j, k) m
mapPred i pts m = Set.fold (insPred i) m pts
case Map.lookup (Symbol {symName = i, symbType = PredAsItemType pt}) smap
PredAsItemType _ -> if i == j then m else Map.insert (i, pt) j m
Map.empty $ sortSet src
opSymMap = Map.foldWithKey
( \ i s m -> Set.fold
( \ t -> Map.insert (idToOpSymbol i t)
Map.empty $ opMap src
predSymMap = Map.foldWithKey
( \ i s m -> Set.fold
( \ t -> Map.insert (idToPredSymbol i t)
Map.empty $ predMap src
foldr Map.union sortSymMap [opSymMap,predSymMap]
sMap = if Map.null sMap2 then sMap1 else
Set.fold ( \ i ->
if i == j then id else Map.insert i j)
Map.empty $ sortSet src
fun_map = if Map.null fMap2 then fMap1 else
Map.foldWithKey ( \ i t m ->
Set.fold ( \ ot ->
Map.insert (i, ot {opKind = Partial }) (ni, k)) m t)
Map.empty $ opMap src,
pred_map = if Map.null pMap2 then pMap1 else
Map.foldWithKey ( \ i t m ->
Set.fold ( \ pt ->
if i == ni then id else Map.insert (i, pt) ni) m t)
Map.empty $ predMap src,
Map.foldWithKey (\s sset b -> b && legalSort s && all legalSort
(Set.toList sset))
True (Rel.toMap (sortRel sigma))
legalSort s = Set.member s sorts
msorts = Set.map (mapSort smap) $ sortSet s1
mops = Map.foldWithKey ( \ i ->
flip $ Set.fold ( \ ot ->
mpreds = Map.foldWithKey ( \ i ->
flip $ Set.fold ( \ pt ->
&& Set.isSubsetOf msorts (sortSet s2)
uo1 = foldr delOp (opMap s1) $ Map.keys omap1
uo2 = foldr delOp (opMap s2) $ Map.keys omap2
delOp (n, ot) m = diffMapSet m $ Map.singleton n $
Set.fromList [ot {opKind = Partial}, ot {opKind =Total}]
up1 = foldr delPred (predMap s1) $ Map.keys pmap1
up2 = foldr delPred (predMap s2) $ Map.keys pmap2
(sds, smap) = foldr ( \ (i, j) (ds, m) -> case Map.lookup i m of
Nothing -> (ds, Map.insert i j m)
(Map.toList smap2 ++ map (\ a -> (a, a))
case Map.lookup isc m of
Nothing -> (ds, Map.insert isc jsc m)
else (ds, Map.insert isc (j, Total) m) else
(sds, omap1) (Map.toList omap2 ++ concatMap
case Map.lookup isc m of
Nothing -> (ds, Map.insert isc j m)
(Map.toList pmap2 ++ concatMap ( \ (a, s) -> map
sort_map = Map.filterWithKey (/=) smap,
fun_map = Map.filterWithKey
(\ (i, ot) (j, k) -> i /= j || k == Total && Set.member ot
pred_map = Map.filterWithKey (\ (i, _) j -> i /= j) pmap,
src = Map.keys sm
where src = Map.keys symmap
pretty (Map.filterWithKey (/=) $ morphismToSymbMap mor)