mmm-0.1.0.0: Minecraft 1.21.4 implementation in Haskell
Copyright(c) axionbuster 2025
LicenseBSD-3-Clause
Safe HaskellNone
LanguageGHC2021

M.PkMacro

Description

This module provides Template Haskell functionality to generate data types with automatic Pack/Unpack instances. It uses a simple grammar to define data types and their field mappings.

Usage

Define data types using the pkmacro quasi-quoter. The syntax is indentation-insensitive:

-- First, set up default instances
setdefaultderives  -- Sets up Generic, Pack, and Unpack derives

-- Define a newtype wrapper with Pack/Unpack instances
newtype AAA = AAA Int32
  deriving stock (Generic)
  deriving newtype (Pack, Unpack)

[pkmacro|
-- Regular data type with two fields
data A {
  f1 :: Int32,                -- Regular field
  f2 :: Int32 via AAA,       -- Field with custom serialization
}

-- Data type with one field and explicit deriving
data B {
  f3 :: Int32,
  deriving (Generic, Show)
}

-- Empty data type (creates constructor with no fields)
data C {}
|]

The grammar supports:

  • Empty data types (no fields)
  • Custom serialization via via clause
  • Multiple data types in one block
  • Indentation-insensitive syntax
  • Comments (both -- and {- -} style)

Syntax

The full syntax for data type definitions is:

data TypeName {
  field1 :: Type1 [via Type2],     -- Field with optional via clause
  field2 :: Type3,                 -- Regular field
  [deriving (Class1, Class2)]      -- Optional proper deriving clause
  [and shadow deriving (Pack, Unpack) with (Class3, Class4)] -- Optional shadow deriving
}

Elements in square brackets are optional.

  • The via clause specifies a different type to use for serialization
  • deriving adds instances to the main data type
  • shadow deriving adds instances to the generated shadow type used for serialization
  • Multiple data types can be defined in a single quasi-quoter block

Type Syntax

Types can include:

  • Simple types: Int32, Text, etc.
  • Parameterized types: Maybe a, [Int]
  • Type applications: a @k
  • Promoted types: 'True, 'Just
  • Type literals: "hello", 123
  • Parenthesized types: (a, b), (Either a b)

See: Pack, Unpack, setdefaultderives, addproperderives, and addshadowderives.

Synopsis

Documentation

setdefaultderives :: Q [Dec] Source #

Set up default derives for subsequent data types. This sets Generic for proper derives and Generic + Pack + Unpack for shadow derives.

Use this at the start of your module to automatically derive the most common instances.

addproperderives :: [Name] -> Q [Dec] Source #

Add proper deriving clauses for subsequent data types. These instances will be derived directly on the main data type.

addproperderives [''Generic, ''Show]  -- Derive Generic and Show

addshadowderives :: [Name] -> Q [Dec] Source #

Add shadow deriving clauses for subsequent data types. These instances will be derived on the shadow data type used for serialization.

addshadowderives [''Generic, ''Pack]  -- Derive Generic and Pack on shadow type

pkmacro :: QuasiQuoter Source #

See module docs (M.PkMacro) for information.