Description : morphisms implementation
Copyright : (c) Christian Maeder and Uni Bremen 2002-2006
Maintainer : Christian.Maeder@dfki.de
mapping entities of morphisms
disjointKeys :: (Ord a, Pretty a, Monad m) =>
Map.Map a b ->
Map.Map a c
(sep [ text "overlapping identifiers for types and classes:"
-- | map a kind along an identifier map
mapKindI :: IdMap -> Kind -> Kind
-- | map a kind along a signature morphism (variance is preserved)
mapKinds :: Morphism -> Kind -> Kind
mapKinds = mapKindI . classIdMap
-- | only rename the kinds in a type
mapKindsOfType :: IdMap -> TypeMap -> IdMap -> Type -> Type
mapKindsOfType jm tm im = foldType mapTypeRec
{ foldTypeAbs = \ _ -> TypeAbs . mapTypeArg jm tm im
, foldKindedType = \ _ t -> KindedType t .
Set.map (mapKindI jm) }
-- | map type, expand it, and also adjust the kinds
mapTypeE :: IdMap -> TypeMap -> IdMap -> Type -> Type
mapKindsOfType jm tm im . expandAliases tm . mapType im
-- | map a kind along a signature morphism (variance is preserved)
mapVarKind :: IdMap -> TypeMap -> IdMap -> VarKind -> VarKind
mapVarKind jm tm im vk = case vk of
VarKind k -> VarKind $ mapKindI jm k
Downset ty -> Downset $ mapTypeE jm tm im ty
mapTypeArg :: IdMap -> TypeMap -> IdMap -> TypeArg -> TypeArg
mapTypeArg jm tm im (TypeArg i v vk rk c s r) =
TypeArg i v (mapVarKind jm tm im vk) rk c s r
mapTypeScheme :: IdMap -> TypeMap -> IdMap -> TypeScheme -> TypeScheme
mapTypeScheme jm tm im (TypeScheme args ty ps) =
TypeScheme (map (mapTypeArg jm tm im) args) (mapTypeE jm tm im ty) ps
mapSen :: IdMap -> TypeMap -> IdMap -> FunMap -> Term -> Term
mapSen jm tm im fm = mapTerm (mapFunSym jm tm im fm, mapTypeE jm tm im)
getDatatypeIds :: DataEntry ->
Set.Set Id
getDatatypeIds (DataEntry _ i _ _ _ alts) =
let getAltIds (Construct _ tys _ sels) =
Set.union getSelIds (Select _ ty _) = getTypeIds ty
getTypeIds = idsOf (== 0)
mapDataEntry :: IdMap -> TypeMap -> IdMap -> FunMap -> DataEntry -> DataEntry
mapDataEntry jm tm im fm (DataEntry dm i k args rk alts) =
newargs = map (mapTypeArg jm tm im) args
in DataEntry nDm i k newargs rk $
Set.map (mapAlt jm tm im fm nIm newargs
mapAlt :: IdMap -> TypeMap -> IdMap -> FunMap -> IdMap -> [TypeArg] -> Type
mapAlt jm tm im fm nIm args dt (Construct mi ts p sels) =
let newTs = map (mapTypeE jm tm nIm) ts
newSels = map (map (mapSel jm tm im fm nIm args dt)) sels
sc = TypeScheme args (getFunType dt p ts) nullRange
(j, TypeScheme _ ty _) = mapFunSym jm tm im fm (i, sc)
in Construct (Just j) newTs (getPartiality newTs ty) newSels
Nothing -> Construct mi newTs p newSels
mapSel :: IdMap -> TypeMap -> IdMap -> FunMap -> IdMap -> [TypeArg] -> Type
mapSel jm tm im fm nIm args dt (Select mid t p) =
let newT = mapTypeE jm tm nIm t
Nothing -> Select mid newT p
sc = TypeScheme args (getSelType dt p t) nullRange
(j, TypeScheme _ ty _) = mapFunSym jm tm im fm (i, sc)
in Select (Just j) newT $ getPartiality [dt] ty
-- | get the partiality from a constructor type
-- with a given number of curried arguments
getPartiality :: [a] -> Type -> Partiality
getPartiality args t = case getTypeAppl t of
(TypeName i _ _, [_, res]) | isArrow i -> case args of
[_] -> if isPartialArrow i then Partial else Total
_ : rs -> getPartiality rs res
(TypeName i _ _, [_]) | i == lazyTypeId ->
if null args then Partial else error "getPartiality"
mapSentence :: Morphism -> Sentence -> Result Sentence
tm = filterAliases . typeMap $ mtarget m
f = mapFunSym jm tm im fm
Formula t -> Formula $ mapSen jm tm im fm t
DatatypeSen td -> DatatypeSen $ map (mapDataEntry jm tm im fm) td
let (ni, nsc) = f (i, sc)
in ProgEqSen ni nsc $ mapEq (f, mapTypeE jm tm im) pe
mapFunSym :: IdMap -> TypeMap -> IdMap -> FunMap -> (Id, TypeScheme)
mapFunSym jm tm im fm (i, sc) =
let msc = mapTypeScheme jm tm im sc
ideMor :: Env -> Morphism
ideMor e = mkMorphism e e
compMor :: Morphism -> Morphism -> Result Morphism
ctm = composeMap (typeMap src) tm1 tm2
ccm = composeMap (classMap src) cm1 cm2
tm = filterAliases $ typeMap tar
in if isInclMor m1 && isInclMor m2 then return emb else do
let p3 = mapFunSym ccm tm tm2 fm2 p2
nSc = mapTypeScheme ccm tm ctm sc
map ( \ o -> ((k, opType o), ())) $
Set.toList os)
showEnvDiff :: Env -> Env -> String
"Signature 1:\n" ++ showDoc e1 "\nSignature 2:\n"
++ showDoc e2 "\nDifference\n" ++ showDoc
legalEnv _ = True -- maybe a closure test?
legalMor :: Morphism -> Bool
morphismUnion :: Morphism -> Morphism -> Result Morphism
t <- merge (mtarget m1) $ mtarget m2
sAs = filterAliases $ typeMap s
tAs = filterAliases $ typeMap t
((i, expand tAs o), (j, expand tAs p)))
Set.map ( \ o -> (i, mapTypeScheme jm tAs im
$ expand sAs $ opType o)) os)
showFun (i, ty) = showId i . (" : " ++) . showDoc ty
tma <- mergeMap ( \ t1 t2 -> if t1 == t2 then return t1 else
fail $ "incompatible type mapping to `"
++ showId t1 "' and '" ++ showId t2 "'") ima1 ima2
cma <- mergeMap ( \ t1 t2 -> if t1 == t2 then return t1 else
fail $ "incompatible class mapping to `"
++ showId t1 "' and '" ++ showId t2 "'") cima1 cima2
fma <- mergeMap ( \ o1 o2 -> if o1 == o2 then return o1 else
fail $ "incompatible mapping to '"
++ showFun o1 "' and '" ++ showFun o2 "'") fma1 fma2
morphismToSymbMap :: Morphism -> SymbolMap
morphismToSymbMap mor = let
tm = filterAliases $ typeMap tar
$ idToClassSymbol tar j k)
Map.empty $ classMap src
$ idToTypeSymbol tar j k) classSymMap $ typeMap src
(j, t2) = mapFunSym jm tm im (funMap mor) (i, ty)
(idToOpSymbol tar j t2)) m s)