| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.PreNatMap
Contents
Synopsis
- data PreNatMap (f :: Type -> Type) (g :: Type -> Type)
- data PreEntry (f :: Type -> Type) (g :: Type -> Type) = PreEntry (f Int) (g Int)
- empty :: forall (f :: Type -> Type) (g :: Type -> Type). PreNatMap f g
- isFull :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Foldable f, Functor g) => Shape f -> PreNatMap f g -> Bool
- fullMatch :: (Ord (f Ignored), Foldable f, Functor g) => f a -> PreNatMap f g -> Maybe (g a)
- match :: (Eq a, Ord (f Ignored), Foldable f, Functor g) => f a -> PreNatMap f g -> Maybe (g a)
- lookup :: (Semigroup a, Ord (f Ignored), Foldable f, Functor g) => f a -> PreNatMap f g -> Maybe (g a)
- lookupWith :: (Semigroup b, Ord (f Ignored), Foldable f, Functor g) => (a -> b) -> f a -> PreNatMap f g -> Maybe (g b)
- lookupShape :: forall (f :: Type -> Type) (g :: Type -> Type). Ord (f Ignored) => Shape f -> PreNatMap f g -> Maybe (Shape g)
- refine :: (Ord a, Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => f a -> g a -> PreNatMap f g -> Maybe (PreNatMap f g)
- refineShape :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => Shape f -> Shape g -> PreNatMap f g -> Maybe (PreNatMap f g)
- toEntries :: forall (f :: Type -> Type) (g :: Type -> Type). (Traversable f, Functor g) => PreNatMap f g -> [PreEntry f g]
- fromEntries :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => [PreEntry f g] -> Maybe (PreNatMap f g)
- make :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => [PreEntry f g] -> PreNatMap f g
- toNatMap :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Traversable f, Functor g) => PreNatMap f g -> NatMap f g
- fromNatMap :: forall (f :: Type -> Type) (g :: Type -> Type). (Traversable f, Traversable g) => NatMap f g -> PreNatMap f g
- toShapeMap :: forall (f :: Type -> Type) (g :: Type -> Type). PreNatMap f g -> Map (Shape f) (Shape g)
- fromShapeMap :: forall (f :: Type -> Type) (g :: Type -> Type). (Foldable f, Traversable g) => Map (Shape f) (Shape g) -> Maybe (PreNatMap f g)
Documentation
data PreNatMap (f :: Type -> Type) (g :: Type -> Type) Source #
PreNatMap f g represents a partially known
natural transformation f ~> g.
Examples
reverse is a natural transformation from list to list.
reverse :: [a] -> [a]
Using refine function, you can accumulate knowledge about how
reverse behaves to PreNatMap [] [] through examples.
>>>p1 = refine "foo" "oof" empty>>>p1Just (make [PreEntry [0,1,1] [1,1,0]])
You can add another example to strengthen the knowledge.
(The monadic bind is used here because >>=refine
returns the result wrapped in Maybe.)
>>>p2 = p1 >>= refine "dad" "dad">>>p2Just (make [PreEntry [0,1,2] [2,1,0]])
>>>p3 = p2 >>= refine "aabb" "bbaa">>>p3Just (make [PreEntry [0,1,2] [2,1,0],PreEntry [0,0,1,1] [1,1,0,0]])
If you add an example contradicting to existing examples,
refine can fail with Nothing.
>>>p3 >>= refine "cccd" "cddd"Nothing
You can also query against the learned knowledge.
match function takes an input and a PreNatMap, and
if the learned knowledge determine the output uniquely,
returns that output.
>>>p3 >>= match "ABC"Just "CBA">>>p3 >>= match "XXYY"Just "YYXX"
If the output is not unique, match can fail,
even when the "shape" of the output is known.
In the following example, it is known that the output for a 4-lengthed list
is a 4-lengthed list again, but match fails because the differences
in the list elements make the output ambiguous.
>>>p3 >>= match "XYZW"Nothing
There is another query lookup which always return
something for "at least the shape is known" case,
by combining ambiguous inputs using Semigroup operation.
>>>p3 >>= lookup ["X", "Y", "Z", "W"]Just ["WZ","WZ","YX","YX"]
Instances
| (Show (f Int), Show (g Int), Traversable f, Functor g) => Show (PreNatMap f g) Source # | |
| (Eq (f Ignored), Eq (g Int)) => Eq (PreNatMap f g) Source # | |
| (Ord (f Ignored), Ord (g Int)) => Ord (PreNatMap f g) Source # | |
Defined in Data.PreNatMap Methods compare :: PreNatMap f g -> PreNatMap f g -> Ordering # (<) :: PreNatMap f g -> PreNatMap f g -> Bool # (<=) :: PreNatMap f g -> PreNatMap f g -> Bool # (>) :: PreNatMap f g -> PreNatMap f g -> Bool # (>=) :: PreNatMap f g -> PreNatMap f g -> Bool # | |
data PreEntry (f :: Type -> Type) (g :: Type -> Type) Source #
Pair of f and g values representing a part of
the learned natural transformation in PreNatMap.
Instances
| (Show (f Int), Show (g Int)) => Show (PreEntry f g) Source # | |
| (Eq (f Int), Eq (g Int)) => Eq (PreEntry f g) Source # | |
| (Ord (f Int), Ord (g Int)) => Ord (PreEntry f g) Source # | |
Defined in Data.PreNatMap | |
initialize
empty :: forall (f :: Type -> Type) (g :: Type -> Type). PreNatMap f g Source #
Empty PreNatMap with no knowledge.
query
isFull :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Foldable f, Functor g) => Shape f -> PreNatMap f g -> Bool Source #
fullMatch :: (Ord (f Ignored), Foldable f, Functor g) => f a -> PreNatMap f g -> Maybe (g a) Source #
Query the output of natural transformation for given input fa :: f a.
Succeeds only when the PreNatMap had full knowledge for the inputs
with same shape to fa.
Compared to match, fullMatch does not require Eq a constraint
for the type of contents, by giving up cases which requires checks for
equality comparison between contents of the input fa.
match :: (Eq a, Ord (f Ignored), Foldable f, Functor g) => f a -> PreNatMap f g -> Maybe (g a) Source #
Query the output of natural transformation for given input. Succeeds only when the output is uniquely determined.
lookup :: (Semigroup a, Ord (f Ignored), Foldable f, Functor g) => f a -> PreNatMap f g -> Maybe (g a) Source #
Query the output of natural transformation for given input fa :: f a.
Succeeds if the shape of the output corresponding to the input
is known.
lookup succeeds even if the output is not determined uniquely.
If there are multiple contents of inputs for a content of the output,
lookup merges all of the candidate contents using Semigroup operation.
lookupWith :: (Semigroup b, Ord (f Ignored), Foldable f, Functor g) => (a -> b) -> f a -> PreNatMap f g -> Maybe (g b) Source #
Query the output of natural transformation for given input fa :: f a,
while mapping its contents to another type b with a Semigroup instance.
lookupWith h fa === lookup (h <$> fa)
lookupShape :: forall (f :: Type -> Type) (g :: Type -> Type). Ord (f Ignored) => Shape f -> PreNatMap f g -> Maybe (Shape g) Source #
update
refine :: (Ord a, Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => f a -> g a -> PreNatMap f g -> Maybe (PreNatMap f g) Source #
Refines knowledge a PreNatMap contains by a pair of example
input-output pair.
Returns Nothing if the given example is not consistent with the already given
examples.
refineShape :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => Shape f -> Shape g -> PreNatMap f g -> Maybe (PreNatMap f g) Source #
refine but only the shapes of the input and output is given.
conversion
toEntries :: forall (f :: Type -> Type) (g :: Type -> Type). (Traversable f, Functor g) => PreNatMap f g -> [PreEntry f g] Source #
Extract PreNatMap as a list of PreEntry.
fromEntries :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => [PreEntry f g] -> Maybe (PreNatMap f g) Source #
Rebuild from a list of PreEntry
make :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Eq (g Ignored), Foldable f, Traversable g) => [PreEntry f g] -> PreNatMap f g Source #
fromEntries but raises error on Nothing case.
toNatMap :: forall (f :: Type -> Type) (g :: Type -> Type). (Ord (f Ignored), Traversable f, Functor g) => PreNatMap f g -> NatMap f g Source #
Convert to NatMap by discarding non-full entries
fromNatMap :: forall (f :: Type -> Type) (g :: Type -> Type). (Traversable f, Traversable g) => NatMap f g -> PreNatMap f g Source #
Convert from NatMap
toShapeMap :: forall (f :: Type -> Type) (g :: Type -> Type). PreNatMap f g -> Map (Shape f) (Shape g) Source #
Extract only the Shape part as a plain Map.
fromShapeMap :: forall (f :: Type -> Type) (g :: Type -> Type). (Foldable f, Traversable g) => Map (Shape f) (Shape g) -> Maybe (PreNatMap f g) Source #
Convert the Map of Shape parts to PreNatMap, by each mapping
of shape f0 :: Shape f to shape g0 :: Shape g as an example input-output
pair of contentless values (f (), g ()).
Note that this operation can fail when f is empty and g is nonempty.
For example, a Map containing mapping Shape Nothing to Shape (Just ())
fails, as no natural transformation Maybe ~> Maybe can turn Nothing to Just x.