PreComorphism.hs revision 3f8cdebaede9921402318d525b57a9af8f9279d3
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Data.List as List
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Data.Set as Set
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Data.Map as Map
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Maude.Sign as MSign
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Maude.Sentence as MSentence
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Maude.Morphism as MMorphism
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Maude.AS_Maude as MAS
e32f2729ffc4e828e41a528f47a4815d4a1689f0Felix Gabriel Manceimport qualified Maude.Symbol as MSym
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport qualified CASL.Sign as CSign
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport qualified CASL.Morphism as CMorphism
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Manceimport qualified CASL.AS_Basic_CASL as CAS
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Manceimport qualified Common.Lib.Rel as Rel
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mancetype IdMap = Map.Map Id Id
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance-- | generates a CASL morphism from a Maude morphism
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancemapMorphism :: MMorphism.Morphism -> Result (CMorphism.CASLMor)
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel MancemapMorphism morph =
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance mk = arrangeKinds (MSign.sorts src) (MSign.subsorts src)
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance cs = kindsFromMap mk
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance smap' = applySortMap2CASLSorts smap cs
1cc559ec103ed20967587fff2e39cc88669f7b8fFelix Gabriel Mance omap' = maudeOpMap2CASLOpMap mk omap
1cc559ec103ed20967587fff2e39cc88669f7b8fFelix Gabriel Mance pmap = createPredMap mk smap
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance (src', _) = maude2casl src []
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance (tgt', _) = maude2casl tgt []
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance in return $ CMorphism.Morphism src' tgt' smap' omap' pmap ()
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance-- | translates the Maude morphism between operators into a CASL morpshim between
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancemaudeOpMap2CASLOpMap :: IdMap -> MMorphism.OpMap -> CMorphism.Op_map
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancemaudeOpMap2CASLOpMap im = Map.foldWithKey (translateOpMapEntry im) Map.empty
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance-- | translates the mapping between two symbols representing operators into
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Mance-- a CASL operators map
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel MancetranslateOpMapEntry :: IdMap -> MSym.Symbol -> MSym.Symbol -> CMorphism.Op_map
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancetranslateOpMapEntry im (MSym.Operator from ar co) (MSym.Operator to _ _) copm = copm'
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance where f = token2id . getName
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance g = \ x -> fromJust $ Map.lookup (f x) im
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance ot = CSign.OpType CAS.Total (map g ar) (g co)
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance cop = (token2id from, ot)
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance to' = (token2id to, CAS.Total)
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel Mance copm' = Map.insert cop to' copm
1341e758a8a0785dd7063b93aed3989f13b36f2aFelix Gabriel MancetranslateOpMapEntry _ _ _ _ = Map.empty
7c2291b25eadfb81c4850bf7ea92a3168f547fa9Felix Gabriel Mance-- | generates a set of CASL symbol from a Maude Symbol
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancemapSymbol :: MSign.Sign -> MSym.Symbol -> Set.Set CSign.Symbol
7c2291b25eadfb81c4850bf7ea92a3168f547fa9Felix Gabriel MancemapSymbol sg (MSym.Sort q) = Set.singleton csym
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance where mk = arrangeKinds (MSign.sorts sg) (MSign.subsorts sg)
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance sym_id = token2id q
c41f2d65ecbf5ad9d3233a21f406a7698338a04bFelix Gabriel Mance kind = fromJust $ Map.lookup sym_id mk
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance pred_data = CSign.PredType [kind]
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance csym = CSign.Symbol sym_id $ CSign.PredAsItemType pred_data
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancemapSymbol sg (MSym.Operator q ar co) = Set.singleton csym
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance where mk = arrangeKinds (MSign.sorts sg) (MSign.subsorts sg)
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Mance q' = token2id q
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Mance ar' = map (maudeSort2caslId mk) ar
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel Mance co' = token2id $ getName co
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance op_data = CSign.OpType CAS.Total ar' co'
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance csym = CSign.Symbol q' $ CSign.OpAsItemType op_data
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance-- | returns the sort in CASL of the Maude sort symbol
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancemaudeSort2caslId :: IdMap -> MSym.Symbol -> Id
137edd3944aacd150d60af8977de962113ead859Felix Gabriel MancemaudeSort2caslId im sym = fromJust $ Map.lookup (token2id $ getName sym) im
137edd3944aacd150d60af8977de962113ead859Felix Gabriel Mance-- | creates the predicate map for the CASL morphism from the Maude sort map and
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance-- the map between sorts and kinds
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel MancecreatePredMap :: IdMap -> MMorphism.SortMap -> CMorphism.Pred_map
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel MancecreatePredMap im = Map.foldWithKey (createPredMap4sort im) Map.empty
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance-- | creates an entry of the predicate map for a single sort
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel MancecreatePredMap4sort :: IdMap -> MSym.Symbol -> MSym.Symbol -> CMorphism.Pred_map
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel MancecreatePredMap4sort im from to m = Map.insert key id_to m
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance where id_from = token2id $ getName from
41ddc1ba03dadd983308781ae8fd638c3faab453Felix Gabriel Mance id_to = token2id $ getName to
41ddc1ba03dadd983308781ae8fd638c3faab453Felix Gabriel Mance kind = fromJust $ Map.lookup id_from im
6e7fe479953725884826bd38e4779229d45d3a40Felix Gabriel Mance key = (id_from, CSign.PredType [kind])
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance-- | computes the sort morphism of CASL from the sort morphism in Maude and the set
fc05327b875b5723b6c17849b83477f29ec12c90Felix Gabriel ManceapplySortMap2CASLSorts :: MMorphism.SortMap -> Set.Set Id -> CMorphism.Sort_map
7c2291b25eadfb81c4850bf7ea92a3168f547fa9Felix Gabriel ManceapplySortMap2CASLSorts sm = Set.fold (applySortMap2CASLSort sm) Map.empty
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel Mance-- | computes the morphism for a single kind
e05e1babc9a0edf2ebd39713d5c44fd0a035d6daFelix Gabriel ManceapplySortMap2CASLSort :: MMorphism.SortMap -> Id -> CMorphism.Sort_map -> CMorphism.Sort_map
396f82c6cd926be759b60fd1e854acfde7068215Felix Gabriel ManceapplySortMap2CASLSort sm sort csm = new_csm
else Map.insert sort new_sort csm
rename :: MMorphism.SortMap -> Token -> Token
where sym = MSym.Sort tok
sym' = if Map.member sym sm
then fromJust $ Map.lookup sym sm
mapSentence sg sen@(MSentence.Equation eq) = case owise ats of
MAS.Eq _ _ _ ats = eq
mapSentence sg sen@(MSentence.Membership mb) = return $ sentence form
MAS.Mb _ _ _ _ = mb
mapSentence sg sen@(MSentence.Rule rl) = return $ sentence form
MAS.Rl _ _ _ _ = rl
maude2casl msign nsens = (csign { CSign.sortSet = cs,
CSign.emptySortSet = cs,
CSign.opMap = cops',
CSign.assocOps = assoc_ops,
CSign.predMap = preds,
CSign.declaredSymbols = syms }, new_sens)
where csign = CSign.emptySign ()
mk = Map.insert (token2id $ mkSimpleId "Universal") (token2id $ mkSimpleId "[Universal]") mk'
ops = MSign.ops msign
predefinedOps kinds om = Set.fold predefinedOpKind om'' kinds
om'' = Map.delete neg_double_eq_id om'
predefinedSens = Set.fold predefinedSensKind []
ifSens :: Id -> [Named CAS.CASLFORMULA]
true_id = CAS.Op_name $ token2id $ mkSimpleId "true"
true_term = CAS.Application true_id [] nullRange
if_id = CAS.Op_name $ token2id $ mkSimpleId "if_then_else_fi"
if_term = CAS.Application if_id [bv, v1, v2] nullRange
prem = CAS.Strong_equation bv true_term nullRange
concl = CAS.Strong_equation if_term v1 nullRange
form = CAS.Implication prem concl True nullRange
neg_prem = CAS.Negation prem nullRange
neg_concl = CAS.Strong_equation if_term v2 nullRange
neg_form = CAS.Implication neg_prem neg_concl True nullRange
equalitySens :: Id -> [Named CAS.CASLFORMULA]
true_id = CAS.Op_name $ token2id $ mkSimpleId "true"
true_term = CAS.Application true_id [] nullRange
false_id = CAS.Op_name $ token2id $ mkSimpleId "false"
false_term = CAS.Application false_id [] nullRange
prem = CAS.Strong_equation v1 v2 nullRange
double_eq_id = CAS.Op_name $ token2id $ mkSimpleId "_==_"
double_eq_term = CAS.Application double_eq_id [v1, v2] nullRange
concl = CAS.Strong_equation double_eq_term true_term nullRange
form = CAS.Implication prem concl True nullRange
neg_prem = CAS.Negation prem nullRange
new_concl = CAS.Strong_equation double_eq_term false_term nullRange
comp_form = CAS.Implication neg_prem new_concl True nullRange
nonEqualitySens :: Id -> [Named CAS.CASLFORMULA]
true_id = CAS.Op_name $ token2id $ mkSimpleId "true"
true_term = CAS.Application true_id [] nullRange
false_id = CAS.Op_name $ token2id $ mkSimpleId "false"
false_term = CAS.Application false_id [] nullRange
prem = CAS.Strong_equation v1 v2 nullRange
double_eq_id = CAS.Op_name $ token2id $ mkSimpleId "_=/=_"
double_eq_term = CAS.Application double_eq_id [v1, v2] nullRange
concl = CAS.Strong_equation double_eq_term false_term nullRange
form = CAS.Implication prem concl True nullRange
neg_prem = CAS.Negation prem nullRange
new_concl = CAS.Strong_equation double_eq_term true_term nullRange
comp_form = CAS.Implication neg_prem new_concl True nullRange
plusSen :: [Named CAS.CASLFORMULA]
plus_id = CAS.Op_name $ token2id $ mkSimpleId "_+_"
succ_id = CAS.Op_name $ token2id $ mkSimpleId "s_"
succ_v1 = CAS.Application succ_id [v1] nullRange
lhs = CAS.Application plus_id [succ_v1, v2] nullRange
add_term = CAS.Application plus_id [v1, v2] nullRange
rhs = CAS.Application succ_id [add_term] nullRange
form = CAS.Strong_equation lhs rhs nullRange
translateOpDeclSet :: IdMap -> MSign.OpDeclSet
translateOpDeclSet im ods tpl = Set.fold (translateOpDecl im) tpl ods
translateOpDecl :: IdMap -> MSign.OpDecl
(sym : _) = Set.toList syms
cop_type = Set.singleton ot
op_name = CAS.Op_name cop_id
then let assoc_f = associativeSen op_name (head $ CSign.opArgs ot)
then let comm_f = commutativeSen op_name (head $ CSign.opArgs ot)
then let idem_f = idempotentSen op_name (head $ CSign.opArgs ot)
identity_f = identitySens op_name (head $ CSign.opArgs ot) iden
lid_f = left_identitySen op_name (head $ CSign.opArgs ot) lid
rid_f = right_identitySen op_name (head $ CSign.opArgs ot) rid
maudeSym2CASLOp im (MSym.Operator op ar co) = Just (token2id op, ot)
g = \ x -> fromJust $ Map.lookup (f x) im
ops2pred im = Set.fold (op2pred im) []
op2pred im (MSym.Operator op ar co) acc = case co of
MSym.Sort s -> let
op' = CAS.Op_name $ token2id op
pred_name = CAS.Pred_name co'
op_term = CAS.Application op' vars nullRange
op_pred = CAS.Predication pred_name [op_term] nullRange
else CAS.Implication conj_form op_pred True nullRange
createConjForm [] = CAS.True_atom nullRange
createConjForm fs = CAS.Conjunction fs nullRange
ops2predPremises im (MSym.Sort s : ss) i = (var : terms, form : forms)
kind = fromJust $ Map.lookup s' im
pred_name = CAS.Pred_name s'
form = CAS.Predication pred_name [var] nullRange
ops2predPremises im (MSym.Kind k : ss) i = (var : terms, forms)
kind = fromJust $ Map.lookup k' im
splitOwiseEqs :: [Named MSentence.Sentence] ->
noOwiseSen2Formula :: IdMap -> Named MSentence.Sentence
-> Named CAS.CASLFORMULA
where MSentence.Equation eq = sentence s
owiseSen2Formula :: IdMap -> [Named CAS.CASLFORMULA]
where MSentence.Equation eq = sentence s
MSentence.Membership mb -> let
MSentence.Rule rl -> let
_ -> makeNamed "" $ CAS.False_atom nullRange
kind = fromJust $ Map.lookup (token2id sub') im
sub_pred_name = CAS.Pred_name $ token2id sub'
super_pred_name = CAS.Pred_name $ token2id super'
sub_form = CAS.Predication sub_pred_name [var] nullRange
super_form = CAS.Predication super_pred_name [var] nullRange
form = CAS.Implication sub_form super_form True nullRange
newVarIndex :: Int -> Id -> CAS.CASLTERM
newVarIndex i sort = CAS.Qual_var var sort nullRange
newVar :: Id -> CAS.CASLTERM
newVar sort = CAS.Qual_var var sort nullRange
noOwiseEq2Formula im (MAS.Eq t t' [] _) = quantifyUniversally form
form = CAS.Strong_equation ct ct' nullRange
noOwiseEq2Formula im (MAS.Eq t t' conds@(_:_) _) = quantifyUniversally form
concl_form = CAS.Strong_equation ct ct' nullRange
form = CAS.Implication conds_form concl_form True nullRange
imp_form = CAS.Implication ex_form eq_form True nullRange
then CAS.Conjunction ex_forms nullRange
conj_form = CAS.Conjunction prems nullRange
neg_form = CAS.Negation ex_form nullRange
createEqs (t1 : ts1) (t2 : ts2) = CAS.Strong_equation t1 t2 nullRange : ls
getLeftApp (CAS.Strong_equation term _ _) = Just (op, ts, [])
getLeftApp (CAS.Implication prem concl _ _) = Just (op, ts, conds)
getLeftAppTerm (CAS.Application op ts _) = Just (op, ts)
getPremisesImplication (CAS.Conjunction forms _) = forms
mb2formula im (MAS.Mb t s [] _) = quantifyUniversally form
pred_name = CAS.Pred_name $ token2id $ getName s
form = CAS.Predication pred_name [ct] nullRange
mb2formula im (MAS.Mb t s conds@(_ : _) _) = quantifyUniversally form
pred_name = CAS.Pred_name $ token2id $ getName s
concl_form = CAS.Predication pred_name [ct] nullRange
form = CAS.Implication conds_form concl_form True nullRange
rl2formula im (MAS.Rl t t' [] _) = quantifyUniversally form
pred_name = CAS.Pred_name rewID
form = CAS.Predication pred_name [ct, ct'] nullRange
rl2formula im (MAS.Rl t t' conds@(_:_) _) = quantifyUniversally form
pred_name = CAS.Pred_name rewID
concl_form = CAS.Predication pred_name [ct, ct'] nullRange
form = CAS.Implication conds_form concl_form True nullRange
conds2formula im conds = CAS.Conjunction forms nullRange
pred_name = CAS.Pred_name $ token2id $ getName s
pred_name = CAS.Pred_name rewID
where kind = fromJust $ Map.lookup (token2id $ getName ty) im
rewPredicatesSens = Set.fold rewPredicateSens []
reflSen :: Id -> Named CAS.CASLFORMULA
pn = CAS.Pred_name rewID
form = CAS.Predication pn [v, v] nullRange
transSen :: Id -> Named CAS.CASLFORMULA
pn = CAS.Pred_name rewID
prem1 = CAS.Predication pn [v1, v2] nullRange
prem2 = CAS.Predication pn [v2, v3] nullRange
concl = CAS.Predication pn [v1, v3] nullRange
conj_form = CAS.Conjunction [prem1, prem2] nullRange
form = CAS.Implication conj_form concl True nullRange
rewPredicates m = Set.fold rewPredicate m
kindsFromMap :: IdMap -> Set.Set Id
where tops = List.sort $ getTop r s
tc = Rel.transClosure r
f = \ x y z -> Map.insert (sym2id y) (sort2id x) z
sameKindList t r (t' : ts) = if MSym.sameKind r t t'
sortsTranslation ss r = sortsTranslationList (Set.toList ss) r
sortsTranslationList [] _ = Set.empty
sortsTranslationList (s : ss) r = Set.insert (sort2id tops) res
where tops@(top : _) = List.sort $ getTop r s
deleteRelated ss sym r = foldr (f sym tc) Set.empty ss
where tc = Rel.transClosure r
f = \ sort trC x y -> if MSym.sameKind trC sort x
else Set.insert x y
sym2id :: MSym.Symbol -> Id
sort2id :: [MSym.Symbol] -> Id
listVarDecl = Map.foldWithKey f []
noQuantification (CAS.Quantification _ vars form _) = (form, vars)
t1 = CAS.Application op [v2, v3] nullRange
t = CAS.Application op [v1, t1] nullRange
t2 = CAS.Application op [v1, v2] nullRange
t' = CAS.Application op [t2, v3] nullRange
form = CAS.Strong_equation t t' nullRange
t = CAS.Application op [v1, v2] nullRange
t' = CAS.Application op [v2, v1] nullRange
form = CAS.Strong_equation t t' nullRange
t1 = CAS.Application op [v, t] nullRange
t2 = CAS.Application op [t, v] nullRange
form1 = quantifyUniversally $ CAS.Strong_equation t1 v nullRange
form2 = quantifyUniversally $ CAS.Strong_equation t2 v nullRange
t' = CAS.Application op [t, v] nullRange
form = quantifyUniversally $ CAS.Strong_equation t' v nullRange
t' = CAS.Application op [v, t] nullRange
form = quantifyUniversally $ CAS.Strong_equation t' v nullRange
t = CAS.Application op [v1, v2] nullRange
form = CAS.Strong_equation t v1 nullRange
kinds2syms = Set.map kind2sym
kind2sym :: Id -> CSign.Symbol
pred2sym pn spt acc = Set.union set acc
createSym4id pn pt acc = Set.insert sym acc
op2sym on sot acc = Set.union set acc
createSymOp4id on ot acc = Set.insert sym acc
getVars (CAS.Quantification _ _ f _) = getVars f
getVars (CAS.Negation f _) = getVars f
getVars (CAS.Definedness t _) = getVarsTerm t
getVars (CAS.Membership t _ _) = getVarsTerm t
getVars (CAS.Mixfix_formula t) = getVarsTerm t
getVars _ = Map.empty
getVarsTerm (CAS.Sorted_term t _ _) = getVarsTerm t
getVarsTerm (CAS.Cast t _ _) = getVarsTerm t
getVarsTerm (CAS.Mixfix_parenthesized ts _) =
getVarsTerm (CAS.Mixfix_bracketed ts _) =
getVarsTerm (CAS.Mixfix_braced ts _) =
getVarsTerm _ = Map.empty
owise :: [MAS.StmntAttr] -> Bool
MAS.Owise -> True
assoc :: [MAS.Attr] -> Bool
MAS.Assoc -> True
comm :: [MAS.Attr] -> Bool
MAS.Comm -> True
idem :: [MAS.Attr] -> Bool
MAS.Idem -> True
identity :: [MAS.Attr] -> Bool
MAS.Id _ -> True
leftId :: [MAS.Attr] -> Bool
MAS.LeftId _ -> True
rightId :: [MAS.Attr] -> Bool
MAS.RightId _ -> True
MAS.Id t -> Just t
MAS.RightId t -> Just t
MAS.LeftId t -> Just t