Open aleeusgr opened 1 week ago
KISS; use grep
instead
To list all type classes in a Haskell module, you can utilize **Template Haskell**, which allows for metaprogramming in Haskell. This approach enables you to inspect the types and instances defined within a module programmatically. Here’s how you can do it: ## Using Template Haskell 1. `git grep -e "TemplateHaskell" -- **/*.hs` 2. plutus-tx/src/PlutusTx/TH.hs 4. **Define a Function to List Type Classes**: You can define a function that uses `reify` to inspect the types and their associated type classes. Here’s a simple example: ```haskell listTypeClasses :: Name -> Q [Name] listTypeClasses className = do info <- reify className case info of TyConI (ClassD _ _ _ _ _) -> return [className] _ -> return [] ``` 5. **Execute in GHCi**: After defining this function, you can load your module in GHCi and call `listTypeClasses` with the name of your type class to retrieve its information. 6. **Show Instances**: If you want to show instances of a specific type class, you can use GHCi commands like `:info ClassName`, which will display all instances currently in scope for that type class. ## Example Usage Here's a complete example that demonstrates how to implement this: ```haskell {-# LANGUAGE TemplateHaskell #-} import Language.Haskell.TH -- Function to list type classes listTypeClasses :: Name -> Q [Name] listTypeClasses className = do info <- reify className case info of TyConI (ClassD _ _ _ _ _) -> return [className] _ -> return [] -- Example invocation in GHCi: -- > :set -XTemplateHaskell -- > $(listTypeClasses ''Eq) ``` ### Notes - **Scope Awareness**: The compiler only knows about type classes that are in scope at any given time. Therefore, ensure that you have imported any necessary modules before running these commands. - **Limitations**: While this method works well for user-defined type classes, it may not capture all instances for built-in classes due to how GHC handles instance declarations. This approach provides a powerful way to introspect type classes and their instances within your Haskell modules using Template Haskell's capabilities[1][2]. Citations: [1] https://stackoverflow.com/questions/5396783/get-a-list-of-the-instances-in-a-type-class-in-haskell [2] https://book.realworldhaskell.org/read/using-typeclasses.html [3] https://www.haskell.org/tutorial/classes.html [4] https://www.haskell.org/tutorial/modules.html [5] https://learnyouahaskell.com/modules [6] https://learnyouahaskell.com/making-our-own-types-and-typeclasses [7] https://www.haskell.org/onlinereport/haskell2010/haskellch5.html [8] https://discourse.haskell.org/t/advice-about-use-of-typeclasses-in-my-project/3055
$ git grep -e "^class .* where" -- **/*.hs
```haskell src/PlutusTx/Applicative.hs:class Functor f => Applicative f where src/PlutusTx/Blueprint/Class.hs:class HasBlueprintSchema (t :: Type) (referencedTypes :: [Type]) where src/PlutusTx/Blueprint/Definition/Derive.hs:class DefinitionsFor' referencedTypes acc where src/PlutusTx/Blueprint/Definition/Unroll.hs:class HasBlueprintDefinition (t :: Type) where src/PlutusTx/Builtins/HasBuiltin.hs:class PLC.DefaultUni `PLC.Contains` a => HasToBuiltin a where src/PlutusTx/Builtins/HasBuiltin.hs:class HasToBuiltin (FromBuiltin arep) => HasFromBuiltin arep where src/PlutusTx/Builtins/HasOpaque.hs:class HasToOpaque a arep | a -> arep where src/PlutusTx/Builtins/HasOpaque.hs:class HasFromOpaque arep a | arep -> a where src/PlutusTx/Builtins/HasOpaque.hs:class MkNil arep where src/PlutusTx/Enum.hs:class Enum a where src/PlutusTx/Eq.hs:class Eq a where src/PlutusTx/Foldable.hs:class Foldable t where src/PlutusTx/Functor.hs:class Functor f where src/PlutusTx/IsData/Class.hs:class ToData (a :: Type) where src/PlutusTx/IsData/Class.hs:class FromData (a :: Type) where src/PlutusTx/IsData/Class.hs:class UnsafeFromData (a :: Type) where src/PlutusTx/Lattice.hs:class JoinSemiLattice a where src/PlutusTx/Lattice.hs:class MeetSemiLattice a where src/PlutusTx/Lattice.hs:class JoinSemiLattice a => BoundedJoinSemiLattice a where src/PlutusTx/Lattice.hs:class MeetSemiLattice a => BoundedMeetSemiLattice a where src/PlutusTx/Lift/Class.hs:class Typeable uni (a :: k) where src/PlutusTx/Lift/Class.hs:class Lift uni a where src/PlutusTx/Monoid.hs:class Semigroup a => Monoid a where src/PlutusTx/Monoid.hs:class Monoid a => Group a where src/PlutusTx/Numeric.hs:class AdditiveSemigroup a where src/PlutusTx/Numeric.hs:class AdditiveSemigroup a => AdditiveMonoid a where src/PlutusTx/Numeric.hs:class AdditiveMonoid a => AdditiveGroup a where src/PlutusTx/Numeric.hs:class MultiplicativeSemigroup a where src/PlutusTx/Numeric.hs:class MultiplicativeSemigroup a => MultiplicativeMonoid a where src/PlutusTx/Numeric.hs:class (Ring s, AdditiveGroup v) => Module s v | v -> s where src/PlutusTx/Ord.hs:class Eq a => Ord a where src/PlutusTx/Semigroup.hs:class Semigroup a where src/PlutusTx/Show/TH.hs:class Show a where src/PlutusTx/Traversable.hs:class (Functor t, Foldable t) => Traversable t where ```
Thanks for the list of the classes! I'll review it myself, that won't block the PR.
addresses #6270
PlutusTx.Ord
git grep -e "^class .* where" -- **/*.hs
Background
Ord
is a type class that defines a total ordering for types.A pragma is a special instruction or directive that provides additional information to the compiler, allowing for optimizations or modifications in how the code is processed. Pragmas are denoted by the syntax
{-# ... #-}
, and they do not generally affect the semantic meaning of the program, but they can influence performance and behavior during compilation.the
MINIMAL
pragma is used to specify the minimal complete definition of a type class, indicating which methods must be implemented for an instance of that class. This is particularly useful for classes that have methods with circular defaults, ensuring that instances adhere to a defined interface.https://downloads.haskell.org/~ghc/7.8.1/docs/html/users_guide/pragmas.html
base
Checklist
Pre-submit checklist: