@@ -3,7 +3,6 @@ module ProjectM36.Atomable where
33--http://stackoverflow.com/questions/13448361/type-families-with-ghc-generics-or-data-data
44--instances to marshal Haskell ADTs to ConstructedAtoms and back
55import ProjectM36.Base
6−import ProjectM36.Relation
76import ProjectM36.DataTypes.Primitive
87import ProjectM36.DataTypes.List
98import ProjectM36.DataTypes.Maybe
@@ -18,6 +17,7 @@ import Data.Time.Calendar
1817import Data.ByteString (ByteString)
1918import Data.Time.Clock
2019import Data.Maybe
20+import Data.Proxy
2121
2222--also add haskell scripting atomable support
2323--rename this module to Atomable along with test
@@ -47,14 +47,14 @@ class (Eq a, NFData a, Binary a, Show a) => Atomable a where
4747 Nothing -> error "no fromAtomG for Atom found"
4848 Just x -> to x
4949
50− toAtomType :: a -> AtomType
51− default toAtomType :: (Generic a, AtomableG (Rep a)) => a -> AtomType
52− toAtomType v = toAtomTypeG (from v)
50+ toAtomType :: Proxy a -> AtomType
51+ default toAtomType :: (Generic a, AtomableG (Rep a)) => Proxy a -> AtomType
52+ toAtomType _ = toAtomTypeG (from (undefined :: a))
5353
5454 -- | Creates DatabaseContextExpr necessary to load the type constructor and data constructor into the database.
55− toAddTypeExpr :: a -> DatabaseContextExpr
56− default toAddTypeExpr :: (Generic a, AtomableG (Rep a)) => a -> DatabaseContextExpr
57− toAddTypeExpr v = toAddTypeExprG (from v) (toAtomType v)
55+ toAddTypeExpr :: Proxy a -> DatabaseContextExpr
56+ default toAddTypeExpr :: (Generic a, AtomableG (Rep a)) => Proxy a -> DatabaseContextExpr
57+ toAddTypeExpr p = toAddTypeExprG (from (undefined ::a)) (toAtomType p)
5858
5959instance Atomable Integer where
6060 toAtom = IntegerAtom
@@ -112,6 +112,7 @@ instance Atomable Bool where
112112 toAtomType _ = BoolAtomType
113113 toAddTypeExpr _ = NoOperation
114114
115+{-
115116instance Atomable Relation where
116117 toAtom = RelationAtom
117118 fromAtom (RelationAtom r) = r
@@ -119,23 +120,24 @@ instance Atomable Relation where
119120 --warning: cannot be used with undefined "Relation"
120121 toAtomType rel = RelationAtomType (attributes rel)
121122 toAddTypeExpr _ = NoOperation
123+-}
122124
123125instance Atomable a => Atomable (Maybe a) where
124− toAtom (Just v) = ConstructedAtom "Just" (maybeAtomType (toAtomType v)) [toAtom v]
125− toAtom Nothing = ConstructedAtom "Nothing" (maybeAtomType (toAtomType (undefined :: a))) []
126+ toAtom (Just v) = ConstructedAtom "Just" (maybeAtomType (toAtomType (Proxy :: Proxy a))) [toAtom v]
127+ toAtom Nothing = ConstructedAtom "Nothing" (maybeAtomType (toAtomType (Proxy :: Proxy a))) []
126128
127129 fromAtom (ConstructedAtom "Just" _ [val]) = Just (fromAtom val)
128130 fromAtom (ConstructedAtom "Nothing" _ []) = Nothing
129131 fromAtom _ = error "improper fromAtom (Maybe a)"
130132
131− toAtomType _ = ConstructedAtomType "Maybe" (M.singleton "a" (toAtomType (undefined :: a)))
133+ toAtomType _ = ConstructedAtomType "Maybe" (M.singleton "a" (toAtomType (Proxy :: Proxy a)))
132134 toAddTypeExpr _ = NoOperation
133135
134136instance (Atomable a, Atomable b) => Atomable (Either a b) where
135− toAtom (Left l) = ConstructedAtom "Left" (eitherAtomType (toAtomType (undefined :: a))
136− (toAtomType (undefined :: b))) [toAtom l]
137− toAtom (Right r) = ConstructedAtom "Right" (eitherAtomType (toAtomType (undefined :: a))
138− (toAtomType (undefined :: b))) [toAtom r]
137+ toAtom (Left l) = ConstructedAtom "Left" (eitherAtomType (toAtomType (Proxy :: Proxy a))
138+ (toAtomType (Proxy :: Proxy b))) [toAtom l]
139+ toAtom (Right r) = ConstructedAtom "Right" (eitherAtomType (toAtomType (Proxy :: Proxy a))
140+ (toAtomType (Proxy :: Proxy b))) [toAtom r]
139141
140142 fromAtom (ConstructedAtom "Left" _ [val]) = Left (fromAtom val)
141143 fromAtom (ConstructedAtom "Right" _ [val]) = Right (fromAtom val)
@@ -143,14 +145,14 @@ instance (Atomable a, Atomable b) => Atomable (Either a b) where
143145
144146--convert to ADT list
145147instance Atomable a => Atomable [a] where
146− toAtom [] = ConstructedAtom "Empty" (listAtomType (toAtomType (undefined :: a))) []
147− toAtom (x:xs) = ConstructedAtom "Cons" (listAtomType (toAtomType x)) (map toAtom (x:xs))
148+ toAtom [] = ConstructedAtom "Empty" (listAtomType (toAtomType (Proxy :: Proxy a))) []
149+ toAtom (x:xs) = ConstructedAtom "Cons" (listAtomType (toAtomType (Proxy :: Proxy a))) (map toAtom (x:xs))
148150
149151 fromAtom (ConstructedAtom "Empty" _ _) = []
150152 fromAtom (ConstructedAtom "Cons" _ (x:xs)) = fromAtom x:map fromAtom xs
151153 fromAtom _ = error "improper fromAtom [a]"
152154
153− toAtomType _ = ConstructedAtomType "List" (M.singleton "a" (toAtomType (undefined :: a)))
155+ toAtomType _ = ConstructedAtomType "List" (M.singleton "a" (toAtomType (Proxy :: Proxy a)))
154156 toAddTypeExpr _ = NoOperation
155157
156158-- Generics
@@ -220,13 +222,13 @@ instance (Atomable a) => AtomableG (K1 c a) where
220222 where headatom (x:_) = x
221223 headatom [] = error "no more atoms for constructor!"
222224 toAtomsG (K1 v) = [toAtom v]
223− toAtomTypeG _ = toAtomType (undefined :: a)
225+ toAtomTypeG _ = toAtomType (Proxy :: Proxy a)
224226 toAddTypeExprG _ _ = undefined
225227 getConstructorsG = undefined
226− getConstructorArgsG (K1 v) = [DataConstructorDefTypeConstructorArg tCons]
228+ getConstructorArgsG (K1 _) = [DataConstructorDefTypeConstructorArg tCons]
227229 where
228230 tCons = PrimitiveTypeConstructor primitiveATypeName primitiveAType
229− primitiveAType = toAtomType v
231+ primitiveAType = toAtomType (Proxy :: Proxy a)
230232 primitiveATypeName = fromMaybe (error ("primitive type missing: " ++ show primitiveAType)) (foldr (\(PrimitiveTypeConstructorDef name typ, _) _ -> if typ == primitiveAType then Just name else Nothing) Nothing primitiveTypeConstructorMapping)
231233
232234instance AtomableG U1 where