root/trunk/Comorphisms/CspCASL2IsabelleHOL.hs @ 10619

Revision 10619, 37.1 kB (checked in by csliam, 15 months ago)

Added production of the first intergation theorem and continued work on
process translation

Line 
1{- |
2Module      :  $Header$
3Description :  embedding from CspCASL to Isabelle-HOL
4Copyright   :  (c) Andy Gimblett, Liam O'Reilly and Markus Roggenbach,
5                   Swansea University 2008
6License     :  similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
7
8Maintainer  :  csliam@swansea.ac.uk
9Stability   :  provisional
10Portability :  non-portable (imports Logic.Logic)
11
12The comorphism from CspCASL to Isabelle-HOL.
13-}
14
15module Comorphisms.CspCASL2IsabelleHOL (
16   CspCASL2IsabelleHOL(..)
17) where
18
19import CASL.AS_Basic_CASL
20import qualified CASL.Inject as CASLInject
21import qualified CASL.Sign as CASLSign
22import Common.AS_Annotation
23import Common.Result
24import qualified Common.Lib.Rel as Rel
25import qualified Comorphisms.CASL2PCFOL as CASL2PCFOL
26import qualified Comorphisms.CASL2SubCFOL as CASL2SubCFOL
27import qualified Comorphisms.CFOL2IsabelleHOL as CFOL2IsabelleHOL
28import Comorphisms.CFOL2IsabelleHOL(IsaTheory)
29import CspCASL.Logic_CspCASL
30import CspCASL.AS_CspCASL
31import CspCASL.Trans_CspProver
32import CspCASL.Trans_Consts
33import CspCASL.SignCSP
34import qualified Data.List as List
35import qualified Data.Set as Set
36import qualified Data.Map as Map
37import Isabelle.IsaConsts as IsaConsts
38import Isabelle.IsaSign as IsaSign
39import Isabelle.Logic_Isabelle
40import Logic.Logic
41import Logic.Comorphism
42
43-- | The identity of the comorphism
44data CspCASL2IsabelleHOL = CspCASL2IsabelleHOL deriving Show
45
46instance Language CspCASL2IsabelleHOL where
47  language_name CspCASL2IsabelleHOL = "CspCASL2Isabelle"
48
49instance Comorphism CspCASL2IsabelleHOL
50               CspCASL ()
51               CspBasicSpec CspCASLSentence SYMB_ITEMS SYMB_MAP_ITEMS
52               CspCASLSign
53               CspMorphism
54               () () ()
55               Isabelle () () IsaSign.Sentence () ()
56               IsaSign.Sign
57               IsabelleMorphism () () ()  where
58    sourceLogic CspCASL2IsabelleHOL = CspCASL
59    sourceSublogic CspCASL2IsabelleHOL = ()
60    targetLogic CspCASL2IsabelleHOL = Isabelle
61    mapSublogic _cid _sl = Just ()
62
63    map_theory CspCASL2IsabelleHOL = transCCTheory
64    map_morphism = mapDefaultMorphism
65    map_sentence CspCASL2IsabelleHOL sign = transCCSentence sign
66    has_model_expansion CspCASL2IsabelleHOL = False
67    is_weakly_amalgamable CspCASL2IsabelleHOL = False
68    isInclusionComorphism CspCASL2IsabelleHOL = True
69
70-- Functions for translating CspCasl theories and sentences to IsabelleHOL
71
72-- | Translate CspCASL theories to Isabelle
73transCCTheory :: (CspCASLSign, [Named CspCASLSentence]) -> Result IsaTheory
74transCCTheory ccTheory =
75    let ccSign = fst ccTheory
76        ccSens = snd ccTheory
77        caslSign = ccSig2CASLSign ccSign
78        casl2pcfol = (map_theory CASL2PCFOL.CASL2PCFOL)
79        pcfol2cfol = (map_theory CASL2SubCFOL.defaultCASL2SubCFOL)
80        cfol2isabelleHol = (map_theory CFOL2IsabelleHOL.CFOL2IsabelleHOL)
81        sortList = Set.toList(CASLSign.sortSet caslSign)
82        --fakeType = Type {typeId = "Fake_Type" , typeSort = [], typeArgs =[]}
83    in do -- Remove Subsorting from the CASL part of the CspCASL specification
84          translation1 <- casl2pcfol (caslSign,[])
85          -- Next Remove partial functions
86          translation2 <- pcfol2cfol translation1
87          -- Next Translate to IsabelleHOL code
88          translation3 <- cfol2isabelleHol translation2
89          -- Next add the preAlpabet construction to the IsabelleHOL code
90          return $ addCspCaslSentences ccSens
91                 $ addAllIntegrationTheorems sortList ccSign
92                 $ addAllChooseFunctions sortList
93                 $ addAllBarTypes sortList
94                 $ addAlphabetType
95                 $ addInstansanceOfEquiv
96                 $ addJustificationTheorems ccSign (fst translation1)
97                 $ addEqFun sortList
98                 $ addAllCompareWithFun ccSign
99                 $ addPreAlphabet sortList
100                 $ addWarning
101                 $ translation3
102
103-- This is not implemented in a sensible way yet and is not used
104transCCSentence :: CspCASLSign -> CspCASLSentence -> Result IsaSign.Sentence
105transCCSentence _ (CspCASLSentence pn _ _) =
106    do return (mkSen (Const (mkVName (show pn))
107                                (Disp (Type "byeWorld" [] []) TFun Nothing)))
108
109-- | Add a list of CspCASL sentences to an Isabelle theory
110addCspCaslSentences :: [Named CspCASLSentence] -> IsaTheory -> IsaTheory
111addCspCaslSentences namedSens isaTh =
112    -- Create datatype for process names
113    -- crate constant
114    -- create primrec
115      -- create list of equations -- one for each sentence
116    foldl addCspCaslSentence isaTh namedSens
117
118-- | Add a single CspCASL sentence to an Isabelle theory
119addCspCaslSentence :: IsaTheory -> Named CspCASLSentence -> IsaTheory
120addCspCaslSentence isaTh namedSen =
121    let sen = sentence namedSen
122    in case sen of
123         CspCASLSentence pn _ proc ->
124             let name = show pn
125                 t1 = conDouble name
126                 t2 = transProcess proc
127             in addDef name t1 t2 isaTh
128
129-- Functions for adding the PreAlphabet datatype to an Isabelle theory
130
131-- | Add the PreAlphabet (built from a list of sorts) to an Isabelle theory
132addPreAlphabet :: [SORT] -> IsaTheory -> IsaTheory
133addPreAlphabet sortList isaTh =
134    let preAlphabetDomainEntry = mkPreAlphabetDE sortList
135    in updateDomainTab preAlphabetDomainEntry isaTh
136
137-- | Make a PreAlphabet Domain Entry from a list of sorts
138mkPreAlphabetDE :: [SORT] -> DomainEntry
139mkPreAlphabetDE sorts =
140    (Type {typeId = preAlphabetS, typeSort = [isaTerm], typeArgs = []},
141         map (\sort ->
142                  (mkVName (mkPreAlphabetConstructor sort),
143                               [Type {typeId = convertSort2String sort,
144                                               typeSort = [isaTerm],
145                                                          typeArgs = []}])
146             ) sorts
147    )
148
149-- Functions for adding the eq functions and the compare_with
150-- functions to an Isabelle theory
151
152-- | Add the eq function to an Isabelle theory using a list of sorts
153addEqFun :: [SORT] -> IsaTheory -> IsaTheory
154addEqFun sortList isaTh =
155    let preAlphabetType = Type {typeId = preAlphabetS,
156                                typeSort = [],
157                                typeArgs =[]}
158        eqtype = mkFunType preAlphabetType $ mkFunType preAlphabetType boolType
159        isaThWithConst = addConst eq_PreAlphabetS eqtype isaTh
160        mkEq sort =
161            let x = mkFree "x"
162                y = mkFree "y"
163                lhs = binEq_PreAlphabet lhs_a y
164                lhs_a = termAppl (conDouble (mkPreAlphabetConstructor sort)) x
165                rhs = termAppl rhs_a y
166                rhs_a = termAppl (conDouble (mkCompareWithFunName sort)) x
167            in binEq lhs rhs
168        eqs = map mkEq sortList
169    in addPrimRec eqs isaThWithConst
170
171-- | Add all compare_with functions for a given list of sorts to an
172--   Isabelle theory. We need to know about the casl signature so that
173--   we can pass it on to the addCompareWithFun function
174addAllCompareWithFun :: CspCASLSign -> IsaTheory -> IsaTheory
175addAllCompareWithFun ccSign isaTh =
176    let sortList = Set.toList(CASLSign.sortSet ccSign)
177    in  foldl (addCompareWithFun ccSign) isaTh sortList
178
179-- | Add a single compare_with function for a given sort to an
180--   Isabelle theory.  We need to know about the casl signature to
181--   work out the RHS of the equations.
182addCompareWithFun :: CspCASLSign -> IsaTheory -> SORT -> IsaTheory
183addCompareWithFun ccSign isaTh sort =
184    let sortList = Set.toList(CASLSign.sortSet ccSign)
185        preAlphabetType = Type {typeId = preAlphabetS,
186                                typeSort = [],
187                                typeArgs =[]}
188        sortType = Type {typeId = convertSort2String sort, typeSort = [],
189                                  typeArgs =[]}
190        funName = mkCompareWithFunName sort
191        funType = mkFunType sortType $ mkFunType preAlphabetType boolType
192        isaThWithConst = addConst funName funType isaTh
193        sortSuperSet = CASLSign.supersortsOf sort ccSign
194        mkEq sort' =
195            let x = mkFree "x"
196                y = mkFree "y"
197                sort'Constructor = mkPreAlphabetConstructor sort'
198                lhs = termAppl lhs_a lhs_b
199                lhs_a = termAppl (conDouble funName) x
200                lhs_b = termAppl (conDouble (sort'Constructor)) y
201                sort'SuperSet =CASLSign.supersortsOf sort' ccSign
202                commonSuperList = Set.toList (Set.intersection
203                                                 sortSuperSet
204                                                 sort'SuperSet)
205
206                -- If there are no tests then the rhs=false else
207                -- combine all tests using binConj
208                rhs = if (null allTests)
209                      then false
210                      else foldr1 binConj allTests
211
212                -- The tests produce a list of equations for each test
213                -- Test 1 =  test equality at: current sort vs current sort
214                -- Test 2 =  test equality at: current sort vs super sorts
215                -- Test 3 =  test equality at: super sorts  vs current sort
216                -- Test 4 =  test equality at: super sorts  vs super sorts
217                allTests = concat [test1, test2, test3, test4]
218                test1 = if (sort == sort') then [binEq x y] else []
219                test2 = if (Set.member sort sort'SuperSet)
220                        then [binEq x (mkInjection sort' sort y)]
221                        else []
222                test3 = if (Set.member sort' sortSuperSet)
223                        then [binEq (mkInjection sort sort' x) y]
224                        else []
225                test4 = if (null commonSuperList)
226                        then []
227                        else map test4_atSort commonSuperList
228                test4_atSort s = binEq (mkInjection sort  s x)
229                                       (mkInjection sort' s y)
230            in binEq lhs rhs
231        eqs = map mkEq sortList
232    in  addPrimRec eqs isaThWithConst
233
234-- Functions for producing the Justification theorems
235
236-- | Add all justification theorems to an Isabelle Theory. We need to
237--   the CspCASL signature and the PFOL Signature to pass it on. We
238--   could recalculate the PFOL signature from the CspCASL signature
239--   here, but we dont as it can be passed in.
240addJustificationTheorems :: CspCASLSign -> CASLSign.CASLSign ->
241                            IsaTheory -> IsaTheory
242addJustificationTheorems ccSign pfolSign isaTh =
243    let sortRel = Rel.toList(CASLSign.sortRel ccSign)
244        sorts = Set.toList(CASLSign.sortSet ccSign)
245    in   addTransitivityTheorem sorts sortRel
246       $ addAllInjectivityTheorems pfolSign sorts sortRel
247       $ addAllDecompositionTheorem pfolSign sorts sortRel
248       $ addSymmetryTheorem sorts
249       $ addReflexivityTheorem
250       $ isaTh
251
252-- | Add the reflexivity theorem and proof to an Isabelle Theory
253addReflexivityTheorem :: IsaTheory -> IsaTheory
254addReflexivityTheorem isaTh =
255    let name = reflexivityTheoremS
256        x = mkFree "x"
257        thmConds = []
258        thmConcl = binEq_PreAlphabet x x
259        proof' = IsaProof {
260                   proof = [Apply (Induct x),
261                            Apply Auto],
262                   end = Done
263                 }
264    in addThreomWithProof name thmConds thmConcl proof' isaTh
265
266-- | Add the symmetry theorem and proof to an Isabelle Theory We need
267--   to know the number of sorts, but instead we are given a list of
268--   all sorts
269addSymmetryTheorem :: [SORT] -> IsaTheory -> IsaTheory
270addSymmetryTheorem sorts isaTh =
271    let numSorts = length(sorts)
272        name = symmetryTheoremS
273        x = mkFree "x"
274        y = mkFree "y"
275        thmConds = [binEq_PreAlphabet x y]
276        thmConcl = binEq_PreAlphabet y x
277        inductY = concat $ map (\i -> [Prefer (i*numSorts+1),
278                                       Apply (Induct y)])
279                    [0..(numSorts-1)]
280        proof' = IsaProof {
281                   proof = [Apply (Induct x)] ++ inductY ++ [Apply Auto],
282                   end = Done
283                 }
284    in addThreomWithProof name thmConds thmConcl proof' isaTh
285
286-- | Add all the injectivity theorems and proofs
287addAllInjectivityTheorems :: CASLSign.CASLSign -> [SORT] -> [(SORT,SORT)] ->
288                             IsaTheory -> IsaTheory
289addAllInjectivityTheorems pfolSign sorts sortRel isaTh =
290    foldl (addInjectivityTheorem pfolSign sorts sortRel) isaTh sortRel
291
292-- | Add the injectivity theorem and proof which should be deduced
293--   from the embedding_Injectivity axioms from the translation
294--   CASL2PCFOL; CASL2SubCFOL for a single injection represented as a
295--   pair of sorts. As a work around, we need to know all sorts to
296--   pass them on
297addInjectivityTheorem :: CASLSign.CASLSign -> [SORT] -> [(SORT,SORT)] ->
298                         IsaTheory -> (SORT,SORT) -> IsaTheory
299addInjectivityTheorem pfolSign sorts sortRel isaTh (s1,s2) =
300    let x = mkFree "x"
301        y = mkFree "y"
302        injX = mkInjection s1 s2 x
303        injY = mkInjection s1 s2 y
304        definedOp_s1 = getDefinedOp sorts s1
305        definedOp_s2 = getDefinedOp sorts s2
306        collectionEmbInjAx = getCollectionEmbInjAx sortRel
307        collectionTotAx = getCollectionTotAx pfolSign
308        collectionNotDefBotAx = getCollectionNotDefBotAx sorts
309        name = getInjectivityThmName(s1, s2)
310        conds = [binEq injX injY]
311        concl = binEq x y
312        proof' = IsaProof [Apply(CaseTac (definedOp_s2 injX)),
313                           -- Case 1
314                           Apply(SubgoalTac(definedOp_s1 x)),
315                           Apply(SubgoalTac(definedOp_s1 y)),
316                           Apply(Insert collectionEmbInjAx),
317                           Apply(Simp),
318                           Apply(SimpAdd Nothing collectionTotAx),
319                           Apply(SimpAdd (Just No_asm_use) collectionTotAx),
320                           -- Case 2
321                           Apply(SubgoalTac(termAppl notOp (definedOp_s1 x))),
322                           Apply(SubgoalTac(termAppl notOp(definedOp_s1 y))),
323                           Apply(SimpAdd Nothing collectionNotDefBotAx),
324                           Apply(SimpAdd Nothing collectionTotAx),
325                           Apply(SimpAdd (Just No_asm_use) collectionTotAx)]
326                           Done
327    in addThreomWithProof name conds concl proof' isaTh
328
329-- | Add all the decoposition theorems and proofs
330addAllDecompositionTheorem :: CASLSign.CASLSign -> [SORT] -> [(SORT,SORT)] ->
331                             IsaTheory -> IsaTheory
332addAllDecompositionTheorem pfolSign sorts sortRel isaTh =
333    let tripples = [(s1,s2,s3) |
334                    (s1,s2) <- sortRel, (s2',s3) <- sortRel, s2==s2']
335    in foldl (addDecompositionTheorem pfolSign sorts sortRel) isaTh tripples
336
337-- | Add the decomposition theorem and proof which should be deduced
338--   from the transitivity axioms from the translation CASL2PCFOL;
339--   CASL2SubCFOL for a pair of injections represented as a tripple of
340--   sorts. e.g. (S,T,U) means produce the lemma and proof for
341--   inj_T_U(inj_S_T(x)) = inj_S_U(x). As a work around, we need to
342--   know all sorts to pass them on.
343addDecompositionTheorem :: CASLSign.CASLSign -> [SORT] -> [(SORT,SORT)] ->
344                         IsaTheory -> (SORT,SORT,SORT) -> IsaTheory
345addDecompositionTheorem pfolSign sorts sortRel isaTh (s1,s2,s3) =
346    let x = mkFree "x"
347        -- These 5 lines make use of currying
348        injOp_s1_s2 = mkInjection s1 s2
349        injOp_s2_s3 = mkInjection s2 s3
350        injOp_s1_s3 = mkInjection s1 s3
351        inj_s1_s2_s3_x = injOp_s2_s3 (injOp_s1_s2 x)
352        inj_s1_s3_x = injOp_s1_s3 x
353
354        definedOp_s1 = getDefinedOp sorts s1
355        definedOp_s3 = getDefinedOp sorts s3
356        collectionTransAx = getCollectionTransAx sortRel
357        collectionTotAx = getCollectionTotAx pfolSign
358        collectionNotDefBotAx = getCollectionNotDefBotAx sorts
359
360        name = getDecompositionThmName(s1, s2, s3)
361        conds = []
362        concl = binEq inj_s1_s2_s3_x inj_s1_s3_x
363
364        proof' = IsaProof [Apply(CaseTac (definedOp_s3 inj_s1_s2_s3_x)),
365                           -- Case 1
366                           Apply(SubgoalTac(definedOp_s1 x)),
367                           Apply(Insert collectionTransAx),
368                           Apply(Simp),
369                           Apply(SimpAdd Nothing collectionTotAx),
370
371                           -- Case 2
372                           Apply(SubgoalTac(
373                               termAppl notOp(definedOp_s3 inj_s1_s3_x))),
374                           Apply(SimpAdd Nothing collectionNotDefBotAx),
375                           Apply(SimpAdd Nothing collectionTotAx)]
376                           Done
377    in addThreomWithProof name conds concl proof' isaTh
378
379
380-- | Add the transitivity theorem and proof to an Isabelle Theory. We
381--   need to know the number of sorts to know how much induction to
382--   perfom and also the sub-sort relation to build the collection of
383--   injectivity theorem names
384addTransitivityTheorem :: [SORT] -> [(SORT,SORT)] -> IsaTheory -> IsaTheory
385addTransitivityTheorem sorts sortRel isaTh =
386    let colInjThmNames = getColInjectivityThmName sortRel
387        colDecompThmNames = getColDecompositionThmName sortRel
388        numSorts = length(sorts)
389        name = transitivityS
390        x = mkFree "x"
391        y = mkFree "y"
392        z = mkFree "z"
393        thmConds = [binEq_PreAlphabet x y, binEq_PreAlphabet y z]
394        thmConcl = binEq_PreAlphabet x z
395        inductY = concat $ map (\i -> [Prefer (i*numSorts+1),
396                                       Apply (Induct y)])
397                  [0..(numSorts-1)]
398        inductZ = concat $ map (\i -> [Prefer (i*numSorts+1),
399                                       Apply (Induct z)])
400                  [0..((numSorts^(2::Int))-1)]
401        proof' = IsaProof {
402                   proof = [Apply (Induct x)] ++
403                            inductY ++
404                            inductZ ++
405                           [Apply (AutoSimpAdd Nothing
406                                  (colDecompThmNames ++ colInjThmNames))],
407                   end = Done
408                 }
409    in addThreomWithProof name thmConds thmConcl proof' isaTh
410
411-- | Function to add preAlphabet as an equivalence relation to an
412--   Isabelle theory
413addInstansanceOfEquiv :: IsaTheory  -> IsaTheory
414addInstansanceOfEquiv  isaTh =
415    let eqvSort = [IsaClass eqvTypeClassS]
416        eqvProof = IsaProof []  (By (Other "intro_classes"))
417        equivSort = [IsaClass equivTypeClassS]
418        equivProof = IsaProof [Apply (Other "intro_classes"),
419                               Apply (Other ("unfold " ++ preAlphabetSimS
420                                             ++ "_def")),
421                               Apply (Other ("rule " ++ reflexivityTheoremS)),
422                               Apply (Other ("rule " ++ transitivityS
423                                                ++", auto")),
424                               Apply (Other ("rule " ++ symmetryTheoremS
425                                                ++ ", simp"))]
426                               Done
427        x = mkFree "x"
428        y = mkFree "y"
429        defLhs = binEqvSim x y
430        defRhs = binEq_PreAlphabet x y
431    in   addInstanceOf preAlphabetS [] equivSort equivProof
432       $ addDef preAlphabetSimS defLhs defRhs
433       $ addInstanceOf preAlphabetS [] eqvSort eqvProof
434       $ isaTh
435
436-- | Function to add the Alphabet type (type synonym) to an Isabelle theory
437addAlphabetType :: IsaTheory -> IsaTheory
438addAlphabetType  isaTh =
439    let preAlphabetType = Type {typeId = preAlphabetS,
440                                typeSort = [],
441                                typeArgs =[]}
442        preAlphabetQuotType = Type {typeId = quotS,
443                             typeSort = [],
444                             typeArgs =[preAlphabetType]}
445        isaTh_sign = fst isaTh
446        isaTh_sign_tsig = tsig isaTh_sign
447        myabbrs = abbrs isaTh_sign_tsig
448        abbrsNew = Map.insert alphabetS ([], preAlphabetQuotType) myabbrs
449
450        isaTh_sign_updated = isaTh_sign {
451                               tsig = (isaTh_sign_tsig {abbrs =abbrsNew})
452                             }
453    in (isaTh_sign_updated, snd isaTh)
454
455-- | Function to add all the bar types to an Isabelle theory.
456addAllBarTypes :: [SORT] -> IsaTheory -> IsaTheory
457addAllBarTypes sorts isaTh = foldl addBarType isaTh sorts
458
459-- | Function to add the bar types of a sort to an Isabelle theory.
460addBarType :: IsaTheory -> SORT -> IsaTheory
461addBarType isaTh sort =
462    let sortBarString = convertSort2String sort ++ barExtS
463        barType = Type {typeId = sortBarString, typeSort = [], typeArgs =[]}
464        alphabetType = Type {typeId = alphabetS,
465                             typeSort = [],
466                             typeArgs =[]}
467        isaTh_sign = fst isaTh
468        isaTh_sen = snd isaTh
469        x = mkFree "x"
470        y = mkFree "y"
471        rhs = termAppl (conDouble (mkPreAlphabetConstructor sort)) y
472        bin_eq = binEq x $ termAppl (conDouble classS ) rhs
473        exist_eq =termAppl (conDouble exS) (Abs y bin_eq NotCont)
474        subset = SubSet x alphabetType exist_eq
475        sen = TypeDef barType subset (IsaProof [] (By Auto))
476        namedSen = (makeNamed sortBarString sen)
477    in (isaTh_sign, isaTh_sen ++ [namedSen])
478
479-- | Add all choose functions for a given list of sorts to an Isabelle
480--   theory.
481addAllChooseFunctions :: [SORT] -> IsaTheory -> IsaTheory
482addAllChooseFunctions sorts isaTh =
483    let isaTh' = foldl addChooseFunction isaTh sorts --add function and def
484    in foldl addChooseFunctionLemma isaTh' sorts --add theorem and proof
485
486-- | Add a single choose function for a given sort to an Isabelle
487--   theory.  The following Isabelle code is produced by this function:
488--   consts choose_Nat :: "Alphabet => Nat"
489--   defs choose_Nat_def: "choose_Nat x == contents{y . class(C_Nat y) = x}"
490addChooseFunction ::  IsaTheory -> SORT -> IsaTheory
491addChooseFunction isaTh sort =
492    let --constant
493        alphabetType = Type {typeId = alphabetS,
494                             typeSort = [],
495                             typeArgs =[]}
496        sortType = Type {typeId = convertSort2String sort,
497                         typeSort = [],
498                         typeArgs =[]}
499        chooseFunName = mkChooseFunName sort
500        chooseFunType = mkFunType alphabetType sortType
501        -- definition
502        x = mkFree "x"
503        y = mkFree "y"
504        contentsOp = termAppl (conDouble "contents")
505        chooseOp = termAppl (conDouble chooseFunName)
506        sortConsOp = termAppl (conDouble (mkPreAlphabetConstructor sort))
507        bin_eq = binEq (classOp $ sortConsOp y) x
508        subset = SubSet y sortType bin_eq
509        lhs = chooseOp x
510        rhs = contentsOp (Set subset)
511    in  addDef chooseFunName lhs rhs -- Add defintion to theory
512      $ addConst chooseFunName chooseFunType isaTh -- Add constant to theory
513
514-- | Add a single choose function lemma for a given sort to an
515--   Isabelle theory.  The following Isabelle code is produced by this
516--   function: lemma "choose_Nat (class (C_Nat x)) = x". The proof is
517--   also produced.
518
519addChooseFunctionLemma ::  IsaTheory -> SORT -> IsaTheory
520addChooseFunctionLemma isaTh sort =
521    let sortType = Type {typeId = convertSort2String sort,
522                         typeSort = [],
523                         typeArgs =[]}
524        chooseFunName = mkChooseFunName sort
525        x = mkFree "x"
526        y = mkFree "y"
527        chooseOp = termAppl (conDouble chooseFunName)
528        sortConsOp = termAppl (conDouble (mkPreAlphabetConstructor sort))
529        -- theorm
530        subgoalTacTermLhsBinEq = binEq (classOp $ sortConsOp y)
531                                       (classOp $ sortConsOp x)
532        subgoalTacTermLhs = Set $ SubSet y sortType subgoalTacTermLhsBinEq
533        subgoalTacTermRhs = Set $ FixedSet [x]
534        subgoalTacTerm = binEq subgoalTacTermLhs subgoalTacTermRhs
535        thmConds = []
536        thmConcl = binEq (chooseOp $ classOp $ sortConsOp x) x
537        proof' = IsaProof [Apply (Other ("unfold " ++ chooseFunName ++ "_def")),
538                           Apply (SubgoalTac subgoalTacTerm),
539                           Apply(Simp),
540                           Apply(Other ("unfold " ++ quotEqualityS)),
541                           Apply(Other ("unfold "++ preAlphabetSimS
542                                           ++ "_def")),
543                           Apply(Auto)]
544                           Done
545    in  addThreomWithProof chooseFunName thmConds thmConcl proof' isaTh
546
547-- | Add all the integration theorems. We need to know all the sorts
548--   to produce all the theorems.
549addAllIntegrationTheorems :: [SORT] -> CspCASLSign -> IsaTheory -> IsaTheory
550addAllIntegrationTheorems sorts ccSign isaTh =
551    let pairs = [(s1,s2) | s1 <- sorts, s2 <- sorts]
552    in foldl (addIntegrationTheorem_A ccSign) isaTh pairs
553
554-- | Add Integration theorem A -- Compare to elements of the Alphabet.
555--   We add the integration theorem based on the sorts of both
556--   elements of the alphabet. We need to know the subsort relation to
557--   find the highest common sort, but we pass in the CC signature for
558--   use in function calls.
559addIntegrationTheorem_A :: CspCASLSign -> IsaTheory -> (SORT,SORT) -> IsaTheory
560addIntegrationTheorem_A ccSign isaTh (s1,s2) =
561    let sortRel = Rel.toList(CASLSign.sortRel ccSign)
562        s1SuperSet = CASLSign.supersortsOf s1 ccSign
563        s2SuperSet = CASLSign.supersortsOf s2 ccSign
564        commonSuperList = Set.toList (Set.intersection
565                                         (Set.insert s1 s1SuperSet)
566                                         (Set.insert s2 s2SuperSet))
567        x = mkFree "x"
568        y = mkFree "y"
569        s1ConsOp = termAppl (conDouble (mkPreAlphabetConstructor s1))
570        s2ConsOp = termAppl (conDouble (mkPreAlphabetConstructor s2))
571        rhs = binEq (classOp $ s1ConsOp x) (classOp $ s2ConsOp y)
572        lhs = if (null commonSuperList)
573              then false
574              else
575                  -- pick any common sort - we should pick the top most one.
576                  let s' = head commonSuperList
577                  in binEq (mkInjection s1 s' x) (mkInjection s2 s' y)
578        thmConds = []
579        thmConcl = binEq rhs lhs
580        -- theorem names for proof
581        colInjThmNames = getColInjectivityThmName sortRel
582        colDecompThmNames = getColDecompositionThmName sortRel
583        proof' = IsaProof [Apply (SimpAdd Nothing ["quot_equality"]),
584                           Apply (Other ("unfold " ++ preAlphabetSimS
585                                             ++ "_def")),
586                           Apply (AutoSimpAdd Nothing
587                                  (colDecompThmNames ++ colInjThmNames))]
588                           Done
589    in  addThreomWithProof "IntegrationTheorem_A" thmConds thmConcl proof' isaTh
590
591-- Function to help keep strings consistent
592
593eq_PreAlphabetS :: String
594eq_PreAlphabetS = "eq_PreAlphabet"
595
596eq_PreAlphabetV :: VName
597eq_PreAlphabetV   = VName eq_PreAlphabetS $ Nothing
598
599binEq_PreAlphabet :: Term -> Term -> Term
600binEq_PreAlphabet = binVNameAppl eq_PreAlphabetV
601
602
603quotEqualityS :: String
604quotEqualityS = "quot_equality"
605
606preAlphabetS :: String
607preAlphabetS = "PreAlphabet"
608
609quotS :: String
610quotS = "quot"
611
612reflexivityTheoremS :: String
613reflexivityTheoremS = "eq_refl"
614
615symmetryTheoremS :: String
616symmetryTheoremS = "eq_symm"
617
618transitivityS :: String
619transitivityS = "eq_trans"
620
621preAlphabetSimS :: String
622preAlphabetSimS = "preAlphabet_sim"
623
624-- | String for the name of the axiomatic type class of equivalence
625-- | relations part 1
626eqvTypeClassS :: String
627eqvTypeClassS  = "eqv"
628
629-- | String for the name of the axiomatic type class of equivalence
630-- | relations part 2
631equivTypeClassS :: String
632equivTypeClassS  = "equiv"
633
634-- | Return the term representing the injection of a term from one sort to another
635--   note: the term is returned if both sorts are the same
636--   This function is not implemented in a satisfactory way
637mkInjection :: SORT -> SORT -> Term -> Term
638mkInjection s s' t =
639    let injName = getInjectionName s s'
640        replace string c s1 = concat (map (\x -> if x==c
641                                                 then s1
642                                                 else [x]) string)
643        injOp = Const {
644                  termName= VName {
645                              new = ("X_" ++ injName),
646                              altSyn = Just (AltSyntax ((replace injName '_' "'_")
647                                                        ++ "/'(_')") [3] 999)
648                            },
649                  termType = Hide {
650                               typ = Type {
651                                       typeId ="dummy",
652                                       typeSort = [IsaClass "type"],
653                                       typeArgs = []
654                                     },
655                               kon = NA,
656                               arit= Nothing
657                             }
658                }
659    in if s == s'
660       then t
661       else termAppl injOp t
662
663-- | Return the name of the injection as it is used in the alternative
664--   syntax of the injection from one sort to another.
665--   This function is not implemented in a satisfactory way
666getInjectionName :: SORT -> SORT -> String
667getInjectionName s s' =
668    let t = CASLSign.toOP_TYPE(CASLSign.OpType{CASLSign.opKind = Total,
669                                               CASLSign.opArgs = [s],
670                                               CASLSign.opRes = s'})
671        injName = show $ CASLInject.uniqueInjName t
672    in injName
673
674-- | Return the injection name of the injection from one sort to another
675--   This function is not implemented in a satisfactory way
676getDefinedOp :: [SORT] -> SORT -> Term -> Term
677getDefinedOp sorts s t =
678    termAppl (con $ VName (getDefinedName sorts s) $ Nothing) t
679
680-- | Return the name of the definedness function for a sort.  We need
681--   to know all sorts to perform this workaround
682--   This function is not implemented in a satisfactory way
683getDefinedName :: [SORT] -> SORT -> String
684getDefinedName sorts s =
685    let index = List.elemIndex s sorts
686        str = case index of
687                Nothing -> ""
688                Just i -> show (i + 1)
689    in "gn_definedX" ++ str
690
691-- Produce the theorem name of the decomposition theorem that we produce
692getDecompositionThmName :: (SORT,SORT,SORT) -> String
693getDecompositionThmName (s, s', s'') =
694    "decomposition_" ++ (convertSort2String s) ++ "_" ++ (convertSort2String s')
695                    ++ "_" ++ (convertSort2String s'')
696
697-- This function is not implemented in a satisfactory way
698-- Return the list of string of all decomposition theorem names that we generate
699getColDecompositionThmName :: [(SORT,SORT)] -> [String]
700getColDecompositionThmName sortRel =
701    let tripples = [(s1,s2,s3) |
702                    (s1,s2) <- sortRel, (s2',s3) <- sortRel, s2==s2']
703    in map getDecompositionThmName tripples
704
705-- Produce the theorem name of the injectivity theorem that we produce
706getInjectivityThmName :: (SORT,SORT) -> String
707getInjectivityThmName (s, s') =
708    "injectivity_" ++ (convertSort2String s) ++ "_" ++ (convertSort2String s')
709
710-- This function is not implemented in a satisfactory way
711-- Return the list of strings of the injectivity theorem names
712-- that we generate
713getColInjectivityThmName :: [(SORT,SORT)] -> [String]
714getColInjectivityThmName sortRel = map getInjectivityThmName sortRel
715
716-- This function is not implemented in a satisfactory way. Return the
717-- list of strings of all transitivity axioms produced by the
718-- translation CASL2PCFOL; CASL2SubCFOL
719getCollectionTransAx :: [(SORT,SORT)] -> [String]
720getCollectionTransAx sortRel =
721    let tripples = [(s1,s2,s3) |
722                    (s1,s2) <- sortRel, (s2',s3) <- sortRel, s2==s2']
723        mkEmbInjAxName = (\i -> "ga_transitivity"
724                                ++ (if (i==0)
725                                    then ""
726                                    else ("_" ++ show i))
727                         )
728    in  map mkEmbInjAxName [0 .. (length(tripples) - 1)]
729
730-- This function is not implemented in a satisfactory way. Return the
731-- list of string of all embedding_injectivity axioms produced by the
732-- translation CASL2PCFOL; CASL2SubCFOL
733getCollectionEmbInjAx :: [(SORT,SORT)] -> [String]
734getCollectionEmbInjAx sortRel =
735    let mkEmbInjAxName = (\i -> "ga_embedding_injectivity"
736                                ++ (if (i==0)
737                                    then ""
738                                    else ("_" ++ show i))
739                         )
740    in map mkEmbInjAxName [0 .. (length(sortRel) - 1)]
741
742-- This function is not implemented in a satisfactory way
743-- Return the list of strings of all gn_totality axioms
744getCollectionTotAx :: CASLSign.CASLSign -> [String]
745getCollectionTotAx pfolSign =
746    let opList = Map.toList $ CASLSign.opMap pfolSign
747        -- This filter is not quite right
748        totFilter (_,setOpType) = let listOpType = Set.toList setOpType
749                                 in CASLSign.opKind (head listOpType) == Total
750        totList = filter totFilter opList
751
752        mkTotAxName = (\i -> "ga_totality"
753                             ++ (if (i==0)
754                                 then ""
755                                 else ("_" ++ show i))
756                      )
757    in map mkTotAxName [0 .. (length(totList) - 1)]
758
759-- This function is not implemented in a satisfactory way
760-- Return the list of strings of all ga_notDefBottom axioms
761getCollectionNotDefBotAx :: [SORT] -> [String]
762getCollectionNotDefBotAx sorts =
763    let mkNotDefBotAxName = (\i -> "ga_notDefBottom"
764                                   ++ (if (i==0)
765                                       then ""
766                                       else ("_" ++ show i)))
767    in map mkNotDefBotAxName [0 .. (length(sorts) - 1)]
768
769-- Function that returns the constructor of PreAlphabet for a given sort
770mkPreAlphabetConstructor :: SORT -> String
771mkPreAlphabetConstructor sort = "C_" ++ (show sort)
772
773-- Function that takes a sort and outputs a the function name for the
774-- corresponing compare_with function
775mkCompareWithFunName :: SORT -> String
776mkCompareWithFunName sort = ("compare_with_" ++ (mkPreAlphabetConstructor sort))
777
778-- Function that takes a sort and outputs a the function name for the
779-- corresponing choose function
780mkChooseFunName :: SORT -> String
781mkChooseFunName sort = ("choose_" ++ (mkPreAlphabetConstructor sort))
782
783-- Functions for manipulating an Isabelle theory
784
785-- Add a single constant to the signature of an Isabelle theory
786addConst :: String -> Typ -> IsaTheory -> IsaTheory
787addConst cName cType isaTh =
788    let isaTh_sign = fst isaTh
789        isaTh_sen = snd isaTh
790        isaTh_sign_ConstTab = constTab isaTh_sign
791        isaTh_sign_ConstTabUpdated =
792            Map.insert (mkVName cName) cType isaTh_sign_ConstTab
793        isaTh_sign_updated = isaTh_sign {constTab = isaTh_sign_ConstTabUpdated}
794    in (isaTh_sign_updated, isaTh_sen)
795
796-- Add a primrec defintion to the sentences of an Isabelle theory
797addPrimRec :: [Term] -> IsaTheory -> IsaTheory
798addPrimRec terms isaTh =
799    let isaTh_sign = fst isaTh
800        isaTh_sen = snd isaTh
801        recDef = RecDef {keyWord = primrecS, senTerms = [terms]}
802        namedRecDef = (makeNamed "what_does_this_word_do?" recDef) {
803                        isAxiom = False,
804                        isDef = True}
805    in (isaTh_sign, isaTh_sen ++ [namedRecDef])
806
807-- Add a DomainEntry to the domain tab of a signature of an Isabelle Theory
808updateDomainTab :: DomainEntry  -> IsaTheory -> IsaTheory
809updateDomainTab domEnt isaTh =
810    let isaTh_sign = fst isaTh
811        isaTh_sen = snd isaTh
812        isaTh_sign_domTab = domainTab isaTh_sign
813        isaTh_sign_updated = isaTh_sign {domainTab = (isaTh_sign_domTab
814                                                      ++ [[domEnt]])}
815    in (isaTh_sign_updated, isaTh_sen)
816
817-- Add a theorem with proof to an Isabelle theory
818addThreomWithProof :: String -> [Term] -> Term -> IsaProof -> IsaTheory ->
819                      IsaTheory
820addThreomWithProof name conds concl proof' isaTh =
821    let isaTh_sign = fst isaTh
822        isaTh_sen = snd isaTh
823        sen = if (null conds)
824              then ((mkSen concl) {thmProof = Just proof'})
825              else ((mkCond conds concl) {thmProof = Just proof'})
826        namedSen = (makeNamed name sen) {isAxiom = False}
827    in (isaTh_sign, isaTh_sen ++ [namedSen])
828
829-- Function to add an instance of command to an Isabelle theory
830addInstanceOf :: String -> [Sort] -> Sort -> IsaProof -> IsaTheory -> IsaTheory
831addInstanceOf name args res pr isaTh =
832    let isaTh_sign = fst isaTh
833        isaTh_sen = snd isaTh
834        sen = Instance name args res pr
835        namedSen = (makeNamed name sen)
836    in (isaTh_sign, isaTh_sen ++ [namedSen])
837
838-- Function to add a def command to an Isabelle theory
839addDef :: String -> Term -> Term -> IsaTheory -> IsaTheory
840addDef name lhs rhs isaTh =
841    let isaTh_sign = fst isaTh
842        isaTh_sen = snd isaTh
843        sen = ConstDef (IsaEq lhs rhs)
844        namedSen = (makeNamed name sen)
845    in (isaTh_sign, isaTh_sen ++ [namedSen])
846
847-- Code below this line is junk
848
849-- Add a warning to an Isabelle theory
850addWarning :: IsaTheory -> IsaTheory
851addWarning isaTh =
852    let fakeType = Type {typeId = "Fake_Type" , typeSort = [], typeArgs =[]}
853    in addConst "Warning_this_is_not_a_stable_or_meaningful_translation"
854       fakeType isaTh
855
856-- Add a string version of the abstract syntax of an Isabelle theory to itself
857{-
858addDebug :: IsaTheory -> IsaTheory
859addDebug isaTh =
860    let isaTh_sign = fst(isaTh)
861        isaTh_sens = snd(isaTh)
862        sensType = Type {typeId = show isaTh_sens , typeSort = [], typeArgs =[]}
863        signType = Type {typeId = show isaTh_sign , typeSort = [], typeArgs =[]}
864    in   addConst "debug_isaTh_sens" sensType
865       $ addConst "debug_isaTh_sign" signType
866       $ isaTh
867-}
Note: See TracBrowser for help on using the browser.