Jonah
Jonah

Reputation: 16242

Default equality for record types?

I'm doing this exercise from the end of chapter 3 in The Purescript Book:

Write a function which looks up an Entry given a street address, by reusing the existing code in findEntry. Test your function in PSCi.

I tried:

findByAddress address = head <<< filter filterEntry
  where
    filterEntry :: Entry -> Boolean
    filterEntry entry = entry.address == address

which errors with:

  No type class instance was found for

    Data.Eq.Eq { street :: String
               , city :: String  
               , state :: String 
               }                 


while applying a function eq
  of type Eq t0 => t0 -> t0 -> Boolean
  to argument entry.address
while inferring the type of eq (entry.address)
in value declaration findByAddress

Question 1: I would think record types have a default equality function, namely that all of the record attributes are equal. Is there one? How do I use it?

Question 2: Just in case my assumption above in Question 1 was false, I tried:

eq :: Address -> Address -> Boolean 
eq address1 address2 = address1.street == address2.street &&
                       address1.city == address2.city &&
                       address1.state == address2.state

But this did not resolve the problem. Why not?

Upvotes: 2

Views: 163

Answers (1)

Gabriel Riba
Gabriel Riba

Reputation: 6738

bower install purescript-record-extra

pulp repl
> import Data.Record.Extra (eqRecord)

> {a:1, b:2.5} `eqRecord` {a:1, b:2.5}
true

> {a:1, b:2.5} `eqRecord` {a:1, b:3.5}
false

-- different record types cannot be compared

> {a:1, b:2.5} `eqRecord` {a:1, b:3.5, c:"abc"}
Error found:
in module $PSCI
at  line 1, column 25 - line 1, column 45

  Type of expression contains additional label c.

while checking that expression { a: 1    
                               , b: 3.5  
                               , c: "abc"
                               }         
  has type { a :: Int   
           , b :: Number
           }            
while applying a function ((eqRecord (#dict RowToList t2 t1)) (#dict EqRecord t1 t2)) { a: 1  
                                                                                      , b: 2.5
                                                                                      }       
  of type { | t0 } -> Boolean
  to argument { a: 1    
              , b: 3.5  
              , c: "abc"
              }         
in value declaration it

where t0 is an unknown type

Update pkg purescript-record has an equality op.

> import Data.Record (equal)
> {a:1, b:2.5} `equal` {a:1, b:2.5}
true

> {a:1, b:2.5} `equal` {a:1, b:3.5}
false

> {a:1, b:2.5} `equal` {a:1, b:2.5, c:"abc"}
Error found:
in module $PSCI
at  line 1, column 22 - line 1, column 42

  Type of expression contains additional label c.

while checking that expression { a: 1    
                               , b: 2.5  
                               , c: "abc"
                               }         
  has type { a :: Int   
           , b :: Number
           }

Using newtypes and derived instances:

> newtype Address = Address {street::String, city::String}

> derive instance eqAddress :: Eq Address

> Address {street:"la Rambla", city:"Barcelona"} == Address {street:"main", city:"LA"}
false

Upvotes: 4

Related Questions