2 <label id="sec:Dynamic">
5 The <tt/Dynamic/ library provides cheap-and-cheerful dynamic types for
6 Haskell. A dynamically typed value is one which carries type
7 information with it at run-time, and is represented here by the
8 abstract type <tt/Dynamic/. Values can be converted into <tt/Dynamic/
9 ones, which can then be combined and manipulated by the program using
10 the operations provided over the abstract, dynamic type. One of
11 these operations allows you to (try to) convert a dynamically-typed
12 value back into a value with the same (monomorphic) type it had before
13 converting it into a dynamically-typed value. If the dynamically-typed
14 value isn't of the desired type, the coercion will fail.
16 The <tt/Dynamic/ library is capable of dealing with monomorphic types
17 only; no support for polymorphic dynamic values, but hopefully that
18 will be added at a later stage.
20 Examples where this library may come in handy (dynamic types, really -
21 hopefully the library provided here will suffice) are: persistent
22 programming, interpreters, distributed programming etc.
24 The following operations are provided over the <tt/Dynamic/ type:
27 data Dynamic -- abstract, instance of: Show --
29 toDyn :: Typeable a => a -> Dynamic
30 fromDyn :: Typeable a => Dynamic -> a -> a
31 fromDynamic :: Typeable a => Dynamic -> Maybe a
35 <item> <tt/toDyn/ converts a value into a dynamic one, provided
36 <tt/toDyn/ knows the (concrete) type representation of the value.
37 The <tt/Typeable/ type class is used to encode this, overloading a
38 function that returns the type representation of a value. More on this
40 <item> There's two ways of going from a dynamic value to one with
41 a concrete type: <tt/fromDyn/, tries to convert the dynamic value into
42 a value with the same type as its second argument. If this fails, the
43 default second argument is just returned. <tt/fromDynamic/ returns a
44 <tt/Maybe/ type instead, <tt/Nothing/ coming back if the conversion
47 The <tt/Dynamic/ type has got a <tt/Show/ instance which returns
48 a pretty printed string of the type of the dynamic value. (Useful when
52 <sect1> <idx/Representing types/
53 <label id="sec:Dynamic:TypeRep">
56 Haskell types are represented as terms using the <tt/TypeRep/
60 data TypeRep -- abstract, instance of: Eq, Show
61 data TyCon -- abstract, instance of: Eq, Show
63 mkTyCon :: String -> TyCon
64 mkAppTy :: TyCon -> [TypeRep] -> TypeRep
65 mkFunTy :: TypeRep -> TypeRep -> TypeRep
66 applyTy :: TypeRep -> TypeRep -> Maybe TypeRep
70 <item> <tt/mkAppTy/ applies a type constructor to a sequence of types,
72 <item> <tt/mkFunTy/ is a special case of <tt/mkAppTy/, applying
73 the function type constructor to a pair of types.
74 <item> <tt/applyTy/ applies a type to a function type. If possible,
75 the result type is returned.
76 <item> Type constructors are represented by the abstract type,
79 Most importantly, <tt/TypeRep/s can be compared for equality.
80 Type equality is used when converting a <tt/Dynamic/ value into a
81 value of some specific type, comparing the type representation that
82 the <tt/Dynamic/ value embeds with equality of the type representation
83 of the type we're trying to convert the dynamically-typed value into.
85 To allow comparisons between <tt/TypeRep/s to be implemented
86 efficiently, the <em/abstract/ <tt/TyCon/ type is used, with
87 the constructor function <tt/mkTyCon/ provided:
90 mkTyCon :: String -> TyCon
93 An implementation of the <tt/Dynamic/ interface guarantees the
97 mkTyCon "a" == mkTyCon "a"
100 A really efficient implementation is possible if we guarantee/demand
101 that the strings are unique, and for a particular type constructor,
102 the application <tt/mkTyCon/ to the string that represents the type
103 constructor is never duplicated. <bf/Q:/ <em>Would this constraint be
104 unworkable in practice?</em>
106 Both <tt/TyCon/ and <tt/TypeRep/ are instances of the <tt/Show/ type
107 classes. To have tuple types be shown in infix form, the <tt/Show/
108 instance guarantees that type constructors consisting of <tt/n/-commas,
109 i.e., (<tt/mkTyCon ",,,,"/), is shown as an <tt/(n+1)/ tuple in infix
113 <sect1><idx>The Typeable class</idx>
114 <nidx>Typeable class</nidx>
115 <label id="sec:Dynamic:Typeable">
118 To ease the construction of <tt/Dynamic/ values, we
119 introduce the following type class to help working with <tt/TypeRep/s:
122 class Typeable a where
123 typeOf :: a -> TypeRep
127 <item> The <tt/typeOf/ function is overloaded to return the type
128 representation associated with a type.
129 <item> <bf/Important:/ The argument to <tt/typeOf/ is only used to
130 carry type information around so that overloading can be resolved.
131 <tt/Typeable/ instances should never, ever look at this argument.
132 <item> The <tt/Dynamic/ library provide <tt/Typeable/ instances
133 for all Prelude and Hugs/GHC extension library types. They are:
137 Int, Char, Bool, Float, Double, Integer, (IO a),
138 [a], (Either a b), (Maybe a), (a->b),
139 (), (,), (,,), (,,,), (,,,,),
140 Ordering, Complex, Array, Handle
142 Addr, Word8, Word16, Word32, Word64,
143 Int8,Int16,Int32,Int64,
144 ForeignObj, MVar, (ST s a), (StablePtr a)
146 Word, ByteArray, MutableByteArray
151 <sect1> <idx/Utility functions/
152 <label id="sec:Dynamic:util">
155 Operations for applying a dynamic function type to a
156 dynamically typed argument are commonly useful, and
160 dynApply :: Dynamic -> Dynamic -> Dynamic -- unsafe.
161 dynApplyMb :: Dynamic -> Dynamic -> Maybe Dynamic