Types In Haskell

* programming haskell
[2025-12-28 Sun]

1. Creating Types In Haskell

Types can be created with data. The part before the = tells us the type. Which is 'NewBool' in this case'

data NewBool = False | True

Everything after the = tells us what value a type can have. So basically the int type would be something like:

data Int = -2147483648 | -2147483647 | ... | -1 | 0 | 1 | 2 | ... | 2147483647  

Value constructors are functions. newtype creates a new type from an existing one, or in this case an Int In my Ethics5.hs project, Agent is created as a type that derives Show, Eq

newtype Agent = Agent Int
  deriving (Show,Eq)

1.1. The deriving part

  • Show: If you type myAgent in your terminal, it will print Agent 101.
  • Eq: You can check if two agents are the same: Agent 1 == Agent 1 will be True.

2. Sum Types in Haskell

Because stimuli can have multiple types as described in Ethics(5), we need to use a sum type. A sum type describes a fixed set of different variants or scenarios that something can represent. Think of Stimulus as a category, and the items separated by | as the specific forms that category can take.

data Stimulus
  = Ambiguous
  | BoundaryViolation Agent
  | AidRequest Agent
  | Neutral
deriving (Eq, Show)

Like before,we have Eq so we can check if two stimuli are identical and Show to debug on the console. We also have constructors like BoundaryViolation Agent . To have a BoundaryViolation, you must have an agent.

3. Record Types In Haskell

This is a record object like a struct in C or object in python. Context is the type name and the Value Constructor (see: creating_types). And the curly braces define named fields.

data Context = Context
  { stimulus :: Stimulus,
    pressure :: Int
  } deriving (Eq, Show)

This creates a stimulus field that requires a Stimulus value we defined earlier like (Neutral or BoundaryViolation), and a pressure field that holds an integer.

4. Elsewhere

4.1. References

4.2. In my garden

Notes that link to this note (AKA backlinks).

Recent changes. Attachment Index Tag Index Bibliography Index Source.