Skip to main content

Overview

The IHP.Prelude module is the foundation of every IHP application, providing a curated set of commonly-used functions and types. It re-exports functionality from multiple libraries and adds IHP-specific utilities. This prelude is automatically imported in:
  • IHP.ControllerPrelude (for controllers)
  • IHP.ViewPrelude (for views)
  • IHP.RouterPrelude (for routing)
  • IHP.MailPrelude (for mail)

Core Exports

Text Operations

IHP uses Text as the primary string type instead of String.
show
Show a => a -> Text
Convert any showable value to Text.
show 123 -- Returns: "123"
show True -- Returns: "True"
tshow
Show a => a -> Text
Alias for show. Converts any showable value to Text.
tshow (5 + 3) -- Returns: "8"
cs
ConvertibleStrings a b => a -> b
Generic string conversion function from Data.String.Conversions.
cs "hello" :: ByteString
cs myText :: String
(++)
Semigroup a => a -> a -> a
Concatenation operator, works with Text, lists, and other semigroups.
"Hello" ++ " World" -- Returns: "Hello World"
[1,2] ++ [3,4] -- Returns: [1,2,3,4]

Safe List Functions

IHP provides safe alternatives to Prelude’s partial list functions that return Maybe instead of throwing errors.
head
[a] -> Maybe a
Safely get the first element of a list.
head [1,2,3] -- Returns: Just 1
head [] -- Returns: Nothing
last
[a] -> Maybe a
Safely get the last element of a list.
last [1,2,3] -- Returns: Just 3
last [] -- Returns: Nothing
tail
[a] -> Maybe [a]
Safely get all elements except the first.
tail [1,2,3] -- Returns: Just [2,3]
tail [] -- Returns: Nothing
init
[a] -> Maybe [a]
Safely get all elements except the last.
init [1,2,3] -- Returns: Just [1,2]
init [] -- Returns: Nothing

File System Paths

OsPath
Type
Cross-platform file path type. Use this instead of String or Text for file paths.
textToOsPath
Text -> OsPath
Convert Text to a file system path using UTF-8 encoding.
let path = textToOsPath "uploads/image.jpg"
osPathToText
OsPath -> Text
Convert a file system path back to Text.
let fileName = osPathToText filePath

Control Flow

when
Applicative f => Bool -> f () -> f ()
Conditionally execute an action.
when (age >= 18) $ putStrLn "Adult"
unless
Applicative f => Bool -> f () -> f ()
Conditionally execute an action when condition is False.
unless (isEmpty list) $ processItems list
mapM
(Traversable t, Monad m) => (a -> m b) -> t a -> m (t b)
Map a monadic action over a traversable structure.
users <- mapM fetchUser userIds
forM
(Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
Like mapM but with arguments flipped.
emails <- forM users \user -> pure user.email

Model Support

Re-exported from IHP.ModelSupport for database operations.
createRecord
CanCreate record => record -> IO record
Insert a new record into the database.
newUser <- newRecord @User
    |> set #email "[email protected]"
    |> createRecord
updateRecord
CanUpdate record => record -> IO record
Update an existing record in the database.
user <- fetch userId
user
    |> set #name "New Name"
    |> updateRecord
deleteRecord
CanDelete record => record -> IO ()
Delete a record from the database.
user <- fetch userId
deleteRecord user

Time Operations

Re-exported from Data.Time.* modules.
UTCTime
Type
Absolute time type from Data.Time.Clock.
getCurrentTime
IO UTCTime
Get the current time.
now <- getCurrentTime
Day
Type
Calendar day type from Data.Time.Calendar.
LocalTime
Type
Local time type (no timezone) from Data.Time.LocalTime.

String Interpolation

plain
QuasiQuoter
String interpolation quasi-quoter.
let name = "Alice"
let greeting = [plain|Hello #{name}!|]
-- Returns: "Hello Alice!"

Exception Handling

throw
Exception e => e -> a
Throw a synchronous exception.
throwIO
Exception e => e -> IO a
Throw an exception in IO.
catch
Exception e => IO a -> (e -> IO a) -> IO a
Catch exceptions.
result <- doSomething `catch` \(e :: SomeException) -> 
    pure defaultValue

Concurrency

async
IO a -> IO (Async a)
Run an IO action asynchronously from Control.Concurrent.Async.
task <- async $ fetchFromAPI url
result <- wait task
wait
Async a -> IO a
Wait for an async action to complete.

Type-Level Programming

Proxy
Type -> Type
Proxy type for type-level programming.
Proxy @User
KnownSymbol
Constraint
Constraint for type-level strings.
symbolVal
KnownSymbol n => proxy n -> String
Get the value of a type-level symbol.

Module Imports

The prelude re-exports from these modules:
  • CorePrelude - Base functionality
  • Data.Text - Text operations
  • Data.Text.IO - Text I/O operations
  • IHP.HaskellSupport - IHP utilities
  • IHP.ModelSupport - Database operations
  • IHP.NameSupport - Naming conventions
  • Data.Time.* - Time operations
  • Control.Monad - Monadic operations
  • Data.String.Conversions - String conversions
  • GHC.Records - Record field access
  • GHC.OverloadedLabels - Label syntax (#fieldName)
  • Control.Exception.Safe - Safe exception handling
  • Control.Concurrent.Async - Asynchronous operations

See Also

Build docs developers (and LLMs) love