PreComorphism.hs revision 3b1e33dd8d2de8301d7a31860dd1819bd3752718
5462N/Amodule Maude.PreComorphism where
5462N/Aimport Data.Maybe
5462N/Aimport qualified Maude.Sign as MSign
5462N/Aimport qualified Maude.Sentence as MSentence
6815N/Aimport qualified Maude.Morphism as MMorphism
5462N/Aimport qualified Maude.AS_Maude as MAS
5462N/Aimport qualified Maude.Symbol as MSym
5462N/Aimport Maude.Meta.HasName
6815N/Aimport qualified CASL.Morphism as CMorphism
5462N/Aimport qualified CASL.AS_Basic_CASL as CAS
5462N/Aimport CASL.StaticAna
5462N/Aimport CASL.Logic_CASL
5462N/Aimport Common.Result
5462N/Aimport Common.AS_Annotation
5462N/Aimport Common.ProofUtils (charMap)
6815N/Aimport qualified Common.Lib.Rel as Rel
5462N/Aimport Comorphisms.GetPreludeLib (readLib)
5462N/Aimport System.IO.Unsafe
5462N/Aimport Static.GTheory
5462N/Aimport Logic.Prover
5462N/Aimport Logic.Coerce
src = MMorphism.source morph
tgt = MMorphism.target morph
smap = MMorphism.sortMap morph
omap = MMorphism.opMap morph
in return $ CMorphism.Morphism src' tgt' smap' omap' pmap ()
g = \ x -> Map.findWithDefault (errorId "translate op map entry") (f x) im
to' = (token2id to, CAS.Total)
copm' = Map.insert cop to' copm
translateOpMapEntry _ _ _ _ = Map.empty
kind = Map.findWithDefault (errorId "map symbol") sym_id mk
pred_data = CSign.PredType [kind]
mapSymbol _ _ = Set.empty
maudeSort2caslId :: IdMap -> MSym.Symbol -> Id
maudeSort2caslId im sym = Map.findWithDefault (errorId "sort to id") (token2id $ getName sym) im
createPredMap4sort im from to m = Map.insert key id_to m
kind = Map.findWithDefault (errorId "predicate for sort") id_from im
key = (id_from, CSign.PredType [kind])
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
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.opMap = cops',
CSign.assocOps = assoc_ops,
CSign.predMap = preds,
CSign.declaredSymbols = syms }, new_sens)
where csign = CSign.emptySign ()
ops = deleteUniversal $ MSign.ops msign
ctor_sen = [ctorSen False (cs, Rel.empty, comps)]
pred_forms = loadLibraries (MSign.sorts msign) ops
loadNaturalNatSens :: [Named CAS.CASLFORMULA]
let lib = head $ unsafePerformIO $ readLib "Maude/MaudeNumbers.casl"
ctorCons :: Named CAS.CASLFORMULA -> Bool
CAS.Sort_gen_ax _ _ -> True
booleanImported :: MSign.OpMap -> Bool
booleanImported = Map.member (mkSimpleId "if_then_else_fi")
where om1 = Map.delete (mkSimpleId "if_then_else_fi") om
om2 = Map.delete (mkSimpleId "_==_") om1
om3 = Map.delete (mkSimpleId "_=/=_") om2
om4 = Map.delete (mkSimpleId "upTerm") om3
om5 = Map.delete (mkSimpleId "downTerm") om4
universalOps kinds om True = Set.fold universalOpKind om kinds
universalSens = Set.fold universalSensKind []
ifSens :: Id -> [Named CAS.CASLFORMULA]
true_id = CAS.Qual_op_name (str2id "true") true_type nullRange
true_term = CAS.Application true_id [] nullRange
if_id = CAS.Qual_op_name if_name if_type nullRange
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.Qual_op_name (str2id "true") b_type nullRange
true_term = CAS.Application true_id [] nullRange
false_id = CAS.Qual_op_name (str2id "false") b_type nullRange
false_term = CAS.Application false_id [] nullRange
prem = CAS.Strong_equation v1 v2 nullRange
double_eq_id = CAS.Qual_op_name double_eq_name double_eq_type nullRange
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.Qual_op_name (str2id "true") b_type nullRange
true_term = CAS.Application true_id [] nullRange
false_id = CAS.Qual_op_name (str2id "false") b_type nullRange
false_term = CAS.Application false_id [] nullRange
prem = CAS.Strong_equation v1 v2 nullRange
double_eq_id = CAS.Qual_op_name double_eq_name double_eq_type nullRange
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
translateOps :: IdMap -> MSign.OpMap -> OpTransTuple
translateOpDeclSet :: IdMap -> MSign.OpDeclSet -> OpTransTuple -> OpTransTuple
translateOpDeclSet im ods tpl = Set.fold (translateOpDecl im) tpl ods
translateOpDecl :: IdMap -> MSign.OpDecl -> OpTransTuple -> OpTransTuple
sym = head $ Set.toList syms
cop_type = Set.singleton ot
assoc_ops' = if any MAS.assoc ats
cs' = if any MAS.ctor ats
then Set.insert (Component cop_id ot) cs
maudeSym2CASLOp im (MSym.Operator op ar co) = Just (token2id op, ot)
g = \ x -> Map.findWithDefault (errorId "Maude_sym2CASL_sym") (f x) im
ops2pred im = Set.fold (op2pred im) []
op2pred im (MSym.Operator op ar co) acc = case co of
MSym.Sort s -> let
kind = Map.findWithDefault (errorId "op-mb to predicate") co' im
f = \ m x -> Map.lookup (token2id $ getName x) m
op' = CAS.Qual_op_name (token2id op) op_type nullRange
pred_type = CAS.Pred_type [kind] nullRange
pred_name = CAS.Qual_pred_name co' pred_type nullRange
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
createImpForm (CAS.True_atom _) form = form
createImpForm form1 form2 = CAS.Implication form1 form2 True nullRange
ops2predPremises im (MSym.Sort s : ss) i = (var : terms, form : forms)
kind = Map.findWithDefault (errorId "mb of op as predicate") s' im
pred_type = CAS.Pred_type [kind] nullRange
pred_name = CAS.Qual_pred_name s' pred_type nullRange
form = CAS.Predication pred_name [var] nullRange
ops2predPremises im (MSym.Kind k : ss) i = (var : terms, forms)
kind = Map.findWithDefault (errorId "mb of op as predicate") 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
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 eq@(MAS.Eq t t' [] _) = quantifyUniversally vars_form
form = CAS.Strong_equation ct ct' nullRange
vars_form = varsImp (MSentence.Equation eq) im form
noOwiseEq2Formula im eq@(MAS.Eq t t' conds@(_:_) _) = quantifyUniversally vars_form
concl_form = CAS.Strong_equation ct ct' nullRange
vars_form = varsImp (MSentence.Equation eq) im form
vars_form = varsImp (MSentence.Equation eq) im eq_form
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 _ _) = case getLeftAppTerm term of
getLeftApp (CAS.Implication prem concl _ _) = case getLeftApp concl of
getLeftAppTerm (CAS.Application op ts _) = Just (op, ts)
getPremisesImplication (CAS.Conjunction forms _) = forms
mb2formula im mb@(MAS.Mb t s [] _) = quantifyUniversally vars_form
kind = Map.findWithDefault (errorId "mb to formula") s' im
pred_type = CAS.Pred_type [kind] nullRange
pred_name = CAS.Qual_pred_name s' pred_type nullRange
form = CAS.Predication pred_name [ct] nullRange
vars_form = varsImp (MSentence.Membership mb) im form
mb2formula im mb@(MAS.Mb t s conds@(_ : _) _) = quantifyUniversally vars_form
kind = Map.findWithDefault (errorId "mb to formula") s' im
pred_type = CAS.Pred_type [kind] nullRange
pred_name = CAS.Qual_pred_name s' pred_type nullRange
concl_form = CAS.Predication pred_name [ct] nullRange
form = CAS.Implication conds_form concl_form True nullRange
vars_form = varsImp (MSentence.Membership mb) im form
rl2formula im rl@(MAS.Rl t t' [] _) = quantifyUniversally vars_form
where ty = token2id $ getName $ MAS.getTermType t
kind = Map.findWithDefault (errorId "rl to formula") ty im
pred_type = CAS.Pred_type [kind, kind] nullRange
pred_name = CAS.Qual_pred_name rewID pred_type nullRange
form = CAS.Predication pred_name [ct, ct'] nullRange
vars_form = varsImp (MSentence.Rule rl) im form
rl2formula im rl@(MAS.Rl t t' conds@(_:_) _) = quantifyUniversally vars_form
where ty = token2id $ getName $ MAS.getTermType t
kind = Map.findWithDefault (errorId "rl to formula") ty im
pred_type = CAS.Pred_type [kind, kind] nullRange
pred_name = CAS.Qual_pred_name rewID pred_type nullRange
concl_form = CAS.Predication pred_name [ct, ct'] nullRange
form = CAS.Implication conds_form concl_form True nullRange
vars_form = varsImp (MSentence.Rule rl) im form
conds2formula im conds = CAS.Conjunction forms nullRange
kind = Map.findWithDefault (errorId "mb cond to formula") s' im
pred_type = CAS.Pred_type [kind] nullRange
pred_name = CAS.Qual_pred_name s' pred_type nullRange
ty = token2id $ getName $ MAS.getTermType t
kind = Map.findWithDefault (errorId "rw cond to formula") ty im
pred_type = CAS.Pred_type [kind, kind] nullRange
pred_name = CAS.Qual_pred_name rewID pred_type nullRange
where kind = Map.findWithDefault (errorId "maude_term2CASL_term") (token2id $ getName ty) im
kind = Map.findWithDefault (errorId "maude_term2CASL_term") ty' im
op = CAS.Qual_op_name name op_type nullRange
kind = Map.findWithDefault (errorId "maude_term2CASL_term") ty' im
op = CAS.Qual_op_name name op_type nullRange
getTypes :: [CAS.CASLTERM] -> [Id]
getType :: CAS.CASLTERM -> Maybe Id
getType (CAS.Qual_var _ kind _) = Just kind
getType (CAS.Application op _ _) = case op of
rewPredicatesSens = Set.fold rewPredicateSens []
reflSen :: Id -> Named CAS.CASLFORMULA
pred_type = CAS.Pred_type [kind, kind] nullRange
pn = CAS.Qual_pred_name rewID pred_type nullRange
form = CAS.Predication pn [v, v] nullRange
transSen :: Id -> Named CAS.CASLFORMULA
pred_type = CAS.Pred_type [kind, kind] nullRange
pn = CAS.Qual_pred_name rewID pred_type nullRange
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
where sym = head $ List.sort syms
listVarDecl = Map.foldWithKey f []
varsImplication (MSentence.Membership mb) im = createConjForm forms
where MAS.Mb t _ conds _ = mb
varsImplication (MSentence.Equation eq) im = createConjForm forms
where MAS.Eq t _ conds _ = eq
varsImplication (MSentence.Rule rl) im = createConjForm forms
where MAS.Rl t _ conds _ = rl
varsImpCond im (MAS.MatchCond t _) = varsImpTerm im t
varsImpCond _ _ = Set.empty
pt = CAS.Pred_type [kind] nullRange
ps = CAS.Qual_pred_name sort pt nullRange
term = CAS.Qual_var q kind nullRange
varsImpTerm im (MAS.Apply _ terms _) = varsImpTerms im terms
varsImpTerm _ _ = Set.empty
noQuantification (CAS.Quantification _ vars form _) = (form, vars)
kinds2syms = Set.map kind2sym
kind2sym :: Id -> CSign.Symbol
pred2sym pn spt acc = Set.fold (createSym4id pn) acc spt
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
ctorSen :: Bool -> GenAx -> Named CAS.CASLFORMULA
let sortList = Set.toList sorts
opSyms = map ( \ c -> let ide = compId c in CAS.Qual_op_name ide
resType _ (CAS.Op_name _) = False
getIndex s = maybe (-1) id $ List.findIndex (== s) sortList
addIndices (CAS.Op_name _) =
error "CASL/StaticAna: Internal error in function addIndices"
addIndices os@(CAS.Qual_op_name _ t _) =
(os,map getIndex $ CAS.args_OP_TYPE t)
CAS.Constraint s (map addIndices $ filter (resType s) allSyms) s
f = CAS.Sort_gen_ax constrs isFree
ms2vcs s = case Map.member s stringMap of
True -> Map.findWithDefault "" s stringMap
False -> let f = \ x y -> if Map.member x charMap
stringMap :: Map.Map String String
stringMap = Map.fromList