I have a type:
newtype User = User
{ id :: String
, email :: String
, last_update :: String
}
and a function:
import Pux.DOM.HTML (HTML)
import Pux.DOM.HTML.Attributes (key)
import Text.Smolder.HTML as H
import Text.Smolder.HTML.Attributes as A
userRow :: User -> HTML Event
userRow user =
H.tr ! key user.id ! A.className "user-item" $ do
H.td ! key ("email") $ text user.email
H.td ! key ("last_update") $ text user.last_update
H.td ! key ("actions") ! A.className "actions" $ do
H.a ! key ("delete") ! A.className "action-delete" #! onClick (pure $ OpenDeleteModal (show user.id)) $ do
H.span
! A.className "dashicons dashicons-trash"
! A.alt "Delete"
! A.title "Delete"
$ text ""
But the types will not unify:
268 H.tr ! key user.id ! A.className "user-item" $ do
^^^^
Could not match type
{ id :: String
| t0
}
with type
User
while checking that type User
is at least as general as type { id :: String
| t0
}
while checking that expression user
has type { id :: String
| t0
}
while checking type of property accessor user.id
in value declaration userRow
where t0 is an unknown type
I can't see what I am doing wrong. It seems to me that the user type has an "id" member which is a string, and therefore is at least as general as
type { id :: String
| t0
}
User is a newtype around a record, so it wraps the record up. In order to access the records fields you first need to unwrap it. The easiest way to do this in your situation is to pattern match on the user argument:
userRow :: User -> HTML Event
userRow (User user) =
H.tr ! key user.id ! A.className "user-item" $ do
...
Related
Consider the following code sample, which creates a new type to represent a customer model:
module Main where
import Effect (Effect)
import Effect.Console ( logShow )
import Prelude (Unit,(+),(/),(*),(-), (<>),discard)
newtype Customer
= Customer
{ firstname :: String
}
sample :: Customer
sample = Customer
{ firstname : "Average"
}
first :: Customer -> String
first a = _.firstname a
main = do
logShow ( first sample )
The expected output would be the value Average, which is equal to sample.name, but instead an error is produced:
Could not match type
{ firstname :: t0
| t1
}
with type
Customer
while checking that type Customer
is at least as general as type { firstname :: t0
| t1
}
while checking that expression a
has type { firstname :: t0
| t1
}
in value declaration first
where t0 is an unknown type
t1 is an unknown type
This is a good error, but doesn't explain how to actually access this value.
How do you access the value of an object created as a newType?
You have to do
first :: Customer -> String
first (Customer a) = _.firstname a
Since newtype is really, a new type.
One another way is to derive Newtype instance for that particular newtype, which exposes certain functions that will let you work on the data wrapped by the newtype.
derive instance newtypeCustomer :: Newtype Customer _
first :: Customer -> String
first = (_.firstname <<< unwrap)
I am back again trying to learn Haskell and, oh boy it is difficult!
I am a trying to do a simple mongoDB insertion inside a Scotty endpoint. Problem is the type return by the insert function is not accepted in the Scotty do statement. The program is quite simple:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
import Control.Monad.Trans(liftIO,lift,MonadIO)
import System.IO
import Data.Text.Lazy.Encoding (decodeUtf8)
import Data.Text.Lazy (pack,unpack)
import Data.Maybe
import Data.Time.Clock.POSIX
import Database.MongoDB (Action, Document, Document, Value, access,
allCollections,insert, close, connect, delete, exclude, find,
host,findOne, insertMany, master, project, rest,
select, liftDB, sort, Val, at, (=:))
main :: IO ()
main = scotty 3000 $ do
post "/logs" $ do
id <- liftIO $ getTimeInMillis
b <- body
let decodedBody = unpack(decodeUtf8 b)
i <- liftIO $ insertLog id decodedBody
text $ "Ok"
--setup database connection
run::MonadIO m => Action m a -> m a
run action = do
pipe <- liftIO(connect $ host "127.0.0.1")
access pipe master "data" action
getTimeInMillis ::Integral b => IO b
getTimeInMillis = round `fmap` getPOSIXTime
insertLog::MonadIO m => Int -> String -> Action m Value
insertLog id body = run $ insert "logs" ["id" =: id, "content" =: body]
the problem comes in the line
i <- liftIO $ insertLog id decodedBody
And the type error is
Expected type: Web.Scotty.Internal.Types.ActionT
Data.Text.Internal.Lazy.Text IO Value
Actual type: Action m0 Value
Any help or tip will be welcome!
I see a different error message with that code. Maybe you made some changes (like adding liftIO).
• Couldn't match type ‘Control.Monad.Trans.Reader.ReaderT
Database.MongoDB.Query.MongoContext m0 Value’
with ‘IO a0’
Expected type: IO a0
Actual type: Action m0 Value
In the line:
i <- liftIO $ insertLog id decodedBody
the liftIO function expects a genuine IO action, of type IO a for some a. However, the expression insertLog id decodedBody doesn't represent an IO action. It is Mongo action of type Action m Value for some m that has a MonadIO constraint. You need to use some function run Mongo Action values in IO. It looks like you've already written such a function, named run. It's written for a general MonadIO m but can be specialized to:
run :: Action IO a -> IO a
so if you first run your Mongo action (to turn it into IO) and then lift that action (to run it in the Scotty action under post), the following should type check:
i <- liftIO $ run $ insertLog id decodedBody
Update: Whoops! I missed the run in the insertLog function. You either want to write:
-- use "run" here
main = do
...
i <- liftIO $ run $ insertLog id decodedBody
-- but no "run" here
insertLog::MonadIO m => Int -> String -> Action m Value
insertLog id body = insert "logs" ["id" =: id, "content" =: body]
OR you want to write:
-- no "run" here
main = do
...
i <- liftIO $ insertLog id decodedBody
-- change the type signature and use "run" here
insertLog :: Int -> String -> IO Value
insertLog id body = run $ insert "logs" ["id" =: id, "content" =: body]
That will avoid the double-run problem.
The reason run didn't work as intended in your original code is a little complicated...
The problem is that run has flexibility to convert its Mongo action to many possible monads by returning m a for any m that supports MonadIO m. Because you gave insertLog a type signature with return type MonadIO m' => Action m' Value (where I changed the variable to keep m and m' distinct), the type checker matched the return type of run to the return type of insertLog:
m a ~ Action m' Value
by setting a ~ Value and m ~ Action m'. So, your run in insertLog was actually used with the following bizarre type:
run :: Action (Action m') Value -> Action m' Value
Normally, this would have caused a type error, but the type of insert is also flexible. Instead of returning an action of type Action IO Value, which would be the "usual" type, it happily adapted itself to return an action of type Action (Action IO) Value to match what run was expecting.
I tried to implement a show function for a custom type called Token. I have tried the following code:
type Token =
{ identifier :: String
, value :: String
}
instance showToken :: Show Token where
show t = "(Token " <> t.identifier <> ", " <> t.value <> ")"
I get the following error however:
Type class instances for type synonyms are disallowed.
The error provides a link, but it does not provide any helpful information. What does it mean and how can it be fixed?
Use newtype to give Token a distinct type from the record. Otherwise, you're simply defining a type alias.
import Prelude
newtype Token = Token
{ identifier :: String
, value :: String
}
instance showToken :: Show Token where
show (Token t) = "(Token " <> t.identifier <> ", " <> t.value <> ")"
Assume I have User records in my PureScript code with the following type:
{ id :: Number
, username :: String
, email :: Maybe String
, isActive :: Boolean
}
A CommonJS module is derived from the PureScript code. Exported User-related functions will be called from external JavaScript code.
In the JavaScript code, a "user" may be represented as:
var alice = {id: 123, username: 'alice', email: 'alice#example.com', isActive: true};
email may be null:
var alice = {id: 123, username: 'alice', email: null, isActive: true};
email may be omitted:
var alice = {id: 123, username: 'alice', isActive: true};
isActive may be omitted, in which case it is assumed true:
var alice = {id: 123, username: 'alice'};
id is unfortunately sometimes a numeric string:
var alice = {id: '123', username: 'alice'};
The five JavaScript representations above are equivalent and should produce equivalent PureScript records.
How do I go about writing a function which takes a JavaScript object and returns a User record? It would use the default value for a null/omitted optional field, coerce a string id to a number, and throw if a required field is missing or if a value is of the wrong type.
The two approaches I can see are to use the FFI in the PureScript module or to define the conversion function in the external JavaScript code. The latter seems hairy:
function convert(user) {
var rec = {};
if (user.email == null) {
rec.email = PS.Data_Maybe.Nothing.value;
} else if (typeof user.email == 'string') {
rec.email = PS.Data_Maybe.Just.create(user.email);
} else {
throw new TypeError('"email" must be a string or null');
}
// ...
}
I'm not sure how the FFI version would work. I haven't yet worked with effects.
I'm sorry that this question is not very clear. I don't yet have enough understanding to know exactly what it is that I want to know.
I've put together a solution. I'm sure much can be improved, such as changing the type of toUser to Json -> Either String User and preserving error information. Please leave a comment if you can see any ways this code could be improved. :)
This solution uses PureScript-Argonaut in addition to a few core modules.
module Main
( User()
, toEmail
, toId
, toIsActive
, toUser
, toUsername
) where
import Control.Alt ((<|>))
import Data.Argonaut ((.?), toObject)
import Data.Argonaut.Core (JNumber(), JObject(), Json())
import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..))
import Global (isNaN, readFloat)
type User = { id :: Number
, username :: String
, email :: Maybe String
, isActive :: Boolean
}
hush :: forall a b. Either a b -> Maybe b
hush = either (const Nothing) Just
toId :: JObject -> Maybe Number
toId obj = fromNumber <|> fromString
where
fromNumber = (hush $ obj .? "id")
fromString = (hush $ obj .? "id") >>= \s ->
let id = readFloat s in if isNaN id then Nothing else Just id
toUsername :: JObject -> Maybe String
toUsername obj = hush $ obj .? "username"
toEmail :: JObject -> Maybe String
toEmail obj = hush $ obj .? "email"
toIsActive :: JObject -> Maybe Boolean
toIsActive obj = (hush $ obj .? "isActive") <|> Just true
toUser :: Json -> Maybe User
toUser json = do
obj <- toObject json
id <- toId obj
username <- toUsername obj
isActive <- toIsActive obj
return { id: id
, username: username
, email: toEmail obj
, isActive: isActive
}
Update: I've made improvements to the code above based on a gist from Ben Kolera.
Have you had a look at purescript-foreign (https://github.com/purescript/purescript-foreign)? I think that's what you're looking for here.
As gb. wrote, that is exactly what the Foreign data type was built for. Off the top of my head:
convert :: Foreign -> F User
convert f = do
id <- f ! "id" >>= readNumber
name <- f ! "name" >>= readString
email <- (f ! "email" >>= readNull >>= traverse readString) <|> pure Nothing
isActive <- (f ! "isActive" >>= readBoolean) <|> pure true
return { id, name, email, isActive }
Just a little more ffi
module User where
import Data.Maybe
import Data.Function
foreign import data UserExternal :: *
type User =
{
id :: Number,
username :: String,
email :: Maybe String,
isActive :: Boolean
}
type MbUser =
{
id :: Maybe Number,
username :: Maybe String,
email :: Maybe String,
isActive :: Maybe Boolean
}
foreign import toMbUserImpl """
function toMbUserImpl(nothing, just, user) {
var result = {},
properties = ['username', 'email', 'isActive'];
var i, prop;
for (i = 0; i < properties.length; i++) {
prop = properties[i];
if (user.hasOwnProperty(prop)) {
result[prop] = just(user[prop]);
} else {
result[prop] = nothing;
}
}
if (!user.hasOwnProperty('id') || isNaN(parseInt(user.id))) {
result.id = nothing;
} else {
result.id = just(user.id);
}
return result;
}
""" :: forall a. Fn3 (Maybe a) (a -> Maybe a) UserExternal MbUser
toMbUser :: UserExternal -> MbUser
toMbUser ext = runFn3 toMbUserImpl Nothing Just ext
defaultId = 0
defaultName = "anonymous"
defaultActive = false
userFromMbUser :: MbUser -> User
userFromMbUser mbUser =
{
id: fromMaybe defaultId mbUser.id,
username: fromMaybe defaultName mbUser.username,
email: mbUser.email,
isActive: fromMaybe defaultActive mbUser.isActive
}
userFromExternal :: UserExternal -> User
userFromExternal ext = userFromMbUser $ toMbUser ext
I have a form in which the user can either select from a pre-existing list of values, or create a new (Text) value.
What will be the most elegant way to code that?
My best (and IMO not so elegant) way of doing so was by defining:
data MyInput = MyInput {createNew :: Bool, newVal :: Maybe Text, existingVal :: Maybe Text}
myForm :: [(Text,Text)] -> Html -> MForm MySite MySite (FormResult MyInput,Widget)
myForm exisingVals = renderTable $ MyInput
<$> areq boolField "Create new" (Just False)
<*> aopt textField "New val" Nothing
<*> aopt (selectField existingVals) "Existing values" Nothing
And once the form is received, pass the result through something like:
getMyValue :: MyInput -> Either ErrorMsg Text
getMyValue i = if createNew i
then if newVal i == Nothing || existingVal i /= Nothing
then Left "Missing new value or illegal input"
else Right . fromJust . newVal $ i
else if existingVal i == Nothing || newVal i /= Nothing
then Left "Missing selection or illegal input"
else Right . fromJust . existingVal $ i
And have the handler decide whether to re-send the form, or proceed according to the result.
Any better/shorter suggestions?
My real form has two such select/create fields, which makes the data structure and processing even more tedious.
Thanks,
You can factor out the common code, use pattern matching and guards, and generalize with a higher-order-function that accepts the accessor functions:
import Control.Arrow ((&&&))
getVal isNew newVal oldVal i | isNew i = checkVal "new value" $ (newVal &&& oldVal) i
| otherwise = checkVal "selection" $ (oldVal &&& newVal) i
where checkVal _ (Just val, Nothing) = Right val
checkVal name _ = Left $ "Missing " ++ name ++ " or illegal input"
getMyVal = getVal createNew newVal existingVal