惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
腾讯CDC
博客园 - Franky
S
SegmentFault 最新的问题
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
I
InfoQ
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed
博客园 - 叶小钗
博客园_首页
有赞技术团队
有赞技术团队
雷峰网
雷峰网
量子位
小众软件
小众软件
月光博客
月光博客
U
Unit 42
D
DataBreaches.Net

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
CLAUDE.md for Haskell: 13 Rules That Make AI Write Idioma...
Olivia Craft · 2026-05-15 · via DEV Community

Haskell has the largest gap between code that compiles and code that's actually idiomatic of any mainstream language. The type system is powerful enough that AI can generate Haskell that type-checks but uses head on empty lists, catches errors with error "", and avoids the very abstractions that make Haskell valuable.

A CLAUDE.md at your project root tells the AI which Haskell you're writing. Here are 13 rules with the highest impact.


Rule 1: GHC version and language extensions

GHC version: 9.6+ (GHC2021 language set or explicit extensions).
Enable these extensions in package.yaml or cabal file (not per-file pragmas except for unusual cases):
  OverloadedStrings, ScopedTypeVariables, LambdaCase, TupleSections,
  DeriveFunctor, DeriveGeneric, GeneralizedNewtypeDeriving,
  FlexibleInstances, FlexibleContexts

Do NOT enable: PartialTypeSignatures (forces you to complete type signatures).
Do NOT use extensions that enable unsafe operations unless documented.

Enter fullscreen mode Exit fullscreen mode

GHC's language extension system is powerful but needs explicit guidance. AI sometimes adds extensions ad-hoc or omits ones that would enable cleaner code.


Rule 2: No partial functions — totality is the goal

Partial functions are banned:
- Never use `head`, `tail`, `init`, `last` — use pattern matching or `NonEmpty`
- Never use `fromJust` — use `maybe`, `fromMaybe`, or pattern match
- Never use `read` on untrusted input — use `readMaybe` from `Text.Read`
- Never use `error` for recoverable failures — use `Either` or `ExceptT`
- Never use `undefined` except as a temporary compile placeholder with a TODO comment

Prefer total alternatives:
  `headMay`, `atMay` from the `safe` package, or pattern matching.

Enter fullscreen mode Exit fullscreen mode

Partial functions are the #1 source of runtime crashes in Haskell. head [] throws an exception that the type system didn't catch. AI uses partial functions because they're shorter and common in tutorials.


Rule 3: Maybe and Either — explicit absence and failure

Absence and failure:
- `Maybe a` for values that may not exist
- `Either e a` for operations that can fail with an error
- `ExceptT e m a` for monadic computations that can fail
- `MaybeT m a` for monadic computations that may produce nothing

Chain with `>>=`, `<$>`, `<*>`, `maybe`, `either`, `fromMaybe`.
Never throw exceptions for business logic — use `Left err`.

For error types: define a sum type, not `String`:
  `data AppError = NotFound Text | InvalidInput Text | DatabaseError Text`

Enter fullscreen mode Exit fullscreen mode

error "something went wrong" is Haskell's equivalent of unchecked exceptions. The type system is there to make errors explicit — use it.


Rule 4: Type signatures on all top-level definitions

Every top-level function and value must have an explicit type signature:
  `processUser :: UserId -> IO (Either AppError User)` — not inferred.

Type signatures serve as:
- Documentation (the primary kind)
- Compiler-checked contracts
- Guidance for type inference in complex expressions

Use `newtype` for type safety around primitives:
  `newtype UserId = UserId { unUserId :: Int } deriving (Show, Eq, Ord)`
  Not: `type UserId = Int` (type aliases provide no safety)

Enter fullscreen mode Exit fullscreen mode

Type inference is powerful but type signatures are still mandatory for top-level definitions. AI sometimes omits them to save space — they're not optional.


Rule 5: Pattern matching — exhaustive and expressive

