| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
H2JVM.Data.TypeMergingList
Contents
Description
A Snoc List type that merges elements of the same constructor. We use this for managing class attributes, where adding a new attribute of the same type should merge with the existing one rather than just being added to the list. For example, suppose we have some data type:
data Entry = IntEntry Int | StringEntry String deriving (Eq, Data, Show) instance Semigroup Entry where IntEntry a <> IntEntry b = IntEntry (a + b) StringEntry a <> StringEntry b = StringEntry (a ++ b) _ <> b = b -- if the constructors don't match, just take the right one
Then we can do:
snoc (TypeMergingList []) (IntEntry 1) = TypeMergingList [IntEntry 1] snoc (TypeMergingList [IntEntry 1]) (IntEntry 2) = TypeMergingList [IntEntry 3] snoc (TypeMergingList [IntEntry 1]) (StringEntry "hello") = TypeMergingList [IntEntry 1, StringEntry "hello"] snoc (TypeMergingList [IntEntry 1, StringEntry "hello"]) (StringEntry "world") = TypeMergingList [IntEntry 1, StringEntry "helloworld"]
Synopsis
- newtype TypeMergingList a = TypeMergingList [a]
- class Data a => DataMergeable a where
- merge :: a -> a -> a
- errorDifferentConstructors :: (Data a, HasCallStack) => a -> a -> b
- snoc :: DataMergeable a => TypeMergingList a -> a -> TypeMergingList a
- toList :: TypeMergingList a -> [a]
- toVector :: TypeMergingList a -> Vector a
- fromList :: (DataMergeable a, Data a) => [a] -> TypeMergingList a
Data types
newtype TypeMergingList a Source #
A list type that merges elements of the same constructor using the merge function from the DataMergeable class.
Constructors
| TypeMergingList [a] |
Instances
class Data a => DataMergeable a where Source #
Class of partially mergeable types.
Instances of this class may assume that the constructors of the two arguments are the same (i.e. ), and
are permitted to be partial if this is not the case. The helper function toConstr x == toConstr yerrorDifferentConstructors is provided for conveniently throwing an error in this case.
Methods
Merge two values of the same constructor. This function is partial if the constructors of the two arguments are different.
Instances
| DataMergeable ClassFileAttribute Source # | |
Defined in H2JVM.ClassFile Methods merge :: ClassFileAttribute -> ClassFileAttribute -> ClassFileAttribute Source # | |
| DataMergeable CodeAttribute Source # | |
Defined in H2JVM.ClassFile.Method Methods merge :: CodeAttribute -> CodeAttribute -> CodeAttribute Source # | |
| DataMergeable CodeAttributeData Source # | |
Defined in H2JVM.ClassFile.Method Methods merge :: CodeAttributeData -> CodeAttributeData -> CodeAttributeData Source # | |
| DataMergeable MethodAttribute Source # | |
Defined in H2JVM.ClassFile.Method Methods merge :: MethodAttribute -> MethodAttribute -> MethodAttribute Source # | |
| (Data a, Semigroup a) => DataMergeable a Source # | |
Defined in H2JVM.Data.TypeMergingList | |
errorDifferentConstructors :: (Data a, HasCallStack) => a -> a -> b Source #
Convenience function for writing invalid merge errors.
Operations
snoc :: DataMergeable a => TypeMergingList a -> a -> TypeMergingList a Source #
Append an element to the end of a TypeMergingList, merging it with the last element if they have the same constructor. \(O(1)\)
>>>data Entry = IntEntry Int | StringEntry String deriving (Eq, Data, Show)>>>instance DataMergeable Entry where { IntEntry a `merge` IntEntry b = IntEntry (a + b); StringEntry a `merge` StringEntry b = StringEntry (a ++ b); _ `merge` b = b}>>>snoc (TypeMergingList [IntEntry 1]) (IntEntry 2)TypeMergingList [IntEntry 3]>>>snoc (TypeMergingList [IntEntry 1]) (StringEntry "hello")TypeMergingList [StringEntry "hello",IntEntry 1]>>>snoc (TypeMergingList [IntEntry 1, StringEntry "hello"]) (StringEntry "world")TypeMergingList [StringEntry "world",IntEntry 1,StringEntry "hello"]
Conversions
toList :: TypeMergingList a -> [a] Source #
Convert a TypeMergingList to a regular list. \(O(N)\)
toVector :: TypeMergingList a -> Vector a Source #
Convert a TypeMergingList to a Vector. \(O(N)\)
fromList :: (DataMergeable a, Data a) => [a] -> TypeMergingList a Source #
Create a TypeMergingList from a regular list. \(O(N)\)