diff options
author | Iavor S. Diatchki <iavor.diatchki@gmail.com> | 2012-03-31 12:47:25 -0700 |
---|---|---|
committer | Iavor S. Diatchki <iavor.diatchki@gmail.com> | 2012-03-31 12:47:25 -0700 |
commit | 05710922ae4c9aa071711051a050f7e8dccf8cc2 (patch) | |
tree | a8f4b4d569c984ddb5d02d9df1846f911aa1d7bc /docs | |
parent | d402d8a61ee729287d417c3355be72c61bb35b78 (diff) | |
download | haskell-05710922ae4c9aa071711051a050f7e8dccf8cc2.tar.gz |
Add a section about promoted literals to the manual.
Diffstat (limited to 'docs')
-rw-r--r-- | docs/users_guide/glasgow_exts.xml | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index 53dff29cf1..14d0630145 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -5371,6 +5371,52 @@ Note that this requires <option>-XTypeOperators</option>. </para> </sect3> +<sect3 id="promoted-literals"> +<title>Promoted Literals</title> +<para> +Numeric and string literals are prmoted to the type level, giving convenient +access to a large number of predefined type-level constants. Numeric literals +are of kind <literal>Nat</literal>, while string literals are of kind +<literal>Symbol</literal>. These kinds are defined in the module +<literal>GHC.TypeLits</literal>. +</para> + +<para> +Here is an exampe of using type-level numeric literals to provide a safe +interface to a low-level function: +<programlisting> +import GHC.TypeLits +import Data.Word +import Foreign + +newtype ArrPtr (n :: Nat) a = ArrPtr (Ptr a) + +clearPage :: ArrPtr 4096 Word8 -> IO () +clearPage (ArrPtr p) = ... +</programlisting> +</para> + +<para> +Here is an example of using type-level string literals to simulate +simple record operations: +<programlisting> +data Label (l :: Symbol) = Get + +class Has a l b | a l -> b where + from :: a -> Label l -> b + +data Point = Point Int Int deriving Show + +instance Has Point "x" Int where from (Point x _) _ = x +instance Has Point "y" Int where from (Point _ y) _ = y + +example = from (Point 1 2) (Get :: Label "x") +</programlisting> +</para> +</sect3> + + + </sect2> <sect2 id="kind-polymorphism-limitations"> |