Purescript - cannot unify type

I'm new to Purescript (as well as Haskell) and I'm stuck with not being able to combine the error. I originally had:

newtype Domain = Domain String

newtype Keyword = Keyword String

type Result = {
        domain    :: Domain,
        occurred   :: Boolean,
        position  :: Number,
        quality   :: Number
    }

is_min_pos :: Maybe Result -> Maybe Result -> Maybe Result
is_min_pos Nothing Nothing = Nothing
is_min_pos Nothing y = y
is_min_pos x Nothing = x
is_min_pos x y = if y.position < x.position then y else x     

      

This gave me an error

Cannot unify type
  Prim.Object
with type
  Data.Maybe.Maybe

      

I assumed it was because he expected x and y to be of type Maybe Record. So to be clear, I changed the code to to match the pattern by type.

data Result = Result {
        domain    :: Domain,
        occurred   :: Boolean,
        position  :: Number,
        quality   :: Number
    }

is_min_pos (Result x) (Result y) = if y.position < x.position then y else x

      

Now I am getting the error

Cannot unify type
  Data.Maybe.Maybe Processor.Result
with type
  Processor.Result

      

And this applies to this section

y.position < x.position -- in the first case

      

and in the second case

Result x -- on the pattern matching side

      


I work on this further

type Results = List Result

get_item_with_min_position :: Results -> Maybe Result
--get_item_with_min_position [] = Nothing
get_item_with_min_position results = foldl is_min_pos Nothing results

      

I am using "foldl" from Foldable. I'm not sure how the pattern matches the empty list. If I could, I would replace the type signature with

is_min_pos :: Maybe Result -> Result -> Maybe Result

      

Now I am getting the error

Cannot unify type
    Prim.Object
with type
    Data.Maybe.Maybe

      

This is understandable because in

foldl is_min_pos Nothing results

      

Results are of type List Result is_min_pos expects May be Result

What would be the cleanest way to solve this problem?

+3
purescript


source to share


1 answer


The type Maybe

has two data constructors Nothing

, which you match correctly, and Just

. If you want to match something of type Maybe a

that contains a value, you must match the constructor Just

.

You need to change the final version as follows:



is_min_pos (Just x) (Just y) = if y.position < x.position 
                                  then Just y 
                                  else Just x

      

Here Just x

has a type Maybe Result

that is correct according to the type signature, therefore x

has a type Result

, so you can use an accessory .position

to read its position

property.

+5


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics