forked from mroman42/vitrea-prototype-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample.hs
171 lines (138 loc) · 5.21 KB
/
Example.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE IncoherentInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LiberalTypeSynonyms #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Example where
import Vitrea
import Control.Arrow
import Data.Char
import Data.Foldable
import Data.Functor.Compose
import Text.Printf
-- The data structures.
data PostalAddr = PostalAddr
{ street' :: String
, code' :: String
, city' :: String
}
data EmailAddr = EmailAddr
{ user' :: String
, domain' :: String
}
data MailList = MailList
{ unMailList :: [(Name, Address, Subscription)]
}
type Address = String
type Street = String
type ZipCode = String
type Domain = String
type Name = String
data Subscription = Daily | Monthly | Weekly deriving (Show)
instance Show PostalAddr where
show (PostalAddr s z c) =
" Street: " ++ s ++
"\n Code: " ++ z ++
"\n City: " ++ c
instance Show EmailAddr where
show (EmailAddr u d) =
" User:" ++ u ++
" Domain:" ++ d
someMailingList :: MailList
someMailingList = MailList
[ ("Turing, Alan" , "[email protected]" , Daily)
, ("Noether, Emily" , "[email protected]" , Monthly)
, ("Gauss, Carl F." , "[email protected]" , Weekly)
]
someAddress :: Address
someAddress = "High Street, OX46NA, Oxford"
-- Lenses.
-- We are explicitly writing down the view/update.
zipcode :: ProfLens' PostalAddr ZipCode
zipcode = mkLens $ ExOptic (id &&& code') (\(p , s) -> p {code' = s})
street :: ProfLens' PostalAddr Street
street = mkLens $ ExOptic (id &&& street') (\(p , s) -> p {street' = s})
city :: ProfLens' PostalAddr Street
city = mkLens $ ExOptic (id &&& city') (\(p , s) -> p {city' = s})
domain :: ProfLens' EmailAddr Domain
domain = mkLens $ ExOptic (id &&& domain') (\(p , s) -> p {domain' = s})
-- Prisms.
postal :: ProfPrism' Address PostalAddr
postal = mkPrism $ ExOptic matchPostal buildPostal
where
matchPostal :: Address -> Either Address PostalAddr
matchPostal a = maybe (Left a) Right $ do
(street, b) <- readUntil ',' a
(city, c) <- readUntil ',' (tail $ tail b)
return $ PostalAddr street city (tail $ tail c)
buildPostal :: Either Address PostalAddr -> Address
buildPostal (Left a) = a
buildPostal (Right (PostalAddr s t c)) = s ++ ", " ++ t ++ ", " ++ c
email :: ProfPrism' Address EmailAddr
email = mkPrism $ ExOptic matchEmail buildEmail
where
matchEmail :: Address -> Either Address EmailAddr
matchEmail a = maybe (Left a) Right $ do
(u, d) <- readUntil '@' a
return $ EmailAddr u $ tail d
buildEmail :: Either Address EmailAddr -> Address
buildEmail (Left a) = a
buildEmail (Right (EmailAddr u d)) = u ++ "@" ++ d
-- Traversals.
mails :: ProfTraversal' MailList Address
mails = mkTraversal $ ExOptic (\m -> Compose (m , fst $ extractAddr m)) set
where
extractAddr :: MailList -> ([Address], [Address] -> MailList)
extractAddr (MailList m) = (map (\(_,a,_) -> a) m, MailList . fmap (\((n,_,s),a) -> (n,a,s)) . zip m)
set :: (Compose ((,) MailList) []) Address -> MailList
set (Compose (m , a)) = (snd $ extractAddr m) a
instance Show MailList where
show = unlines
. ((:) (printf "| %20s | %30s | %10s |" "Name" "Email" "Frequency"))
. ((:) (replicate 70 '-'))
. fmap (\(n,a,s) -> printf "| %20s | %30s | %10s |" n a (show s)) . unMailList
---------------
-- EXAMPLE 1 --
---------------
-- "15 Parks Rd, OX1 3QD, Oxford"^.postal.street
-- >> "High Street"
-- "High Street, OX46NA, Oxford"^.postal.street <~ "Banbury Road"
-- >> "Banbury Road, OX46NA, Oxford"
-- "High Street, OX46NA, Oxford"^.postal.zipcode <~~ (++ "(UK)")
-- >> "High Street, OX46NA(UK), Oxford"
---------------
---------------
-- EXAMPLE 2 --
---------------
-- someMailingList^..mails.email.domain <~~~ uppercase
--
-- >> | Name | Email | Frequency |
-- >> ----------------------------------------------------------------------
-- >> | Turing, Alan | [email protected] | Daily |
-- >> | Noether, Emily | [email protected] | Monthly |
-- >> | Gauss, Carl F. | [email protected] | Weekly |
---------------
-- Auxiliary functions.
uppercase, lowercase :: String -> String
uppercase = fmap toUpper
lowercase = fmap toLower
readUntil :: Char -> String -> Maybe (String , String)
readUntil c a = if elem c a
then Just (takeWhile (/= c) a, dropWhile (/= c) a)
else Nothing