Debian Codegraph @

c2hs/base/general/UNames.hs

raw symbols SCIP
1 -- The HiPar Toolkit: generates unique names
2 --
3 -- Author : Manuel M T Chakravarty
4 -- Created: 3 April 98
5 --
6 -- Version $Revision: 1.2 $ from $Date: 2004/11/13 17:26:50 $
7 --
8 -- Copyright (C) [1998..2003] Manuel M T Chakravarty
9 --
10 -- This file is free software; you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as published by
12 -- the Free Software Foundation; either version 2 of the License, or
13 -- (at your option) any later version.
14 --
15 -- This file is distributed in the hope that it will be useful,
16 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
17 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 -- GNU General Public License for more details.
19 --
20 --- DESCRIPTION ---------------------------------------------------------------
21 --
22 -- Generates unique names according to a method of L. Augustsson, M. Rittri
23 -- & D. Synek ``Functional pearl: On generating unique names'', Journal of
24 -- Functional Programming 4(1), pp 117-123, 1994.
25 --
26 -- WARNING: DON'T tinker with the implementation! It uses UNSAFE low-level
27 -- operations!
28 --
29 --- DOCU ----------------------------------------------------------------------
30 --
31 -- language: Haskell 98
32 --
33 -- * This module provides an ordering relation on names (e.g., for using
34 -- `Maps'), but no assumption maybe made on the order in which names
35 -- are generated from the name space. Furthermore, names are instances of
36 -- `Ix' to allow to use them as indices.
37 --
38 -- * A supply should be used *at most* once to *either* split it or extract a
39 -- stream of names. A supply used repeatedly will always generate the same
40 -- set of names (otherwise, the whole thing wouldn't be referential
41 -- transparent).
42 --
43 -- * If you ignored the warning below, looked at the implementation, and lost
44 -- faith, consider that laziness means call-by-need *and* sharing, and that
45 -- sharing is realized by updating evaluated thunks.
46 --
47 -- * ATTENTION: No clever CSE or unnecessary argument elimination may be
48 -- applied to the function `names'!
49 --
50 --- TODO
51 --
52
53 module UNames (NameSupply, Name,
54 rootSupply, splitSupply, names,
55 saveRootNameSupply, restoreRootNameSupply, unsafeResetRootNameSupply)
56 where
57
58 import Control.Monad (when)
59 import Data.Ix
60 import System.IO.Unsafe (unsafePerformIO)
61 import Data.IORef (IORef, newIORef, readIORef, writeIORef)
62 import Binary (Binary(..))
63
64
65 -- Name supply definition (EXPORTED ABSTRACTLY)
66 --
67 newtype NameSupply = NameSupply (IORef Int)
68
69 -- Name (EXPORTED ABSTRACTLY)
70 --
71 newtype Name = Name Int
72 -- deriving (Show, Eq, Ord, Ix)
73 -- FIXME: nhc98, v1.08 can't derive Ix
74 deriving (Eq, Ord)
75 instance Ix Name where
76 range (Name from, Name to) = map Name (range (from, to))
77 index (Name from, Name to) (Name idx) = index (from, to) idx
78 inRange (Name from, Name to) (Name idx) = inRange (from, to) idx
79
80 -- we want to show the number only, to be useful for generating unique
81 -- printable names
82 --
83 instance Show Name where
84 show (Name i) = show i
85
86
87 -- *** DON'T TOUCH THE FOLLOWING ***
88 -- and if you believe in the lambda calculus better also don't look at it
89 -- ! here lives the daemon of unordered destructive updates !
90
91 -- The initial supply (EXPORTED)
92 --
93 rootSupply :: NameSupply
94 {-# NOINLINE rootSupply #-}
95 rootSupply = NameSupply (unsafeNewIntRef 1)
96
97 -- Split a name supply into a stream of supplies (EXPORTED)
98 --
99 splitSupply :: NameSupply -> [NameSupply]
100 splitSupply s = repeat s
101
102 -- Given a name supply, yield a stream of names (EXPORTED)
103 --
104 names :: NameSupply -> [Name]
105 --
106 -- The recursion of `theNames' where `s' is passed as an argument is crucial,
107 -- as it forces the creation of a new closure for `unsafeReadAndIncIntRef s'
108 -- in each recursion step. Sharing a single closure or building a cyclic
109 -- graph for a nullary `theNames' would always result in the same name! If
110 -- the compiler ever gets clever enough to optimize this, we have to prevent
111 -- it from doing so.
112 --
113 names (NameSupply s) =
114 theNames s
115 where
116 theNames s = Name (unsafeReadAndIncIntRef s) : theNames s
117
118 -- Actions for saving and restoring the state of the whole program. (EXPORTED)
119 -- The rules for these functions are as follows:
120 -- you must not use the root name supply after saving it
121 -- you must not use the root namue supply before restoring it
122 -- Otherwise bad things will happen, your unique Ids will no longer be unique
123 saveRootNameSupply :: IO Name
124 saveRootNameSupply =
125 case rootSupply of
126 NameSupply ref -> do
127 val <- readIORef ref
128 writeIORef ref 0
129 return (Name val)
130
131 restoreRootNameSupply :: Name -> IO ()
132 restoreRootNameSupply (Name val) =
133 case rootSupply of
134 NameSupply ref -> do
135 prev <- readIORef ref
136 when (prev > 1) (error "UName: root name supply used before restoring")
137 writeIORef ref val
138
139 -- Resets the root name supply
140 -- you must not do this unless you are done with all the names
141 unsafeResetRootNameSupply :: IO ()
142 unsafeResetRootNameSupply =
143 case rootSupply of
144 NameSupply ref -> writeIORef ref 1
145
146 {-! for Name derive : GhcBinary !-}
147 {-* Generated by DrIFT : Look, but Don't Touch. *-}
148 instance Binary Name where
149 put_ bh (Name aa) = do
150 put_ bh aa
151 get bh = do
152 aa <- get bh
153 return (Name aa)
154
155 -- UNSAFE mutable variables
156 -- ------------------------
157
158 -- WARNING: The following does not exist, or at least, it belongs to another
159 -- world. And if you believe into the lambda calculus, you don't
160 -- want to know about this other world.
161 --
162 -- *** DON'T TOUCH NOR USE THIS STUFF ***
163 -- (unless you really know what you are doing!)
164
165 -- UNSAFELY create a mutable integer (EXPORTED)
166 --
167 unsafeNewIntRef :: Int -> IORef Int
168 unsafeNewIntRef i = unsafePerformIO (newIORef i)
169
170 -- UNSAFELY increment a mutable integer and yield its value before the
171 -- increment (EXPORTED)
172 --
173 unsafeReadAndIncIntRef :: IORef Int -> Int
174 unsafeReadAndIncIntRef mv = unsafePerformIO $ do
175 v <- readIORef mv
176 when (v<1) $
177 error "UName: root name supply used after saving"
178 writeIORef mv (v + 1)
179 return v