Suggestion for  Haskell-2 library module  Random.


Haskell-98 declares
--------------------------------------------------------------------
class Random a where  randomR :: RandomGen g => (a, a) -> g -> (a, g)
                      ... 
...
instance Random Int     where ...
instance Random Integer where ...
...
 * randomR  takes a range (lo,hi) and a random number generator g, and
   returns a random value 
>  uniformly distributed   in the   closed interval [lo,hi], 
   together with a new generator. 
>  It is unspecified what happens if  lo > hi. 
   ...
---------------------------------------------------------------------
- here i re-formatted the text and marked two lines with `>'.


I suggest to change this ">" part to

-- (h2) ----------------------------------------------------------
  ...
  "uniformly distributed" in the "closed interval" [lo,hi], 
  together with a new generator. 
  It is required             randomR (lo,hi) g == randomR (hi,lo) g
  to hold for each  g.
  "Uniformly distributed" means the usual notion for the "numeric"
  types  Integer,Float,... - as well as for finite ones Bool,Char...
  Defining the Random instance for other types
  (say,  T = (Random a, Random b) => (a,b) ),
  the programmer has to rely on one's own understanding of 
  "uniform distribution" in domain T.
  Similar is the notion "closed interval [lo,hi]".
  For  Integer, Float, ...,  it is standard. 
  For other domains T, maybe even not supplied with ordering, it 
  means some subset in T defined by  lo, hi :: T, 
  this subset specification is on the programmer.
----------------------------------------------------------------

I do not insist on this formulation literally. 
This is only the idea.
Reason:
the programmer could define, for example,

  instance (Random a, Random b) => Random (a,b)       --instance (p)
    where
    randomR ((l,l'),(h,h')) g = case  randomR (l,h) g  of

      (a,g') -> case randomR (l',h') g' of  (b,g'') -> ((a,b),g'')

Then, the obstacles arise:
(1) what does might mean "closed interval [lo,hi]"  in (a,b) ?
(2) applying  instance (p)  to, say,  (Int,Int)
    may cause evaluation of   randomR (l,h) g  with  l > h  :: Int.

The above suggestion (h2) provides some explanation for this.

Personally, my aim was to provide random polynomials  f :: P a  
over various coefficient domains `a', for the need of testing of 
some functions. And the format   
                        randomR (polynomial1,polynomial2) g 

derived from what  Haskell-98 Random  provides, looks useful to 
define random values inside various subsets of  P a. 


------------------
Sergey Mechveliani
[EMAIL PROTECTED]








Reply via email to