Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What symbol namespaces are there in Haskell?

Tags:

haskell

I'm trying to reduce my confusion about Haskell's syntax and would like to find out what the separate namespaces are in Haskell.

Namespaces meaning syntactical namespaces corresponding to the various symbols tables the compiler manages, not name scopes defined in code.

For example:

  • Value names (like function names)
  • Data constructors
  • Type constructors
  • Type parameters (in type definitions)
  • instances ?
  • ...?

I'm interested because I'm having trouble reading Haskell code (definitely more than with any other language) because I frequently have a hard time figuring out what exactly I'm looking at (especially with data/type constructors/type declarations).

Haskell seems to reuse a handful of syntactical constructs (esp. <name> <name> ...) in many places and relies on context - just turns out that the compiler is a lot better at this than me...

like image 382
Martin Avatar asked May 18 '12 21:05

Martin


2 Answers

The Haskell Report §1.4 says

There are six kinds of names in Haskell: those for variables and constructors denote values; those for type variables, type constructors, and type classes refer to entities related to the type system; and module names refer to modules. There are two constraints on naming:

  1. Names for variables and type variables are identifiers beginning with lowercase letters or underscore; the other four kinds of names are identifiers beginning with uppercase letters.
  2. An identifier must not be used as the name of a type constructor and a class in the same scope.

These are the only constraints; for example, Int may simultaneously be the name of a module, class, and constructor within a single scope.

like image 165
dave4420 Avatar answered Sep 21 '22 16:09

dave4420


The confusion can be avoided if you make sure you understand what you are reading:

  1. an expression: here every upper case name is a data constructor or a qualified variable or constructor whereas lower case are values
  2. a type: here every upper case name is a type constructor or class name, whereas lower case names are type variables.
like image 34
Ingo Avatar answered Sep 21 '22 16:09

Ingo