Haskell function to check if two values are identical -
in haskell have function (==) :: eq => a->a->bool
fine large number of datatypes instance of eq
can defined. there types there no reasonable way define instance of eq
. 1 example simple function type (a->b)
i looking function tell me if 2 values same -- not equal identical.
for example f(id,id)
==> true
f((+),(-))
= false
clarification:
i don't want know if 2 function same thing. not possible in general case so. want know if i've gotten same thing started with. let me give stripped down example:
data foo = foo (foo->foo) --contains function no eq instance x :: foo x = foo id -- or matter foo undefined y :: foo y = foo (const x) :: foo = let (foo fy) = y in fy x
it clear inspection once evaluated, a
x
. let's assume don't know function in y want test if foo put in same 1 got - fy x
give me x
. how do this?
one way wasn't mentioned in pointer equality in haskell? reallyunsafeptrequality#
. name suggests can unpredictable , should't used can interesting see how ghc works. here's how can use in ghci:
> :set -package ghc-prim > import ghc.prim > import ghc.types > istrue# (reallyunsafeptrequality# id id) true > let x = x + 2 :: int > istrue# (reallyunsafeptrequality# a) true > let b x = x + 2 :: int > istrue# (reallyunsafeptrequality# b) false
if function isn't monomorphic doesn't work:
> let c x = x + 2 > istrue# (reallyunsafeptrequality# c c) false
some more examples:
> let d = c in istrue# (reallyunsafeptrequality# d d) false > :set -xmonomorphismrestriction > let d = c in istrue# (reallyunsafeptrequality# d d) true
you can compare polymorphic types if wrap them in newtype:
> :set -xrankntypes > newtype x = x (forall a. num => -> a) > let d = x c > istrue# (reallyunsafeptrequality# d d) true
applying makes them not equal
> istrue# (reallyunsafeptrequality# (id ()) (id ())) false
but when compiling optimisations true
.
hopefully enough convince want bad idea. 1 of solutions in pointer equality in haskell? better.
Comments
Post a Comment