Record of STRefs better than STRef to a Record?

Jorge Adriano
Thu, 14 Nov 2002 01:10:01 +0000

> > I'm passing that 'record' around as an implicit value. The record as
> > STRefs that I use to collect info, but it also has some pure fields w=
> > 'read-only' info. Something like,

<opss let me change 'max' to 'm'>=20
> > data E s =3D E{
> >       refi :: STRef s Int,
> >       refc :: STRef s Char,
> >       m    :: Int =20
> >     }
> >
> > In some functions I might need only some pure fields, and none of the
> > STRefs, but since I pass something of type 'E s' around, everything e=
> > up beeing monadic.
> there is no reason why that should be the case.  you only need to be
> within the ST monad when you read the references, so if in some functio=
> you only look at the pure values, it can have a pure type.
> hope this helped
> iavor

That's what I thought, untill I tried it.=20
Well, actually you must be right since the pure field defines a pure=20
(projection) function... Hmmm, ok, can someone explain this to me,

data E s =3D E{
      refi :: STRef s Int,
      refc :: STRef s Char,
      m    :: Int

-- this is fine, obviously...
pure   :: E s -> Int
pure e =3D m e

-- but this is not...
pure2 :: (?e :: E s) =3D> Int
pure2 =3D m (?e)

Why exactly isn't this allowed? What is the workaround?
Error msg:
    Ambiguous constraint `?e :: E s'
        At least one of the forall'd type variables mentioned by the=20
        must be reachable from the type after the '=3D>'
    In the type: forall s. (?e :: E s) =3D> Int
    While checking the type signature for `pure2'
Failed, modules loaded: none.