[project @ 1999-07-27 07:42:31 by simonpj]
[ghc-hetmet.git] / ghc / docs / users_guide / libmisc.vsgml
index f9a104a..7a604a3 100644 (file)
@@ -239,6 +239,50 @@ accumulating any errors that occur.
 
 %************************************************************************
 %*                                                                      *
+<sect2>The @Memo@ library
+<label id="memo-library">
+<p>
+<nidx>Memo (GHC syslib)</nidx>
+%*                                                                      *
+%************************************************************************
+
+The @Memo@ library provides fast polymorphic memo functions using hash
+tables.  The interface is:
+
+<tscreen><verb>
+memo :: (a -> b) -> a -> b
+</verb></tscreen>
+
+So, for example, @memo f@ is a version of @f@ that caches the results
+of previous calls.  
+
+The searching is very fast, being based on pointer equality.  One
+consequence of this is that the caching will only be effective if
+<em/exactly the same argument is passed again to the memoised
+function/.  This means not just a copy of a previous argument, but the
+same instance.  It's not useful to memoise integer functions using
+this interface, because integers are generally copied a lot and two
+instances of '27' are unlikely to refer to the same object.
+
+This memoisation library works well when the keys are large (or even
+infinite).
+
+The memo table implementation uses weak pointers and stable names (see
+the GHC/Hugs library document) to avoid space leaks and allow hashing
+for arbitrary Haskell objects.  NOTE: while individual memo table
+entries will be garbage collected if the associated key becomes
+garbage, the memo table itself will not be collected if the function
+becomes garbage.  We plan to fix this in a future version.
+
+There's another version of @memo@ if you want to explicitly give a
+size for the hash table (the default size is 1001 buckets):
+
+<tscreen><verb>
+memo_sized :: Int -> (a -> b) -> a -> b
+</verb></tscreen>
+
+%************************************************************************
+%*                                                                      *
 <sect2>The @PackedString@ type
 <label id="PackedString">
 <p>
@@ -247,7 +291,7 @@ accumulating any errors that occur.
 %************************************************************************
 
 You need to @import PackedString@ and heave in your
-@-syslib misc@ to use @PackedString@s.
+@-syslib ghc@ to use @PackedString@s.
 
 The basic type and functions available are:
 <tscreen><verb>
@@ -532,7 +576,7 @@ Readline library.  As such, you will need to look at the GNU
 documentation (and have a @libreadline.a@ file around somewhere...)
 
 You'll need to link any Readlining program with @-lreadline -ltermcap@,
-besides the usual @-syslib misc@ (and @-fhaskell-1.3@).
+besides the usual @-syslib ghc@ (and @-fhaskell-1.3@).
 
 The main function you'll use is:
 <tscreen><verb>
@@ -556,8 +600,8 @@ will see in the GNU readline documentation.)
 
 %************************************************************************
 %*                                                                      *
-<sect2>The @Regexp@ and @MatchPS@ interfaces
-<label id="Regexp">
+<sect2>The @Regex@ and @MatchPS@ interfaces
+<label id="Regex">
 <p>
 <nidx>Regex library (GHC syslib)</nidx>
 <nidx>MatchPS library (GHC syslib)</nidx>
@@ -568,9 +612,10 @@ will see in the GNU readline documentation.)
 (Sigbjorn Finne supplied the regular-expressions interface.)
 
 The @Regex@ library provides quite direct interface to the GNU
-regular-expression library, for doing manipulation on
-@PackedString@s.  You probably need to see the GNU documentation
-if you are operating at this level.
+regular-expression library, for doing manipulation on @PackedString@s.
+You probably need to see the GNU documentation if you are operating at
+this level.  Alternatively, you can use the simpler and higher-level
+@RegexString@ interface.
 
 The datatypes and functions that @Regex@ provides are:
 <tscreen><verb>
@@ -700,6 +745,36 @@ matchPrefixPS :: PackedString -> PackedString -> Int
 
 %************************************************************************
 %*                                                                      *
+<sect2>The @RegexString@ interface
+<label id="RegexString">
+<p>
+<nidx>RegexString library (GHC syslib)</nidx>
+<nidx>regular-expressions library</nidx>
+%*                                                                      *
+%************************************************************************
+
+(Simon Marlow supplied the String Regex wrapper.)
+
+For simple regular expression operations, the @Regex@ library is a
+little heavyweight.  @RegexString@ permits regex matching on ordinary
+Haskell @String@s.
+
+The datatypes and functions that @RegexString@ provides are:
+<tscreen><verb>
+data Regex              -- a compiled regular expression
+
+mkRegEx
+        :: String       -- regexp to compile
+        -> Regex        -- compiled regexp
+
+matchRegex
+        :: Regex        -- compiled regexp
+        -> String       -- string to match
+        -> Maybe [String] -- text of $1, $2, ... (if matched)
+</verb></tscreen>
+
+%************************************************************************
+%*                                                                      *
 <sect2>Network-interface toolkit---@Socket@ and @SocketPrim@
 <label id="Socket">
 <p>
@@ -762,11 +837,17 @@ recvFrom  :: Hostname -> PortID -> IO String
 socketPort     :: Socket -> IO PortID
 
 data PortID    -- PortID is a non-abstract type
-  = Service String     -- Service Name eg "ftp"
-  | PortNumber Int     -- User defined Port Number
-  | UnixSocket String  -- Unix family socket in file system
+  = Service String       -- Service Name eg "ftp"
+  | PortNumber PortNumber -- User defined Port Number
+  | UnixSocket String    -- Unix family socket in file system
 
 type Hostname = String
+
+ -- 16-bit value (stored in network byte order).
+data PortNumber
+ -- instance of: Eq, Num, Show.
+
+mkPortNumber :: Int -> PortNumber
 </verb></tscreen>
 
 Various examples of networking Haskell code are provided in