| 1 | {- | |
|---|
| 2 | Module : $Header$ |
|---|
| 3 | Description : CASL signatures and local environments for basic analysis |
|---|
| 4 | Copyright : (c) Christian Maeder and Uni Bremen 2002-2006 |
|---|
| 5 | License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt |
|---|
| 6 | |
|---|
| 7 | Maintainer : Christian.Maeder@dfki.de |
|---|
| 8 | Stability : provisional |
|---|
| 9 | Portability : portable |
|---|
| 10 | |
|---|
| 11 | CASL signatures also serve as local environments for the basic static analysis |
|---|
| 12 | -} |
|---|
| 13 | |
|---|
| 14 | module CASL.Sign where |
|---|
| 15 | |
|---|
| 16 | import CASL.AS_Basic_CASL |
|---|
| 17 | import CASL.ToDoc () |
|---|
| 18 | import qualified Data.Map as Map |
|---|
| 19 | import qualified Data.Set as Set |
|---|
| 20 | import qualified Common.Lib.Rel as Rel |
|---|
| 21 | import qualified Common.Lib.State as State |
|---|
| 22 | import Common.Keywords |
|---|
| 23 | import Common.Id |
|---|
| 24 | import Common.Result |
|---|
| 25 | import Common.AS_Annotation |
|---|
| 26 | import Common.GlobalAnnotations |
|---|
| 27 | import Common.Doc |
|---|
| 28 | import Common.DocUtils |
|---|
| 29 | |
|---|
| 30 | import Data.List (isPrefixOf) |
|---|
| 31 | import Control.Monad (when, unless) |
|---|
| 32 | |
|---|
| 33 | -- constants have empty argument lists |
|---|
| 34 | data OpType = OpType {opKind :: OpKind, opArgs :: [SORT], opRes :: SORT} |
|---|
| 35 | deriving (Show, Eq, Ord) |
|---|
| 36 | |
|---|
| 37 | data PredType = PredType {predArgs :: [SORT]} deriving (Show, Eq, Ord) |
|---|
| 38 | |
|---|
| 39 | type OpMap = Map.Map Id (Set.Set OpType) |
|---|
| 40 | |
|---|
| 41 | data SymbType = SortAsItemType |
|---|
| 42 | | OpAsItemType OpType |
|---|
| 43 | -- since symbols do not speak about totality, the totality |
|---|
| 44 | -- information in OpType has to be ignored |
|---|
| 45 | | PredAsItemType PredType |
|---|
| 46 | deriving Show |
|---|
| 47 | |
|---|
| 48 | -- Ordering and equality of symbol types has to ingore totality information |
|---|
| 49 | instance Ord SymbType where |
|---|
| 50 | compare st1 st2 = case (st1, st2) of |
|---|
| 51 | (SortAsItemType, SortAsItemType) -> EQ |
|---|
| 52 | (SortAsItemType, _) -> LT |
|---|
| 53 | (OpAsItemType ot1, OpAsItemType ot2) -> |
|---|
| 54 | compare (opArgs ot1, opRes ot1) (opArgs ot2, opRes ot2) |
|---|
| 55 | (OpAsItemType _, SortAsItemType) -> GT |
|---|
| 56 | (OpAsItemType _, PredAsItemType _) -> LT |
|---|
| 57 | (PredAsItemType pt1, PredAsItemType pt2) -> compare pt1 pt2 |
|---|
| 58 | (PredAsItemType _, _) -> GT |
|---|
| 59 | |
|---|
| 60 | instance Eq SymbType where |
|---|
| 61 | t1 == t2 = compare t1 t2 == EQ |
|---|
| 62 | |
|---|
| 63 | data Symbol = Symbol {symName :: Id, symbType :: SymbType} |
|---|
| 64 | deriving (Show, Eq, Ord) |
|---|
| 65 | |
|---|
| 66 | instance GetRange Symbol where |
|---|
| 67 | getRange = getRange . symName |
|---|
| 68 | |
|---|
| 69 | idToSortSymbol :: Id -> Symbol |
|---|
| 70 | idToSortSymbol idt = Symbol idt SortAsItemType |
|---|
| 71 | |
|---|
| 72 | idToOpSymbol :: Id -> OpType -> Symbol |
|---|
| 73 | idToOpSymbol idt = Symbol idt . OpAsItemType |
|---|
| 74 | |
|---|
| 75 | idToPredSymbol :: Id -> PredType -> Symbol |
|---|
| 76 | idToPredSymbol idt = Symbol idt . PredAsItemType |
|---|
| 77 | |
|---|
| 78 | dummy :: Sign f s -> a -> () |
|---|
| 79 | dummy _ _ = () |
|---|
| 80 | |
|---|
| 81 | dummyMin :: b -> c -> Result () |
|---|
| 82 | dummyMin _ _ = return () |
|---|
| 83 | |
|---|
| 84 | type CASLSign = Sign () () |
|---|
| 85 | |
|---|
| 86 | data Sign f e = Sign |
|---|
| 87 | { sortSet :: Set.Set SORT |
|---|
| 88 | , emptySortSet :: Set.Set SORT |
|---|
| 89 | -- a subset of the sort set of possibly empty sorts |
|---|
| 90 | , sortRel :: Rel.Rel SORT |
|---|
| 91 | , opMap :: OpMap |
|---|
| 92 | , assocOps :: OpMap |
|---|
| 93 | , predMap :: Map.Map Id (Set.Set PredType) |
|---|
| 94 | , varMap :: Map.Map SIMPLE_ID SORT |
|---|
| 95 | , sentences :: [Named (FORMULA f)] |
|---|
| 96 | , declaredSymbols :: Set.Set Symbol |
|---|
| 97 | , envDiags :: [Diagnosis] |
|---|
| 98 | , annoMap :: Map.Map Symbol (Set.Set Annotation) |
|---|
| 99 | , globAnnos :: GlobalAnnos |
|---|
| 100 | , extendedInfo :: e |
|---|
| 101 | } deriving Show |
|---|
| 102 | |
|---|
| 103 | -- better ignore assoc flags for equality |
|---|
| 104 | instance (Eq f, Eq e) => Eq (Sign f e) where |
|---|
| 105 | e1 == e2 = |
|---|
| 106 | sortSet e1 == sortSet e2 && |
|---|
| 107 | emptySortSet e1 == emptySortSet e2 && |
|---|
| 108 | sortRel e1 == sortRel e2 && |
|---|
| 109 | opMap e1 == opMap e2 && |
|---|
| 110 | predMap e1 == predMap e2 && |
|---|
| 111 | extendedInfo e1 == extendedInfo e2 |
|---|
| 112 | |
|---|
| 113 | emptySign :: e -> Sign f e |
|---|
| 114 | emptySign e = Sign |
|---|
| 115 | { sortSet = Set.empty |
|---|
| 116 | , emptySortSet = Set.empty |
|---|
| 117 | , sortRel = Rel.empty |
|---|
| 118 | , opMap = Map.empty |
|---|
| 119 | , assocOps = Map.empty |
|---|
| 120 | , predMap = Map.empty |
|---|
| 121 | , varMap = Map.empty |
|---|
| 122 | , sentences = [] |
|---|
| 123 | , declaredSymbols = Set.empty |
|---|
| 124 | , envDiags = [] |
|---|
| 125 | , annoMap = Map.empty |
|---|
| 126 | , globAnnos = emptyGlobalAnnos |
|---|
| 127 | , extendedInfo = e } |
|---|
| 128 | |
|---|
| 129 | -- | proper subsorts (possibly excluding input sort) |
|---|
| 130 | subsortsOf :: SORT -> Sign f e -> Set.Set SORT |
|---|
| 131 | subsortsOf s e = Rel.predecessors (sortRel e) s |
|---|
| 132 | |
|---|
| 133 | -- | proper supersorts (possibly excluding input sort) |
|---|
| 134 | supersortsOf :: SORT -> Sign f e -> Set.Set SORT |
|---|
| 135 | supersortsOf s e = Rel.succs (sortRel e) s |
|---|
| 136 | |
|---|
| 137 | toOP_TYPE :: OpType -> OP_TYPE |
|---|
| 138 | toOP_TYPE OpType { opArgs = args, opRes = res, opKind = k } = |
|---|
| 139 | Op_type k args res nullRange |
|---|
| 140 | |
|---|
| 141 | toPRED_TYPE :: PredType -> PRED_TYPE |
|---|
| 142 | toPRED_TYPE PredType { predArgs = args } = Pred_type args nullRange |
|---|
| 143 | |
|---|
| 144 | toOpType :: OP_TYPE -> OpType |
|---|
| 145 | toOpType (Op_type k args r _) = OpType k args r |
|---|
| 146 | |
|---|
| 147 | toPredType :: PRED_TYPE -> PredType |
|---|
| 148 | toPredType (Pred_type args _) = PredType args |
|---|
| 149 | |
|---|
| 150 | instance Pretty OpType where |
|---|
| 151 | pretty = pretty . toOP_TYPE |
|---|
| 152 | |
|---|
| 153 | instance Pretty PredType where |
|---|
| 154 | pretty = pretty . toPRED_TYPE |
|---|
| 155 | |
|---|
| 156 | instance (Pretty f, Pretty e) => Pretty (Sign f e) where |
|---|
| 157 | pretty = printSign pretty pretty |
|---|
| 158 | |
|---|
| 159 | instance Pretty Symbol where |
|---|
| 160 | pretty sy = let n = pretty (symName sy) in |
|---|
| 161 | case symbType sy of |
|---|
| 162 | SortAsItemType -> n |
|---|
| 163 | PredAsItemType pt -> let p = n <+> colon <+> pretty pt in |
|---|
| 164 | case predArgs pt of |
|---|
| 165 | [_] -> text predS <+> p |
|---|
| 166 | _ -> p |
|---|
| 167 | OpAsItemType ot -> let o = n <+> colon <> pretty ot in |
|---|
| 168 | case opArgs ot of |
|---|
| 169 | [] | opKind ot == Total -> text opS <+> o |
|---|
| 170 | _ -> o |
|---|
| 171 | |
|---|
| 172 | instance Pretty SymbType where |
|---|
| 173 | pretty st = case st of |
|---|
| 174 | OpAsItemType ot -> pretty ot |
|---|
| 175 | PredAsItemType pt -> space <> pretty pt |
|---|
| 176 | SortAsItemType -> empty |
|---|
| 177 | |
|---|
| 178 | printSign :: (f -> Doc) -> (e -> Doc) -> Sign f e -> Doc |
|---|
| 179 | printSign _ fE s = let |
|---|
| 180 | printRel (supersort, subsorts) = |
|---|
| 181 | ppWithCommas (Set.toList subsorts) <+> text lessS <+> |
|---|
| 182 | idDoc supersort |
|---|
| 183 | esorts = emptySortSet s |
|---|
| 184 | nsorts = Set.difference (sortSet s) esorts in |
|---|
| 185 | (if Set.null nsorts then empty else text (sortS++sS) <+> |
|---|
| 186 | sepByCommas (map idDoc (Set.toList nsorts))) $+$ |
|---|
| 187 | (if Set.null esorts then empty else text (esortS++sS) <+> |
|---|
| 188 | sepByCommas (map idDoc (Set.toList esorts))) $+$ |
|---|
| 189 | (if Rel.null (sortRel s) then empty |
|---|
| 190 | else text (sortS++sS) <+> |
|---|
| 191 | (fsep $ punctuate semi $ map printRel $ Map.toList |
|---|
| 192 | $ Rel.toMap $ Rel.transpose $ sortRel s)) |
|---|
| 193 | $+$ printSetMap (text opS) empty (opMap s) |
|---|
| 194 | $+$ printSetMap (text predS) space (predMap s) |
|---|
| 195 | $+$ fE (extendedInfo s) |
|---|
| 196 | |
|---|
| 197 | -- working with Sign |
|---|
| 198 | |
|---|
| 199 | diffRel :: Ord a => Rel.Rel a -> Rel.Rel a -> Rel.Rel a |
|---|
| 200 | diffRel a = Rel.irreflex . Rel.transClosure . Rel.difference a |
|---|
| 201 | |
|---|
| 202 | diffSig :: (e -> e -> e) -> Sign f e -> Sign f e -> Sign f e |
|---|
| 203 | diffSig dif a b = let s = sortSet a `Set.difference` sortSet b in a |
|---|
| 204 | { sortSet = s |
|---|
| 205 | , emptySortSet = Set.difference s |
|---|
| 206 | $ nonEmptySortSet a `Set.difference` nonEmptySortSet b |
|---|
| 207 | , sortRel = diffRel (sortRel a) $ sortRel b |
|---|
| 208 | , opMap = opMap a `diffOpMapSet` opMap b |
|---|
| 209 | , assocOps = assocOps a `diffOpMapSet` assocOps b |
|---|
| 210 | , predMap = predMap a `diffMapSet` predMap b |
|---|
| 211 | , annoMap = annoMap a `diffMapSet` annoMap b |
|---|
| 212 | , extendedInfo = dif (extendedInfo a) $ extendedInfo b } |
|---|
| 213 | -- transClosure needed: {a < b < c} - {a < c; b} |
|---|
| 214 | -- is not transitive! |
|---|
| 215 | |
|---|
| 216 | diffOpMapSet :: OpMap -> OpMap -> OpMap |
|---|
| 217 | diffOpMapSet m = diffMapSet m . Map.map (rmOrAddParts False) |
|---|
| 218 | |
|---|
| 219 | diffMapSet :: (Ord a, Ord b) => Map.Map a (Set.Set b) |
|---|
| 220 | -> Map.Map a (Set.Set b) -> Map.Map a (Set.Set b) |
|---|
| 221 | diffMapSet = Map.differenceWith |
|---|
| 222 | (\ s t -> let d = Set.difference s t in |
|---|
| 223 | if Set.null d then Nothing else Just d) |
|---|
| 224 | |
|---|
| 225 | addMapSet :: (Ord a, Ord b) => Map.Map a (Set.Set b) -> Map.Map a (Set.Set b) |
|---|
| 226 | -> Map.Map a (Set.Set b) |
|---|
| 227 | addMapSet = Map.unionWith Set.union |
|---|
| 228 | |
|---|
| 229 | makePartial :: Set.Set OpType -> Set.Set OpType |
|---|
| 230 | makePartial = Set.mapMonotonic (\ o -> o { opKind = Partial }) |
|---|
| 231 | |
|---|
| 232 | -- | remove (True) or add (False) partial op if it is included as total |
|---|
| 233 | rmOrAddParts :: Bool -> Set.Set OpType -> Set.Set OpType |
|---|
| 234 | rmOrAddParts b s = |
|---|
| 235 | let t = makePartial $ Set.filter ((== Total) . opKind) s |
|---|
| 236 | in (if b then Set.difference else Set.union) s t |
|---|
| 237 | |
|---|
| 238 | addOpMapSet :: OpMap -> OpMap -> OpMap |
|---|
| 239 | addOpMapSet m = Map.map (rmOrAddParts True). addMapSet m |
|---|
| 240 | |
|---|
| 241 | interMapSet :: (Ord a, Ord b) => Map.Map a (Set.Set b) -> Map.Map a (Set.Set b) |
|---|
| 242 | -> Map.Map a (Set.Set b) |
|---|
| 243 | interMapSet m = |
|---|
| 244 | Map.filter (not . Set.null) . Map.intersectionWith Set.intersection m |
|---|
| 245 | |
|---|
| 246 | interOpMapSet :: OpMap -> OpMap -> OpMap |
|---|
| 247 | interOpMapSet m = Map.filter (not . Set.null) |
|---|
| 248 | . Map.intersectionWith |
|---|
| 249 | (\ s t -> rmOrAddParts True $ Set.intersection (rmOrAddParts False s) |
|---|
| 250 | $ rmOrAddParts False t) m |
|---|
| 251 | |
|---|
| 252 | uniteCASLSign :: Sign () () -> Sign () () -> Sign () () |
|---|
| 253 | uniteCASLSign = addSig (\_ _ -> ()) |
|---|
| 254 | |
|---|
| 255 | addRel :: Ord a => Rel.Rel a -> Rel.Rel a -> Rel.Rel a |
|---|
| 256 | addRel a = Rel.irreflex . Rel.transClosure . Rel.union a |
|---|
| 257 | |
|---|
| 258 | nonEmptySortSet :: Sign f e -> Set.Set Id |
|---|
| 259 | nonEmptySortSet s = Set.difference (sortSet s) $ emptySortSet s |
|---|
| 260 | |
|---|
| 261 | addSig :: (e -> e -> e) -> Sign f e -> Sign f e -> Sign f e |
|---|
| 262 | addSig ad a b = let s = sortSet a `Set.union` sortSet b in a |
|---|
| 263 | { sortSet = s |
|---|
| 264 | , emptySortSet = Set.difference s |
|---|
| 265 | $ nonEmptySortSet a `Set.union` nonEmptySortSet b |
|---|
| 266 | , sortRel = addRel (sortRel a) $ sortRel b |
|---|
| 267 | , opMap = addOpMapSet (opMap a) $ opMap b |
|---|
| 268 | , assocOps = addOpMapSet (assocOps a) $ assocOps b |
|---|
| 269 | , predMap = addMapSet (predMap a) $ predMap b |
|---|
| 270 | , annoMap = addMapSet (annoMap a) $ annoMap b |
|---|
| 271 | , extendedInfo = ad (extendedInfo a) $ extendedInfo b } |
|---|
| 272 | |
|---|
| 273 | interRel :: Ord a => Rel.Rel a -> Rel.Rel a -> Rel.Rel a |
|---|
| 274 | interRel a = Rel.irreflex . Rel.transClosure . Rel.fromSet |
|---|
| 275 | . Set.intersection (Rel.toSet a) . Rel.toSet |
|---|
| 276 | |
|---|
| 277 | interSig :: (e -> e -> e) -> Sign f e -> Sign f e -> Sign f e |
|---|
| 278 | interSig ef a b = let s = sortSet a `Set.intersection` sortSet b in a |
|---|
| 279 | { sortSet = s |
|---|
| 280 | , emptySortSet = Set.difference s |
|---|
| 281 | $ nonEmptySortSet a `Set.intersection` nonEmptySortSet b |
|---|
| 282 | , sortRel = interRel (sortRel a) $ sortRel b |
|---|
| 283 | , opMap = interOpMapSet (opMap a) $ opMap b |
|---|
| 284 | , assocOps = interOpMapSet (assocOps a) $ assocOps b |
|---|
| 285 | , predMap = interMapSet (predMap a) $ predMap b |
|---|
| 286 | , annoMap = interMapSet (annoMap a) $ annoMap b |
|---|
| 287 | , extendedInfo = ef (extendedInfo a) $ extendedInfo b } |
|---|
| 288 | |
|---|
| 289 | isEmptySig :: (e -> Bool) -> Sign f e -> Bool |
|---|
| 290 | isEmptySig ie s = |
|---|
| 291 | Set.null (sortSet s) && |
|---|
| 292 | Rel.null (sortRel s) && |
|---|
| 293 | Map.null (opMap s) && |
|---|
| 294 | Map.null (predMap s) && ie (extendedInfo s) |
|---|
| 295 | |
|---|
| 296 | isSubMapSet :: (Ord a, Ord b) => Map.Map a (Set.Set b) -> Map.Map a (Set.Set b) |
|---|
| 297 | -> Bool |
|---|
| 298 | isSubMapSet = Map.isSubmapOfBy Set.isSubsetOf |
|---|
| 299 | |
|---|
| 300 | isSubOpMap :: OpMap -> OpMap -> Bool |
|---|
| 301 | isSubOpMap = Map.isSubmapOfBy $ \ s t -> |
|---|
| 302 | Set.fold ( \ e r -> r && (Set.member e t || case opKind e of |
|---|
| 303 | Partial -> Set.member e {opKind = Total} t |
|---|
| 304 | Total -> False)) True s |
|---|
| 305 | |
|---|
| 306 | isSubSig :: (e -> e -> Bool) -> Sign f e -> Sign f e -> Bool |
|---|
| 307 | isSubSig isSubExt a b = Set.isSubsetOf (sortSet a) (sortSet b) |
|---|
| 308 | && Rel.isSubrelOf (sortRel a) (sortRel b) |
|---|
| 309 | -- ignore empty sort sorts |
|---|
| 310 | && isSubOpMap (opMap a) (opMap b) |
|---|
| 311 | -- ignore associativity properties! |
|---|
| 312 | && isSubMapSet (predMap a) (predMap b) |
|---|
| 313 | && isSubExt (extendedInfo a) (extendedInfo b) |
|---|
| 314 | |
|---|
| 315 | addDiags :: [Diagnosis] -> State.State (Sign f e) () |
|---|
| 316 | addDiags ds = do |
|---|
| 317 | e <- State.get |
|---|
| 318 | State.put e { envDiags = reverse ds ++ envDiags e } |
|---|
| 319 | |
|---|
| 320 | addAnnoSet :: Annoted a -> Symbol -> State.State (Sign f e) () |
|---|
| 321 | addAnnoSet a s = do |
|---|
| 322 | addSymbol s |
|---|
| 323 | let v = Set.union (Set.fromList $ l_annos a) $ Set.fromList $ r_annos a |
|---|
| 324 | unless (Set.null v) $ do |
|---|
| 325 | e <- State.get |
|---|
| 326 | State.put e { annoMap = Map.insertWith Set.union s v $ annoMap e } |
|---|
| 327 | |
|---|
| 328 | addSymbol :: Symbol -> State.State (Sign f e) () |
|---|
| 329 | addSymbol s = do |
|---|
| 330 | e <- State.get |
|---|
| 331 | State.put e { declaredSymbols = Set.insert s $ declaredSymbols e } |
|---|
| 332 | |
|---|
| 333 | addSort :: SortsKind -> Annoted a -> SORT -> State.State (Sign f e) () |
|---|
| 334 | addSort sk a s = do |
|---|
| 335 | e <- State.get |
|---|
| 336 | let m = sortSet e |
|---|
| 337 | em = emptySortSet e |
|---|
| 338 | known = Set.member s m |
|---|
| 339 | if known then addDiags [mkDiag Hint "redeclared sort" s] |
|---|
| 340 | else do |
|---|
| 341 | State.put e { sortSet = Set.insert s m } |
|---|
| 342 | addDiags $ checkNamePrefix s |
|---|
| 343 | case sk of |
|---|
| 344 | NonEmptySorts -> when (Set.member s em) $ do |
|---|
| 345 | e2 <- State.get |
|---|
| 346 | State.put e2 { emptySortSet = Set.delete s em } |
|---|
| 347 | addDiags [mkDiag Warning "redeclared sort as non-empty" s] |
|---|
| 348 | PossiblyEmptySorts -> if known then |
|---|
| 349 | addDiags [mkDiag Warning "non-empty sort remains non-empty" s] |
|---|
| 350 | else do |
|---|
| 351 | e2 <- State.get |
|---|
| 352 | State.put e2 { emptySortSet = Set.insert s em } |
|---|
| 353 | addAnnoSet a $ Symbol s SortAsItemType |
|---|
| 354 | |
|---|
| 355 | hasSort :: Sign f e -> SORT -> [Diagnosis] |
|---|
| 356 | hasSort e s = |
|---|
| 357 | [ mkDiag Error "unknown sort" s |
|---|
| 358 | | not $ Set.member s $ sortSet e ] |
|---|
| 359 | |
|---|
| 360 | checkSorts :: [SORT] -> State.State (Sign f e) () |
|---|
| 361 | checkSorts s = do |
|---|
| 362 | e <- State.get |
|---|
| 363 | addDiags $ concatMap (hasSort e) s |
|---|
| 364 | |
|---|
| 365 | addSubsort :: SORT -> SORT -> State.State (Sign f e) () |
|---|
| 366 | addSubsort = addSubsortOrIso True |
|---|
| 367 | |
|---|
| 368 | addSubsortOrIso :: Bool -> SORT -> SORT -> State.State (Sign f e) () |
|---|
| 369 | addSubsortOrIso b super sub = do |
|---|
| 370 | when b $ checkSorts [super, sub] |
|---|
| 371 | e <- State.get |
|---|
| 372 | let r = sortRel e |
|---|
| 373 | State.put e { sortRel = (if b then id else Rel.insert super sub) |
|---|
| 374 | $ Rel.insert sub super r } |
|---|
| 375 | let p = posOfId sub |
|---|
| 376 | rel = " '" ++ |
|---|
| 377 | showDoc sub (if b then " < " else " = ") ++ showDoc super "'" |
|---|
| 378 | if super == sub then addDiags [mkDiag Warning "void reflexive subsort" sub] |
|---|
| 379 | else if b then |
|---|
| 380 | if Rel.path super sub r then |
|---|
| 381 | if Rel.path sub super r |
|---|
| 382 | then addDiags [Diag Warning ("sorts are isomorphic" ++ rel) p] |
|---|
| 383 | else addDiags [Diag Warning ("added subsort cycle by" ++ rel) p] |
|---|
| 384 | else when (Rel.path sub super r) |
|---|
| 385 | $ addDiags [Diag Hint ("redeclared subsort" ++ rel) p] |
|---|
| 386 | else if Rel.path super sub r then |
|---|
| 387 | if Rel.path sub super r |
|---|
| 388 | then addDiags [Diag Hint ("redeclared isomoprhic sorts" ++ rel) p] |
|---|
| 389 | else addDiags [Diag Warning ("subsort '" ++ |
|---|
| 390 | showDoc super "' made isomorphic by" ++ rel) $ posOfId super] |
|---|
| 391 | else when (Rel.path sub super r) |
|---|
| 392 | $ addDiags [Diag Warning ("subsort '" ++ |
|---|
| 393 | showDoc sub "' made isomorphic by" ++ rel) p] |
|---|
| 394 | |
|---|
| 395 | closeSubsortRel :: State.State (Sign f e) () |
|---|
| 396 | closeSubsortRel= |
|---|
| 397 | do e <- State.get |
|---|
| 398 | State.put e { sortRel = Rel.transClosure $ sortRel e } |
|---|
| 399 | |
|---|
| 400 | checkNamePrefix :: Id -> [Diagnosis] |
|---|
| 401 | checkNamePrefix i = |
|---|
| 402 | [ mkDiag Warning "identifier may conflict with generated names" i |
|---|
| 403 | | isPrefixOf genNamePrefix $ showId i ""] |
|---|
| 404 | |
|---|
| 405 | alsoWarning :: String -> String -> Id -> [Diagnosis] |
|---|
| 406 | alsoWarning new old i = let is = ' ' : showId i "'" in |
|---|
| 407 | [Diag Warning ("new '" ++ new ++ is ++ " is also known as '" ++ old ++ is) |
|---|
| 408 | $ posOfId i] |
|---|
| 409 | |
|---|
| 410 | checkWithOtherMap :: String -> String -> Map.Map Id a -> Id -> [Diagnosis] |
|---|
| 411 | checkWithOtherMap s1 s2 m i = |
|---|
| 412 | case Map.lookup i m of |
|---|
| 413 | Nothing -> [] |
|---|
| 414 | Just _ -> alsoWarning s1 s2 i |
|---|
| 415 | |
|---|
| 416 | addVars :: VAR_DECL -> State.State (Sign f e) () |
|---|
| 417 | addVars (Var_decl vs s _) = do |
|---|
| 418 | checkSorts [s] |
|---|
| 419 | mapM_ (addVar s) vs |
|---|
| 420 | |
|---|
| 421 | addVar :: SORT -> SIMPLE_ID -> State.State (Sign f e) () |
|---|
| 422 | addVar s v = |
|---|
| 423 | do e <- State.get |
|---|
| 424 | let m = varMap e |
|---|
| 425 | i = simpleIdToId v |
|---|
| 426 | ds = case Map.lookup v m of |
|---|
| 427 | Just _ -> [mkDiag Hint "known variable shadowed" v] |
|---|
| 428 | Nothing -> [] |
|---|
| 429 | State.put e { varMap = Map.insert v s m } |
|---|
| 430 | addDiags $ ds ++ checkWithOtherMap varS opS (opMap e) i |
|---|
| 431 | ++ checkWithOtherMap varS predS (predMap e) i |
|---|
| 432 | ++ checkNamePrefix i |
|---|
| 433 | |
|---|
| 434 | addOpTo :: Id -> OpType -> OpMap -> OpMap |
|---|
| 435 | addOpTo k v m = |
|---|
| 436 | let l = Map.findWithDefault Set.empty k m |
|---|
| 437 | in Map.insert k (Set.insert v l) m |
|---|
| 438 | |
|---|
| 439 | -- | extract the sort from an analysed term |
|---|
| 440 | sortOfTerm :: TERM f -> SORT |
|---|
| 441 | sortOfTerm t = case t of |
|---|
| 442 | Qual_var _ ty _ -> ty |
|---|
| 443 | Application (Qual_op_name _ ot _) _ _ -> res_OP_TYPE ot |
|---|
| 444 | Sorted_term _ ty _ -> ty |
|---|
| 445 | Cast _ ty _ -> ty |
|---|
| 446 | Conditional t1 _ _ _ -> sortOfTerm t1 |
|---|
| 447 | _ -> genName "unknown" |
|---|
| 448 | |
|---|
| 449 | -- | create binding if variables are non-null |
|---|
| 450 | mkForall :: [VAR_DECL] -> FORMULA f -> Range -> FORMULA f |
|---|
| 451 | mkForall vl f ps = if null vl then f else Quantification Universal vl f ps |
|---|
| 452 | |
|---|
| 453 | -- | convert a singleton variable declaration into a qualified variable |
|---|
| 454 | toQualVar :: VAR_DECL -> TERM f |
|---|
| 455 | toQualVar (Var_decl v s ps) = |
|---|
| 456 | if isSingle v then Qual_var (head v) s ps else error "toQualVar" |
|---|
| 457 | |
|---|
| 458 | |
|---|