on master help sign in
Merge branch 'add_shortcuts' of git://github.com/YuMingLiao/project-m36 into YuMingLiao-add_shortcuts

refactor shortcuts to fix hlint suggestions # Conflicts: # project-m36.cabal

commit5d8a0720e29fb2007633e242d77c23c045502e50 authorAgentM <agentm@themactionfaction.com> dateSat 8 May 2021, 21:35 (5 years ago) parentdc88bb3385c720
3 files changed+250−2
project-m36.cabal+2−1 src/lib/ProjectM36/Shortcuts.hs+247 test/TutorialD/Interpreter/Import/TutorialD.hs+1−1
project-m36.cabal+2−1
@@ -35,7 +35,7 @@ Flag haskell-scripting
3535 Default: True
3636
3737Library
38− Build-Depends: base>=4.8 && < 4.15, ghc-paths, mtl, containers, unordered-containers, hashable, haskeline, directory, MonadRandom, random-shuffle, uuid >= 1.3.12, cassava >= 0.4.5.1 && < 0.6, text, bytestring, deepseq, deepseq-generics, vector, parallel, monad-parallel, exceptions, transformers, gnuplot, filepath, zlib, directory, temporary, stm, time, hashable-time, old-locale, rset, attoparsec, either, base64-bytestring, data-interval, extended-reals, aeson >= 1.1, path-pieces, conduit, resourcet, http-api-data, semigroups, QuickCheck, quickcheck-instances, list-t, stm-containers >= 0.2.15, foldl, optparse-applicative, Glob, cryptohash-sha256, text-manipulate >= 0.2.0.1 && < 0.4, winery, curryer-rpc, network, async, vector-instances, recursion-schemes, streamly >= 0.7.2
38+ Build-Depends: base>=4.8 && < 4.15, ghc-paths, mtl, containers, unordered-containers, hashable, haskeline, directory, MonadRandom, random-shuffle, uuid >= 1.3.12, cassava >= 0.4.5.1 && < 0.6, text, bytestring, deepseq, deepseq-generics, vector, parallel, monad-parallel, exceptions, transformers, gnuplot, filepath, zlib, directory, temporary, stm, time, hashable-time, old-locale, rset, attoparsec, either, base64-bytestring, data-interval, extended-reals, aeson >= 1.1, path-pieces, conduit, resourcet, http-api-data, semigroups, QuickCheck, quickcheck-instances, list-t, stm-containers >= 0.2.15, foldl, optparse-applicative, Glob, cryptohash-sha256, text-manipulate >= 0.2.0.1 && < 0.4, winery, curryer-rpc, network, async, vector-instances, recursion-schemes, streamly >= 0.7.2, convertible
3939 if flag(haskell-scripting)
4040 Build-Depends: ghc >= 8.2 && < 8.11
4141 CPP-Options: -DPM36_HASKELL_SCRIPTING
@@ -63,6 +63,7 @@ Library
6363 ProjectM36.AtomFunction,
6464 ProjectM36.AtomFunctionError,
6565 ProjectM36.ScriptSession,
66+ ProjectM36.Shortcuts,
6667 ProjectM36.DatabaseContextFunction,
6768 ProjectM36.DatabaseContextFunctionError,
6869 ProjectM36.DatabaseContextFunctionUtils,
src/lib/ProjectM36/Shortcuts.hsnew file+247
@@ -0,0 +1,247 @@
1+{-# OPTIONS_GHC -fno-warn-orphans #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE TypeApplications #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE MultiParamTypeClasses #-}
6+{-# LANGUAGE TypeFamilies #-}
7+{-# LANGUAGE ExtendedDefaultRules #-}
8+{-# LANGUAGE ExistentialQuantification #-}
9+{-# LANGUAGE FlexibleContexts #-}
10+{-# LANGUAGE GADTs #-}
11+{-# LANGUAGE UndecidableInstances #-}
12+module ProjectM36.Shortcuts where
13+-- users need OverloadedLabels, OverloadedLists, and default(Int,Text) to use these shortcuts.
14+import Data.Text hiding (foldl, map)
15+import ProjectM36.Base
16+import ProjectM36.Relation
17+import ProjectM36.Atomable
18+import Prelude hiding ((!!))
19+import Data.Proxy
20+import GHC.OverloadedLabels
21+import GHC.TypeLits hiding (Text)
22+import qualified Data.Text as T
23+import qualified Data.Map as M
24+import qualified Data.Set as S
25+import GHC.Exts (IsList(..))
26+import Data.Convertible
27+
28+default (Text)
29+
30+instance IsList (AttributeNamesBase ()) where
31+ type Item (AttributeNamesBase ()) = AttributeName
32+ fromList = AttributeNames . S.fromList
33+ toList (AttributeNames ns) = S.toList ns
34+ toList _ = error "needs AttributeNames"
35+
36+instance IsList (TupleExprsBase ()) where
37+ type Item TupleExprs = TupleExpr
38+ fromList ts = TupleExprs () ts
39+ toList (TupleExprs _ ts) = ts
40+
41+instance IsList TupleExpr where
42+ type Item TupleExpr = (AttributeName, AtomExpr)
43+ fromList attributeValues = TupleExpr (M.fromList attributeValues)
44+ toList (TupleExpr attributeValues) = M.toList attributeValues
45+
46+
47+-- #xxx :: Text
48+instance KnownSymbol x => IsLabel x Text where
49+ fromLabel = T.pack $ symbolVal @x Proxy
50+
51+-- #relvarName :: RelationalExpr
52+instance KnownSymbol x => IsLabel x RelationalExpr where
53+ fromLabel = RelationVariable (T.pack $ symbolVal @x Proxy) ()
54+
55+-- *Main> #a Int :: AttributeExpr
56+-- NakedAttributeExpr (Attribute "a" IntAtomType)
57+-- *Main> #a (Attr @[Int]) :: AttributeExpr
58+-- NakedAttributeExpr (Attribute "a" (ConstructedAtomType "List" (fromList [("a",IntAtomType)])))
59+-- can't offer a Relation atomtype -- don't know how to express a Relation type in haskell type. Maybe something a HList of (Text, a) ?
60+--
61+-- ps. I don't understand the usage of "AttributeAndTypeNameExpr AttributeName TypeConstructor a"
62+instance (KnownSymbol x, Atomable a)=> IsLabel x (HaskAtomType a -> AttributeExpr) where
63+ fromLabel atyn = NakedAttributeExpr (Attribute name atyn) . toAtomType''
64+ where name = T.pack $ symbolVal @x Proxy
65+
66+-- (#a 1) :: ExtendTupleExpr
67+-- no need for :=
68+instance (Convertible a AtomExpr, KnownSymbol x) => IsLabel x (a -> ExtendTupleExpr) where
69+ fromLabel x = AttributeExtendTupleExpr name (convert x)
70+ where name = T.pack $ symbolVal @x Proxy
71+
72+-- #name AtomExpr
73+-- ex. tuple [ #name 3 ]
74+-- default(Text) is needed in client code to avoid `no Atomable Char`
75+instance (Convertible a AtomExpr, KnownSymbol x) => IsLabel x (a -> (AttributeName, AtomExpr)) where
76+ fromLabel = \x -> (name, convert x)
77+ where name = T.pack $ symbolVal @x Proxy
78+
79+-- *Main> #a [1] :: AtomExpr
80+-- FunctionAtomExpr "a" [NakedAtomExpr (IntegerAtom 1)] ()
81+--
82+-- This usage is not working in RestrictionPredicateExpr and AttributeExtendTupleExpr. Use f "a" [1] instead.
83+instance (KnownSymbol x, Convertible a AtomExpr) => IsLabel x ([a] -> AtomExpr) where
84+ fromLabel = \as' -> FunctionAtomExpr name (map convert as') ()
85+ where name = T.pack $ symbolVal @x Proxy
86+
87+instance (KnownSymbol x) => IsLabel x AtomExpr where
88+ fromLabel = AttributeAtomExpr name
89+ where name = T.pack $ symbolVal @x Proxy
90+
91+
92+data HaskAtomType a where
93+ Int :: HaskAtomType Int
94+ Integer :: HaskAtomType Integer
95+ Double :: HaskAtomType Double
96+ Text :: HaskAtomType Text
97+-- Day :: HaskAtomType Day
98+-- DateTime :: HaskAtomType DateTime
99+-- ByteString :: HaskAtomType ByteString
100+ Bool :: HaskAtomType Bool
101+ Attr :: Atomable a => HaskAtomType a -- a Proxy-like value for type application.
102+
103+toAtomType'' :: Atomable a => HaskAtomType a -> AtomType
104+toAtomType'' (_ :: HaskAtomType a) = toAtomType (Proxy @a)
105+
106+-- usage: relation [tuple [#a 1, #b "b"], tuple [#a 2, #b "b"]]
107+relation :: [TupleExpr] -> RelationalExpr
108+relation ts = MakeRelationFromExprs Nothing (TupleExprs () ts)
109+
110+relation' :: [AttributeExprBase ()] -> [TupleExpr] -> RelationalExpr
111+relation' as' ts = MakeRelationFromExprs (Just as') (TupleExprs () ts)
112+
113+-- usage: tuple [#name "Mike",#age 6]
114+tuple :: [(AttributeName, AtomExpr)] -> TupleExprBase ()
115+tuple as' = TupleExpr (M.fromList as')
116+
117+-- #a rename [#b `as` #c]
118+rename :: RelationalExpr -> [(AttributeName,AttributeName)] -> RelationalExpr
119+rename relExpr renameList = case renameList of
120+ [] -> Restrict TruePredicate relExpr
121+ renames ->
122+ foldl (\acc (old,new) -> Rename old new acc) relExpr renames
123+
124+--project !!
125+-- #a !! [#b,#c]
126+infix 9 !!
127+(!!) :: RelationalExpr -> AttributeNames -> RelationalExpr
128+relExpr !! xs = Project xs relExpr
129+
130+--join ><
131+-- #a >< #b
132+(><) :: RelationalExpr -> RelationalExpr -> RelationalExpr
133+a >< b = Join a b
134+
135+allBut :: AttributeNames -> AttributeNames
136+allBut (AttributeNames ns) = InvertedAttributeNames ns
137+allBut _ = error "give allBut something other than attribute names."
138+
139+allFrom :: RelationalExpr -> AttributeNames
140+allFrom = RelationalExprAttributeNames
141+
142+as :: AttributeNames -> AttributeName -> (AttributeNames, AttributeName)
143+as = (,)
144+
145+-- #a `group` ([#b,#c] `as` #d)
146+group :: RelationalExpr -> (AttributeNames, AttributeName) -> RelationalExpr
147+group relExpr (aNames, aName) = Group aNames aName relExpr
148+
149+-- #a `ungroup` #b
150+ungroup :: RelationalExpr -> AttributeName -> RelationalExpr
151+ungroup relExpr aName = Ungroup aName relExpr
152+
153+-- *Main> #a #:= true #: ( #b (f "count" [1,2]))
154+-- Assign "a" (Extend (AttributeExtendTupleExpr "b" (FunctionAtomExpr "count" [NakedAtomExpr (IntegerAtom 1),NakedAtomExpr (IntegerAtom 2)] ())) (ExistingRelation (Relation attributesFromList [] (RelationTupleSet {asList = [RelationTuple attributesFromList [] []]}))))
155+(#:) :: RelationalExpr -> ExtendTupleExpr -> RelationalExpr
156+a #: b = Extend b a
157+infix 8 #:
158+
159+instance Convertible AtomExpr AtomExpr where
160+ safeConvert n = Right n
161+
162+instance Convertible RelVarName AtomExpr where
163+ safeConvert n = Right $ RelationAtomExpr (RelationVariable n ())
164+
165+instance Convertible RelationalExpr AtomExpr where
166+ safeConvert relExpr = Right $ RelationAtomExpr relExpr
167+
168+instance Convertible RelVarName RelationalExpr where
169+ safeConvert n = Right $ RelationVariable n ()
170+
171+-- @ in tutd
172+-- (@@) "aaa"
173+(@@) :: AttributeName -> AtomExpr
174+(@@) = AttributeAtomExpr
175+
176+-- works in RestrictedPredicateExpr and AttributeExtendTupleExpr
177+-- usage: f "gte" [1]
178+f :: Convertible a AtomExpr => AtomFunctionName -> [a] -> AtomExpr
179+f n as' = FunctionAtomExpr n (map convert as') ()
180+
181+-- DatabaseContextExpr
182+-- define
183+(#::) :: RelVarName -> [AttributeExpr] -> DatabaseContextExpr
184+s #:: xs = Define s xs
185+infix 5 #::
186+
187+-- assign
188+(#:=) :: RelVarName -> RelationalExpr -> DatabaseContextExpr
189+s #:= r = Assign s r
190+infix 5 #:=
191+
192+class Boolean a b where
193+ (&&&) :: a -> b -> RestrictionPredicateExpr
194+ infixl 6 &&&
195+ (|||) :: a -> b -> RestrictionPredicateExpr
196+ infixl 5 |||
197+
198+-- where: @~ mimics the restriction symbol in algebra
199+-- usage: true #: (#a 1) @~ #a ?= 1 &&& not' false ||| (f "gte" [1])
200+(@~) :: Convertible a RestrictionPredicateExpr => RelationalExpr -> a -> RelationalExpr
201+(@~) relExpr resPreExpr = Restrict (convert resPreExpr) relExpr
202+infix 4 @~
203+
204+true :: RelationalExpr
205+true = ExistingRelation relationTrue
206+
207+false :: RelationalExpr
208+false = ExistingRelation relationFalse
209+
210+trueP :: RestrictionPredicateExprBase a
211+trueP = TruePredicate
212+
213+falseP :: RestrictionPredicateExprBase a
214+falseP = NotPredicate TruePredicate
215+
216+(?=) :: Convertible a AtomExpr => AttributeName -> a -> RestrictionPredicateExpr
217+(?=) name a = AttributeEqualityPredicate name (convert a)
218+infix 9 ?=
219+
220+not' :: Convertible a RestrictionPredicateExpr => a -> RestrictionPredicateExpr
221+not' = NotPredicate . convert
222+
223+instance (Convertible a RestrictionPredicateExpr, Convertible b RestrictionPredicateExpr) => Boolean a b where
224+ a &&& b = AndPredicate (convert a) (convert b)
225+ a ||| b = OrPredicate (convert a) (convert b)
226+
227+instance {-# Incoherent #-} Atomable a => Convertible a RestrictionPredicateExpr where
228+ safeConvert n = Right $ AtomExprPredicate $ toAtomExpr . toAtom $ n
229+
230+instance {-# Incoherent #-} Convertible RelationalExpr RestrictionPredicateExpr where
231+ safeConvert a = Right $ RelationalExprPredicate a
232+
233+instance {-# Incoherent #-} Convertible AtomExpr RestrictionPredicateExpr where
234+ safeConvert a = Right $ AtomExprPredicate a
235+
236+instance {-# Incoherent #-} Convertible RestrictionPredicateExpr RestrictionPredicateExpr where
237+ safeConvert a = Right a
238+
239+instance {-# Incoherent #-} Atomable a => Convertible a AtomExpr where
240+ safeConvert n = Right $ toAtomExpr . toAtom $ n
241+
242+toAtomExpr :: Atom -> AtomExpr
243+toAtomExpr (ConstructedAtom n _ xs) = ConstructedAtomExpr n (toAtomExpr <$> xs) ()
244+toAtomExpr a = NakedAtomExpr a
245+
246+
247+
test/TutorialD/Interpreter/Import/TutorialD.hs+1−1
@@ -31,7 +31,7 @@ testTutdFileImport = TestCase $
3131 Assign "y" (MakeRelationFromExprs Nothing
3232 $ TupleExprs () [TupleExpr (M.fromList [("b", NakedAtomExpr (TextAtom "漢字"))])])]
3333 --on Windows, the file URI should not include the drive letter "/c/Users..." -> "/Users"
34− let uri = "file://" <> map (\c -> if c == '\\' then '/' else c) ( joinDrive "/" (dropDrive (tempPath)))
34+ let uri = "file://" <> map (\c -> if c == '\\' then '/' else c) ( joinDrive "/" (dropDrive tempPath))
3535 fileURI <- mkURI (T.pack uri)
3636 imported <- importTutorialDFromFile fileURI Nothing
3737 assertEqual "import tutd" (Right expectedExpr) imported