type-fold: A library for folding types to a value.

[ data, library, mit, type-level-programming ] [ Propose Tags ] [ Report a vulnerability ]

This module provides an interface for a generic folding mechanism over types, allowing library authors to define the shape of fold, while library users can define custom folding logic for their types by implementing the TypeFoldStep type class.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (<5) [details]
License MIT
Author Eiko
Maintainer eikochanowo@outlook.com
Uploaded by eiko at 2026-02-10T21:15:55Z
Category Data, Type-Level Programming
Source repo head: git clone https://github.com/Eiko-Tokura/type-fold.git
Distributions
Downloads 1 total (1 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for type-fold-0.1.0.0

[back to package description]

type-fold

This module provides an interface for a generic folding mechanism over types, allowing library authors to define the shape of fold, while library users can define custom folding logic for their types by implementing the TypeFoldStep type class.

The TypeFold type class serves as a marker for types that can be folded, while the RecursiveInput type family defines the input type for each folding step.

Let's see an example, folding [Natural] to Integer:

(The TypeFold instances for [a] is already defined by the library, so we only need to define the TypeFoldStep instances for our specific folding logic.)

{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE KindSignatures        #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeApplications      #-}
{-# LANGUAGE TypeOperators         #-}
{-# OPTIONS_GHC -Wno-orphans       #-}
import Data.Proxy
import GHC.TypeLits
import Type.Fold

instance TypeFoldStep ('[] :: [Natural]) Integer where
  foldTStep _ = 0

instance KnownNat n => TypeFoldStep (n ': xs) Integer where
  foldTStep = (natVal (Proxy @n) +)

example :: Integer
example = foldT @_ @[1, 2, 3]

RequiredTypeArguments

If you enable RequiredTypeArguments, (which requires ghc >= 9.10.1), you can use the helpers foldT_ and foldTStep_ to avoid the @_ @MyType noise:

example :: Integer
example = foldT_ [1, 2, 3]