Thursday, 2 June 2016

Haskell: Functors


Haskell GHC.Base library defines Functor class like below.
class Functor f where
  fmap :: (a -> b) -> f a -> f b

fmap function takes a function, A functor instance as an argument and return another functor instance.

For example
Prelude> (+2) 10
12
Prelude> 
Prelude> (*2) 20
40
Prelude> 
Prelude> (/2) 30
15.0

Observe above snippet, you able to apply the sections (+2), (*2) and (/2) on numbers. Will you able to do the same thing on ‘Just 10’, ‘Just 20’ and ‘Just 30’?

Prelude> (+2) Just 10

<interactive>:28:1:
    Non type-variable argument in the constraint: Num (a -> Maybe a)
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall a. (Num a, Num (a -> Maybe a)) => Maybe a
Prelude> 
Prelude> (*2) Just 20

<interactive>:30:1:
    Non type-variable argument in the constraint: Num (a -> Maybe a)
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall a. (Num a, Num (a -> Maybe a)) => Maybe a
Prelude> 
Prelude> (/2) Just 30

<interactive>:32:1:
    Non type-variable argument
      in the constraint: Fractional (a -> Maybe a)
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall a. (Fractional (a -> Maybe a), Num a) => Maybe a


Here Functor class came into picture.  It defines fmap for Maybe type like below.
instance  Functor Maybe  where
    fmap _ Nothing       = Nothing
    fmap f (Just a)      = Just (f a)

Prelude> fmap (+2) (Just 10) 
Just 12
Prelude> 
Prelude> fmap (*2) (Just 20)
Just 40
Prelude> 
Prelude> fmap (/2) (Just 30)
Just 15.0
Prelude> 
Prelude> fmap (/2) Nothing
Nothing

What is a Functor?

A functor is any data type, that defines how fmap applies to it.
Prelude> :info Functor
class Functor (f :: * -> *) where
  fmap :: (a -> b) -> f a -> f b
  (<$) :: a -> f b -> f a
    -- Defined in ‘GHC.Base’
instance Functor (Either a) -- Defined in ‘Data.Either’
instance Functor [] -- Defined in ‘GHC.Base’
instance Functor Maybe -- Defined in ‘GHC.Base’
instance Functor IO -- Defined in ‘GHC.Base’
instance Functor ((->) r) -- Defined in ‘GHC.Base’
instance Functor ((,) a) -- Defined in ‘GHC.Base’
Prelude> 

Observe above snippet, Maybe, IO, Either all defines how fmap applies to them, so Maybe, IO and Either are functors.


Other Examples
Prelude> fmap length (Just "Hello Haskell")
Just 13
Prelude> 
Prelude> fmap head (Just "Hello Haskell")
Just 'H'
Prelude> 
Prelude> fmap tail (Just "Hello Haskell")
Just "ello Haskell"
Prelude> 
Prelude> fmap (zip [1, 2, 3])  (Just "Hello Haskell")
Just [(1,'H'),(2,'e'),(3,'l')]









Previous                                                 Next                                                 Home

Haskell: Working with file access and modification times

Haskell System.Directory module provides following funcitons to get and set file (or) directory access and modification times. Following table summarizes the functions.

Function
Signature
Description
getAccessTime
FilePath -> IO UTCTime
Get the time at which the file or directory was last accessed.
getModificationTime
FilePath -> IO UTCTime
Get the time at which the file or directory was last modified.
setAccessTime
FilePath -> UTCTime -> IO () Source
Change the time at which the file or directory was last accessed.
setModificationTime
FilePath -> UTCTime -> IO ()
Change the time at which the file or directory was last modified.

Above functions may fail with following errors.

isPermissionError if the user is not permitted to alter the access time.
isDoesNotExistError if the file or directory does not exist.


FileTimeUtil.hs
import System.Directory
import System.IO

main = do
    putStrLn "Enter file (or) directory"
    file <- getLine

    accessTime <- getAccessTime file
    modificationTime <- getModificationTime file

    putStrLn $ "access time : " ++ (show accessTime)
    putStrLn $ "modification time : " ++ (show modificationTime)

