root/trunk/Comorphisms/CspCASL2IsabelleHOL.hs @ 11216

Revision 11216, 41.2 kB (checked in by maeder, 11 months ago)

extended symbol kinds i.e. for CspCASL

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