Skip to content

Commit

Permalink
hash slices
Browse files Browse the repository at this point in the history
  • Loading branch information
phischu committed Sep 9, 2014
1 parent f1db3c7 commit 6d47546
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 3 additions & 1 deletion fragnix.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ executable fragnix
tagged >=0.7.2 && <0.8,
aeson >=0.8.0.0 && <0.9,
bytestring >=0.10.4.0 && <0.11,
hashable >=1.2.2.0 && <1.3
hashable >=1.2.2.0 && <1.3,
transformers >=0.3.0.0 && <0.4
hs-source-dirs: src
default-language: Haskell2010

Expand All @@ -59,5 +60,6 @@ test-suite test
aeson >=0.8.0.0 && <0.9,
bytestring >=0.10.4.0 && <0.11,
hashable >=1.2.2.0 && <1.3,
transformers >=0.3.0.0 && <0.4,
tasty >= 0.8.1.3 && <0.9,
tasty-golden >= 2.2.2.4 && <2.3
21 changes: 19 additions & 2 deletions src/Fragnix/Resolver.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Distribution.HaskellSuite.Packages (getInstalledPackages)
import Distribution.Simple.Compiler (PackageDB(GlobalPackageDB))
import Data.Proxy (Proxy(Proxy))

import Data.Hashable (hash)

import Control.Exception (Exception,throwIO)
import Data.Typeable (Typeable)
import Data.Data (Data)
Expand Down Expand Up @@ -70,8 +72,23 @@ extractSlices scopedModule = do
return (Slice key (Fragment [pack source]) usages))

computeHashes :: [Slice] -> IO [Slice]
computeHashes slices = forM slices (\slice -> do
return slice)
computeHashes tempSlices = return (map (computeHash tempSliceMap) tempSlices) where
tempSliceMap = Map.fromList [(tempSliceID,tempSlice) | tempSlice@(Slice tempSliceID _ _) <- tempSlices]

computeHash :: Map TempID Slice -> Slice -> Slice
computeHash tempSliceMap (Slice _ fragment tempUsages) = Slice sliceID fragment usages where
sliceID = fromIntegral (hash (fragment,usages))
usages = map f tempUsages
f (Usage qualification usedName (OtherSlice tempID)) = (Usage qualification usedName (OtherSlice otherSliceID)) where
Just tempSlice = Map.lookup tempID tempSliceMap
Slice otherSliceID _ _ = computeHash tempSliceMap tempSlice
f usage = usage

replaceUsageID :: Map TempID SliceID -> Usage -> Maybe Usage
replaceUsageID sliceIDMap (Usage qualification usedName (OtherSlice tempID)) = do
sliceID <- Map.lookup tempID sliceIDMap
return (Usage qualification usedName (OtherSlice sliceID))
replaceUsageID _ usage = Just usage

type TempID = Integer

Expand Down
17 changes: 15 additions & 2 deletions src/Fragnix/Slice.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{-# LANGUAGE OverloadedStrings,StandaloneDeriving #-}
{-# LANGUAGE OverloadedStrings,StandaloneDeriving,DeriveGeneric #-}
module Fragnix.Slice where

import Data.Aeson (
ToJSON(toJSON),object,(.=),
FromJSON(parseJSON),withObject,(.:))

import GHC.Generics (Generic)
import Data.Hashable (Hashable)

import Data.Text (Text)

import Control.Applicative ((<$>),(<*>),(<|>))
Expand Down Expand Up @@ -43,15 +46,19 @@ instance FromJSON Slice where


deriving instance Show Fragment
deriving instance Generic Fragment

instance ToJSON Fragment where
toJSON (Fragment declarations) = toJSON declarations

instance FromJSON Fragment where
parseJSON = fmap Fragment . parseJSON

instance Hashable Fragment


deriving instance Show Usage
deriving instance Generic Usage

instance ToJSON Usage where
toJSON (Usage qualification usedName reference) = object [
Expand All @@ -63,10 +70,13 @@ instance FromJSON Usage where
parseJSON = withObject "usage" (\o ->
Usage <$> o .: "qualification" <*> o .: "usedName" <*> o .: "reference")

instance Hashable Usage


deriving instance Show UsedName
deriving instance Eq UsedName
deriving instance Ord UsedName
deriving instance Generic UsedName

instance ToJSON UsedName where
toJSON (VarId name) = object ["varId" .= name]
Expand All @@ -81,8 +91,11 @@ instance FromJSON UsedName where
VarSym <$> o .: "varSym" <|>
ConSym <$> o .: "conSym")

instance Hashable UsedName


deriving instance Show Reference
deriving instance Generic Reference

instance ToJSON Reference where
toJSON (OtherSlice sliceID) = object ["otherSlice" .= sliceID]
Expand All @@ -93,5 +106,5 @@ instance FromJSON Reference where
OtherSlice <$> o .: "otherSlice" <|>
Primitive <$> o .: "originalModule")


instance Hashable Reference

2 changes: 1 addition & 1 deletion src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import Fragnix.Nest (writeSlice)
import Fragnix.Compiler (compile)

main :: IO ()
main = resolve "tests/examples/HelloFragnix.hs" >>= mapM writeSlice >> compile 1 >>= print
main = resolve "tests/examples/HelloFragnix.hs" >>= mapM writeSlice >> compile 5980034736339281833 >>= print


0 comments on commit 6d47546

Please sign in to comment.