CSMOF2CASL.hs revision 9f226cec9f978edaba67aee4c4e04e3d3b994b87
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances #-}
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsModule : $Header$
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsDescription : Coding CSMOF into CASL
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsCopyright : (c) Daniel Calegari Universidad de la Republica, Uruguay 2013
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsLicense : GPLv2 or higher, see LICENSE.txt
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsMaintainer : dcalegar@fing.edu.uy
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsStability : provisional
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsPortability : portable
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs ( CSMOF2CASL (..), mapSign, generateVars
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsimport CSMOF.As as CSMOFAs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsimport CSMOF.Sign as CSMOF
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsimport qualified Common.Lib.Rel as Rel
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsimport qualified Common.Lib.MapSet as MapSet
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsimport qualified Data.Set as Set
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsimport qualified Data.Map as Map
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs-- | lid of the morphism
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsdata CSMOF2CASL = CSMOF2CASL deriving Show
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsinstance Language CSMOF2CASL -- default is ok
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsinstance Comorphism CSMOF2CASL
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs CASL_Sublogics
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs CASLBasicSpec
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs SYMB_MAP_ITEMS
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sourceLogic CSMOF2CASL = CSMOF
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sourceSublogic CSMOF2CASL = ()
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs targetLogic CSMOF2CASL = CASL
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs mapSublogic CSMOF2CASL _ = Just $ caslTop
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs { has_part = False
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , sub_features = LocFilSub
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , cons_features = SortGen True True }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs map_theory CSMOF2CASL = mapTheory
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs map_sentence CSMOF2CASL s = return . mapSen (mapSign s)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs map_morphism CSMOF2CASL = mapMor
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs -- map_symbol CSMOF2CASL _ = Set.singleton . mapSym
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs is_model_transportable CSMOF2CASL = True
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs has_model_expansion CSMOF2CASL = True
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs is_weakly_amalgamable CSMOF2CASL = True
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isInclusionComorphism CSMOF2CASL = True
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsmapTheory :: (CSMOF.Sign, [Named CSMOF.Sen]) -> Result (CASLSign, [Named CASLFORMULA])
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsmapTheory (s, ns) = let cs = mapSign s in
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (cs, map (mapNamed $ mapSen cs) ns ++ sentences cs)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsmapSign :: CSMOF.Sign -> CASLSign
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sorts = getSorts (types s) (typeRel s)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs ops = getOperations (instances s)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs predd = getPredicates (properties s)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sent = getSentencesRels (links s) (instances s)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sentDisEmb = getSortGen (typeRel s) (abstractClasses s) (types s) (instances s)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs noConfBetOps = getNoConfusionBetweenSets (instances s) (typeRel s)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs { sortRel = sorts
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , revSortRel = Just $ Rel.fromList (map revertOrder (Rel.toList sorts))
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , emptySortSet = Set.empty
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , opMap = ops
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , predMap = fst predd
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , sentences = snd predd ++ sent ++ sentDisEmb ++ noConfBetOps
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , declaredSymbols = Set.empty
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , envDiags = []
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , globAnnos = emptyGlobalAnnos
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs , extendedInfo = ()
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetSorts :: Set.Set TypeClass -> Rel.Rel TypeClass -> Rel.Rel SORT
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetSorts setC relC =
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs let relS = Set.fold (insertSort . name) Rel.empty setC
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs in foldr (insertPair) relS (Rel.toList relC)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertSort :: String -> Rel.Rel SORT -> Rel.Rel SORT
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertSort s relS = Rel.insertPair (stringToId s) (stringToId s) relS
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertPair :: (TypeClass,TypeClass) -> Rel.Rel SORT -> Rel.Rel SORT
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertPair (t1,t2) relS = Rel.insertPair (stringToId $ name t1) (stringToId $ name t2) relS
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsrevertOrder :: (SORT,SORT) -> (SORT,SORT)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsrevertOrder (a,b) = (b,a)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetPredicates :: Set.Set PropertyT -> (PredMap,[Named (FORMULA f)])
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetPredicates props = Set.fold (insertPredicate) (MapSet.empty,[]) props
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertPredicate :: PropertyT -> (PredMap,[Named (FORMULA f)]) -> (PredMap,[Named (FORMULA f)])
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertPredicate prop (predM,form) =
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sort1 = stringToId $ name $ sourceType prop
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sort2 = stringToId $ name $ targetType prop
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs pname1 = stringToId $ targetRole prop
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs ptype1 = PredType $ sort1 : [sort2]
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs pname2 = stringToId $ sourceRole prop
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs ptype2 = PredType $ sort2 : [sort1]
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs nam = "equiv_" ++ targetRole prop ++ "_" ++ sourceRole prop
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs varsD = [Var_decl [(mkSimpleId "x")] sort2 nullRange, Var_decl [(mkSimpleId "y")] sort1 nullRange]
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (C.Predication (C.Qual_pred_name pname2 (Pred_type [sort2,sort1] nullRange) nullRange)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (C.Qual_var (mkSimpleId "x") sort2 nullRange : [C.Qual_var (mkSimpleId "y") sort1 nullRange])
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (C.Predication (C.Qual_pred_name pname1 (Pred_type [sort1,sort2] nullRange) nullRange)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (C.Qual_var (mkSimpleId "y") sort1 nullRange : [C.Qual_var (mkSimpleId "x") sort2 nullRange])
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sent = Quantification Universal varsD sentRel nullRange
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs -- MapSet does not allows repeated elements, but predicate names can be repeated
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs -- (this must be corrected by creating a more complex ID)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if sourceRole prop == "_"
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs then (MapSet.insert pname1 ptype1 predM, form)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs else if targetRole prop == "_"
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs then (MapSet.insert pname2 ptype2 predM, form)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs else (MapSet.insert pname1 ptype1 $ MapSet.insert pname2 ptype2 predM, (makeNamed nam sent) : form)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetOperations :: Map.Map String TypeClass -> OpMap
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetOperations ops = foldr (insertOperations) MapSet.empty (Map.toList ops)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertOperations :: (String,TypeClass) -> OpMap -> OpMap
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsinsertOperations (na,tc) opM =
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs opName = stringToId na
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs opType = OpType Total [] (stringToId $ name tc)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs MapSet.insert opName opType opM
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetSentencesRels :: Set.Set LinkT -> Map.Map String TypeClass -> [Named (CASLFORMULA)]
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacsgetSentencesRels linkk ops = completenessOfRelations linkk ops
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacscompletenessOfRelations :: Set.Set LinkT -> Map.Map String TypeClass -> [Named (CASLFORMULA)]
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacscompletenessOfRelations linkk ops =
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs let ordLinks = getLinksByProperty linkk
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs in foldr ((++) . (createComplFormula ops)) [] (Map.toList ordLinks)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacscreateComplFormula :: Map.Map String TypeClass -> (String,[LinkT]) -> [Named (CASLFORMULA)]
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks MalnacscreateComplFormula ops (nam,linksL) =
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs varA = mkSimpleId "x"
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs varB = mkSimpleId "y"
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case linksL of
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (LinkT _ _ pr) : _ -> let
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sorA = stringToId $ name $ sourceType pr
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sorB = stringToId $ name $ targetType pr
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs varsD = [Var_decl [varA] sorA nullRange, Var_decl [varB] sorB nullRange]
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (C.Predication (C.Qual_pred_name (stringToId $ targetRole pr) (Pred_type [sorA,sorB] nullRange) nullRange)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (C.Qual_var varA sorA nullRange
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs : [C.Qual_var varB sorB nullRange])
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs (Junction Dis (foldr ((:) . (getPropHold ops varA sorA varB sorB)) [] linksL) nullRange)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sentQuan = Quantification Universal varsD sent nullRange
getPropHold :: Map.Map String TypeClass -> VAR -> SORT -> VAR -> SORT -> LinkT -> CASLFORMULA
souObj = Map.lookup (sourceVar lin) ops
tarObj = Map.lookup (targetVar lin) ops
let elems = Set.elems linkk
in foldr (getByProperty) Map.empty elems
prope = CSMOF.property lin
nameLook = sourceRole prope ++ name (sourceType prope) ++ targetRole prope ++ name (targetType prope)
setProp = Map.lookup nameLook mapL
getSortGen :: Rel.Rel TypeClass -> Set.Set TypeClass -> Set.Set TypeClass -> Map.Map String TypeClass -> [Named (CASLFORMULA)]
sortGenerationNonAbstractSuperClasses :: Rel.Rel TypeClass -> Set.Set TypeClass -> Set.Set TypeClass -> Map.Map String TypeClass -> [Named (CASLFORMULA)]
nonAbsClassesWChilds = filter (\x -> not (null (snd x))) (Set.fold ((:) . (getSubClasses typpR)) [] nonAbsClasses)
childObjects = foldr ((:) . (getClassSubObjects ordObj)) [] nonAbsClassesWChilds -- [(TypeClass,[String])]
-- Takes the objects, and a class with its child classes and returns the descendent objects of such class
getClassSubObjects :: Map.Map TypeClass [String] -> (TypeClass,[TypeClass]) -> (TypeClass,[String])
findObjectInMap :: Map.Map TypeClass [String] -> TypeClass -> [String]
case Map.lookup tc objs of
getNonAbstractClasess absCl classes = Set.difference classes absCl
getSubClasses :: Rel.Rel TypeClass -> TypeClass -> (TypeClass,[TypeClass])
let subCla = map (fst) (filter (isParent tc) (Rel.toList typpR))
toSortConstraintNonAbsClass :: Map.Map String TypeClass -> (TypeClass, [String]) -> Named (CASLFORMULA)
getEqualityVarObject :: SORT -> Map.Map String TypeClass -> String -> CASLFORMULA
let oTyp = case Map.lookup obj inst of
sortGeneration :: Map.Map String TypeClass -> [Named (CASLFORMULA)]
noJunk = foldr ((:) . toSortConstraint) [] (Map.toList ordObj)
case Map.lookup tc mapTC of
Nothing -> Map.insert tc [ob] mapTC
getNoConfusionBSetsAxiom :: [(TypeClass, [String])] -> Rel.Rel TypeClass -> (TypeClass, [String]) -> Maybe (Named (CASLFORMULA))
diffOfRestConstants :: (TypeClass, [String]) -> Rel.Rel TypeClass -> (TypeClass, [String]) -> [CASLFORMULA]
haveCommonSort :: TypeClass -> TypeClass -> Rel.Rel TypeClass -> Bool
reach e s = if Set.member e s then s
diffOfRestOpsDiffSort (tc1,lisObj1) tc2 objName = concat $ map (diffOpsDiffSorts tc2 objName tc1) lisObj1
injSyms = map (\ (s, t) -> (Qual_op_name (mkUniqueInjName (stringToId $ name s) (stringToId $ name t))
$ Rel.irreflex rel
collectOps s = Constraint (stringToId $ name s) (filter (resType (stringToId $ name s)) injSyms) (stringToId $ name s)
sortList = Set.toList absCl
mapSen :: CASLSign -> CSMOF.Sen -> CASLFORMULA
predTypes = MapSet.lookup (stringToId $ getRole con) predM -- Set PredType
correctPredType = Set.fold (getCorrectPredType con) [] predTypes
case (stringToId $ name (CSMOF.getType con)) == head (predArgs pt) of
generateVarDiff (Var_decl vars sor _) = Junction Con (foldr ((++) . diffOfRest sor vars) [] vars) nullRange
Predication (C.Qual_pred_name rol (Pred_type [sor,sor2] nullRange) nullRange)
predTypes = MapSet.lookup (stringToId $ getRole con) predM -- Set PredType
correctPredType = Set.fold (getCorrectPredType con) [] predTypes
mapMor :: CSMOF.Morphism -> Result CASLMor
mapMor m = return C.Morphism
, sort_map = Map.empty
, op_map = Map.empty
, pred_map = Map.empty