root/trunk/Comorphisms/CFOL2IsabelleHOL.hs @ 11215

Revision 11215, 11.6 kB (checked in by csliam, 11 months ago)

Large commit - improved static analysis of cspcasl and translation of cspcasl to isabelle

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