| 1 | {- | |
|---|
| 2 | Module : $Header$ |
|---|
| 3 | Description : embedding from CASL (CFOL) to Isabelle-HOL |
|---|
| 4 | Copyright : (c) Till Mossakowski and Uni Bremen 2003-2005 |
|---|
| 5 | License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt |
|---|
| 6 | |
|---|
| 7 | Maintainer : Christian.Maeder@dfki.de |
|---|
| 8 | Stability : provisional |
|---|
| 9 | Portability : non-portable (imports Logic.Logic) |
|---|
| 10 | |
|---|
| 11 | The embedding comorphism from CASL to Isabelle-HOL. |
|---|
| 12 | -} |
|---|
| 13 | |
|---|
| 14 | module Comorphisms.CFOL2IsabelleHOL |
|---|
| 15 | ( CFOL2IsabelleHOL(..) |
|---|
| 16 | , transTheory |
|---|
| 17 | , typeToks |
|---|
| 18 | , mapSen |
|---|
| 19 | , IsaTheory |
|---|
| 20 | , SignTranslator |
|---|
| 21 | , FormulaTranslator |
|---|
| 22 | , quantifyIsa |
|---|
| 23 | , quantify |
|---|
| 24 | , transSort |
|---|
| 25 | , transOP_SYMB |
|---|
| 26 | ) where |
|---|
| 27 | |
|---|
| 28 | import Logic.Logic |
|---|
| 29 | import Logic.Comorphism |
|---|
| 30 | |
|---|
| 31 | import CASL.Logic_CASL |
|---|
| 32 | import CASL.AS_Basic_CASL |
|---|
| 33 | import CASL.Sublogic as SL |
|---|
| 34 | import CASL.Sign |
|---|
| 35 | import CASL.Morphism |
|---|
| 36 | import CASL.Quantification |
|---|
| 37 | import CASL.Fold |
|---|
| 38 | import CASL.Induction |
|---|
| 39 | |
|---|
| 40 | import Isabelle.IsaSign as IsaSign |
|---|
| 41 | import Isabelle.IsaConsts |
|---|
| 42 | import Isabelle.Logic_Isabelle |
|---|
| 43 | import Isabelle.Translate |
|---|
| 44 | |
|---|
| 45 | import Common.AS_Annotation |
|---|
| 46 | import Common.DocUtils |
|---|
| 47 | import Common.Id |
|---|
| 48 | import Common.ProofTree |
|---|
| 49 | import Common.Result |
|---|
| 50 | import Common.Utils (nubOrdOn) |
|---|
| 51 | |
|---|
| 52 | import qualified Data.Map as Map |
|---|
| 53 | import qualified Data.Set as Set |
|---|
| 54 | import qualified Data.List as List |
|---|
| 55 | |
|---|
| 56 | isSingleton :: Set.Set a -> Bool |
|---|
| 57 | isSingleton s = Set.size s == 1 |
|---|
| 58 | |
|---|
| 59 | -- | The identity of the comorphism |
|---|
| 60 | data CFOL2IsabelleHOL = CFOL2IsabelleHOL deriving Show |
|---|
| 61 | |
|---|
| 62 | -- Isabelle theories |
|---|
| 63 | type IsaTheory = (IsaSign.Sign, [Named IsaSign.Sentence]) |
|---|
| 64 | |
|---|
| 65 | -- extended signature translation |
|---|
| 66 | type SignTranslator f e = CASL.Sign.Sign f e -> e -> IsaTheory -> IsaTheory |
|---|
| 67 | |
|---|
| 68 | -- extended signature translation for CASL |
|---|
| 69 | sigTrCASL :: SignTranslator () () |
|---|
| 70 | sigTrCASL _ _ = id |
|---|
| 71 | |
|---|
| 72 | -- extended formula translation |
|---|
| 73 | type FormulaTranslator f e = CASL.Sign.Sign f e -> Set.Set String -> f -> Term |
|---|
| 74 | |
|---|
| 75 | -- extended formula translation for CASL |
|---|
| 76 | formTrCASL :: FormulaTranslator () () |
|---|
| 77 | formTrCASL _ _ = error "CFOL2IsabelleHOL: No extended formulas allowed in CASL" |
|---|
| 78 | |
|---|
| 79 | instance Language CFOL2IsabelleHOL where |
|---|
| 80 | language_name CFOL2IsabelleHOL = "CASL2Isabelle" |
|---|
| 81 | |
|---|
| 82 | instance Comorphism CFOL2IsabelleHOL |
|---|
| 83 | CASL CASL_Sublogics |
|---|
| 84 | CASLBasicSpec CASLFORMULA SYMB_ITEMS SYMB_MAP_ITEMS |
|---|
| 85 | CASLSign |
|---|
| 86 | CASLMor |
|---|
| 87 | Symbol RawSymbol ProofTree |
|---|
| 88 | Isabelle () () IsaSign.Sentence () () |
|---|
| 89 | IsaSign.Sign |
|---|
| 90 | IsabelleMorphism () () () where |
|---|
| 91 | sourceLogic CFOL2IsabelleHOL = CASL |
|---|
| 92 | sourceSublogic CFOL2IsabelleHOL = SL.cFol |
|---|
| 93 | targetLogic CFOL2IsabelleHOL = Isabelle |
|---|
| 94 | mapSublogic cid sl = if sl `isSubElem` sourceSublogic cid |
|---|
| 95 | then Just () else Nothing |
|---|
| 96 | map_theory CFOL2IsabelleHOL = transTheory sigTrCASL formTrCASL |
|---|
| 97 | map_morphism = mapDefaultMorphism |
|---|
| 98 | map_sentence CFOL2IsabelleHOL sign = |
|---|
| 99 | return . mapSen formTrCASL sign (typeToks sign) |
|---|
| 100 | has_model_expansion CFOL2IsabelleHOL = True |
|---|
| 101 | is_weakly_amalgamable CFOL2IsabelleHOL = True |
|---|
| 102 | isInclusionComorphism CFOL2IsabelleHOL = True |
|---|
| 103 | |
|---|
| 104 | ---------------------------- Signature ----------------------------- |
|---|
| 105 | baseSign :: BaseSig |
|---|
| 106 | baseSign = Main_thy |
|---|
| 107 | |
|---|
| 108 | typeToks :: CASL.Sign.Sign f e -> Set.Set String |
|---|
| 109 | typeToks = Set.map (\ s -> showIsaTypeT s baseSign) . sortSet |
|---|
| 110 | |
|---|
| 111 | transTheory :: Pretty f => SignTranslator f e -> |
|---|
| 112 | FormulaTranslator f e -> |
|---|
| 113 | (CASL.Sign.Sign f e, [Named (FORMULA f)]) |
|---|
| 114 | -> Result IsaTheory |
|---|
| 115 | transTheory trSig trForm (sign, sens) = do |
|---|
| 116 | gens <- |
|---|
| 117 | mapM (\ (Sort_gen_ax constr False) -> inductionScheme constr) genTypes |
|---|
| 118 | fmap (trSig sign (extendedInfo sign)) $ return (IsaSign.emptySign { |
|---|
| 119 | baseSig = baseSign, |
|---|
| 120 | tsig = emptyTypeSig {arities = |
|---|
| 121 | Set.fold (\s -> let s1 = showIsaTypeT s baseSign in |
|---|
| 122 | Map.insert s1 [(isaTerm, [])]) |
|---|
| 123 | Map.empty (sortSet sign)}, |
|---|
| 124 | constTab = Map.foldWithKey insertPreds |
|---|
| 125 | (Map.foldWithKey insertOps Map.empty |
|---|
| 126 | $ opMap sign) $ predMap sign, |
|---|
| 127 | domainTab = dtDefs}, |
|---|
| 128 | zipWith (\ n -> makeNamed ("ga_induction_" ++ show n) . myMapSen) |
|---|
| 129 | [1 :: Int ..] gens ++ |
|---|
| 130 | map (mapNamed myMapSen) real_sens) |
|---|
| 131 | -- for now, no new sentences |
|---|
| 132 | where |
|---|
| 133 | tyToks = typeToks sign |
|---|
| 134 | myMapSen = mkSen . transFORMULA sign tyToks trForm (getAssumpsToks sign) |
|---|
| 135 | (real_sens, sort_gen_axs) = List.partition |
|---|
| 136 | (\ s -> case sentence s of |
|---|
| 137 | Sort_gen_ax _ _ -> False |
|---|
| 138 | _ -> True) sens |
|---|
| 139 | unique_sort_gen_axs = nubOrdOn (\ (Sort_gen_ax cs _) -> |
|---|
| 140 | Set.fromList $ map newSort cs) |
|---|
| 141 | $ map sentence sort_gen_axs |
|---|
| 142 | (freeTypes, genTypes) = List.partition (\ (Sort_gen_ax _ b) -> b) |
|---|
| 143 | $ unique_sort_gen_axs |
|---|
| 144 | dtDefs = makeDtDefs sign tyToks freeTypes |
|---|
| 145 | ga = globAnnos sign |
|---|
| 146 | insertOps op ts m = if isSingleton ts then |
|---|
| 147 | let t = Set.findMin ts in Map.insert |
|---|
| 148 | (mkIsaConstT False ga (length $ opArgs t) op baseSign tyToks) |
|---|
| 149 | (transOpType t) m |
|---|
| 150 | else foldl (\ m1 (t, i) -> Map.insert |
|---|
| 151 | (mkIsaConstIT False ga (length $ opArgs t) op i baseSign tyToks) |
|---|
| 152 | (transOpType t) m1) m $ zip (Set.toList ts) [1..] |
|---|
| 153 | insertPreds pre ts m = if isSingleton ts then |
|---|
| 154 | let t = Set.findMin ts in Map.insert |
|---|
| 155 | (mkIsaConstT True ga (length $ predArgs t) pre baseSign tyToks) |
|---|
| 156 | (transPredType t) m |
|---|
| 157 | else foldl (\ m1 (t, i) -> Map.insert |
|---|
| 158 | (mkIsaConstIT True ga (length $ predArgs t) pre i baseSign tyToks) |
|---|
| 159 | (transPredType t) m1) m $ zip (Set.toList ts) [1..] |
|---|
| 160 | |
|---|
| 161 | makeDtDefs :: CASL.Sign.Sign f e -> Set.Set String -> [FORMULA f] |
|---|
| 162 | -> [[(Typ,[(VName,[Typ])])]] |
|---|
| 163 | makeDtDefs sign = map . makeDtDef sign |
|---|
| 164 | |
|---|
| 165 | makeDtDef :: CASL.Sign.Sign f e -> Set.Set String -> FORMULA f |
|---|
| 166 | -> [(Typ,[(VName,[Typ])])] |
|---|
| 167 | makeDtDef sign tyToks nf = case nf of |
|---|
| 168 | Sort_gen_ax constrs True -> map makeDt srts where |
|---|
| 169 | (srts,ops,_maps) = recover_Sort_gen_ax constrs |
|---|
| 170 | makeDt s = (transSort s, map makeOp (filter (hasTheSort s) ops)) |
|---|
| 171 | makeOp opSym = (transOP_SYMB sign tyToks opSym, transArgs opSym) |
|---|
| 172 | hasTheSort s (Qual_op_name _ ot _) = s == res_OP_TYPE ot |
|---|
| 173 | hasTheSort _ _ = error "CFOL2IsabelleHOL.hasTheSort" |
|---|
| 174 | transArgs (Qual_op_name _ ot _) = map transSort $ args_OP_TYPE ot |
|---|
| 175 | transArgs _ = error "CFOL2IsabelleHOL.transArgs" |
|---|
| 176 | _ -> error "CFOL2IsabelleHOL.makeDtDef" |
|---|
| 177 | |
|---|
| 178 | transSort :: SORT -> Typ |
|---|
| 179 | transSort s = Type (showIsaTypeT s baseSign) [] [] |
|---|
| 180 | |
|---|
| 181 | transOpType :: OpType -> Typ |
|---|
| 182 | transOpType ot = mkCurryFunType (map transSort $ opArgs ot) |
|---|
| 183 | $ transSort (opRes ot) |
|---|
| 184 | |
|---|
| 185 | transPredType :: PredType -> Typ |
|---|
| 186 | transPredType pt = mkCurryFunType (map transSort $ predArgs pt) boolType |
|---|
| 187 | |
|---|
| 188 | ------------------------------ Formulas ------------------------------ |
|---|
| 189 | |
|---|
| 190 | getAssumpsToks :: CASL.Sign.Sign f e -> Set.Set String |
|---|
| 191 | getAssumpsToks sign = Map.foldWithKey ( \ i ops s -> |
|---|
| 192 | Set.union s $ Set.unions |
|---|
| 193 | $ zipWith ( \ o _ -> getConstIsaToks i o baseSign) [1..] |
|---|
| 194 | $ Set.toList ops) |
|---|
| 195 | (Map.foldWithKey ( \ i prds s -> |
|---|
| 196 | Set.union s $ Set.unions |
|---|
| 197 | $ zipWith ( \ o _ -> getConstIsaToks i o baseSign) [1..] |
|---|
| 198 | $ Set.toList prds) Set.empty $ predMap sign) $ opMap sign |
|---|
| 199 | |
|---|
| 200 | transVar :: Set.Set String -> VAR -> String |
|---|
| 201 | transVar toks v = let |
|---|
| 202 | s = showIsaConstT (simpleIdToId v) baseSign |
|---|
| 203 | renVar t = if Set.member t toks then renVar $ "X_" ++ t else t |
|---|
| 204 | in renVar s |
|---|
| 205 | |
|---|
| 206 | quantifyIsa :: String -> (String, Typ) -> Term -> Term |
|---|
| 207 | quantifyIsa q (v, _) phi = |
|---|
| 208 | termAppl (conDouble q) (Abs (mkFree v) phi NotCont) |
|---|
| 209 | |
|---|
| 210 | quantify :: Set.Set String -> QUANTIFIER -> (VAR, SORT) -> Term -> Term |
|---|
| 211 | quantify toks q (v,t) phi = |
|---|
| 212 | quantifyIsa (qname q) (transVar toks v, transSort t) phi |
|---|
| 213 | where |
|---|
| 214 | qname Universal = allS |
|---|
| 215 | qname Existential = exS |
|---|
| 216 | qname Unique_existential = ex1S |
|---|
| 217 | |
|---|
| 218 | transOP_SYMB :: CASL.Sign.Sign f e -> Set.Set String -> OP_SYMB -> VName |
|---|
| 219 | transOP_SYMB sign tyToks (Qual_op_name op ot _) = let |
|---|
| 220 | ga = globAnnos sign |
|---|
| 221 | l = length $ args_OP_TYPE ot in |
|---|
| 222 | case (do ots <- Map.lookup op (opMap sign) |
|---|
| 223 | if isSingleton ots |
|---|
| 224 | then return $ mkIsaConstT False ga l op baseSign tyToks |
|---|
| 225 | else do |
|---|
| 226 | i <- List.elemIndex (toOpType ot) (Set.toList ots) |
|---|
| 227 | return $ mkIsaConstIT False ga l op (i+1) baseSign tyToks) of |
|---|
| 228 | Just vn -> vn |
|---|
| 229 | Nothing -> error ("CASL2Isabelle unknown op: " ++ show op) |
|---|
| 230 | transOP_SYMB _ _ (Op_name _) = error "CASL2Isabelle: unqualified operation" |
|---|
| 231 | |
|---|
| 232 | transPRED_SYMB :: CASL.Sign.Sign f e -> Set.Set String -> PRED_SYMB -> VName |
|---|
| 233 | transPRED_SYMB sign tyToks (Qual_pred_name p pt@(Pred_type args _) _) = let |
|---|
| 234 | ga = globAnnos sign |
|---|
| 235 | l = length args in |
|---|
| 236 | case (do pts <- Map.lookup p (predMap sign) |
|---|
| 237 | if isSingleton pts |
|---|
| 238 | then return $ mkIsaConstT True ga l p baseSign tyToks |
|---|
| 239 | else do |
|---|
| 240 | i <- List.elemIndex (toPredType pt) (Set.toList pts) |
|---|
| 241 | return $ mkIsaConstIT True ga l p (i+1) baseSign tyToks) of |
|---|
| 242 | Just vn -> vn |
|---|
| 243 | Nothing -> mkIsaConstT True ga (-1) p baseSign tyToks |
|---|
| 244 | -- for predicate names in induction schemes |
|---|
| 245 | transPRED_SYMB _ _ (Pred_name _) = error "CASL2Isabelle: unqualified predicate" |
|---|
| 246 | |
|---|
| 247 | mapSen :: FormulaTranslator f e -> CASL.Sign.Sign f e -> Set.Set String |
|---|
| 248 | -> FORMULA f -> Sentence |
|---|
| 249 | mapSen trForm sign tyToks phi = |
|---|
| 250 | mkSen $ transFORMULA sign tyToks trForm (getAssumpsToks sign) phi |
|---|
| 251 | |
|---|
| 252 | transRecord :: CASL.Sign.Sign f e -> Set.Set String -> FormulaTranslator f e |
|---|
| 253 | -> Set.Set String -> Record f Term Term |
|---|
| 254 | transRecord sign tyToks tr toks = Record |
|---|
| 255 | { foldQuantification = \ _ qu vdecl phi _ -> |
|---|
| 256 | foldr (quantify toks qu) phi (flatVAR_DECLs vdecl) |
|---|
| 257 | , foldConjunction = \ _ phis _ -> |
|---|
| 258 | if null phis then true else foldr1 binConj phis |
|---|
| 259 | , foldDisjunction = \ _ phis _ -> |
|---|
| 260 | if null phis then false else foldr1 binDisj phis |
|---|
| 261 | , foldImplication = \ _ phi1 phi2 _ _ -> binImpl phi1 phi2 |
|---|
| 262 | , foldEquivalence = \ _ phi1 phi2 _ -> binEqv phi1 phi2 |
|---|
| 263 | , foldNegation = \ _ phi _ -> termAppl notOp phi |
|---|
| 264 | , foldTrue_atom = \ _ _ -> true |
|---|
| 265 | , foldFalse_atom = \ _ _ -> false |
|---|
| 266 | , foldPredication = \ _ psymb args _ -> |
|---|
| 267 | foldl termAppl (con $ transPRED_SYMB sign tyToks psymb) args |
|---|
| 268 | , foldDefinedness = \ _ _ _ -> true -- totality assumed |
|---|
| 269 | , foldExistl_equation = \ _ t1 t2 _ -> binEq t1 t2 -- equal types assumed |
|---|
| 270 | , foldStrong_equation = \ _ t1 t2 _ -> binEq t1 t2 -- equal types assumed |
|---|
| 271 | , foldMembership = \ _ _ _ _ -> true -- no subsorting assumed |
|---|
| 272 | , foldMixfix_formula = error "transRecord: Mixfix_formula" |
|---|
| 273 | , foldSort_gen_ax = error "transRecord: Sort_gen_ax" |
|---|
| 274 | , foldExtFORMULA = \ _ phi -> tr sign tyToks phi |
|---|
| 275 | , foldQual_var = \ _ v _ _ -> mkFree $ transVar toks v |
|---|
| 276 | , foldApplication = \ _ opsymb args _ -> |
|---|
| 277 | foldl termAppl (con $ transOP_SYMB sign tyToks opsymb) args |
|---|
| 278 | , foldSorted_term = \ _ t _ _ -> t -- no subsorting assumed |
|---|
| 279 | , foldCast = \ _ t _ _ -> t -- no subsorting assumed |
|---|
| 280 | , foldConditional = \ _ t1 phi t2 _ -> -- equal types assumed |
|---|
| 281 | If phi t1 t2 NotCont |
|---|
| 282 | , foldMixfix_qual_pred = error "transRecord: Mixfix_qual_pred" |
|---|
| 283 | , foldMixfix_term = error "transRecord: Mixfix_term" |
|---|
| 284 | , foldMixfix_token = error "transRecord: Mixfix_token" |
|---|
| 285 | , foldMixfix_sorted_term = error "transRecord: Mixfix_sorted_term" |
|---|
| 286 | , foldMixfix_cast = error "transRecord: Mixfix_cast" |
|---|
| 287 | , foldMixfix_parenthesized = error "transRecord: Mixfix_parenthesized" |
|---|
| 288 | , foldMixfix_bracketed = error "transRecord: Mixfix_bracketed" |
|---|
| 289 | , foldMixfix_braced = error "transRecord: Mixfix_braced" |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | transFORMULA :: CASL.Sign.Sign f e -> Set.Set String -> FormulaTranslator f e |
|---|
| 293 | -> Set.Set String -> FORMULA f -> Term |
|---|
| 294 | transFORMULA sign tyToks tr = foldFormula . transRecord sign tyToks tr |
|---|