$ runghc FileTimeUtil.hs 
Enter file (or) directory
/Users/harikrishna_gurram/
access time : 2016-04-07 09:36:16 UTC
modification time : 2016-04-07 05:45:44 UTC



Previous                                                 Next                                                 Home

Haskell: Work with file permissions

Haskell System.Directory module Provides number of functions to work with file/directory permissions. Following table summarizes the functions.

Function
Signature
Description
emptyPermissions
emptyPermissions :: Permissions
Return Permissions instance by clearing all.

Prelude System.Directory> emptyPermissions
Permissions {readable = False, writable = False, executable = False, searchable = False}
readable
readable :: Permissions -> Bool
Return True, if the Permissions instance has the property readable = True, else False.
writable
writable :: Permissions -> Bool
Return True, if the Permissions instance has the property writable = True, else False.
executable
executable :: Permissions -> Bool
Return True, if the Permissions instance has the property executable = True, else False.
searchable
searchable :: Permissions -> Bool
Return True, if the Permissions instance has the property searchable = True, else False
setOwnerReadable
setOwnerReadable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the readable permission to the owner, else unset.
setOwnerWritable
setOwnerWritable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the writable permission to the owner, else unset.
setOwnerExecutable
setOwnerExecutable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the executable permission to the owner, else unset.
setOwnerSearchable
setOwnerSearchable :: Bool -> Permissions -> Permissions
If the Bool argument is True, it sets the searchable permission to the owner, else unset.
getPermissions
getPermissions :: FilePath -> IO Permissions
Get the permissions of a file (or) directory.
setPermissions
setPermissions :: FilePath -> Permissions -> IO ()
Set permissins to given file.
copyPermissions
copyPermissions :: FilePath -> FilePath -> IO ()
Copy permissions of one file to other.

PermissionUtil.hs
import System.IO
import System.Directory

main = do
    putStrLn "Enter directory (or) file"
    file <- getLine

    permissions <- getPermissions file
    let isReadable = readable permissions
    let isWritable = writable permissions
    let isExecutable = executable permissions
    let isSearchable = searchable permissions

    putStrLn $ "permissions : " ++ (show permissions)
    putStrLn $ "isReadable : " ++ (show isReadable)
    putStrLn $ "isWritable : " ++ (show isWritable)
    putStrLn $ "isExecutable : " ++ (show isExecutable)
    putStrLn $ "isSearchable : " ++ (show isSearchable)

$ runghc PermissionUtil.hs 
Enter directory (or) file
/Users/harikrishna_gurram
permissions : Permissions {readable = True, writable = True, executable = False, searchable = True}
isReadable : True
isWritable : True
isExecutable : False
isSearchable : True
$ 
$ runghc PermissionUtil.hs 
Enter directory (or) file
/usr/bin/java
permissions : Permissions {readable = True, writable = False, executable = True, searchable = False}
isReadable : True
isWritable : False
isExecutable : True
isSearchable : False

Note
getPermissions, setPermissions functions may fail with following errors.

isPermissionError if the user is not permitted to access the permissions; or
isDoesNotExistError if the file or directory does not exist.




Previous                                                 Next                                                 Home

Haskell: doesDirectoryExist: Check for directory existence

System.Directory module provides doesDirectoryExist funciton, which takes a directory name as input and return True, if the directory exists, else False. Following is the signature of doesDirectoryExist function.

doesDirectoryExist :: FilePath -> IO Bool
Prelude System.Directory> doesDirectoryExist "/Users"
True
Prelude System.Directory> doesDirectoryExist "/Users123"
False
Prelude System.Directory> doesDirectoryExist "/Users/harikrishna_gurram"
True



Previous                                                 Next                                                 Home

Haskell: doesFileExist: Check for file existence

System.Directory module provides doesFileExist function, which return true if the file exist, else false.
Prelude System.Directory> :t doesFileExist 
doesFileExist :: FilePath -> IO Bool
Prelude System.Directory> 
Prelude System.Directory> doesFileExist  "/Users"
False
Prelude System.Directory> doesFileExist  "/Users/harikrishna_gurram/academyLogin.log"
True



Previous                                                 Next                                                 Home