AgentSkillsCN

fact-haskell-patterns

FACT: 本代码库特有的Haskell惯用法与模式——类型、导入、错误处理。

SKILL.md
--- frontmatter
name: fact-haskell-patterns
description: "FACT: Haskell idioms and patterns specific to this codebase - types, imports, error handling."
type: fact

Haskell Patterns for This Project

Language-level facts specific to how Haskell is used in this codebase.

Type Conventions

UseDon't UseBoundary Exception
TextStringHakyll APIs require String
Maybe afromJustNever
Either e aerrorOnly in env var checks
Explicit type signaturesType inference onlyAlways annotate top-level

Import Style

haskell
-- Qualified imports for common clashes
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Text as T

-- Explicit imports for everything else
import Data.Maybe (fromMaybe, listToMaybe)
import Control.Monad (when, unless, forM_)

Error Handling Hierarchy

code
Preferred (most → least):
1. Maybe a          -- Simple absence
2. Either Text a    -- Recoverable with message
3. MonadFail m      -- In monadic context
4. error "msg"      -- ONLY for "impossible" states (env vars)

Hakyll-Specific Patterns

Context Composition

haskell
-- Specific → General → Default
myContext = 
    specificField <>
    sharedFields <>
    defaultContext

Route Patterns

haskell
-- Always use explicit language in route
route $ customRoute $ \ident ->
    lang </> section </> slug </> "index.html"

Compiler Patterns

haskell
-- Snapshot for reuse
compile $ do
    pandocCompiler
        >>= saveSnapshot "content"  -- For feeds, lists
        >>= loadAndApplyTemplate "templates/post.html" ctx

Common Helpers

These exist in the codebase:

HelperLocationPurpose
slugFromPathsrc/Site.hsExtract slug from identifier
langStrsrc/Site.hsLanguage code as String
safeInitsrc/Site.hsSafe init (no error on [])
nubOrdsrc/Site.hsO(n log n) deduplication

Anti-Patterns in This Codebase

Anti-PatternWhy Bad HereBetter
head, tailPartial functionsPattern match or listToMaybe
nubO(n²)Use nubOrd
readPartialUse readMaybe
Wildcard importsUnclear dependenciesExplicit imports
String concatenationInefficientUse Text with <>

Module Organization

code
src/
├── Site.hs          # Main rules, routes
├── Config.hs        # YAML config types
├── Context.hs       # Template contexts
├── Paginate.hs      # Pagination helpers
├── Feed.hs          # RSS generation
└── Compiler/
    ├── Pandoc.hs    # Markdown → HTML
    ├── KaTeX.hs     # Math rendering
    ├── Mermaid.hs   # Diagram rendering
    └── Cache.hs     # Content-addressed caching

GHC Warnings Active

This project uses strict warnings:

yaml
ghc-options:
  - -Werror                    # All warnings are errors
  - -Wall                      # Standard warnings
  - -Wunused-packages          # Unused dependencies
  - -Wmissing-export-lists     # Must export explicitly
  # ... (see package.yaml for full list)

Implication: Code that compiles has passed all these checks.