Changeset 19959

Show
Ignore:
Timestamp:
02/20/08 18:52:10 (9 months ago)
Author:
gwern
Message:

Cleanup Str.hs and Types.hs, and add necessary bytestring updates

Location:
src/Pugs
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Meta/Str.hs

    r16504 r19959  
    1515_StrClass = mkPureClass "Str" 
    1616    [ "reverse"     ... Str.reverse 
    17     , "join"        ... Str.join 
     17    , "join"        ... Str.intercalate 
    1818    , "chop"        ... (\str -> if Str.null str then str else Str.init str) 
    1919    , "index"       ... (\str sub pos -> fromMaybe (-1) $ Str.findSubstring sub $ Str.drop pos str) 
  • src/Pugs/Types.hs

    r17044 r19959  
    1 {-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans -funbox-strict-fields -fallow-overlapping-instances -fparr #-}  
     1{-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans -funbox-strict-fields -fallow-overlapping-instances -fparr #-} 
    22{-| 
    33    Implementation Types. 
     
    1010-} 
    1111 
    12 module Pugs.Types  
     12module Pugs.Types 
    1313{- 
    1414( 
     
    1616    ClassTree, initTree, 
    1717 
    18     Cxt(..),  
     18    Cxt(..), 
    1919    cxtItem, cxtSlurpy, cxtVoid, cxtItemAny, cxtSlurpyAny, 
    2020    typeOfCxt, isSlurpyCxt, isItemCxt, isVoidCxt, 
     
    3535import qualified Data.IntSet as IntSet 
    3636import qualified Data.ByteString.Char8 as Buf -- Intentionally not UTF8! 
     37import qualified Data.ByteString as B (findSubstring) 
    3738 
    3839data Type 
     
    215216 
    216217instance ((:>:) ByteString) Pkg where 
    217     cast (MkPkg ns) = Buf.join (__"::") ns 
     218    cast (MkPkg ns) = Buf.intercalate (__"::") ns 
    218219 
    219220instance Show Var where 
     
    398399 
    399400{-| 
    400 Transform an operator name, for example @&infix:\<+\>@ or @&prefix:«[+]»@,  
     401Transform an operator name, for example @&infix:\<+\>@ or @&prefix:«[+]»@, 
    401402into its internal name (@&infix:+@ and @&prefix:[+]@ respectively). 
    402403-} 
     
    480481            _   -> (TNil, toPkg (tokenPkg afterSig)) 
    481482    afterTwi = tokenPkg (Buf.tail afterSig) 
    482     toPkg (pkg, rest) = (MkPkg pkg, rest) 
     483    toPkg (pg, rest) = (MkPkg pg, rest) 
    483484    tokenPkg :: ByteString -> ([ByteString], (VarCateg, ByteString)) 
    484485    tokenPkg str = case Buf.elemIndex ':' str of 
    485         Just idx1 -> case Buf.findSubstring (__":(") str of 
     486        Just idx1 -> case B.findSubstring (__":(") str of 
    486487            Just idxSig | idx1 == idxSig -> ([], (CNil, str)) 
    487             _ -> case Buf.findSubstring (__"::") str of 
     488            _ -> case B.findSubstring (__"::") str of 
    488489                Nothing  -> ([], (cast (Buf.take idx1 str), Buf.drop (succ idx1) str)) 
    489490                Just 0   -> tokenPkg (Buf.drop 2 str) -- '$::x' is the same as $x 
     
    491492                    | idx == idx1 -> case cast (Buf.take idx1 str) of 
    492493                        -- &infix::= should parse as infix:<:=>, not infix::<=> 
    493                         Just cat -> ([], (cat, Buf.drop (succ idx1) str)) 
     494                        Just ct -> ([], (ct, Buf.drop (succ idx1) str)) 
    494495                        -- &Infix::= should parse as Infix::<=>, not Infix:<:=> 
    495496                        _        -> let (rest, final) = tokenPkg (Buf.drop (idx + 2) str) in 
     
    497498                    | otherwise -> ([], (cast (Buf.take idx1 str), Buf.drop (succ idx1) str)) 
    498499        _ -> ([], (CNil, str)) 
    499     (name, longname) = case Buf.findSubstring (__":(") fullname of 
     500    (name, longname) = case B.findSubstring (__":(") fullname of 
    500501        Just idx -> (cast (Buf.take idx fullname), cast (Buf.drop idx fullname)) 
    501502        _        -> (cast fullname, nullID) 
     
    511512        | C_infix <- cat 
    512513        , __">>" `Buf.isPrefixOf` afterCat 
    513         , __"<<" `Buf.isSuffixOf` afterCat  
     514        , __"<<" `Buf.isSuffixOf` afterCat 
    514515        = (Buf.drop 2 (dropEnd 2 afterCat), MHyper) 
    515516        | C_prefix <- cat 
     
    518519        = case Buf.drop 2 (Buf.init afterCat) of 
    519520            maybeHyper | __">>" `Buf.isPrefixOf` maybeHyper 
    520                        , __"<<" `Buf.isSuffixOf` maybeHyper  
     521                       , __"<<" `Buf.isSuffixOf` maybeHyper 
    521522                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperScan) 
    522523            maybeHyper | __"\187" `Buf.isPrefixOf` maybeHyper 
    523                        , __"\171" `Buf.isSuffixOf` maybeHyper  
     524                       , __"\171" `Buf.isSuffixOf` maybeHyper 
    524525                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperScan) 
    525526            other -> (other, MScan) 
     
    529530        = case Buf.tail (Buf.init afterCat) of 
    530531            maybeHyper | __">>" `Buf.isPrefixOf` maybeHyper 
    531                        , __"<<" `Buf.isSuffixOf` maybeHyper  
     532                       , __"<<" `Buf.isSuffixOf` maybeHyper 
    532533                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperFold) 
    533534            maybeHyper | __"\187" `Buf.isPrefixOf` maybeHyper 
    534                        , __"\171" `Buf.isSuffixOf` maybeHyper  
     535                       , __"\171" `Buf.isSuffixOf` maybeHyper 
    535536                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperFold) 
    536537            other -> (other, MFold) 
     
    542543        = case Buf.drop 2 (dropEnd 3 afterCat) of 
    543544            maybeHyper | __">>" `Buf.isPrefixOf` maybeHyper 
    544                        , __"<<" `Buf.isSuffixOf` maybeHyper  
     545                       , __"<<" `Buf.isSuffixOf` maybeHyper 
    545546                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperScanPost) 
    546547            maybeHyper | __"\187" `Buf.isPrefixOf` maybeHyper 
    547                        , __"\171" `Buf.isSuffixOf` maybeHyper  
     548                       , __"\171" `Buf.isSuffixOf` maybeHyper 
    548549                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperScanPost) 
    549550            other -> (other, MScanPost) 
     
    553554        = case Buf.tail (dropEnd 3 afterCat) of 
    554555            maybeHyper | __">>" `Buf.isPrefixOf` maybeHyper 
    555                        , __"<<" `Buf.isSuffixOf` maybeHyper  
     556                       , __"<<" `Buf.isSuffixOf` maybeHyper 
    556557                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperFoldPost) 
    557558            maybeHyper | __"\187" `Buf.isPrefixOf` maybeHyper 
    558                        , __"\171" `Buf.isSuffixOf` maybeHyper  
     559                       , __"\171" `Buf.isSuffixOf` maybeHyper 
    559560                -> (Buf.drop 2 (dropEnd 2 maybeHyper), MHyperFoldPost) 
    560561            other -> (other, MFoldPost) 
     
    656657the tree, the result is a very large number. 
    657658 
    658 > <scook0> is deltaType supposed to be returning large positive numbers for  
     659> <scook0> is deltaType supposed to be returning large positive numbers for 
    659660>            types that are actually incompatible? 
    660661> <autrijus> that is a open design question. 
     
    691692(in this case Haskell's @(||)@). The result is thus @True@. 
    692693-} 
    693 junctivate :: (t -> t -> t) -- ^ Function to combine results over disjunctive  
     694junctivate :: (t -> t -> t) -- ^ Function to combine results over disjunctive 
    694695                            --     (@|@) types 
    695            -> (t -> t -> t) -- ^ Function to combine results over conjunctive  
     696           -> (t -> t -> t) -- ^ Function to combine results over conjunctive 
    696697                            --     (@\&@) types 
    697698           -> (Type -> Type -> t) 
    698                             -- ^ Function that will actually perform the  
     699                            -- ^ Function that will actually perform the 
    699700                            --     comparison (on non-junctive types) 
    700701           -> Type          -- ^ First type to compare 
    701702           -> Type          -- ^ Second type to compare 
    702703           -> t 
    703 junctivate or and f base target 
     704junctivate ors ands f base target 
    704705    | TypeOr t1 t2 <- target 
    705     = redo base t1 `or` redo base t2 
     706    = redo base t1 `ors` redo base t2 
    706707    | TypeOr b1 b2 <- base 
    707     = redo b1 target `or` redo b2 target 
     708    = redo b1 target `ors` redo b2 target 
    708709    | TypeAnd t1 t2 <- target 
    709     = redo base t1 `and` redo base t2 
     710    = redo base t1 `ands` redo base t2 
    710711    | TypeAnd b1 b2 <- base 
    711     = redo b1 target `and` redo b2 target 
     712    = redo b1 target `ands` redo b2 target 
    712713    | otherwise 
    713714    = f base target 
    714715    where 
    715     redo = junctivate or and f 
     716      redo = junctivate ors ands f 
    716717 
    717718-- When saying Int.isa(Scalar), Scalar is the base, Int is the target 
     
    726727 
    727728{-| 
    728 Return true if the second type (the \'target\') is derived-from or equal-to the  
     729Return true if the second type (the \'target\') is derived-from or equal-to the 
    729730first type (the \'base\'), in the context of the given class tree. 
    730731 
     
    743744-} 
    744745distanceType :: Type -> Type -> Int 
    745 distanceType base@(MkType b) target@(MkType t) =  
     746distanceType base@(MkType b) target@(MkType t) = 
    746747    IntMap.findWithDefault (distanceType base' target') (bk `shiftL` 16 + tk) initCache 
    747748--  | not (castOk base target)  = 0 
     
    787788Compatible types will produce a number indicating how distant they are. 
    788789Incompatible types produce a negative number indicating how much the base type 
    789 would need to be relaxed. If one (or both) types doesn't exist in the tree, a  
     790would need to be relaxed. If one (or both) types doesn't exist in the tree, a 
    790791large negative number is produced 
    791792