SymbolMapAnalysis.hs revision f6e50d86cf46a89fcda7f875277c2d62bd008732
97a9a944b5887e91042b019776c41d5dd74557aferikabeleModule : $Header$
97a9a944b5887e91042b019776c41d5dd74557aferikabeleDescription : symbol map analysis for the CASL logic.
a945f35eff8b6a88009ce73de6d4c862ce58de3csliveCopyright : (c) Till Mossakowski, C. Maeder and Uni Bremen 2002-2005
a945f35eff8b6a88009ce73de6d4c862ce58de3csliveLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
5a58787efeb02a1c3f06569d019ad81fd2efa06endMaintainer : Christian.Maeder@dfki.de
5a58787efeb02a1c3f06569d019ad81fd2efa06endStability : provisional
5a58787efeb02a1c3f06569d019ad81fd2efa06endPortability : portable
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowenSymbol map analysis for the CASL logic.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen Follows Sect. III:4.1 of the CASL Reference Manual.
5a58787efeb02a1c3f06569d019ad81fd2efa06end ( inducedFromMorphism
5a58787efeb02a1c3f06569d019ad81fd2efa06end , inducedFromToMorphism
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen , inducedFromMorphismExt
3f08db06526d6901aa08c110b5bc7dde6bc39905nd , inducedFromToMorphismExt
5a58787efeb02a1c3f06569d019ad81fd2efa06end , cogeneratedSign
5a58787efeb02a1c3f06569d019ad81fd2efa06end , generatedSign
5a58787efeb02a1c3f06569d019ad81fd2efa06end , finalUnion
3f08db06526d6901aa08c110b5bc7dde6bc39905nd , constMorphExt
e1e8390280254f7f0580d701e583f670643d4f3fnilgunimport CASL.Overload (leqF, leqP)
3b3b7fc78d1f5bfc2769903375050048ff41ff26ndimport qualified Data.Map as Map
5a58787efeb02a1c3f06569d019ad81fd2efa06endimport qualified Data.Set as Set
5a58787efeb02a1c3f06569d019ad81fd2efa06endimport qualified Common.Lib.Rel as Rel
ced7ef1f8c0df1805da0e87dbc5a1b6282910573ndimport Data.List (partition, find)
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7sliveimport Data.Maybe (catMaybes, isJust)
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7sliveinducedFromMorphism :: RawSymbolMap -> sign -> Result morphism
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7sliveHere is Bartek Klin's algorithm that has benn used for CATS.
97a9a944b5887e91042b019776c41d5dd74557aferikabeleOur algorithm deviates from it. The exact details need to be checked.
ffb01336be79c64046b636e59fa8ddca8ec029edsfInducing morphism from raw symbol map and signature
f8396ed8364b56ec8adeaa49cac35a929758a29esliveInput: raw symbol map "Rsm"
5a58787efeb02a1c3f06569d019ad81fd2efa06end signature "Sigma1"
5a58787efeb02a1c3f06569d019ad81fd2efa06endOutput: morphims "Mrph": Sigma1 -> "Sigma2".
5a58787efeb02a1c3f06569d019ad81fd2efa06end//preparation
deeee6bb6fd94c0ba5f3730b58abd9d299c89ccdnd1. let "Ssm" be an empty list of pairs (symbol, raw symbol).
4db28ee269aa06f7c6232e11cd01f58c3349af23noodl2. for each pair "Rsym1,Rsym2" in Rsm do:
117c1f888a14e73cdd821dc6c23eb0411144a41cnd 2.1. if there is no symbol in Sigma1 matching Rsym1, return error.
117c1f888a14e73cdd821dc6c23eb0411144a41cnd 2.2. for each symbol "Sym" from Sigma1 matching Rsym1
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf 2.2.1. add a pair "Sym,Rsym2" to Ssm.
117c1f888a14e73cdd821dc6c23eb0411144a41cnd//computing the "sort part" of the morphism
ffb01336be79c64046b636e59fa8ddca8ec029edsf3. let Sigma2 be an empty signature.
117c1f888a14e73cdd821dc6c23eb0411144a41cnd4. let Mrph be an empty "morphism" from Sigma1 to Sigma2.
117c1f888a14e73cdd821dc6c23eb0411144a41cnd5. for each pair "Sym,Rsym2" in Ssm such that Sym is a sort symbol
117c1f888a14e73cdd821dc6c23eb0411144a41cnd 5.1. if Rsym2 is not a sort raw symbol, return error.
2bc7f1cf720973a67f8ff7a8d523e40569ae5b6cnd 5.2. if in Mrph there is a mapping of sort in Sym to sort with
117c1f888a14e73cdd821dc6c23eb0411144a41cnd name other than that in Rsym2, return error.
117c1f888a14e73cdd821dc6c23eb0411144a41cnd 5.3. if in Mrph there is no mappinh of sort in Sym
117c1f888a14e73cdd821dc6c23eb0411144a41cnd 5.3.1. add sort from Rsym2 to Sigma2
117c1f888a14e73cdd821dc6c23eb0411144a41cnd 5.3.2. add mapping from sort(Sym) to sort(Rsym2) to Mrph.
117c1f888a14e73cdd821dc6c23eb0411144a41cnd6. for each sort symbol "S" in Sigma1
117c1f888a14e73cdd821dc6c23eb0411144a41cnd 6.1. if S is not mapped by Mrph,
87ffb6e33f3cbef3b9bb406cc2d27039fa336eaatrawick 6.1.1. add sort S to Sigma2
4db28ee269aa06f7c6232e11cd01f58c3349af23noodl 6.1.2. add mapping from S to S to Mrph.
5a58787efeb02a1c3f06569d019ad81fd2efa06end//computing the "function/predicate part" of the morphism
5a58787efeb02a1c3f06569d019ad81fd2efa06end7. for each pair "Sym,Rsym2" in Ssm such that Sym is a function/predicate
5a58787efeb02a1c3f06569d019ad81fd2efa06end 7.1. let "F" be name contained in Sym, let "Fprof" be the profile.
5a58787efeb02a1c3f06569d019ad81fd2efa06end 7.2. let "Fprof1" be the value of Fprof via Mrph
5a58787efeb02a1c3f06569d019ad81fd2efa06end (it can be computed, as we already have the "sort" part of
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh 7.3. if Rsym2 is not of appriopriate type, return error, otherwise
5a58787efeb02a1c3f06569d019ad81fd2efa06end let "F2" be the name of the symbol.
5a58787efeb02a1c3f06569d019ad81fd2efa06end 7.4. if Rsym2 enforces the profile of the symbol (i.e., it is not
5a58787efeb02a1c3f06569d019ad81fd2efa06end an implicit symbol), compare the profile to Fprof1. If it is
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf not equal, return error.
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick 7.5. if in Mrph there is a mapping of F1 with profile Fprof to
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick some name different than F2, return error.
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick 7.6. add an operation/predicate with name F2 and profile Fprof1 to
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick Sigma2. If it is a partial function and if in Sigma2 there
06ba4a61654b3763ad65f52283832ebf058fdf1cslive exists a total function with the same name and profile, do not
654d8eb036bedc99e90e11910ee02d3421417697rbowen add it. Otherwise if it is a total function and if in Sigma2
06ba4a61654b3763ad65f52283832ebf058fdf1cslive there exists a partial function with the same name and profile,
06ba4a61654b3763ad65f52283832ebf058fdf1cslive add the total function removing the partial one.
06ba4a61654b3763ad65f52283832ebf058fdf1cslive 7.7. add to Mrph a mapping from operation/predicate of name F1 and
b1b0d8ff12f69d873f28a9bf79dfba6b67b45a4bigalic profile Fprof to name F2.
97a9a944b5887e91042b019776c41d5dd74557aferikabele8. for each operation/predicate symbol "F" with profile "Fprof" in Sigma1
654d8eb036bedc99e90e11910ee02d3421417697rbowen that is not mapped by Mrph,
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive 8.1. as in 7.2
97a9a944b5887e91042b019776c41d5dd74557aferikabele 8.2. as in 7.6, replacing F2 with F1.
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive 8.3. as in 7.7, replacing F2 with F1.
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive9. for each sort relation "S1,S2" in Sigma1,
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive 9.1. compute S3=(S1 via Mrph) and S4=(S2 via Mrph)
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive 9.2. add sort relation "S3,S4" in Sigma2.
ffb01336be79c64046b636e59fa8ddca8ec029edsf10. Compute transitive closure of subsorting relation in Sigma2.
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawicktype InducedMorphism e m = RawSymbolMap -> e -> Result m
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawickconstMorphExt :: m -> InducedMorphism e m
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawickconstMorphExt m _ _ = return m
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsfinducedFromMorphism :: (Pretty e, Show f) => m -> RawSymbolMap -> Sign f e
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick -> Result (Morphism f e m)
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsfinducedFromMorphism =
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf inducedFromMorphismExt (\ _ _ _ _ -> extendedInfo) . constMorphExt
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawickinducedFromMorphismExt :: (Pretty e, Show f) => InducedSign f e m e
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf -> InducedMorphism e m
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick -> RawSymbolMap -> Sign f e -> Result (Morphism f e m)
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawickinducedFromMorphismExt extInd extEm rmap sigma = do
77c77cf89621f21c8e2bbad63058b5eaa5f88d4ajim -- ??? Missing: check preservation of overloading relation
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick -- compute the sort map (as a Map)
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf sort_Map <- Set.fold (\ s m -> do
ced7ef1f8c0df1805da0e87dbc5a1b6282910573nd s' <- sortFun rmap s
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf return $ if s' == s then m1 else Map.insert s s' m1)
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf (return Map.empty) (sortSet sigma)
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf -- compute the op map (as a Map)
ced7ef1f8c0df1805da0e87dbc5a1b6282910573nd op_Map <- Map.foldWithKey (opFun sigma rmap sort_Map)
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick (return Map.empty) (opMap sigma)
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick -- compute the pred map (as a Map)
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick pred_Map <- Map.foldWithKey (predFun sigma rmap sort_Map)
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf (return Map.empty) (predMap sigma)
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf em <- extEm rmap $ extendedInfo sigma
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick -- return assembled morphism
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick return (embedMorphism em sigma $ closeSortRel
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick $ inducedSignAux extInd sort_Map op_Map pred_Map em sigma)
ffb01336be79c64046b636e59fa8ddca8ec029edsf { sort_map = sort_Map
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick , op_map = op_Map
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive , pred_map = pred_Map }
97a9a944b5887e91042b019776c41d5dd74557aferikabele -- the sorts of the source signature
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive -- sortFun is the sort map as a Haskell function
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalicsortFun :: RawSymbolMap -> Id -> Result Id
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalicsortFun rmap s =
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic -- rsys contains the raw symbols to which s is mapped to
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic if Set.null rsys then return s -- use default = identity mapping
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic return $ rawSymName $ Set.findMin rsys -- take the unique rsy
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic else plain_error s -- ambiguity! generate an error
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic ("sort " ++ showId s
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic " is mapped ambiguously: " ++ showDoc rsys "")
06ba4a61654b3763ad65f52283832ebf058fdf1cslive $ getRange rsys
e8d485701957d5c6de870111c112e168a894d49and -- get all raw symbols to which s is mapped to
654d8eb036bedc99e90e11910ee02d3421417697rbowen rsys = Set.fromList $ catMaybes $ map (flip Map.lookup rmap)
654d8eb036bedc99e90e11910ee02d3421417697rbowen [ ASymbol $ idToSortSymbol s
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive , AKindedSymb Implicit s ]
9bcfc3697a91b5215893a7d0206865b13fc72148nd {- to a Op_map, add everything resulting from mapping (id, ots)
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive according to rmap -}
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7sliveopFun :: Sign f e -> RawSymbolMap -> Sort_map -> Id -> Set.Set OpType
06ba4a61654b3763ad65f52283832ebf058fdf1cslive -> Result Op_map -> Result Op_map
4a31db3c3a0202003c1b9f87affa7cc143e120e5sfopFun src rmap sort_Map ide ots m =
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive -- first consider all directly mapped profiles
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive let otls = Rel.partSet (leqF src) ots
709e3a21ba73b8433462959cd56c773454b34441trawick m1 = foldr (directOpMap rmap sort_Map ide) m otls
709e3a21ba73b8433462959cd56c773454b34441trawick -- now try the remaining ones with (un)kinded raw symbol
709e3a21ba73b8433462959cd56c773454b34441trawick in case (Map.lookup (AKindedSymb Ops_kind ide) rmap,
709e3a21ba73b8433462959cd56c773454b34441trawick Map.lookup (AKindedSymb Implicit ide) rmap) of
709e3a21ba73b8433462959cd56c773454b34441trawick (Just rsy1, Just rsy2) -> let
709e3a21ba73b8433462959cd56c773454b34441trawick m2 = Set.fold (insertmapOpSym sort_Map ide rsy1) m1 ots
709e3a21ba73b8433462959cd56c773454b34441trawick in Set.fold (insertmapOpSym sort_Map ide rsy2) m2 ots
5a58787efeb02a1c3f06569d019ad81fd2efa06end (Just rsy, Nothing) ->
5a58787efeb02a1c3f06569d019ad81fd2efa06end Set.fold (insertmapOpSym sort_Map ide rsy) m1 ots
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd (Nothing, Just rsy) ->
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd Set.fold (insertmapOpSym sort_Map ide rsy) m1 ots
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd -- Anything not mapped explicitly is left unchanged
e1e8390280254f7f0580d701e583f670643d4f3fnilgun (Nothing, Nothing) -> m1
727872d18412fc021f03969b8641810d8896820bhumbedooh -- try to map an operation symbol directly
0d0ba3a410038e179b695446bb149cce6264e0abnd -- collect all opTypes that cannot be mapped directly
727872d18412fc021f03969b8641810d8896820bhumbedoohdirectOpMap :: RawSymbolMap -> Sort_map -> Id -> Set.Set OpType
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh -> Result Op_map -> Result Op_map
0d0ba3a410038e179b695446bb149cce6264e0abnddirectOpMap rmap sort_Map ide' ots m' =
727872d18412fc021f03969b8641810d8896820bhumbedooh rl = map (lookupOpSymbol rmap ide') ol
0d0ba3a410038e179b695446bb149cce6264e0abnd in case catMaybes rl of
0d0ba3a410038e179b695446bb149cce6264e0abnd rsy : _ -> foldr (\ (ot, mrsy) m ->
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh insertmapOpSym sort_Map ide'
0d0ba3a410038e179b695446bb149cce6264e0abnd (case mrsy of
0d0ba3a410038e179b695446bb149cce6264e0abnd Nothing -> AKindedSymb Implicit $ rawSymName rsy
0d0ba3a410038e179b695446bb149cce6264e0abnd Just rsy2 -> rsy2) ot m) m' $ zip ol rl
0d0ba3a410038e179b695446bb149cce6264e0abndlookupOpSymbol :: RawSymbolMap -> Id -> OpType -> Maybe RawSymbol
0d0ba3a410038e179b695446bb149cce6264e0abndlookupOpSymbol rmap ide' ot = let mkS = idToOpSymbol ide' in
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh case Map.lookup (ASymbol (mkS $ mkPartial ot)) rmap of
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen (ASymbol (mkS $ mkTotal ot)) rmap
0d0ba3a410038e179b695446bb149cce6264e0abnd res -> res
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd -- map op symbol (ide, ot) to raw symbol rsy
7fec19672a491661b2fe4b29f685bc7f4efa64d4ndmappedOpSym :: Sort_map -> Id -> OpType -> RawSymbol -> Result (Id, OpKind)
5a58787efeb02a1c3f06569d019ad81fd2efa06endmappedOpSym sort_Map ide ot rsy =
m2 = Map.insert (ide, mkPartial ot) (ide', kind') m1
case Map.lookup (ide, mkPartial ot) m1 of
predFun :: Sign f e -> RawSymbolMap -> Sort_map -> Id -> Set.Set PredType
let ptls = Rel.partSet (leqP src) pts
in case (Map.lookup (AKindedSymb Preds_kind ide) rmap,
Map.lookup (AKindedSymb Implicit ide) rmap) of
m2 = Set.fold (insertmapPredSym sort_Map ide rsy1) m1 pts
in Set.fold (insertmapPredSym sort_Map ide rsy2) m2 pts
Set.fold (insertmapPredSym sort_Map ide rsy) m1 pts
Set.fold (insertmapPredSym sort_Map ide rsy) m1 pts
directPredMap :: RawSymbolMap -> Sort_map -> Id -> Set.Set PredType
let pl = Set.toList pts
rl = map (\ pt -> Map.lookup (ASymbol $ idToPredSymbol ide pt) rmap) pl
m2 = Map.insert (ide, pt) ide' m1
case Map.lookup (ide, pt) m1 of
The algorithm has additionally to take care of default values (i.e.
1. compute the morphism mor1 induced by rmap and sigma1 (i.e. the renaming)
pos = concatMapRange getRange $ Map.keys rmap
combs = pairs (map ASymbol $ Set.toList ss1)
$ map ASymbol $ Set.toList sy2
$ concatMapRange getRange $ Map.keys rmap
if not (sys `Set.isSubsetOf` symset) -- 2.
, opMap = Map.empty
, predMap = Map.empty }) sys -- 4.
{ sortRel = sortRel sigma `Rel.restrict` sortSet sigma1
, emptySortSet = Set.intersection (sortSet sigma1) $ emptySortSet sigma }
sigma1 {sortSet = Set.insert (symName sy) $ sortSet sigma1}
sigma1 { sortSet = foldr Set.insert (sortSet sigma1)
, opMap = Rel.setInsert (symName sy) ot $ opMap sigma1 }
sigma1 { sortSet = foldr Set.insert (sortSet sigma1) $ predArgs pt
, predMap = Rel.setInsert (symName sy) pt $ predMap sigma1 }
3.1.1.2. For each function/predicate symbol in
if Set.isSubsetOf symset symset0 -- 2.
symset1 = Set.fold revealSym' symset0 symset -- 3.
Set.filter (not . profileContains (symName sy) . symbType)
$ Set.delete sy symset1'
Set.delete sy symset1'
Set.delete sy symset1'
mkp = Map.map makePartial
let extP = Map.map (map $ \ s -> (s, [], False))
d1 = Map.differenceWith (listOfSetDiff True) o1 o3
e1 = Map.differenceWith (listOfSetDiff True) p1 p3
let sIn = Set.isSubsetOf s
Nothing -> error "CASL.finalUnion.listOfSetDiff1"
(s2, filter (flip Set.isSubsetOf s2) l1, b) : l, r)
_ -> error "CASL.finalUnion.listOfSetDiff2")