newtype and access control

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

newtype and access control

Steven Leiva

Hello everyone,


I am watching a talk on YouTube called Wrangling Monad Transformer Stacks.


The talk has the following code:



code

{-# LANGUAGE GeneralizedNewtypeDeriving -#}

module DBTrans where

newtype WithTrans a = WithTrans (WithDBConn a) deriving (Functor, Applicative, Monad)

inDBTrans :: WithTrans a -> WithConn a

inDBTrans = ...

 NOT USING MIXMAX YET?


The speaker mentions that by using a newtype, we can perform "access control". The function inDBTrans is "aware" that the type constructor WithTrans is simply a wrapper for WithDBConn, and that functions outside of the module are not privy to that information. 


Out of necessity, the speaker has to assume a certain level of Haskell knowledge that I don't have, because I am confused as to what language principles would allow for inDBTrans to be used for access control.


I understand that a newtype declaration does create a new type from a "type safey" point of view; that a newtype declaration is required to have a single data constructor with a single (not named) field; and that, once the type-checking is done, the compiler can strip out the newtype wrapper and leave the underlying type (informally speaking, of course), but that still doesn't get me to inDBTrans can do access control.


Thank you for any help!


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: newtype and access control

Brandon Allbery
On Sun, Oct 29, 2017 at 7:52 PM, Steven Leiva <[hidden email]> wrote:

Out of necessity, the speaker has to assume a certain level of Haskell knowledge that I don't have, because I am confused as to what language principles would allow for inDBTrans to be used for access control.


As presented, that doesn't seem to be true. If the module exported only the type constructor, then it would be impossible for other modules to pattern match it and access the wrapped value. But the module has no export list, so it exports everything including the data constructor and modules can pattern match to unwrap it; the only other way to get that kind of isolation is for the wrapped data type to not be in scope, but that also doesn't seem to be the case.

--
brandon s allbery kf8nh                               sine nomine associates
[hidden email]                                  [hidden email]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: newtype and access control

Steven Leiva
Hi Brandon,

I think that exporting the data constructor was a simple oversight. I can definitely see how, if you don't export the data constructors, other modules can't pattern match on the newtype value and therefore can't "get at" the WithDBConn a value. 

Thank you!

On Oct 29, 2017 7:58 PM, "Brandon Allbery" <[hidden email]> wrote:
On Sun, Oct 29, 2017 at 7:52 PM, Steven Leiva <[hidden email]> wrote:

Out of necessity, the speaker has to assume a certain level of Haskell knowledge that I don't have, because I am confused as to what language principles would allow for inDBTrans to be used for access control.


As presented, that doesn't seem to be true. If the module exported only the type constructor, then it would be impossible for other modules to pattern match it and access the wrapped value. But the module has no export list, so it exports everything including the data constructor and modules can pattern match to unwrap it; the only other way to get that kind of isolation is for the wrapped data type to not be in scope, but that also doesn't seem to be the case.

--
brandon s allbery kf8nh                               sine nomine associates
[hidden email]                                  [hidden email]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.