nat-map
Safe HaskellNone
LanguageHaskell2010

Data.PreNatMap

Synopsis

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
>>> p1
Just (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"
>>> p2
Just (make [PreEntry [0,1,2] [2,1,0]])
>>> p3 = p2 >>= refine "aabb" "bbaa"
>>> p3
Just (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

Instances details
(Show (f Int), Show (g Int), Traversable f, Functor g) => Show (PreNatMap f g) Source # 
Instance details

Defined in Data.PreNatMap

Methods

showsPrec :: Int -> PreNatMap f g -> ShowS #

show :: PreNatMap f g -> String #

showList :: [PreNatMap f g] -> ShowS #

(Eq (f Ignored), Eq (g Int)) => Eq (PreNatMap f g) Source # 
Instance details

Defined in Data.PreNatMap

Methods

(==) :: PreNatMap f g -> PreNatMap f g -> Bool #

(/=) :: PreNatMap f g -> PreNatMap f g -> Bool #

(Ord (f Ignored), Ord (g Int)) => Ord (PreNatMap f g) Source # 
Instance details

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 #

max :: PreNatMap f g -> PreNatMap f g -> PreNatMap f g #

min :: PreNatMap f g -> PreNatMap f g -> PreNatMap f g #

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.

Constructors

PreEntry (f Int) (g Int) 

Instances

Instances details
(Show (f Int), Show (g Int)) => Show (PreEntry f g) Source # 
Instance details

Defined in Data.PreNatMap

Methods

showsPrec :: Int -> PreEntry f g -> ShowS #

show :: PreEntry f g -> String #

showList :: [PreEntry f g] -> ShowS #

(Eq (f Int), Eq (g Int)) => Eq (PreEntry f g) Source # 
Instance details

Defined in Data.PreNatMap

Methods

(==) :: PreEntry f g -> PreEntry f g -> Bool #

(/=) :: PreEntry f g -> PreEntry f g -> Bool #

(Ord (f Int), Ord (g Int)) => Ord (PreEntry f g) Source # 
Instance details

Defined in Data.PreNatMap

Methods

compare :: PreEntry f g -> PreEntry f g -> Ordering #

(<) :: PreEntry f g -> PreEntry f g -> Bool #

(<=) :: PreEntry f g -> PreEntry f g -> Bool #

(>) :: PreEntry f g -> PreEntry f g -> Bool #

(>=) :: PreEntry f g -> PreEntry f g -> Bool #

max :: PreEntry f g -> PreEntry f g -> PreEntry f g #

min :: PreEntry f g -> PreEntry f g -> PreEntry f g #

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 #

Looks up only the shape.

lookupShape (Shape f) p === Shape <$> lookup (void f) p

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.