Changeset 1722 for branches

Show
Ignore:
Timestamp:
24.09.2003 21:00:02 (6 years ago)
Author:
2maeder
Message:

implemented type inference and made static analysis compile

Location:
branches/raw-kinds/HasCASL
Files:
11 modified

Legend:

Unmodified
Added
Removed
  • branches/raw-kinds/HasCASL/As.hs

    r1701 r1722  
    1818 
    1919import Common.Id 
     20import Common.Keywords 
    2021import Common.AS_Annotation  
     22import HasCASL.HToken 
    2123 
    2224data BasicSpec = BasicSpec [Annoted BasicItem] 
     
    6466data Kind = Universe [Pos] 
    6567          | MissingKind  
    66           | Downset (Maybe Token) Type RawKind [Pos] 
    67           | ClassKind ClassId RawKind 
     68          | Downset (Maybe Token) Type Kind [Pos] 
     69          | ClassKind ClassId Kind 
    6870          | Intersection [Kind] [Pos] 
    6971          | FunKind ExtKind Kind [Pos] 
     
    7476type RawKind = Kind 
    7577 
    76 star :: Kind 
     78star, starPlus, funKind, prodKind :: RawKind 
    7779star = Universe [] 
     80 
     81starPlus = ExtKind star CoVar [] 
     82 
     83funKind = FunKind (ExtKind star ContraVar []) 
     84             (FunKind starPlus star []) [] 
     85prodKind = FunKind starPlus (FunKind starPlus star []) [] 
    7886 
    7987data TypeItem  = TypeDecl [TypePattern] Kind [Pos] 
     
    119127 
    120128data Arrow = FunArr| PFunArr | ContFunArr | PContFunArr  
    121              deriving (Show, Eq, Ord) 
     129             deriving (Eq, Ord) 
     130 
     131instance Show Arrow where 
     132    show FunArr = funS 
     133    show PFunArr = pFun 
     134    show ContFunArr = contFun 
     135    show PContFunArr = pContFun  
     136 
     137arrowId :: Arrow -> Id 
     138arrowId a = Id (map mkSimpleId [place, show a, place]) [] [] 
     139 
     140productId :: Id 
     141productId = Id (map mkSimpleId [place, timesS, place]) [] [] 
    122142 
    123143data Pred = IsIn ClassId [Type] 
  • branches/raw-kinds/HasCASL/AsToLe.hs

    r1673 r1722  
    1717import Common.Lib.State 
    1818import qualified Common.Lib.Map as Map 
    19 import qualified Common.Lib.Set as Set 
    2019import Common.Named 
    2120import Common.Result 
     
    2827import HasCASL.TypeCheck 
    2928import Data.Maybe 
     29import Data.List 
    3030 
    3131-- | basic analysis 
     
    4949diffClass :: ClassInfo -> ClassInfo -> Maybe ClassInfo 
    5050diffClass c1 c2 =  
    51     let diff = Set.difference (superClasses c1) (superClasses c2) 
    52     in if Set.isEmpty diff 
     51    let diff = classKinds c1 \\ classKinds c2 
     52    in if null diff 
    5353       then Nothing 
    54        else Just c1 { superClasses = diff } 
     54       else Just c1 { classKinds = diff } 
    5555 
    5656-- | compute difference of type infos 
  • branches/raw-kinds/HasCASL/ClassAna.hs

    r1701 r1722  
    3939    case c of 
    4040    Universe _ -> c 
    41     MissingKind -> error "rawKind" 
     41    MissingKind -> error "rawKind1" 
    4242    ClassKind _ rk -> rawKind rk 
    4343    Downset _ _ rk _ -> rawKind rk 
    44     Intersection l _ ->  
    45         foldl1 ( \ k1 k2 ->  
    46                  fromJust $ minKind True k1 k2) $ map rawKind l 
     44    Intersection l _ -> if null l then error "rawKind2" 
     45                                              else rawKind $ head l 
    4746    FunKind e k ps  -> FunKind (rawKind e) (rawKind k) ps 
    4847    ExtKind k v ps -> ExtKind (rawKind k) v ps  
    49  
    50  
    5148 
    5249checkIntersection :: RawKind -> [Kind] -> [Diagnosis] 
    5350checkIntersection _ [] = [] 
    5451checkIntersection k (f:r) =  
    55     let m = minKind True k (rawKind f) in 
    56         case m of  
    57         Nothing ->  
     52        case k == rawKind f of  
     53        False ->  
    5854              Diag Error ("incompatible kind of: " ++ showPretty f ""  
    5955                ++  "\n  for raw kind: " ++ showPretty k "") 
    6056              (posOf [f]) : checkIntersection k r 
    61         Just n -> checkIntersection n r 
     57        True -> checkIntersection k r 
    6258 
    63 eqRawKindDiag :: (PosItem a, PrettyPrint a) =>  
     59diffKindDiag :: (PosItem a, PrettyPrint a) =>  
    6460                 a -> RawKind -> RawKind -> [Diagnosis] 
    65 eqRawKindDiag a k1 k2 =  
    66     if k1 == k2 then [] 
    67        else [ Diag Error 
     61diffKindDiag a k1 k2 =  
     62           [ Diag Error 
    6863              ("incompatible kind of: " ++ showPretty a "" ++ expected k1 k2) 
    6964            $ posOf [a] ] 
     
    10499    do let k3 = rawKind k1 
    105100           k4 = rawKind k2 
    106        addDiags (eqRawKindDiag p k3 k4) 
     101       if k3 == k4 then return () 
     102          else addDiags $ diffKindDiag p k1 k2 
  • branches/raw-kinds/HasCASL/ClassDecl.hs

    r1673 r1722  
    1515import HasCASL.As 
    1616import qualified Common.Lib.Map as Map 
    17 import qualified Common.Lib.Set as Set 
    1817import Common.Id 
    1918import HasCASL.Le 
     
    2423import HasCASL.ClassAna 
    2524import HasCASL.TypeAna 
    26 import HasCASL.Merge 
    2725 
    2826-- --------------------------------------------------------------------------- 
     
    3331anaClassDecls (ClassDecl cls k ps) =  
    3432    do ak <- anaKind k 
    35        mapM_ (addClassDecl ak Set.empty Nothing) cls 
     33       mapM_ (addClassDecl ak) cls 
    3634       return $ ClassDecl cls ak ps 
    37  
    38 anaClassDecls (SubclassDecl _ _ (Downset _) _) = error "anaClassDecl" 
    39 anaClassDecls (SubclassDecl cls k sc@(Intersection supcls ps) qs) = 
    40     do ak <- anaKind k 
    41        if Set.isEmpty supcls then  
    42           do addDiags [Diag Warning 
    43                           "redundant universe class"  
    44                          $ headPos (ps ++ qs)] 
    45              mapM_ (addClassDecl ak supcls Nothing) cls 
    46              return $ SubclassDecl cls ak sc qs 
    47           else let (hd, tl) = Set.deleteFindMin supcls in  
    48               if Set.isEmpty tl then 
    49                  do e <- get  
    50                     mk <- anaClassId hd 
    51                     case mk of  
    52                         Nothing -> do  
    53                           put e 
    54                           addClassDecl ak tl Nothing hd 
    55                           addDiags [mkDiag Warning  
    56                                       "implicit declaration of superclass" hd] 
    57                         Just sk -> checkKinds hd sk ak 
    58                     mapM_ (addClassDecl ak (Set.single hd) Nothing) cls 
    59                     return $ SubclassDecl cls ak sc qs 
    60                   else do (sk, newSc@(Intersection newSups _)) <- anaClass sc 
    61                           checkKinds hd sk ak 
    62                           mapM_ (addClassDecl ak newSups Nothing) cls 
    63                           return $ SubclassDecl cls ak newSc qs 
    64  
    65  
    66 anaClassDecls (ClassDefn ci k ic ps) = 
    67     do ak <- anaKind k 
    68        (dk, newIc) <- anaClass ic 
    69        checkKinds ci dk ak 
    70        (newKind, _, mDefn) <- addClassDecl ak Set.empty (Just newIc) ci  
    71        return $ case mDefn of  
    72            Nothing -> ClassDecl [ci] newKind ps  
    73            Just newDefn -> ClassDefn ci newKind newDefn ps 
    74  
    75 anaClassDecls (DownsetDefn ci v t ps) =  
    76     do mt <- anaStarType t  
    77        (newKind, _, mDefn) <- addClassDecl star Set.empty (fmap Downset mt) ci 
    78        return $ case mDefn of  
    79            Nothing -> ClassDecl [ci] newKind ps  
    80            Just newDefn -> case newDefn of 
    81                Downset newT -> DownsetDefn ci v newT ps 
    82                _ -> ClassDefn ci newKind newDefn ps 
    8335 
    8436-- --------------------------------------------------------------------------- 
     
    9143 
    9244-- | store a class  
    93 addClassDecl :: Kind -> Set.Set ClassId -> Maybe Class  
    94              -> ClassId -> State Env (Kind, Set.Set ClassId, Maybe Class) 
     45addClassDecl :: Kind -> ClassId  
     46             -> State Env () 
    9547-- check with merge 
    96 addClassDecl kind sups defn ci =  
     48addClassDecl kind ci =  
    9749    if showId ci "" == "Type" then  
    9850       do addDiags [mkDiag Error  
    9951                    "illegal universe class declaration" ci] 
    100           return (kind, Set.empty, Nothing) 
    10152    else do 
    10253       cMap <- gets classMap 
    10354       case Map.lookup ci cMap of 
    10455            Nothing -> do putClassMap $ Map.insert ci   
    105                                       newClassInfo { classKind = kind 
    106                                                    , superClasses = sups 
    107                                                    , classDefn = defn } cMap 
    108                           return (kind, sups, defn) 
     56                              ClassInfo { classKinds = [kind] } cMap 
    10957            Just info -> do  
    110                 addDiags [mkDiag Warning "redeclared class" ci] 
    111                 let oldDefn = classDefn info 
    112                     oldSups = superClasses info 
    113                     oldKind = classKind info 
    114                     (ds, newDefn) = case (oldDefn, defn) of  
    115                          (Nothing, Nothing) -> ([], Nothing) 
    116                          (Just _, Nothing) ->  
    117                              ([mkDiag Error  
    118                                "alias class cannot become a real class" ci] 
    119                               , oldDefn) 
    120                          (Nothing, Just _) ->  
    121                              ([mkDiag Error  
    122                                "class cannot become an alias class" ci] 
    123                               , Nothing) 
    124                          (Just d1, Just d2) ->  
    125                              let Result es _ = merge d1 d2 in 
    126                                  (map (improveDiag ci) es, oldDefn) 
    127                 checkKinds ci kind oldKind 
    128                 addDiags ds  
    129                 possSups <- getLegalSuperClasses ci oldSups sups 
    130                 let newSups = case newDefn of Just _ -> Set.empty 
    131                                               Nothing -> possSups 
    132                 putClassMap $ Map.insert ci info  
    133                                       { superClasses = newSups 
    134                                       , classDefn = newDefn } cMap 
    135                 return (oldKind, newSups, newDefn) 
     58                let superClasses = classKinds info 
     59                checkKinds ci kind $ head superClasses 
     60                if kind `elem` superClasses then 
     61                   addDiags [mkDiag Warning "redeclared class" ci] 
     62                   else putClassMap $ Map.insert ci info  
     63                                      { classKinds = kind:superClasses } cMap 
    13664 
    137 getLegalSuperClasses :: ClassId -> Set.Set ClassId 
    138                      -> Set.Set ClassId -> State Env (Set.Set ClassId) 
    139 getLegalSuperClasses ci oldCs ses = 
    140     do ce <- gets classMap 
    141        let cs = Set.toList ses 
    142            scs = zip (map (allSuperClasses ce) cs) cs 
    143            (scycs, sOk) = partition ((ci `Set.member`) . fst) scs 
    144            defCs = map snd sOk 
    145            newCs = Set.fromList defCs `Set.union` oldCs 
    146            cycles = map snd scycs 
    147            dubs = filter (`Set.member` allSuperClasses ce ci) defCs 
    148            myDiag k s l = Diag k (s ++ " '" ++ showClassList l "'") 
    149                           $ posOfId $ head l 
    150            in do if null cycles then return () 
    151                     else addDiags [myDiag Error  
    152                               "cyclic class relation via" cycles] 
    153                  if null dubs then return () 
    154                     else addDiags [myDiag Warning  
    155                               "already known as super class" dubs] 
    156                  return newCs 
     65-- cycle check missing 
    15766 
    15867showClassList :: [ClassId] -> ShowS 
    15968showClassList is = showParen (not $ isSingle is) 
    16069                   $ showSepList ("," ++) showId is 
    161  
    162 -- check with merge 
    163 mergeDefns :: ClassId -> Class -> Class -> [Diagnosis] 
    164  
    165 mergeDefns ci (Downset oldT) (Downset t) = 
    166     if oldT == t then [] else wrongClassDecl ci 
    167  
    168 mergeDefns ci (Intersection oldIs _) (Intersection is _) = 
    169        if oldIs == is then [] 
    170           else wrongClassDecl ci 
    171  
    172 mergeDefns ci _ _ = wrongClassDecl ci 
    17370 
    17471wrongClassDecl :: ClassId -> [Diagnosis] 
  • branches/raw-kinds/HasCASL/DataAna.hs

    r1673 r1722  
    1919import Common.Result 
    2020import HasCASL.Le 
    21 import HasCASL.ClassAna 
    2221import HasCASL.TypeAna 
    2322import Data.Maybe 
  • branches/raw-kinds/HasCASL/Merge.hs

    r1673 r1722  
    7171 
    7272instance Mergeable ClassInfo where 
    73     merge c1 c2 = do k <- merge (classKind c1) (classKind c2) 
    74                      d <- merge (classDefn c1) (classDefn c2) 
    75                      let s1 = superClasses c1 
    76                          s2 = superClasses c2 
    77                      if s1 == s2 then  
    78                         return $ ClassInfo s1 k d  
     73    merge c1 c2 = if c1 == c2 then  
     74                        return c1 
    7975                        else fail "merge: non-equal super classes" 
    8076 
     
    8278    merge k1 k2 = if k1 == k2 then return k1 
    8379                  else fail "merge: non-equal kinds" 
    84  
    85 instance Mergeable Class where 
    86     merge c1@(Downset t1) (Downset t2) = 
    87         if t1 == t2 then return c1 
    88            else fail "inconsistent downset" 
    89     merge c1@(Intersection i1 _) (Intersection i2 _) = 
    90        if i1 == i2 then return c1 
    91           else fail "inconsistent intersection class"  
    92     merge _ _ = fail "inconsistent class redefinition" 
    9380 
    9481mergeList :: Eq a => [a] -> [a] -> Result [a] 
  • branches/raw-kinds/HasCASL/PrintAs.hs

    r1701 r1722  
    138138 
    139139instance PrettyPrint Arrow where  
    140     printText0 _ FunArr = text funS 
    141     printText0 _ PFunArr = text pFun 
    142     printText0 _ ContFunArr = text contFun 
    143     printText0 _ PContFunArr = text pContFun  
    144  
     140    printText0 _ a = text $ show a 
    145141 
    146142instance PrettyPrint Quantifier where  
  • branches/raw-kinds/HasCASL/TypeAna.hs

    r1716 r1722  
    2424import Common.Result 
    2525import Common.Lib.State 
     26import Common.PrettyPrint 
    2627 
    2728-- -------------------------------------------------------------------------- 
     
    2930-- -------------------------------------------------------------------------- 
    3031 
    31 anaKind :: Kind -> State Env (Maybe Kind) 
    32 anaKind k =  
     32toKind :: Maybe Kind -> Kind  
     33toKind mk = case mk of Nothing -> star 
     34                       Just k -> k 
     35 
     36anaKind :: Kind -> State Env Kind 
     37anaKind k = fmap toKind (anaKindM k) 
     38 
     39anaKindM :: Kind -> State Env (Maybe Kind) 
     40anaKindM k =  
    3341    case k of 
    3442    Universe _  -> return (Just k) 
     
    4048                                               Downset v newT rk ps 
    4149    ClassKind ci _ -> anaClassId ci 
    42     Intersection ks ps -> do mks <- mapM anaKind ks 
     50    Intersection ks ps -> do mks <- mapM anaKindM ks 
    4351                             let newKs = catMaybes mks  
    4452                             if null newKs then return Nothing 
     
    5058                                        else do addDiags ds 
    5159                                                return Nothing 
    52     FunKind k1 k2 ps -> do m1 <- anaKind k1 
    53                            m2 <- anaKind k2  
     60    FunKind k1 k2 ps -> do m1 <- anaKindM k1 
     61                           m2 <- anaKindM k2  
    5462                           return $ do k3 <- m1 
    5563                                       k4 <- m2 
    5664                                       return $ FunKind k3 k4 ps 
    57     ExtKind ek v ps -> do m <- anaKind ek 
     65    ExtKind ek v ps -> do m <- anaKindM ek 
    5866                          return $ do nk <- m 
    5967                                      return $ ExtKind nk v ps 
     
    197205    case j of  
    198206    ExtKind ek _ _ -> checkTypeRawKind t ek 
    199     _ -> do k <- inferRawKind t  
    200             addDiags (eqRawKindDiag t j k) 
     207    _ -> do k <- inferRawKind t 
     208            if k == j then return () else addDiags $ diffKindDiag t j k 
    201209 
    202210getIdRawKind :: Id -> State Env Kind 
     
    213221       case Map.lookup i tk of 
    214222       Nothing -> Nothing 
    215        Just (TypeInfo k l _ _) -> Just k 
     223       Just (TypeInfo k _ _ _) -> Just k 
    216224 
    217225-- --------------------------------------------------------------------------- 
     
    219227-- --------------------------------------------------------------------------- 
    220228 
    221 inferKind :: Type -> State Env (Maybe Kind) 
    222 inferKind (TypeName i _ _) = getIdKind i 
    223 {- 
    224 inferKind (TypeAppl t1 t2) =  
    225     do mk1 <- inferKind t1  
     229checkMaybeKinds :: (PosItem a, PrettyPrint a) =>  
     230              a -> Maybe Kind -> Maybe Kind -> State Env (Maybe Kind) 
     231checkMaybeKinds a mk1 mk2 = 
     232    case mk1 of 
     233           Nothing -> return mk2 
     234           Just k1 -> case mk2 of  
     235               Nothing -> return mk1 
     236               Just k2 ->  
     237                   if lesserKind k1 k2 then return mk1 
     238                      else if lesserKind k2 k1 then return mk2 
     239                      else do addDiags $ diffKindDiag a k1 k2 
     240                              return Nothing 
     241 
     242checkFunKind :: Maybe Kind -> Type -> Type -> Kind -> State Env (Maybe Kind) 
     243checkFunKind mk t1 t2 k1 =  
     244    case k1 of  
     245        FunKind fk ak _ -> do  
     246            mk2 <- inferKind (Just fk) t2 
     247            case mk2 of  
     248                Nothing -> return mk 
     249                Just _ -> checkMaybeKinds (TypeAppl t1 t2) mk (Just ak) 
     250        Intersection l ps -> do 
     251            ml <- mapM (checkFunKind mk t1 t2) l 
     252            let ks = catMaybes ml 
     253            return $ if null ks then Nothing else if isSingle ks then 
     254               Just $ head ks else Just $ Intersection ks ps 
     255        ClassKind _ k -> checkFunKind mk t1 t2 k 
     256        Downset _ _ k _ -> checkFunKind mk t1 t2 k 
     257        ExtKind k _ _ -> checkFunKind mk t1 t2 k 
     258        _ -> do addDiags [mkDiag Error  
     259                                 "expected higher order kind for type" t1] 
     260                return Nothing 
     261 
     262inferKind :: Maybe Kind -> Type -> State Env (Maybe Kind) 
     263inferKind mk (TypeName i _ _) =  
     264    do m <- getIdKind i  
     265       checkMaybeKinds i mk m 
     266 
     267inferKind mk (TypeAppl t1 t2) =  
     268    do mk1 <- inferKind Nothing t1  
    226269       case mk1 of  
    227                   
    228                FunKind _ _ _ -> checkTypeKind t2 k1  
    229                Intersection  
    230  
    231 if ak lesserKind k1 then Just k2 
    232                      else Nothing 
    233                Intersection ks _ ->  
    234  
    235  
    236        let l = concatMap ( \ fk ->  
    237                catMaybes ( \ ak ->  
    238                case fk of  
    239                FunKind k1 k2 _ -> if ak lesserKind k1 then Just k2 
    240                      else Nothing 
    241                Intersection ks _ ->  
    242  
    243  
    244 do checkTypeKind t2 k1 
    245                                       return k2 
    246                 _ -> do addDiags [mkDiag Error  
    247                                   "incompatible kind of type" t1]  
    248                         return mk1 
    249 inferKind b (FunType t1 _ t2 _) =  
    250     do checkTypeKind t1 star  
    251        checkTypeKind t2 star 
    252        return star  
    253 inferKind b (ProductType ts _) =  
    254     do mapM_ ( \ t -> checkTypeKind t star) ts  
    255        return star  
    256 inferKind b (LazyType t _) =  
    257     do checkTypeKind t star 
    258        return star  
    259 inferKind b (TypeToken t) = getIdKind $ simpleIdToId t 
    260 inferKind b (KindedType t k _) = 
    261     do checkTypeKind t k 
    262        return k 
    263 inferKind b t = 
     270           Nothing -> return mk 
     271           Just k1 -> checkFunKind mk t1 t2 k1 
     272 
     273inferKind mk (FunType t1 a t2 _) =  
     274    do let i = arrowId a 
     275       mfk <- getIdKind i 
     276       let fk = case mfk of Nothing -> funKind 
     277                            Just k -> k 
     278           tn = TypeName i fk 0 
     279       inferKind mk (TypeAppl (TypeAppl tn t1) t2) 
     280 
     281inferKind mk t@(ProductType ts _) =  
     282    if null ts then checkMaybeKinds t mk (Just star) 
     283    else do mpk <- getIdKind productId 
     284            let pk = case mpk of Nothing -> prodKind 
     285                                 Just k -> k 
     286                tn = TypeName productId pk 0 
     287                mkAppl [t1] = t1 
     288                mkAppl (t1:tr) = TypeAppl (TypeAppl tn t1) $ mkAppl tr 
     289                mkAppl [] = error "inferKind: mkAppl" 
     290            inferKind mk $ mkAppl ts 
     291inferKind mk (LazyType t _) =  
     292    do inferKind mk t 
     293inferKind mk (KindedType t k _) = 
     294    do mk2 <- inferKind (Just k) t 
     295       checkMaybeKinds t mk mk2 
     296inferKind _ t = 
    264297    do addDiags [mkDiag Error "unresolved type" t]  
    265        return star 
    266  
    267 checkTypeKind :: Type -> Kind -> State Env () 
    268 checkTypeKind t j = do k <- inferKind t  
    269                        checkKinds t j k 
    270  
    271 -} 
     298       return Nothing 
    272299 
    273300getIdKind :: Id -> State Env (Maybe Kind) 
     
    284311       case Map.lookup i tk of 
    285312       Nothing -> Nothing 
    286        Just (TypeInfo k l _ _) -> Just $   
     313       Just (TypeInfo _ l _ _) -> Just $   
    287314           if isSingle l then head l else Intersection l [] 
    288315 
  • branches/raw-kinds/HasCASL/TypeCheck.hs

    r1673 r1722  
    6363                star, AliasTypeDefn $ simpleTypeScheme logicalType),  
    6464               (simpleIdToId $ mkSimpleId "Pred",  
    65                 KindAppl star star [], 
     65                FunKind star star [], 
    6666                AliasTypeDefn defType)] 
    6767 
  • branches/raw-kinds/HasCASL/TypeDecl.hs

    r1673 r1722  
    156156    do k <- anaKind kind 
    157157       checkKinds pat star k 
    158        newDerivs <- case derivs of  
    159                    Just c -> do (dk, newC) <- anaClass c 
    160                                 checkKinds newC star dk 
    161                                 return $ Just newC 
    162                    Nothing -> return Nothing 
    163        let Result ds m = convertTypePattern pat 
     158       cs <- mapM anaClassId derivs 
     159       let jcs = catMaybes cs 
     160       mapM (checkKinds pat star) jcs 
     161       let newDerivs = foldr( \ ck l -> case ck of  
     162                                           ClassKind ci _ -> ci:l 
     163                                           _ -> l) [] jcs 
     164           Result ds m = convertTypePattern pat 
    164165       addDiags ds 
    165166       case m of  
     
    189190anaPseudoType mk (TypeScheme tArgs (q :=> ty) p) = 
    190191    do k <- case mk of  
    191             Nothing -> return star 
    192             Just j -> anaKind j 
     192            Nothing -> return Nothing 
     193            Just j -> anaKindM j 
    193194       tm <- gets typeMap    -- save global variables   
    194195       mapM_ anaTypeVarDecl tArgs 
     
    208209    if null tArgs then k 
    209210       else typeArgsListToKind (init tArgs)  
    210             (KindAppl (( \ (TypeArg _ xk _ _) -> xk) $ last tArgs) k [])  
     211            (FunKind (( \ (TypeArg _ xk _ _) -> xk) $ last tArgs) k [])  
    211212 
    212213-- | convert mixfix type patterns to ids 
  • branches/raw-kinds/HasCASL/VarDecl.hs

    r1673 r1722  
    1717import HasCASL.ClassAna 
    1818import qualified Common.Lib.Map as Map 
    19 import qualified Common.Lib.Set as Set 
    2019import Common.Id 
    2120import HasCASL.Le 
     
    4140-- type args not yet considered for kind construction  
    4241addTypeId defn _ kind i =  
    43     do nk <- expandKind kind 
     42    do let nk = rawKind kind 
    4443       if placeCount i <= kindArity TopLevel nk then 
    4544          do addTypeKind defn i kind 
     
    5958addSingleTypeKind d i k =  
    6059    do tk <- gets typeMap 
     60       let rk = rawKind k 
    6161       case Map.lookup i tk of 
    6262              Nothing -> putTypeMap $ Map.insert i  
    63                          (TypeInfo k [] [] d) tk 
     63                         (TypeInfo rk [k] [] d) tk 
    6464              Just (TypeInfo ok ks sups defn) ->  
    65                   -- check with merge 
    66                   do checkKinds i k ok 
    67                      if any (==k) (ok:ks) 
    68                         then addDiags [mkDiag Warning  
     65                  if rk == ok 
     66                        then if k `elem` ks then 
     67                             addDiags [mkDiag Warning  
    6968                                 "redeclared type" i] 
    7069                                 else putTypeMap $ Map.insert i  
    7170                                         (TypeInfo ok 
    7271                                               (k:ks) sups defn) tk 
     72                        else addDiags $ diffKindDiag i ok k  
    7373 
    7474-- | analyse a type argument 
     
    8686-- | compute arity from a 'Kind' 
    8787kindArity :: ApplMode -> Kind -> Int 
    88 kindArity m (KindAppl k1 k2 _) = 
    89     case m of  
    90                TopLevel -> kindArity OnlyArg k1 +  
    91                            kindArity TopLevel k2 
    92                OnlyArg -> 1 
    93 kindArity m (ExtClass _ _ _) = case m of 
    94                              TopLevel -> 0 
    95                              OnlyArg -> 1 
     88kindArity m k =  
     89    case k of  
     90    FunKind k1 k2 _ -> case m of  
     91                       TopLevel -> kindArity OnlyArg k1 +  
     92                                   kindArity TopLevel k2 
     93                       OnlyArg -> 1 
     94    Universe _ -> case m of 
     95                  TopLevel -> 0 
     96                  OnlyArg -> 1 
     97    ClassKind _ ck -> kindArity m ck 
     98    Downset _ _ dk _ -> kindArity m dk 
     99    Intersection (k1:_) _ -> kindArity m k1 
     100    ExtKind ek _ _ -> kindArity m ek 
     101    _ -> error "kindArity" 
     102 
    96103 
    97104-- --------------------------------------------------------------------------- 
     
    146153    anaTypeVarDecl t >>= (return . fmap GenTypeVarDecl) 
    147154 
    148 convertTypeToClass :: Type -> State Env (Maybe Class) 
    149 convertTypeToClass (TypeToken t) =  
    150        if tokStr t == "Type" then return $ Just universe else do 
    151           let ci = simpleIdToId t 
    152           e <- get                                              
    153           mk <- anaClassId ci 
    154           case mk of  
    155                   Nothing -> do put e 
    156                                 return Nothing 
    157                   _ -> return $ Just $ Intersection  (Set.single ci) [] 
    158 convertTypeToClass (BracketType Parens ts ps) =  
    159        do cs <- mapM convertTypeToClass ts 
    160           if all isJust cs then  
    161              return $ Just $ Intersection (Set.unions $ map iclass $  
    162                                     catMaybes cs) ps 
    163              else return Nothing 
    164  
    165 convertTypeToClass _ =  
    166     do return Nothing 
    167  
    168155convertTypeToKind :: Type -> State Env (Maybe Kind) 
    169156convertTypeToKind (FunType t1 FunArr t2 ps) =  
     
    171158       mk2 <- convertTypeToKind t2 
    172159       case (mk1, mk2) of 
    173            (Just k1, Just k2) -> return $ Just $ KindAppl k1 k2 ps 
     160           (Just k1, Just k2) -> case k2 of  
     161               ExtKind _ _ _ -> return Nothing 
     162               _ -> return $ Just $ FunKind k1 k2 ps 
    174163           _ -> return Nothing 
    175164 
     165convertTypeToKind (BracketType Parens [] _) =  
     166    return Nothing 
    176167convertTypeToKind (BracketType Parens [t] _) =  
    177     do convertTypeToKind t 
    178  
     168    convertTypeToKind t 
     169convertTypeToKind (BracketType Parens ts ps) =  
     170       do cs <- mapM convertTypeToKind ts 
     171          if all isJust cs then  
     172             do let k:ks = catMaybes cs  
     173                    rk = rawKind k 
     174                if all ((==rk) . rawKind) ks then 
     175                   return $ Just $ Intersection (k:ks) ps 
     176                   else return Nothing 
     177             else return Nothing 
    179178convertTypeToKind (MixfixType [t1, TypeToken t]) =  
    180179    let s = tokStr t  
     
    185184    in case v of  
    186185              InVar -> do return Nothing 
    187               _ -> do mk1 <- convertTypeToClass t1 
     186              _ -> do mk1 <- convertTypeToKind t1 
    188187                      case mk1 of  
    189                           Just k1 -> return $ Just $ ExtClass k1 v [tokPos t] 
     188                          Just k1 -> return $ Just $ ExtKind k1 v [tokPos t] 
    190189                          _ -> return Nothing 
    191 convertTypeToKind t =  
    192     do mc <- convertTypeToClass t 
    193        return $ fmap ( \ c -> ExtClass c InVar []) mc 
     190convertTypeToKind(TypeToken t) =  
     191       if tokStr t == "Type" then return $ Just $ Universe [tokPos t] else do 
     192          let ci = simpleIdToId t 
     193          e <- get                                              
     194          mk <- anaClassId ci 
     195          case mk of  
     196                  Nothing -> do put e 
     197                                return Nothing 
     198                  Just k -> return $ Just $ ClassKind ci k 
     199convertTypeToKind _ =  
     200    do return Nothing 
    194201 
    195202optAnaVarDecl :: VarDecl -> State Env (Maybe GenVarDecl)