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 convert a dynamically-typed value back
12 into a value with the same (monomorphic) type it had before converting
13 it into a dynamically-typed value.
15 The <tt/Dynamic/ library is capable of dealing with monomorphic types
16 only; no support for polymorphic dynamic values, but hopefully that
17 can be added at a later stage.
19 Examples where this library may come in handy (dynamic types, really -
20 hopefully the library provided here will suffice) are: persistent
21 programming, interpreters, distributed programming etc.
23 The following operations are provided over the <tt/Dynamic/ type:
26 data Dynamic -- abstract, instance of: Show --
28 toDyn :: Typeable a => a -> Dynamic
29 fromDyn :: Typeable a => Dynamic -> a -> a
30 fromDynamic :: Typeable a => Dynamic -> Maybe a
34 <item> <tt/toDyn/ converts a value into a dynamic one, provided
35 <tt/toDyn/ knows the (concrete) type representation of the value.
36 The <tt/Typeable/ type class is used to encode this, overloading a
37 function that returns the type representation of a value. More on this
39 <item> There's two ways of going from a dynamic value to one with
40 a concrete type: <tt/fromDyn/, tries to convert the dynamic value into
41 a value with the same type as its second argument. If this fails, the
42 default second argument is just returned. <tt/fromDynamic/ returns a
43 <tt/Maybe/ type instead, <tt/Nothing/ coming back if the conversion
46 The <tt/Dynamic/ type has got a <tt/Show/ instance which returns
47 a pretty printed string of the type of the dynamic value. (Useful when
51 <sect1> <idx/Representing types/
52 <label id="sec:Dynamic:TypeRep">
55 Haskell types are represented as terms using the <tt/TypeRep/
59 data TypeRep -- abstract, instance of: Eq, Show
60 data TyCon -- abstract, instance of: Eq, Show
62 mkTyCon :: String -> TyCon
63 mkAppTy :: TyCon -> [TypeRep] -> TypeRep
64 mkFunTy :: TypeRep -> TypeRep -> TypeRep
65 applyTy :: TypeRep -> TypeRep -> Maybe TypeRep
69 <item> <tt/mkAppTy/ applies a type constructor to a sequence of types,
71 <item> <tt/mkFunTy/ is a special case of <tt/mkAppTy/, applying
72 the function type constructor to a pair of types.
73 <item> <tt/applyTy/ applies a type to a function type. If possible,
74 the result type is returned.
75 <item> Type constructors are represented by the abstract type,
78 Most importantly, <tt/TypeRep/s can be compared for equality.
79 Type equality is used when converting a <tt/Dynamic/ value into a
80 value of some specific type, comparing the type representation that
81 the <tt/Dynamic/ value embeds with equality of the type representation
82 of the type we're trying to convert the dynamically-typed value into.
84 To allow comparisons between <tt/TypeRep/s to be implemented
85 efficiently, the <em/abstract/ <tt/TyCon/ type is used, with
86 the constructor function <tt/mkTyCon/ provided:
89 mkTyCon :: String -> TyCon
92 An implementation of the <tt/Dynamic/ interface guarantees the
96 mkTyCon "a" == mkTyCon "a"
99 A really efficient implementation is possible if we guarantee/demand
100 that the strings are unique, and for a particular type constructor,
101 the application <tt/mkTyCon/ to the string that represents the type
102 constructor is never duplicated. <bf/Q:/ <em>Would this constraint be
103 unworkable in practice?</em>
105 Both <tt/TyCon/ and <tt/TypeRep/ are instances of the <tt/Show/ type
106 classes. To have tuple types be shown in infix form, the <tt/Show/
107 instance guarantees that type constructors consisting of <tt/n/-commas,
108 i.e., (<tt/mkTyCon ",,,,"/), is shown as an <tt/(n+1)/ tuple in infix
112 <sect1>The <tt/Typeable/ class
113 <nidx>Typeable class</nidx>
114 <label id="sec:Dynamic:Typeable">
117 To ease the construction of <tt/Dynamic/ values, we
118 introduce the following type class to help working with <tt/TypeRep/s:
121 class Typeable a where
122 typeOf :: a -> TypeRep
126 <item> The <tt/typeOf/ function is overloaded to return the type
127 representation associated with a type.
128 <item> <bf/Important:/ The argument to <tt/typeOf/ is only used to
129 carry type information around so that overloading can be resolved.
130 <tt/Typeable/ instances should never, ever look at this argument.
131 <item> The <tt/Dynamic/ library provide <tt/Typeable/ instances
132 for all Prelude and Hugs/GHC extension library types. They are:
136 Int, Char, Bool, Float, Double, Integer, (IO a),
137 [a], (Either a b), (Maybe a), (a->b),
138 (), (,), (,,), (,,,), (,,,,),
139 Ordering, Complex, Array, Handle
141 Addr, Word8, Word16, Word32, Word64,
142 Int8,Int16,Int32,Int64,
143 ForeignObj, MVar, (ST s a), (StablePtr a)
145 Word, ByteArray, MutableByteArray
150 <sect1> <idx/Utility functions/
151 <label id="sec:Dynamic:util">
154 Operations for applying a dynamic function type to a
155 dynamically typed argument are commonly useful, and
159 dynApply :: Dynamic -> Dynamic -> Dynamic -- unsafe.
160 dynApplyMb :: Dynamic -> Dynamic -> Maybe Dynamic