cca57895b52ee345d3ca7bb03f519db6bb2947cc
[ghc-base.git] / Data / Types.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Data.Types
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  portable
10 --
11 -- This module provides a style of encoding types as values and using
12 -- them. This style is seens as an alternative to the pragmatic style
13 -- used in Data.Typeable and elsewhere, i.e., simply use an undefined
14 -- to denote a type argument. This pragmatic style suffers from lack
15 -- of robustness: one fells tempted to pattern match on undefineds.
16 --
17 -----------------------------------------------------------------------------
18
19 module Data.Types
20   (
21
22         -- * Types as values
23         TypeVal,                -- view type "a" as "a -> ()"
24         typeVal,                -- :: TypeVal a
25         typeValOf,              -- :: a -> TypeVal a
26         undefinedType,          -- :: TypeVal a -> a
27         withType,               -- :: a -> TypeVal a -> a
28         argType,                -- :: (a -> b) -> TypeVal a
29         resType,                -- :: (a -> b) -> TypeVal b
30         TypeFun                 -- functions on types
31
32   ) where
33
34
35 -------------------------------------------------------------
36 --
37 --      Types as values
38 --
39 -------------------------------------------------------------
40
41
42 -- Type as values to stipulate use of undefineds
43 type TypeVal a = a -> ()
44
45
46 --- The value that denotes a type
47 typeVal :: TypeVal a
48 typeVal = const ()
49
50
51 -- Map a value to its type
52 typeValOf :: a -> TypeVal a
53 typeValOf _ = typeVal
54
55
56 -- Stipulate this idiom!
57 undefinedType :: TypeVal a -> a
58 undefinedType _ = undefined
59
60
61 -- Constrain a type
62 withType :: a -> TypeVal a -> a
63 withType x _ = x
64
65
66 -- The argument type of a function
67 argType :: (a -> b) -> TypeVal a
68 argType _ = typeVal
69
70
71 -- The result type of a function
72 resType :: (a -> b) -> TypeVal b
73 resType _ = typeVal
74
75
76 -- Type functions,
77 -- i.e., functions mapping types to values
78 --
79 type TypeFun a r = TypeVal a -> r