Skip to content

Commit

Permalink
Rename Rows with Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nonowarn committed May 4, 2010
1 parent 69d4510 commit bc66826
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 56 deletions.
16 changes: 8 additions & 8 deletions examples/DefaultParameter.hs
Expand Up @@ -10,10 +10,10 @@ data Greeting = Greeting; type instance TypeOf Greeting = String
data NumGreet = NumGreet; type instance TypeOf NumGreet = Int
data WithNewLine = WithNewLine; type instance TypeOf WithNewLine = Bool

type GreetOpt = RowOf Name
:&: RowOf Greeting
:&: RowOf NumGreet
:&: RowOf WithNewLine
type GreetOpt = FieldOf Name
:&: FieldOf Greeting
:&: FieldOf NumGreet
:&: FieldOf WithNewLine

greet :: GreetOpt -> IO ()
greet gopt = replicateM_
Expand All @@ -31,7 +31,7 @@ main = do
Name ^= name
$ Greeting ^= greeting
$ NumGreet ^= numGreet $ parse params
parse _ = rowOf "an anonymous user"
& rowOf "Hello"
& rowOf 1
& rowOf True
parse _ = fieldOf "an anonymous user"
& fieldOf "Hello"
& fieldOf 1
& fieldOf True
4 changes: 2 additions & 2 deletions examples/Points.hs
Expand Up @@ -8,7 +8,7 @@ data X = X; type instance TypeOf X = Int
data Y = Y; type instance TypeOf Y = Int
data Z = Z; type instance TypeOf Z = Int

type Point2D = RowOf X :&: RowOf Y
type Point2D = FieldOf X :&: FieldOf Y

getXY :: (Has X p, Has Y p) => p -> (Int,Int)
getXY = liftA2 (,) (X ^.) (Y ^.)
Expand All @@ -27,7 +27,7 @@ r = p2 8 (-2)
d0 = dist2d p q
d1 = dist2d p r

type Point3D = RowOf X :&: RowOf Y :&: RowOf Z
type Point3D = FieldOf X :&: FieldOf Y :&: FieldOf Z

getXYZ :: (Has X p, Has Y p, Has Z p)
=> p -> (Int,Int,Int)
Expand Down
30 changes: 15 additions & 15 deletions examples/Time.hs
Expand Up @@ -17,17 +17,17 @@ data TimeZone = TimeZone; type instance TypeOf TimeZone = Time.TimeZone
-- Define Records
-- Note reusing entities here

type TimeOfDay = RowOf Hour :&: RowOf Minute :&: RowOf Second
type TimeOfDay = FieldOf Hour :&: FieldOf Minute :&: FieldOf Second

type Time = RowOf Day :&: RowOf Hour :&: RowOf Minute :&: RowOf Second
type Time = FieldOf Day :&: FieldOf Hour :&: FieldOf Minute :&: FieldOf Second

type ZonedTime = RowOf TimeZone :&: RowOf Day
:&: RowOf Hour :&: RowOf Minute :&: RowOf Second
type ZonedTime = FieldOf TimeZone :&: FieldOf Day
:&: FieldOf Hour :&: FieldOf Minute :&: FieldOf Second

-- But that was too verbose
-- You can write same type as follows
-- > type Time = RowOf Day :&: TimeOfDay
-- > type ZonedTime = RowOf TimeZone :&: Time
-- > type Time = FieldOf Day :&: TimeOfDay
-- > type ZonedTime = FieldOf TimeZone :&: Time

-- And you can write:

Expand All @@ -45,9 +45,9 @@ addHours time hours =

getTod :: (Has Hour a, Has Minute a, Has Second a)
=> a -> TimeOfDay
getTod a = rowOf (Hour ^. a)
& rowOf (Minute ^. a)
& rowOf (Second ^. a)
getTod a = fieldOf (Hour ^. a)
& fieldOf (Minute ^. a)
& fieldOf (Second ^. a)

getUnixTime :: IO Integer
getUnixTime = fmap calc getUTCTime
Expand Down Expand Up @@ -83,22 +83,22 @@ epoch = Time.toModifiedJulianDay $ Time.fromGregorian 1970 1 1
fromUTCTime :: Time.UTCTime -> Time
fromUTCTime utctime =
let tod = Time.timeToTimeOfDay (Time.utctDayTime utctime)
in rowOf (Time.toModifiedJulianDay (Time.utctDay utctime))
in fieldOf (Time.toModifiedJulianDay (Time.utctDay utctime))
& fromTOD tod

fromZonedTime :: Time.ZonedTime -> ZonedTime
fromZonedTime zonedtime =
let localtime = Time.zonedTimeToLocalTime zonedtime
day = Time.toModifiedJulianDay $ Time.localDay localtime
tod = Time.localTimeOfDay localtime
in rowOf (Time.zonedTimeZone zonedtime)
& rowOf day
in fieldOf (Time.zonedTimeZone zonedtime)
& fieldOf day
& fromTOD tod

fromTOD :: Time.TimeOfDay -> TimeOfDay
fromTOD tod = rowOf (Time.todHour tod)
& rowOf (Time.todMin tod)
& rowOf (Time.todSec tod)
fromTOD tod = fieldOf (Time.todHour tod)
& fieldOf (Time.todMin tod)
& fieldOf (Time.todSec tod)

showTime :: (Has Day a, Has Hour a, Has Minute a, Has Second a)
=> a -> String
Expand Down
26 changes: 13 additions & 13 deletions src/Data/Has.hs
Expand Up @@ -18,8 +18,8 @@ module Data.Has
Has

