PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/02-development/Hets/Propositional/Morphism.hs

https://bitbucket.org/jmelo_lyncode/thesis
Haskell | 149 lines | 103 code | 14 blank | 32 comment | 11 complexity | 2f32d8d623985ff2ea5cb63024ac7160 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0
  1. {- |
  2. Module : $Header$
  3. Description : Morphisms in Propositional logic
  4. Copyright : (c) Dominik Luecke, Uni Bremen 2007
  5. License : GPLv2 or higher, see LICENSE.txt
  6. Maintainer : luecke@informatik.uni-bremen.de
  7. Stability : experimental
  8. Portability : portable
  9. Definition of morphisms for propositional logic
  10. copied to "Temporal.Morphism"
  11. -}
  12. {-
  13. Ref.
  14. Till Mossakowski, Joseph Goguen, Razvan Diaconescu, Andrzej Tarlecki.
  15. What is a Logic?.
  16. In Jean-Yves Beziau (Ed.), Logica Universalis, pp. 113-@133. Birkhaeuser.
  17. 2005.
  18. -}
  19. module Propositional.Morphism
  20. ( Morphism (..) -- datatype for Morphisms
  21. , pretty -- pretty printing
  22. , idMor -- identity morphism
  23. , isLegalMorphism -- check if morhpism is ok
  24. , composeMor -- composition
  25. , inclusionMap -- inclusion map
  26. , mapSentence -- map of sentences
  27. , mapSentenceH -- map of sentences, without Result type
  28. , applyMap -- application function for maps
  29. , applyMorphism -- application function for morphism
  30. , morphismUnion
  31. ) where
  32. import qualified Data.Map as Map
  33. import qualified Data.Set as Set
  34. import Propositional.Sign as Sign
  35. import qualified Common.Result as Result
  36. import qualified Propositional.AS_BASIC_Propositional as AS_BASIC
  37. import Common.Id as Id
  38. import Common.Result
  39. import Common.Doc
  40. import Common.DocUtils
  41. -- | The datatype for morphisms in propositional logic as
  42. -- maps of sets
  43. data Morphism = Morphism
  44. { source :: Sign
  45. , target :: Sign
  46. , propMap :: Map.Map Id Id
  47. } deriving (Eq, Ord, Show)
  48. instance Pretty Morphism where
  49. pretty = printMorphism
  50. -- | Constructs an id-morphism
  51. idMor :: Sign -> Morphism
  52. idMor a = inclusionMap a a
  53. -- | Determines whether a morphism is valid
  54. isLegalMorphism :: Morphism -> Result ()
  55. isLegalMorphism pmor =
  56. let psource = items $ source pmor
  57. ptarget = items $ target pmor
  58. pdom = Map.keysSet $ propMap pmor
  59. pcodom = Set.map (applyMorphism pmor) psource
  60. in if Set.isSubsetOf pcodom ptarget && Set.isSubsetOf pdom psource
  61. then return () else fail "illegal Propositional morphism"
  62. -- | Application funtion for morphisms
  63. applyMorphism :: Morphism -> Id -> Id
  64. applyMorphism mor idt = Map.findWithDefault idt idt $ propMap mor
  65. -- | Application function for propMaps
  66. applyMap :: Map.Map Id Id -> Id -> Id
  67. applyMap pmap idt = Map.findWithDefault idt idt pmap
  68. -- | Composition of morphisms in propositional Logic
  69. composeMor :: Morphism -> Morphism -> Result Morphism
  70. composeMor f g =
  71. let fSource = source f
  72. gTarget = target g
  73. fMap = propMap f
  74. gMap = propMap g
  75. in return Morphism
  76. { source = fSource
  77. , target = gTarget
  78. , propMap = if Map.null gMap then fMap else
  79. Set.fold ( \ i -> let j = applyMap gMap (applyMap fMap i) in
  80. if i == j then id else Map.insert i j)
  81. Map.empty $ items fSource }
  82. -- | Pretty printing for Morphisms
  83. printMorphism :: Morphism -> Doc
  84. printMorphism m = pretty (source m) <> text "-->" <> pretty (target m)
  85. <> vcat (map ( \ (x, y) -> lparen <> pretty x <> text ","
  86. <> pretty y <> rparen) $ Map.assocs $ propMap m)
  87. -- | Inclusion map of a subsig into a supersig
  88. inclusionMap :: Sign.Sign -> Sign.Sign -> Morphism
  89. inclusionMap s1 s2 = Morphism
  90. { source = s1
  91. , target = s2
  92. , propMap = Map.empty }
  93. -- | sentence translation along signature morphism
  94. -- here just the renaming of formulae
  95. mapSentence :: Morphism -> AS_BASIC.FORMULA -> Result.Result AS_BASIC.FORMULA
  96. mapSentence mor = return . mapSentenceH mor
  97. mapSentenceH :: Morphism -> AS_BASIC.FORMULA -> AS_BASIC.FORMULA
  98. mapSentenceH mor frm = case frm of
  99. AS_BASIC.Negation form rn -> AS_BASIC.Negation (mapSentenceH mor form) rn
  100. AS_BASIC.Conjunction form rn ->
  101. AS_BASIC.Conjunction (map (mapSentenceH mor) form) rn
  102. AS_BASIC.Disjunction form rn ->
  103. AS_BASIC.Disjunction (map (mapSentenceH mor) form) rn
  104. AS_BASIC.Implication form1 form2 rn -> AS_BASIC.Implication
  105. (mapSentenceH mor form1) (mapSentenceH mor form2) rn
  106. AS_BASIC.Equivalence form1 form2 rn -> AS_BASIC.Equivalence
  107. (mapSentenceH mor form1) (mapSentenceH mor form2) rn
  108. AS_BASIC.True_atom rn -> AS_BASIC.True_atom rn
  109. AS_BASIC.False_atom rn -> AS_BASIC.False_atom rn
  110. AS_BASIC.Predication predH -> AS_BASIC.Predication
  111. $ id2SimpleId $ applyMorphism mor $ Id.simpleIdToId predH
  112. morphismUnion :: Morphism -> Morphism -> Result.Result Morphism
  113. morphismUnion mor1 mor2 =
  114. let pmap1 = propMap mor1
  115. pmap2 = propMap mor2
  116. p1 = source mor1
  117. p2 = source mor2
  118. up1 = Set.difference (items p1) $ Map.keysSet pmap1
  119. up2 = Set.difference (items p2) $ Map.keysSet pmap2
  120. (pds, pmap) = foldr ( \ (i, j) (ds, m) -> case Map.lookup i m of
  121. Nothing -> (ds, Map.insert i j m)
  122. Just k -> if j == k then (ds, m) else
  123. (Diag Error
  124. ("incompatible mapping of prop " ++ showId i " to "
  125. ++ showId j " and " ++ showId k "")
  126. nullRange : ds, m)) ([], pmap1)
  127. (Map.toList pmap2 ++ map (\ a -> (a, a))
  128. (Set.toList $ Set.union up1 up2))
  129. in if null pds then return Morphism
  130. { source = unite p1 p2
  131. , target = unite (target mor1) $ target mor2
  132. , propMap = pmap } else Result pds Nothing