camfort / fortran-src

Fortran parsing and static analysis infrastructure
https://hackage.haskell.org/package/fortran-src
Other
44 stars 20 forks source link

Add Ord instances #282

Open Boarders opened 2 weeks ago

Boarders commented 2 weeks ago

It would be helpful to add derived Ord instances to almost all types so they can be used in standard containers such as Data.Set. I'd be happy to do this if the idea seems palatable.

Boarders commented 2 weeks ago

Note: I just tried using StandAloneDeriving to do this in order to replace nub with nubOrd and it involves the following deriving clauses:

deriving instance Ord Intent
deriving instance (Ord a) => Ord (Suffix a)
deriving instance (Ord a) => Ord (Declarator a)
deriving instance (Ord a) => Ord (StructureItem a)
deriving instance (Ord a) => Ord (UnionMap a)
deriving instance (Ord a) => Ord (DataGroup a)
deriving instance (Ord a) => Ord (Namelist a)
deriving instance (Ord a) => Ord (CommonGroup a)
deriving instance (Ord a) => Ord (FormatItem a)
deriving instance (Ord a) => Ord (ImpList a)
deriving instance (Ord a) => Ord (ImpElement a)
deriving instance (Ord a) => Ord (Block a)
deriving instance (Ord a) => Ord (Comment a)
deriving instance (Ord a) => Ord (ForallHeader a)
deriving instance (Ord a) => Ord (ForallHeaderPart a)
deriving instance (Ord a) => Ord (Prefix a)
deriving instance (Ord a) => Ord (ControlPair a)
deriving instance (Ord a) => Ord (FlushSpec a)
deriving instance (Ord a) => Ord (AllocOpt a)
deriving instance (Ord a) => Ord (Use a)
deriving instance (Ord a) => Ord (ProcInterface a)
deriving instance (Ord a) => Ord (ProcDecl a)
deriving instance Ord (ModuleNature)
deriving instance Ord Only
deriving instance (Ord a) => Ord (ProgramUnit a)
deriving instance (Ord a) => Ord (DimensionDeclarator a)
deriving instance (Ord a) => Ord (Attribute a)
deriving instance (Ord a) => Ord (Selector a)
deriving instance (Ord a) => Ord (TypeSpec a)
deriving instance (Ord a) => Ord (Statement a)
deriving instance (Ord a) => Ord (DoSpecification a)
deriving instance (Ord a) => Ord (ArgumentExpression a)
deriving instance (Ord a) => Ord (Argument a)
deriving instance (Ord a) => Ord (ComplexPart a)
deriving instance (Ord a) => Ord (ComplexLit a)
deriving instance (Ord a) => Ord (KindParam a)
deriving instance (Ord a) => Ord (Value a)
deriving instance (Eq a , Ord a, Ord  (t a)) => Ord (AList t a)
deriving instance (Eq a , Ord a, Ord  (t a)) => Ord (ATuple t t a)
deriving instance (Ord a) =>  Ord (Index a)
deriving instance (Ord a) => Ord  (Expression a)
deriving instance (Ord a) => Ord (DeclaratorType a)

I think it makes more sense for this to all be in the library

raehik commented 2 weeks ago

Thanks @Boarders . Sensible and adds a bit of convenience, I see no major reason not to. (Compilation time is a concern, but it doesn't seem to have an outrageous impact.) We should note that AST Ord instances don't guarantee any specific behaviour, other than being a valid instance. I've pushed a PR over at #283 .

Boarders commented 1 week ago

Thanks, that is very helpful! I'm not sure if this would make more sense as a request after the modules have been split up slightly (if compilation times are the worry), but it may make sense at some point to also add Hashable and NFData (though I would say those are less important)