Comparing StableNames of different type

Manuel M T Chakravarty chak at cse.unsw.edu.au
Tue Aug 28 09:08:06 CEST 2012


Emil Axelsson <emax at chalmers.se>:
> 2012-08-26 08:03, Manuel M T Chakravarty skrev:
>> Emil Axelsson <emax at chalmers.se>:
>>> 2012-08-24 11:08, Simon Marlow skrev:
>>>> On 24/08/2012 07:39, Emil Axelsson wrote:
>>>>> Hi!
>>>>> 
>>>>> Are there any dangers in comparing two StableNames of different type?
>>>>> 
>>>>>   stEq :: StableName a -> StableName b -> Bool
>>>>>   stEq a b = a == (unsafeCoerce b)
>>>>> 
>>>>> I could guard the coercion by first comparing the type representations,
>>>>> but that would give me a `Typeable` constraint that would spread
>>>>> throughout the code.
>>>> It should be safe even if the types are
>>>> different, but I presume you expect the types to be the same, since
>>>> otherwise the comparison would be guaranteed to return False, right?
>>> 
>>> No, I want to do observable sharing of heterogeneously typed expressions which means I will be comparing expressions of different type.
>> 
>> You may like to have a look at how we have done this in the Accelerate EDSL:
>> 
>>   https://github.com/AccelerateHS/accelerate/blob/master/Data/Array/Accelerate/Smart.hs
>> 
>> In particular, the Eq instance for stable names of AST nodes is at
>> 
>>   https://github.com/AccelerateHS/accelerate/blob/master/Data/Array/Accelerate/Smart.hs#L763
> 
> Hm, doesn't this impose a `Typeable` constraint on all AST nodes (which is what I'm trying to avoid)?

Yes, you need Typeable, but arguably that is cleaner than unsafeCoerce.

In any case, as I am not aware of any other system than Accelerate that implements observable sharing on typed ASTs, you may find the Accelerate code to be a helpful source of inspiration.

Manuel




More information about the Glasgow-haskell-users mailing list