[Haskell-beginners] Simple function comparison

Stephen Renehan d11124067 at mydit.ie
Wed Nov 25 14:32:07 UTC 2015


Many thanks Franceso, I had thought as much as I was dealing with them
only abstractly. Appreciate the quick reply and thanks for the help.

On 25 November 2015 at 14:20, Francesco Ariis <fa-ml at ariis.it> wrote:
> On Wed, Nov 25, 2015 at 11:39:15AM +0000, Stephen Renehan wrote:
>> Hi,
>>
>> I'm looking to do a comparison between 2 simple functions to see if
>> they are equivalent but I appear to be running into some problems if
>> anyone can help.
>>
>> The two functions I want to compare are f (g a) and g (f a).
>>
>> I have f defined and g defined as
>>  f :: a -> a
>>  f = undefined
>>
>> g :: a -> a.
>> g =  undefined
>>
>> The compiler was complaining about the type signature lacking a
>> binding so added undefined to get around the error. I understand that
>> I must be missing something very basic as they look incomparable in
>> this form. Any suggestions of what changes I should make such a
>> comparison practical?
>
> Hello Stephen,
>     I don't think there is a way to /prove/ f (g a) == g (f a) if their
> domain is not finite inside Haskell (you could do it with pen and
> paper).
>
> If you have two functions and a finite domain you can do, say:
>
>     -- function function domain
>     compfun :: (Eq a) => (a -> a) -> (a -> a) -> [a] -> Bool
>     compfun f g d = and $ map (\x -> (f . g) x == (g . f) x) d
>
>     λ> compfun (+1) (+17) [1..10]
>     True
>     λ> compfun (*5) (+17) [1..10]
>     False
>
> Of course this is a toy example, use a proper library (quickcheck, etc.)
> if you are writing a real program.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-- 


This email originated from DIT. If you received this email in error, please 
delete it from your system. Please note that if you are not the named 
addressee, disclosing, copying, distributing or taking any action based on 
the contents of this email or attachments is prohibited. www.dit.ie

Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí 
earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an 
seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon 
dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa 
ríomhphost nó sna hiatáin seo. www.dit.ie

Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to 
Grangegorman <http://www.dit.ie/grangegorman>


More information about the Beginners mailing list