Pattern matching rules:
- Always handle all constructors — enable `-Wincomplete-patterns` in GHC options
- Use `LambdaCase` for concise single-argument matches:
    `\case { Just x -> ...; Nothing -> ... }`
- Deconstruct in function arguments, not in the body:
    `processUser (User uid name) = ...` not `processUser u = let uid = userId u in ...`
- Use `@` patterns to bind the whole value while matching: `processUser u@(User uid _) = ...`
- Wildcards `_` only when the value is genuinely unused

Enter fullscreen mode Exit fullscreen mode

Exhaustive pattern matching with compiler warnings turns runtime errors into compile errors. AI often uses incomplete patterns or reaches for field accessors where destructuring is cleaner.


Rule 6: Functors, Applicatives, Monads — use the right abstraction

Abstraction selection:
- `fmap` / `<$>` when transforming a value inside a context (Functor)
- `<*>` when applying a wrapped function to a wrapped value (Applicative)
- `>>=` / `do` notation for sequential effects where each step depends on the previous (Monad)
- `traverse` for `t a -> (a -> f b) -> f (t b)` (effects over a traversable)
- `sequence` for `t (f a) -> f (t a)`

Prefer `do` notation for readability when there are 3+ monadic steps.
Prefer point-free style for simple compositions: `f . g . h` not `\x -> f (g (h x))`.

Enter fullscreen mode Exit fullscreen mode

The Functor/Applicative/Monad hierarchy exists to express precisely how much power you need. AI often reaches for Monad/do when Applicative or fmap is sufficient — the less powerful abstraction is usually clearer.


Rule 7: Text, not String

String handling:
- Use `Text` (from `Data.Text`) everywhere, not `String` ([Char])
- Enable `OverloadedStrings` so string literals work as `Text`
- Use `Data.Text.pack`/`unpack` only at system boundaries (IO, legacy APIs)
- For building text: `Data.Text.Builder` or `fmt` library — not `++` concatenation
- For parsing: `attoparsec` or `megaparsec` — not manual `String` manipulation
- ByteString for binary data — never treat `ByteString` as text

Enter fullscreen mode Exit fullscreen mode

String = [Char] is a linked list of characters. It's the default for historical reasons and is terrible for performance. Text is the correct type for textual data. AI uses String because tutorials use String.


Rule 8: Records and lenses

Record conventions:
- Use record syntax for data types with multiple fields
- Prefix field names with the type name to avoid ambiguity:
    `data User = User { userId :: UserId, userName :: Text, userEmail :: Email }`
- Use `lens` or `optics` for nested record access/update
- Avoid the `RecordWildCards` extension — it makes imports invisible
- For large records: use `Generic` + `lens` derivation

When using lenses:
  `user ^. userName` not `userName user`
  `user & userName .~ "Alice"` not `user { userName = "Alice" }`

Enter fullscreen mode Exit fullscreen mode

Record field names in Haskell pollute the module namespace without prefixing. Lenses provide composable access and update for nested data.


Rule 9: IO and effects — keep pure code pure

Effect discipline:
- Maximize pure functions — if it doesn't need IO, don't put it in IO
- Use `ReaderT env IO` as the application monad (not raw IO or a complex mtl stack)
- Keep the `IO` boundary at the edges: parse input, process purely, render output
- Use `IORef` only when mutation is genuinely required (rare)
- For config: pass explicit records, not global IORef or unsafePerformIO

For concurrent code: use `async` + `stm` (Software Transactional Memory).
Never use `unsafePerformIO` outside of FFI boundaries.

Enter fullscreen mode Exit fullscreen mode

"IO everywhere" is a beginner pattern. Pure functions are easier to test, reason about, and compose. The discipline of keeping IO at the edges is one of Haskell's greatest design patterns.


Rule 10: Testing with Hspec and QuickCheck

