Haskell 入门 - 零碎版
GHC20xx
GHC2021
GHC 9.2.1 (2021-10-29) introduced the new GHC2021 language extension set.
Haskell 2010
4. Declarations and Bindings
4.1 Overview of Types and Classes
4.1.2 Syntax of Types
- Type variables
- Type constructors
- type constants with kind ∗
- types with kind ∗ → ∗
- built-in type constructors
- trivial type, as () with *
- function type, as (->), kind ∗ → ∗ → ∗
- list type, as [], kind ∗ → ∗
- tuple types, as (,), (,,), and so on, kind ∗ → ∗ → ∗ and so on.
- Special syntax, built-in type constructors for functions, tuples, and lists,
- function type, t1 -> t2
- tuple type, (t1, . . . , tk)
- list type, [t]
- Type application, 接收类型化参数,具化为新类型
- A parenthesized type, form (t), is identical to the type t.
4.2 User-Defined Datatypes
- algebraic datatypes (data declarations)
- renamed datatypes (newtype declarations)
- type synonyms (type declarations)
In general, an algebraic type specifies a sum of one or more [or zero][EmptyDataDecls] alternatives, where each alternative is a product of zero or more fields.
... the rule that constructor names always begin with a capital, making it easy to distinguish constructors (like Leaf and Branch) from variables (like x, t, and u).
In Haskell, instead of a special construct, the module system is used to support data abstraction. One constructs an abstract data type by introducing an algebraic type, and then exporting the type but hiding its constructors.
It was unfortunate that the Haskell definition of Stack given above forced the representation of stacks to be not quite isomorphic to lists, as lifting added a new bottom value ⊥ distinct from Stk ⊥. Now one could avoid this problem by replacing the data declaration in Stack above with the following declaration.
newtype Stack a = Stk [a]
vsdata Stack a = Stk [a]
One of the most obvious omissions from early versions of Haskell was the absence of records, offering named fields.
There are a huge number of record systems, variously supporting record extension, concatenation, update, and polymorphism. All of them have a complicating effect on the type system (e.g., row polymorphism and/or subtyping), which was already complicated enough.
so the committee adopted a minimalist design originally suggested by Mark Jones: record syntax in Haskell 1.3 (and subsequently) is simply syntactic sugar for equivalent operation on regular algebraic data types. Neither record-polymorphic operations nor subtyping are supported. This minimal design has left the field open for more sophisticated proposals.
4.2.1 Algebraic Datatype Declarations
示例:
-- libraries/base/GHC/Maybe.hs
data Maybe a = Nothing | Just a
deriving ( Eq, Ord )
-- libraries/ghc-prim/GHC/Types.hs
data List a = [] | a : List a
4.2.2 Type Synonym Declarations
4.2.3 Datatype Renamings
4.3 Type Classes and Overloading
The basic idea of type classes is simple enough.
4.3.1 Class Declarations
4.3.2 Instance Declarations
参考
- GHC2021 - <ghc-proposals/0380-ghc2021>
- Hudak, P., Hughes, J., Peyton Jones, S., & Wadler, P. A history of Haskell: Being lazy with class. The Third ACM SIGPLAN History of Programming Languages Conference (HOPL-III) San Diego, California, June 9-10, 2007. https://doi.org/10.1145/1238844.1238856