on master help sign in
Merge branch 'feature/arbitrary_tupleset' of https://github.com/YuMingLiao/project-m36 into YuMingLiao-feature/arbitrary_tupleset

fix warnings # Conflicts: # src/lib/ProjectM36/Attribute.hs # src/lib/ProjectM36/Base.hs

commitb09d2f21fe6113a01b759dbca08f3e26e502cfc4 authorAgentM <agentm@themactionfaction.com> dateWed 30 May 2018, 14:16 (8 years ago) parent53bab289ac483f
7 files changed+148−109
project-m36.cabal+2−2 src/bin/TutorialD/Interpreter/DatabaseContextIOOperator.hs+4−5 src/bin/TutorialD/tutd.hs−3 src/lib/ProjectM36/Arbitrary.hs+129 src/lib/ProjectM36/Base.hs+4−12 src/lib/ProjectM36/MyTest.hs−83 src/lib/ProjectM36/RelationalExpression.hs+9−4
project-m36.cabal+2−2
@@ -73,7 +73,6 @@ Library
7373 ,http-api-data
7474 -- used for arbitrary tuples
7575 ,QuickCheck
76− ,quickcheck-arbitrary-adt
7776--needed for remote access
7877 ,distributed-process-client-server >= 0.2.3
7978 ,distributed-process >= 0.6.6
@@ -155,7 +154,8 @@ Library
155154 ProjectM36.TypeConstructor,
156155 ProjectM36.TypeConstructorDef,
157156 ProjectM36.FileLock,
158− ProjectM36.FSType
157+ ProjectM36.FSType,
158+ ProjectM36.Arbitrary
159159 GHC-Options: -Wall -rdynamic
160160 if os(windows)
161161 Build-Depends: Win32 >= 2.3
src/bin/TutorialD/Interpreter/DatabaseContextIOOperator.hs+4−5
@@ -4,7 +4,6 @@ import ProjectM36.Base
44
55import TutorialD.Interpreter.Base
66import TutorialD.Interpreter.Types
7−import TutorialD.Interpreter.RelationalExpr
87import Text.Megaparsec
98import Text.Megaparsec.Text
109import Data.Text
@@ -20,10 +19,10 @@ createArbitraryRelationP = do
2019 reserved "createarbitraryrelation"
2120 relVarName <- identifier
2221 attrExprs <- makeAttributeExprsP :: Parser [AttributeExpr]
23− min <- fromInteger <$> integer
24− symbol "-"
25− max <- fromInteger <$> integer
26− pure $ CreateArbitraryRelation relVarName attrExprs (min,max)
22+ min' <- fromInteger <$> integer
23+ _ <- symbol "-"
24+ max' <- fromInteger <$> integer
25+ pure $ CreateArbitraryRelation relVarName attrExprs (min',max')
2726
2827dbioexprP :: String -> (Text -> [TypeConstructor] -> Text -> DatabaseContextIOExpr) -> Parser DatabaseContextIOExpr
2928dbioexprP res adt = do
src/bin/TutorialD/tutd.hs−3
@@ -82,9 +82,6 @@ printWelcome = do
8282
8383main :: IO ()
8484main = do
85− hSetEncoding stdin utf8
86− hSetEncoding stdout utf8
87− hSetEncoding stderr utf8
8885 interpreterConfig <- execParser opts
8986 let connInfo = connectionInfoForConfig interpreterConfig
9087 fscheck <- checkFSType (checkFSForConfig interpreterConfig) (fromMaybe NoPersistence (persistenceStrategyForConfig interpreterConfig))
src/lib/ProjectM36/Arbitrary.hsnew file+129
@@ -0,0 +1,129 @@
1+{-# LANGUAGE ExistentialQuantification,DeriveGeneric,DeriveAnyClass,TypeSynonymInstances,FlexibleInstances,OverloadedStrings #-}
2+{-# OPTIONS_GHC -fno-warn-orphans #-}
3+
4+module ProjectM36.Arbitrary where
5+import ProjectM36.Base
6+import ProjectM36.Error
7+import ProjectM36.AtomType
8+import ProjectM36.Attribute (atomType)
9+import ProjectM36.DataConstructorDef as DCD
10+import qualified Data.Vector as V
11+import Data.Text (Text,pack)
12+import Test.QuickCheck
13+import qualified Data.ByteString.Char8 as B
14+import Data.Time
15+import Control.Monad.Reader
16+
17+arbitrary' :: AtomType -> WithTCMap Gen (Either RelationalError Atom)
18+arbitrary' IntegerAtomType = do
19+ i <- lift (arbitrary :: Gen Integer)
20+ pure $ Right $ IntegerAtom i
21+
22+arbitrary' (RelationAtomType attrs) = do
23+ tcMap <-ask
24+ maybeRel <- lift $ runReaderT (arbitraryRelation attrs (0,5)) tcMap
25+ case maybeRel of
26+ Left err -> pure $ Left err
27+ Right rel -> pure $ Right $ RelationAtom rel
28+
29+arbitrary' IntAtomType = do
30+ i <- lift (arbitrary :: Gen Int)
31+ pure $ Right $ IntAtom i
32+
33+arbitrary' DoubleAtomType = do
34+ i <- lift (arbitrary :: Gen Double)
35+ pure $ Right $ DoubleAtom i
36+
37+arbitrary' TextAtomType = do
38+ i <- lift (arbitrary :: Gen Text)
39+ pure $ Right $ TextAtom i
40+
41+arbitrary' DayAtomType = do
42+ i <- lift (arbitrary :: Gen Day)
43+ pure $ Right $ DayAtom i
44+
45+arbitrary' DateTimeAtomType = do
46+ i <- lift (arbitrary :: Gen UTCTime)
47+ pure $ Right $ DateTimeAtom i
48+
49+arbitrary' ByteStringAtomType = do
50+ bs <- lift (arbitrary :: Gen B.ByteString)
51+ pure $ Right $ ByteStringAtom bs
52+
53+arbitrary' BoolAtomType = do
54+ i <- lift (arbitrary :: Gen Bool)
55+ pure $ Right $ BoolAtom i
56+
57+arbitrary' (IntervalAtomType atomTy) = do
58+ tcMap <- ask
59+ eitherAtomType <- lift $ runReaderT (arbitrary' atomTy) tcMap
60+ case eitherAtomType of
61+ Left err -> pure $ Left err
62+ Right atomType' -> do
63+ a <- lift $ (arbitrary :: Gen Bool)
64+ b <- lift $ (arbitrary :: Gen Bool)
65+ pure $ Right $ IntervalAtom atomType' atomType' a b
66+
67+arbitrary' constructedAtomType@(ConstructedAtomType tcName tvMap) = do
68+ tcMap <- ask
69+ let maybeTCons = findTypeConstructor tcName tcMap
70+ let eitherTCons = maybeToRight (NoSuchTypeConstructorName tcName) maybeTCons
71+ let eitherDCDefs = snd <$> eitherTCons
72+ let eitherGenDCDef = elements <$> eitherDCDefs
73+ case eitherGenDCDef of
74+ Left err -> pure $ Left err
75+ Right genDCDef -> do
76+ dcDef <- lift $ genDCDef
77+ case resolvedAtomTypesForDataConstructorDefArgs tcMap tvMap dcDef of
78+ Left err -> pure $ Left err
79+ Right atomTypes -> do
80+ let genListOfEitherAtom = mapM (\aTy->runReaderT (arbitrary' aTy) tcMap) atomTypes
81+ listOfEitherAtom <- lift genListOfEitherAtom
82+ let eitherListOfAtom = sequence listOfEitherAtom
83+ case eitherListOfAtom of
84+ Left err -> pure $ Left err
85+ Right listOfAtom -> pure $ Right $ ConstructedAtom (DCD.name dcDef) constructedAtomType listOfAtom
86+arbitrary' (TypeVariableType _) = error "arbitrary on type variable"
87+
88+maybeToRight :: b -> Maybe a -> Either b a
89+maybeToRight _ (Just x) = Right x
90+maybeToRight y Nothing = Left y
91+
92+instance Arbitrary Text where
93+ arbitrary = pack <$> elements ["Mary", "Johnny", "Sunny", "Ted"]
94+
95+instance Arbitrary Day where
96+ arbitrary = ModifiedJulianDay <$> (arbitrary :: Gen Integer)
97+
98+instance Arbitrary UTCTime where
99+ arbitrary = UTCTime <$> arbitrary <*> (secondsToDiffTime <$> choose(0,86400))
100+
101+instance Arbitrary B.ByteString where
102+ arbitrary = B.pack <$> (arbitrary :: Gen String)
103+
104+arbitraryRelationTuple :: Attributes -> WithTCMap Gen (Either RelationalError RelationTuple)
105+arbitraryRelationTuple attris = do
106+ tcMap <- ask
107+ listOfMaybeAType <- lift $ sequence $ map ((\aTy -> runReaderT (arbitrary' aTy) tcMap) . atomType) (V.toList attris)
108+ case sequence listOfMaybeAType of
109+ Left err -> pure $ Left err
110+ Right listOfAttr -> do
111+ let vectorOfAttr = V.fromList listOfAttr
112+ pure $ Right $ RelationTuple attris vectorOfAttr
113+
114+arbitraryWithRange :: Gen (Either RelationalError RelationTuple) -> Range -> Gen [Either RelationalError RelationTuple]
115+arbitraryWithRange genEitherTuple range = do
116+ num <- choose range
117+ vectorOf num genEitherTuple
118+
119+arbitraryRelation :: Attributes -> Range -> WithTCMap Gen (Either RelationalError Relation)
120+arbitraryRelation attris range = do
121+ tcMap <- ask
122+ let genEitherTuple = runReaderT (arbitraryRelationTuple attris) tcMap
123+ listOfEitherTuple <- lift $ arbitraryWithRange genEitherTuple range
124+ let eitherTupleList = sequence listOfEitherTuple
125+ case eitherTupleList of
126+ Left err -> pure $ Left err
127+ Right tupleList -> pure $ Right $ Relation attris $ RelationTupleSet tupleList
128+
129+type WithTCMap a = ReaderT TypeConstructorMapping a
src/lib/ProjectM36/Base.hs+4−12
@@ -1,4 +1,4 @@
1−{-# LANGUAGE ExistentialQuantification,DeriveGeneric,DeriveAnyClass, TypeSynonymInstances, FlexibleInstances,OverloadedStrings #-}
1+{-# LANGUAGE ExistentialQuantification,DeriveGeneric,DeriveAnyClass,TypeSynonymInstances,FlexibleInstances,OverloadedStrings #-}
22{-# OPTIONS_GHC -fno-warn-orphans #-}
33
44module ProjectM36.Base where
@@ -15,7 +15,7 @@ import Control.DeepSeq.Generics (genericRnf)
1515import GHC.Generics (Generic)
1616import qualified Data.Vector as V
1717import qualified Data.List as L
18−import Data.Text (Text,unpack,pack)
18+import Data.Text (Text,unpack)
1919import Data.Binary
2020import Data.Vector.Binary()
2121import Data.Time.Clock
@@ -24,16 +24,6 @@ import Data.Time.Calendar (Day,toGregorian,fromGregorian)
2424import Data.Hashable.Time ()
2525import Data.Typeable
2626import Data.ByteString (ByteString)
27−--for generating arbitrary tuples
28−import Test.QuickCheck
29−import Test.QuickCheck.Gen
30−import Test.QuickCheck.Random
31−import Test.QuickCheck.Arbitrary.ADT
32−import qualified Data.ByteString.Char8 as B
33−import Data.Time
34−import Data.Time.Clock
35−
36−
3727type StringType = Text
3828
3929-- | Database atoms are the smallest, undecomposable units of a tuple. Common examples are integers, text, or unique identity keys.
@@ -541,6 +531,7 @@ instance Hashable DatabaseContextFunction where
541531instance Eq DatabaseContextFunction where
542532 f1 == f2 = dbcFuncName f1 == dbcFuncName f2
543533
534+{-
544535-- for creating arbitrary tuples
545536instance Arbitrary Text where
546537 arbitrary = pack <$> elements ["Mary", "Johnny", "Sunny", "Ted"]
@@ -611,6 +602,7 @@ arbitrary' _ = undefined
611602
612603getAtomType :: Attribute -> AtomType
613604getAtomType (Attribute _ atomType) = atomType
605+-}
614606
615607attrTypeVars :: Attribute -> S.Set TypeVarName
616608attrTypeVars (Attribute _ aType) = case aType of
src/lib/ProjectM36/MyTest.hsdeleted−83
@@ -1,83 +0,0 @@
1−{-# Language TypeSynonymInstances,FlexibleInstances,OverloadedStrings #-}
2−module ProjectM36.MyTest where
3−import ProjectM36.Base
4−import Test.QuickCheck
5−import Test.QuickCheck.Gen
6−import Test.QuickCheck.Random
7−import Test.QuickCheck.Arbitrary.ADT
8−import qualified Data.Text as T
9−import qualified Data.ByteString.Char8 as B
10−import Data.Time
11−import Data.Time.Clock
12−import qualified Data.Vector as V
13−import qualified Data.Map as M
14−
15−{-
16−instance Arbitrary Atom where
17− arbitrary = IntAtom <$> int
18− where int = (arbitrary :: Gen Int)
19−
20−genAtom :: IO Atom
21−genAtom = do
22− seed <- newQCGen
23− return $ unGen (arbitrary :: Gen Atom) seed (2^29-1)
24−-}
25−
26−instance Arbitrary T.Text where
27− arbitrary = T.pack <$> elements ["Mary", "Johnny", "Sunny", "Ted"] --(arbitrary :: Gen String)
28−
29−instance Arbitrary Day where
30− arbitrary = ModifiedJulianDay <$> (arbitrary :: Gen Integer)
31−
32−instance Arbitrary Attribute where
33− arbitrary = genericArbitrary
34−
35−instance (Arbitrary a) => Arbitrary (V.Vector a) where
36− arbitrary = do
37− maxbound <- choose (0,5)
38− V.fromList <$> vectorOf maxbound arbitrary
39−
40−instance {-# OVERLAPPING #-} Arbitrary TypeVarMap where
41− arbitrary = undefined
42−
43−instance Arbitrary UTCTime where
44− arbitrary = UTCTime <$> arbitrary <*> (secondsToDiffTime <$> arbitrary)
45−
46−instance Arbitrary B.ByteString where
47− arbitrary = B.pack <$> (arbitrary :: Gen String)
48−
49−-- arbitrary attributes seems to involve dependant-type techniques and seems unneccsary for a dud example.
50−attributeNames = ["Number","People","Time","Thing"]
51−atomtypes = [IntAtomType, TextAtomType, DayAtomType, TextAtomType]
52−attributes = V.fromList $ zipWith Attribute attributeNames atomtypes
53−
54−instance Arbitrary RelationTuple where
55− arbitrary = do
56− a <- IntAtom <$> (arbitrary :: Gen Int)
57− b <- TextAtom <$> (arbitrary :: Gen T.Text)
58− c <- DayAtom <$> (arbitrary :: Gen Day)
59− d <- TextAtom <$> (arbitrary :: Gen T.Text)
60− let sample = V.fromList [a,b,c,d]
61− return $ RelationTuple attributes sample
62−
63−instance Arbitrary Relation where
64− arbitrary = do
65− list <- listOf (arbitrary :: Gen RelationTuple)
66− return $ Relation attributes (RelationTupleSet list)
67−
68−
69−instance Arbitrary AtomType where
70− arbitrary = genericArbitrary
71−
72−instance Arbitrary Atom where
73− arbitrary = genericArbitrary
74−
75−instance ToADTArbitrary Atom
76−
77−
78−--three parts
79−-- s# generation
80−-- expr -> Relation
81−-- keyword arbitrary
82−-- database context: seed + expr
83−--
src/lib/ProjectM36/RelationalExpression.hs+9−4
@@ -11,6 +11,7 @@ import ProjectM36.ScriptSession
1111import ProjectM36.DataTypes.Primitive
1212import ProjectM36.AtomFunction
1313import ProjectM36.DatabaseContextFunction
14+import ProjectM36.Arbitrary
1415import qualified ProjectM36.Attribute as A
1516import qualified Data.Map as M
1617import qualified Data.HashSet as HS
@@ -595,10 +596,14 @@ evalDatabaseContextIOExpr _ currentContext (CreateArbitraryRelation relVarName a
595596 Nothing -> pure $ Left (RelVarNotDefinedError relVarName)
596597 Just existingRel -> do
597598 let expectedAttributes = attributes existingRel
598− rel <- generate $ arbitraryRel expectedAttributes range
599− case runState (setRelVar relVarName rel) elems of
600− (Left err,_) -> pure (Left err)
601− (Right (), (ctx',_,_)) -> pure $ Right ctx'
599+ let tcMap = typeConstructorMapping ctx
600+ eitherRel <- generate $ runReaderT (arbitraryRelation expectedAttributes range) tcMap
601+ case eitherRel of
602+ Left err -> pure $ Left err
603+ Right rel -> do
604+ case runState (setRelVar relVarName rel) elems of
605+ (Left err,_) -> pure (Left err)
606+ (Right (), (ctx',_,_)) -> pure $ Right ctx'
602607
603608updateTupleWithAtomExprs :: M.Map AttributeName AtomExpr -> DatabaseContext -> RelationTuple -> Either RelationalError RelationTuple
604609updateTupleWithAtomExprs exprMap context tupIn = do