Copyright | (c) 2020 Galois Inc. |
---|---|

License | BSD3 |

Maintainer | cryptol@galois.com |

Stability | provisional |

Portability | portable |

Safe Haskell | Safe-Inferred |

Language | Haskell2010 |

This module implements an "order insensitive" datastructure for record types and values. For most purposes, we want to deal with record fields in a canonical order; but for user interaction purposes, we generally want to display the fields in the order they were specified by the user (in source files, at the REPL, etc.).

## Synopsis

- data RecordMap a b
- displayOrder :: RecordMap a b -> [a]
- canonicalFields :: RecordMap a b -> [(a, b)]
- displayFields :: (Show a, Ord a) => RecordMap a b -> [(a, b)]
- recordElements :: RecordMap a b -> [b]
- fieldSet :: Ord a => RecordMap a b -> Set a
- recordFromFields :: (Show a, Ord a) => [(a, b)] -> RecordMap a b
- recordFromFieldsErr :: (Show a, Ord a) => [(a, b)] -> Either (a, b) (RecordMap a b)
- recordFromFieldsWithDisplay :: (Show a, Ord a) => [a] -> [(a, b)] -> RecordMap a b
- lookupField :: Ord a => a -> RecordMap a b -> Maybe b
- adjustField :: forall a b. Ord a => a -> (b -> b) -> RecordMap a b -> Maybe (RecordMap a b)
- traverseRecordMap :: Applicative t => (a -> b -> t c) -> RecordMap a b -> t (RecordMap a c)
- mapWithFieldName :: (a -> b -> c) -> RecordMap a b -> RecordMap a c
- zipRecordsM :: forall t a b c d. (Ord a, Monad t) => (a -> b -> c -> t d) -> RecordMap a b -> RecordMap a c -> t (Either (Either a a) (RecordMap a d))
- zipRecords :: forall a b c d. Ord a => (a -> b -> c -> d) -> RecordMap a b -> RecordMap a c -> Either (Either a a) (RecordMap a d)
- recordMapAccum :: (a -> b -> (a, c)) -> a -> RecordMap k b -> (a, RecordMap k c)

# Documentation

An "order insensitive" datastructure. The fields can be accessed either according to a "canonical" order, or based on a "display" order, which matches the order in which the fields were originally specified.

#### Instances

Functor (RecordMap a) Source # | |

Foldable (RecordMap a) Source # | |

Defined in Cryptol.Utils.RecordMap fold :: Monoid m => RecordMap a m -> m # foldMap :: Monoid m => (a0 -> m) -> RecordMap a a0 -> m # foldMap' :: Monoid m => (a0 -> m) -> RecordMap a a0 -> m # foldr :: (a0 -> b -> b) -> b -> RecordMap a a0 -> b # foldr' :: (a0 -> b -> b) -> b -> RecordMap a a0 -> b # foldl :: (b -> a0 -> b) -> b -> RecordMap a a0 -> b # foldl' :: (b -> a0 -> b) -> b -> RecordMap a a0 -> b # foldr1 :: (a0 -> a0 -> a0) -> RecordMap a a0 -> a0 # foldl1 :: (a0 -> a0 -> a0) -> RecordMap a a0 -> a0 # toList :: RecordMap a a0 -> [a0] # null :: RecordMap a a0 -> Bool # length :: RecordMap a a0 -> Int # elem :: Eq a0 => a0 -> RecordMap a a0 -> Bool # maximum :: Ord a0 => RecordMap a a0 -> a0 # minimum :: Ord a0 => RecordMap a a0 -> a0 # | |

Traversable (RecordMap a) Source # | |

Defined in Cryptol.Utils.RecordMap | |

(Ord a, Eq b) => Eq (RecordMap a b) Source # | |

(Ord a, Ord b) => Ord (RecordMap a b) Source # | |

Defined in Cryptol.Utils.RecordMap compare :: RecordMap a b -> RecordMap a b -> Ordering # (<) :: RecordMap a b -> RecordMap a b -> Bool # (<=) :: RecordMap a b -> RecordMap a b -> Bool # (>) :: RecordMap a b -> RecordMap a b -> Bool # (>=) :: RecordMap a b -> RecordMap a b -> Bool # | |

(Show a, Ord a, Show b) => Show (RecordMap a b) Source # | |

(NFData a, NFData b) => NFData (RecordMap a b) Source # | |

Defined in Cryptol.Utils.RecordMap |

displayOrder :: RecordMap a b -> [a] Source #

The order in which the fields originally appeared.

canonicalFields :: RecordMap a b -> [(a, b)] Source #

Return a list of field/value pairs in canonical order.

displayFields :: (Show a, Ord a) => RecordMap a b -> [(a, b)] Source #

Return a list of field/value pairs in display order.

recordElements :: RecordMap a b -> [b] Source #

Retrieve the elements of the record in canonical order of the field names

recordFromFields :: (Show a, Ord a) => [(a, b)] -> RecordMap a b Source #

Generate a record from a list of field/value pairs. Precondition: each field identifier appears at most once in the given list.

recordFromFieldsErr :: (Show a, Ord a) => [(a, b)] -> Either (a, b) (RecordMap a b) Source #

Generate a record from a list of field/value pairs. If any field name is repeated, the first repeated name/value pair is returned. Otherwise the constructed record is returned.

recordFromFieldsWithDisplay :: (Show a, Ord a) => [a] -> [(a, b)] -> RecordMap a b Source #

Generate a record from a list of field/value pairs, and also provide the "display" order for the fields directly. Precondition: each field identifier appears at most once in each list, and a field name appears in the display order iff it appears in the field list.

adjustField :: forall a b. Ord a => a -> (b -> b) -> RecordMap a b -> Maybe (RecordMap a b) Source #

Update the value of a field by applying the given function. If the field is not present in the record, return Nothing.

traverseRecordMap :: Applicative t => (a -> b -> t c) -> RecordMap a b -> t (RecordMap a c) Source #

Traverse the elements of the given record map in canonical order, applying the given action.

mapWithFieldName :: (a -> b -> c) -> RecordMap a b -> RecordMap a c Source #

Apply the given function to each element of a record.

zipRecordsM :: forall t a b c d. (Ord a, Monad t) => (a -> b -> c -> t d) -> RecordMap a b -> RecordMap a c -> t (Either (Either a a) (RecordMap a d)) Source #

Zip together the fields of two records using the provided action.
If some field is present in one record, but not the other,
an `Either a a`

will be returned, indicating which record is missing
the field, and returning the name of the missing field.

The `displayOrder`

of the resulting record will be taken from the first
argument (rather arbitrarily).

zipRecords :: forall a b c d. Ord a => (a -> b -> c -> d) -> RecordMap a b -> RecordMap a c -> Either (Either a a) (RecordMap a d) Source #

Pure version of `zipRecordsM`

recordMapAccum :: (a -> b -> (a, c)) -> a -> RecordMap k b -> (a, RecordMap k c) Source #

The function recordMapAccum threads an accumulating argument through the map in canonical order of fields.