-- * Rows in records
, Row
, (&), (:&:), row
, Field
, (&), (:&:), field

-- * Update and Lookup values from records
, (^=), (^.), (^:)
Expand All @@ -29,7 +29,7 @@ module Data.Has
, Labelled(), (:>), (.>)

-- ** Defining labels
, TypeOf, RowOf, rowOf
, TypeOf, FieldOf, fieldOf

-- * Make parsing error messages easier
, (:::)(), TyNil(), Contains()
Expand All @@ -48,8 +48,8 @@ import Data.Has.TypeList ((:::), TyNil)
newtype Labelled lab a = Label { unLabelled :: a }
deriving (Eq,Ord,Show,Read,Bounded)

-- | Represents labelled row.
type lab :> a = Row (Labelled lab a)
-- | Represents labelled field.
type lab :> a = Field (Labelled lab a)
infix 6 :>

-- | Attaches a label.
Expand All @@ -60,9 +60,9 @@ label _ a = Label a
unlabel :: lab -> Labelled lab a -> a
unlabel _ = unLabelled

-- | Makes a labelled row.
-- | Makes a labelled field.
(.>) :: lab -> a -> lab :> a
(.>) = (row .) . label
(.>) = (field .) . label

infix 6 .>

Expand All @@ -85,13 +85,13 @@ updl lab f a = let b = prjl lab a in injl lab (f b) a
-- | TypeOf @a@ should indicate a type labelled by @a@
type family TypeOf a

-- | > RowOf a == a :> TypeOf a
type family RowOf a
type instance RowOf a = a :> TypeOf a
-- | > FieldOf a == a :> TypeOf a
type family FieldOf a
type instance FieldOf a = a :> TypeOf a

-- | Creates a row labelled by @a@
rowOf :: TypeOf a -> RowOf a
rowOf a = undefined .> a
-- | Creates a field labelled by @a@
fieldOf :: TypeOf a -> FieldOf a
fieldOf a = undefined .> a

-- | Same as @Knows lab (TypeOf lab) s@, Useful on writing type
-- signitures.
Expand Down
24 changes: 12 additions & 12 deletions src/Data/Has/Engine.hs
Expand Up @@ -5,27 +5,27 @@ module Data.Has.Engine where

import Data.Has.TypeList

-- | @Row a@ is a type list which contains only one element of
-- @a@. And every row in the records should be this type.
type Row a = a ::: TyNil
-- | @Field a@ is a type list which contains only one element of
-- @a@. And every field in the records should be this type.
type Field a = a ::: TyNil

-- | Creates a 'Row' of @a@.
row :: a -> Row a
row a = a ::: TyNil
-- | Creates a 'Field' of @a@.
field :: a -> Field a
field a = a ::: TyNil

-- | Concatenates between 'Row's or records. Records are
-- | Concatenates between 'Field's or records. Records are
-- concatenated rows. For example, Following expressions are
-- valid.
--
-- > -- Concatenation of rows (i.e. record)
-- > row "string" & row True
-- > field "string" & field True
--
-- > -- Concatenation of records
-- > (row 'c' & row ()) & (row False & row "string")
-- > (field 'c' & field ()) & (field False & field "string")
--
-- > -- ... And concatenations between a row and a record
-- > row () & (row False & row "string")
-- > (row 'c' & row ()) & row False
-- > -- ... And concatenations between a field and a record
-- > field () & (field False & field "string")
-- > (field 'c' & field ()) & field False
(&) :: (Append a b) => a -> b -> a :&: b
(&) = (.++.)
infixr 5 &
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Has/TypeList.hs
Expand Up @@ -53,4 +53,4 @@ const2 = const . const
instance Eq TyNil where (==) = const2 True
instance Ord TyNil where compare = const2 EQ
instance Bounded TyNil where maxBound = TyNil; minBound = TyNil
instance Show TyNil where show _ = "TyNil"
instance Show TyNil where show _ = "TyNil"
10 changes: 5 additions & 5 deletions test/Main.hs
Expand Up @@ -20,8 +20,8 @@ newtype P = P Int deriving (Eq,Show)
newtype Q = Q Int deriving (Eq,Show)
newtype R = R Int deriving (Eq,Show)

pqr :: Int -> Int -> Int -> Row P :&: Row Q :&: Row R
pqr p q r = row (P p) & row (Q q) & row (R r)
pqr :: Int -> Int -> Int -> Field P :&: Field Q :&: Field R
pqr p q r = field (P p) & field (Q q) & field (R r)

test_typical_usage =
[ eq "Project by Type" (Q 2) (prj (pqr 1 2 3))
Expand All @@ -42,7 +42,7 @@ test_typical_usage =
(pqr 1 2 3)
(inj (P 1) . inj (R 3) . inj (Q 2) $ undefined)

, let intBool = row (1::Int) & row True
, let intBool = field (1::Int) & field True
in eq "prj selects a value from record with type inference"
(2::Int) (if prj intBool then prj intBool + 1 else -1)
]
Expand Down Expand Up @@ -72,9 +72,9 @@ type instance TypeOf X = String
type instance TypeOf Y = String
type instance TypeOf Z = String

type C = RowOf X :&: RowOf Y :&: RowOf Z
type C = FieldOf X :&: FieldOf Y :&: FieldOf Z
mkC :: String -> String -> String -> C
mkC x y z = rowOf x & rowOf y & rowOf z
mkC x y z = fieldOf x & fieldOf y & fieldOf z

test_labelled_values =
[ eq "inject a value by a label"
Expand Down

0 comments on commit bc66826

Please sign in to comment.