Skip to content

Instantly share code, notes, and snippets.

View poscat0x04's full-sized avatar

Poscat poscat0x04

  • PRC
  • 11:35 (UTC +08:00)
View GitHub Profile

Modern haskell - Concurrency (2)


上篇文章简单的介绍了一下 Haskell (GHC) 提供的 user-level concurrency primitives, 这篇将介绍 Haskell 的异常与资源管理。本文默认读者对其他语言中的异常有一定了解。


众所周知,处理异常是非常 impure 的(这里将 purity 定义为 confluence modulo exceptions and non-terminations), 而且 haskell 已经有处理异常的方式了: EitherExceptT, 为什么还需要加入 exception 呢?

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
  1. Crappy record system: no constructor/field overloading, no dot syntax, no row polymorphism
  2. Crappy module system There's backpack, apparently
  3. CPP -- this could be solved by pragmas
  4. Crappy dependent type support
    1. Boilerplate & Code duplication
    2. No totality checking
    3. Crappy type system (Type :: Type, Weak type-level functions)
    4. Barely usable typed holes, extreamly bad editor integration
    5. Weird behaviors when checking GADTs
  5. Over restrictive typeclass resolution (no backtracking, crappy termination checking leading to unnecessary use of UndecidableInstances)

Modern Haskell - Concurrency


When the limestone of imperative programming has worn away, the granite of functional programming will be revealed underneath -- Simon Peyton Jones


虽然 Haskell 拥有丰富而成熟的并发特性,但在平时交谈时这些特性似乎很少被提到。因此我想通过 Concurrency 系列文章从用户角度科普一下 Haskell 的并发编程的特性 (先从 User-level concurrency primitives 讲起)。本文将默认读者对 Haskell 和并发编程有基本的了解。

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
poscat0x04 / Common patterns in Haskell C FFI design.txt
Created February 25, 2020 14:19
What I learnd from writing alpm4h
This is a draft for a future blog post and might be deleted at any time.
Audiances are assumed to be familiar with the c2hs library and Haskell c ffi and have basic knowledge of c.
1. Overall design
I would suggest to do a two-layer design, that is one-level for binding and basic marshalling and one-level for abstraction.
2. Representing C datatypes in Haskell
When designing FFI, you almost always need to represent C datatypes as some Haskell datatypes. You'll need to further add Storable
instance for that Haskell type if you need to marshal between C and Haskell. Associating a C type with Haskell is relatively easy
with c2hs so I'll only cover the marshalling part.
This is my philosophical argument on why functional programming matters.
1. Abstraction
Functional programming unlocks a whole varieties of abstractions (monads, arrows and profunctors for example) that are meaningless to
mimic or impractical and unable to implement in imperative languages. These abstractions help us to tackle and understand a complex
software system to effectively reduce the complexity
(insert notation as a tool of thought citation)
2. Constraints
Funcitonal programming let us specify what exactly a program can do and cannot do -- type signatures, class constraints, dependent types,
poscat0x04 /
Last active May 24, 2020 14:31
Airprint zeroconf service generating script (python3 ver)
#!/usr/bin/env python
Copyright (c) 2010 Timothy J Fontaine <>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is