-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scratch.hs
132 lines (105 loc) · 3.78 KB
/
Scratch.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, TypeApplications, DataKinds,
AllowAmbiguousTypes, OverloadedLabels #-}
module Scratch where
import GHC.Types (Nat)
import Data.Set (fromList, Set)
import Data.Text.Format
import Database.PostgreSQL.Simple(query_, connect, defaultConnectInfo, connectDatabase, connectUser, connectPassword, Connection)
import Database.PostgreSQL.Simple.Types(Query(..), fromQuery)
import Database.PostgreSQL.Simple.Internal(escapeIdentifier, escapeStringConn)
import Database.PostgreSQL.Simple.FromRow
import Lens
import Lens.FunDep.Affected (affected)
import Lens.Record.Base (RecoverEnv(..), Row, Fields)
import Lens.Database.Query
import Lens.Predicate.Hybrid
import Lens.Predicate.Base ((:=),Phrase(..))
import Lens.Database.Base (LensGet, get)
import Lens.Database.Postgres (PostgresDatabase)
import Lens.Put.Incremental (put, put_wif, LensPut)
import FunDep
import Lens.Record.Sorted (RecordsSet, recs)
import Delta (fromSet)
import Tables (RecoverTables)
import qualified Lens.Types as T
import qualified Lens.Predicate.Base as P
import qualified Lens.Predicate.Dynamic as DP
db_connect = connect defaultConnectInfo {
connectDatabase = "links",
connectUser = "links",
connectPassword = "links"
}
test_get :: (LensGet s PostgresDatabase) => Lens s -> IO (Set (QueryRow s))
test_get (l :: Lens s) = do
conn <- db_connect
res <- get conn l
-- mapM_ Prelude.print res
return res
test_put_debug :: LensPut PostgresDatabase s =>
Lens s -> RecordsSet (Rt s) -> IO ()
test_put_debug l rs =
do conn <- db_connect
put_wif conn l rs
test_put :: LensPut PostgresDatabase s => Lens s -> RecordsSet (Rt s) -> IO ()
test_put l rs =
do conn <- db_connect
put conn l rs
-- Bohanonn et al. PODS 2016 examples
type Albums = '[ '("album", String), '("quantity", Int)]
albums = prim @"albums" @Albums
@'[ '["album"] --> '["quantity"]]
type Tracks = '[
'("track", String),
'("date", Int),
'("rating", Int),
'("album", String)]
tracks = prim @"tracks" @Tracks
@'[ '["track"] --> '["date", "rating"]]
tracks1 = join tracks albums
tracks2 = dropl @'[ '("date", 'P.Int 2020)] @'["track"] tracks1
tracks3 = select (#quantity #> di 2) tracks2
type Output = '[ '("quantity", Int), '("date", Int), '("rating", Int), '("album", String)]
type Tracks3 = '[ '("track", String), '("rating", Int), '("album", String), '("quantity", Int)]
type PredRow = '[ '("quantity", Int), '("album", String)]
-- exampleUnchanged = lrows tracks3
-- [ (4, "Lovesong", 5, "Paris"),
-- (3, "Lullaby", 3, "Show"),
-- (5, "Trust", 4, "Wish")]
unchangedAlbums = recs @Albums
[ ("Show", 3),
("Galore", 1),
("Paris", 4),
("Wish", 5),
("Eponymous", 42),
("Disintegration", 6)]
unchangedTracks = recs @Tracks
[ ("Lovesong", 1989, 5, "Galore"),
("Lovesong", 1989, 5, "Paris"),
("Lullaby", 1989, 3, "Galore"),
("Lullaby", 1989, 3, "Show"),
("Trust", 1992, 4, "Wish") ]
examplePut = recs @Tracks3
[ ("Lullaby", 4, "Show", 3),
("Lovesong", 5, "Disintegration", 7)]
-- my_hybrid_lenses :: Bool -> Int -> String -> IO [Row Output]
my_hybrid_lenses b i s = do
test_get tracks3 where
pred = if b
then (erased @PredRow @Bool (var @"quantity" #> di i))
else (erased @PredRow @Bool (var @"album" #= ds s))
tracks1 = Lens.join tracks albums
tracks2 = select pred tracks1
tracks3 = dropl @'[ '("date", 'P.Int 2020)] @'["track"] tracks2
type FdsEx = '[ '["album"] --> '["quantity"],
'["quantity"] --> '["date", "rating"]]
from_year ::
Selectable (Var "date" := Erased '[] Int) s snew
=> Int -> Lens s -> Lens snew
from_year year l =
select p l where
p = var @"date" #= di year
tracks_2020 = from_year 2020 tracks
affect = do
res <- test_get tracks3
q <- DP.print $ affected @FdsEx $ res
return q