Testing:
- Hspec for unit and integration tests (BDD-style: `describe`/`it`/`shouldBe`)
- QuickCheck for property-based testing — generate invariants, not just examples
- `hspec-discover` for automatic spec discovery
- Test pure functions without IO — makes tests fast and deterministic
- For IO: use `hspec` with `before`/`after` for setup/teardown
- Golden tests: `hspec-golden` for output comparison

Property patterns to always write:
  roundtrip: `encode . decode = id`
  idempotence: `f (f x) = f x`
  commutativity where applicable

Enter fullscreen mode Exit fullscreen mode

QuickCheck is one of Haskell's most valuable tools and is underused in AI-generated test suites. Property-based tests catch edge cases that example-based tests miss by definition.


Rule 11: Cabal/Stack and dependency management

Build system: Cabal with cabal.project, or Stack with stack.yaml — pick one and be consistent.

cabal conventions:
- Pin GHC version in cabal.project: `with-compiler: ghc-9.6.x`
- Use `cabal freeze` for reproducible builds
- Separate library, executable, and test stanzas in .cabal file
- Enable warnings in all stanzas: `-Wall -Wcompat -Widentities`

HLS (Haskell Language Server): configure in hie.yaml for IDE support.
Do NOT use `cabal install` globally — use `cabal run` or add to PATH via nix/ghcup.

Enter fullscreen mode Exit fullscreen mode


Rule 12: Deriving and Generic

Use `deriving` to avoid boilerplate:
  `deriving (Show, Eq, Ord, Generic)`

For JSON: use `aeson` with Generic derivation:
  `instance ToJSON User; instance FromJSON User` — no manual instances unless customizing.

For other typeclasses: prefer `deriving` via `GeneralizedNewtypeDeriving` or `DerivingVia`.

Do NOT write manual `Show` instances unless the output format is specifically required.
Do NOT write manual `Eq` instances unless the equality semantics differ from structural equality.

Enter fullscreen mode Exit fullscreen mode

Boilerplate Show/Eq/Ord instances are noise. deriving is the correct default. AI sometimes writes them manually.


Rule 13: Haddock and documentation

Documentation:
- All exported functions must have Haddock comments
- Format: `-- | Description. Example: @functionName arg@`
- Document the invariants and preconditions, not the implementation
- Use `@since` tags for API additions
- Run `cabal haddock` to verify documentation builds

Module exports: explicit export lists on all modules — no `module Foo where` without an export list.
Explicit exports make the API surface clear and prevent accidental exports.

Enter fullscreen mode Exit fullscreen mode


Your CLAUDE.md starting point

# Haskell Project — AI Coding Rules

## Safety
No partial functions: no head/tail/fromJust/read/error/undefined.
Use Maybe/Either/ExceptT for absence and failure. Total functions only.

## Types
Explicit type signatures on all top-level definitions.
newtype over type aliases for domain types.
Text not String. OverloadedStrings enabled.

## Style
Exhaustive pattern matching (-Wincomplete-patterns enforced).
LambdaCase for single-argument matches.
Pure functions maximized — IO only at edges.
ReaderT env IO as application monad.

## Abstractions
Use fmap/Applicative when Monad is not needed.
traverse for effects over traversables.
Point-free for simple compositions.

## Records
Prefix field names with type name. lens/optics for nested access.

## Testing
Hspec + QuickCheck. Properties: roundtrip, idempotence. Pure functions tested without IO.

## Build
GHC 9.6+. -Wall -Wcompat -Wincomplete-patterns. Explicit export lists on all modules.

Enter fullscreen mode Exit fullscreen mode


Why Haskell especially needs this

Haskell's type system can catch an enormous class of bugs at compile time — but only if you use it correctly. Partial functions, String instead of Text, and avoiding the abstraction hierarchy all leave power on the table.

CLAUDE.md is the difference between AI that uses Haskell and AI that uses Haskell well.

The full rules pack across 15+ languages is at gumroad — $27.