{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
module Frames.Folds.Maybe
(
EndoFold
, FoldEndo(..)
, FoldRecord(..)
, toFoldRecord
, recFieldF
, fieldToFieldFold
, sequenceRecFold
, sequenceEndoFolds
, foldAll
, foldAllConstrained
, maybeFoldAllConstrained
, foldAllMonoid
)
where
import qualified Frames.MapReduce.General as MG
import qualified Frames.Folds.General as FG
import Frames.Folds.General ( FoldEndo(..)
, FoldRecord(..)
)
import Frames.Folds ( EndoFold
, MonoidalField
)
import qualified Control.Foldl as FL
import qualified Data.Vinyl as V
import Data.Vinyl ( ElField )
import qualified Data.Vinyl.TypeLevel as V
import qualified Frames as F
import Frames ( (:.) )
import qualified Frames.Melt as F
toFoldRecord
:: (a -> g b)
-> FL.Fold (record (Maybe :. ElField) rs) a
-> FoldRecord record Maybe g rs b
toFoldRecord :: (a -> g b)
-> Fold (record (Maybe :. ElField) rs) a
-> FoldRecord record Maybe g rs b
toFoldRecord = (a -> g b)
-> Fold (record (Maybe :. ElField) rs) a
-> FoldRecord record Maybe g rs b
forall k1 k2 a (g :: k1 -> *) (b :: k1)
(record :: ((Symbol, *) -> *) -> k2 -> *) (f :: * -> *) (rs :: k2).
(a -> g b)
-> Fold (record (f :. ElField) rs) a -> FoldRecord record f g rs b
FG.toFoldRecord
recFieldF
:: forall t rs a record
. V.KnownField t
=> FL.Fold a (V.Snd t)
-> (record (Maybe :. ElField) rs -> Maybe a)
-> FG.FoldRecord record Maybe (Maybe :. ElField) rs t
recFieldF :: Fold a (Snd t)
-> (record (Maybe :. ElField) rs -> Maybe a)
-> FoldRecord record Maybe (Maybe :. ElField) rs t
recFieldF = (forall x. Maybe x -> Maybe x)
-> Fold a (Snd t)
-> (record (Maybe :. ElField) rs -> Maybe a)
-> FoldRecord record Maybe (Maybe :. ElField) rs t
forall k (t :: (Symbol, *)) (rs :: k) a
(record :: ((Symbol, *) -> *) -> k -> *) (f :: * -> *).
(KnownField t, Applicative f) =>
(forall x. f x -> Maybe x)
-> Fold a (Snd t)
-> (record (f :. ElField) rs -> f a)
-> FoldRecord record f (f :. ElField) rs t
FG.recFieldF forall a. a -> a
forall x. Maybe x -> Maybe x
id
{-# INLINABLE recFieldF #-}
fieldToFieldFold
:: forall x y rs record
. ( V.KnownField x
, V.KnownField y
, F.ElemOf rs x
, MG.RecGetFieldC x record Maybe rs
)
=> FL.Fold (V.Snd x) (V.Snd y)
-> FG.FoldRecord record Maybe (Maybe :. ElField) rs y
fieldToFieldFold :: Fold (Snd x) (Snd y)
-> FoldRecord record Maybe (Maybe :. ElField) rs y
fieldToFieldFold = (forall x. Maybe x -> Maybe x)
-> Fold (Snd x) (Snd y)
-> FoldRecord record Maybe (Maybe :. ElField) rs y
forall (x :: (Symbol, *)) (y :: (Symbol, *)) (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
(KnownField x, KnownField y, ElemOf rs x,
RecGetFieldC x record f rs, Applicative f) =>
(forall z. f z -> Maybe z)
-> Fold (Snd x) (Snd y) -> FoldRecord record f (f :. ElField) rs y
FG.fieldToFieldFold @x forall a. a -> a
forall x. Maybe x -> Maybe x
id
{-# INLINABLE fieldToFieldFold #-}
sequenceRecFold
:: forall as rs record
. (MG.IsoRec rs record Maybe)
=> F.Rec (FG.FoldRecord record Maybe (Maybe :. ElField) as) rs
-> FL.Fold (record (Maybe :. ElField) as) (record (Maybe :. ElField) rs)
sequenceRecFold :: Rec (FoldRecord record Maybe (Maybe :. ElField) as) rs
-> Fold
(record (Maybe :. ElField) as) (record (Maybe :. ElField) rs)
sequenceRecFold = Rec (FoldRecord record Maybe (Maybe :. ElField) as) rs
-> Fold
(record (Maybe :. ElField) as) (record (Maybe :. ElField) rs)
forall (as :: [(Symbol, *)]) (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
IsoRec rs record f =>
Rec (FoldRecord record f (f :. ElField) as) rs
-> Fold (record (f :. ElField) as) (record (f :. ElField) rs)
FG.sequenceRecFold
{-# INLINABLE sequenceRecFold #-}
sequenceEndoFolds
:: forall rs record
. ( V.RApply rs
, V.RPureConstrained V.KnownField rs
, FG.EndoFieldFoldsToRecordFolds rs record Maybe
, MG.IsoRec rs record Maybe
)
=> F.Rec (FG.FoldEndo Maybe) rs
-> FL.Fold (record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
sequenceEndoFolds :: Rec (FoldEndo Maybe) rs
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
sequenceEndoFolds = Rec (FoldEndo Maybe) rs
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
forall (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
(RApply rs, RPureConstrained KnownField rs,
EndoFieldFoldsToRecordFolds rs record f, IsoRec rs record f,
Functor f) =>
Rec (FoldEndo f) rs
-> Fold (record (f :. ElField) rs) (record (f :. ElField) rs)
FG.sequenceEndoFolds
{-# INLINABLE sequenceEndoFolds #-}
foldAll
:: ( V.RPureConstrained V.KnownField rs
, V.RApply rs
, FG.EndoFieldFoldsToRecordFolds rs record Maybe
, MG.IsoRec rs record Maybe
)
=> (forall a . FL.Fold a a)
-> FL.Fold (record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
foldAll :: (forall a. Fold a a)
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
foldAll = (forall a. Fold a a)
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
forall (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
(RPureConstrained KnownField rs, RApply rs,
EndoFieldFoldsToRecordFolds rs record f, IsoRec rs record f,
Functor f) =>
(forall a. Fold a a)
-> Fold (record (f :. ElField) rs) (record (f :. ElField) rs)
FG.foldAll
{-# INLINABLE foldAll #-}
foldAllConstrained
:: forall c rs record
. ( V.RPureConstrained (FG.ConstrainedField c) rs
, V.RPureConstrained V.KnownField rs
, V.RApply rs
, FG.EndoFieldFoldsToRecordFolds rs record Maybe
, MG.IsoRec rs record Maybe
)
=> (forall a . c a => FL.Fold a a)
-> FL.Fold (record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
foldAllConstrained :: (forall a. c a => Fold a a)
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
foldAllConstrained = (forall x. Maybe x -> Maybe x)
-> (forall a. c a => Fold a a)
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
forall (c :: * -> Constraint) (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
(RPureConstrained (ConstrainedField c) rs,
RPureConstrained KnownField rs, RApply rs,
EndoFieldFoldsToRecordFolds rs record f, IsoRec rs record f,
Applicative f) =>
(forall a. f a -> Maybe a)
-> (forall a. c a => Fold a a)
-> Fold (record (f :. ElField) rs) (record (f :. ElField) rs)
FG.foldAllConstrained @c forall a. a -> a
forall x. Maybe x -> Maybe x
id
{-# INLINABLE foldAllConstrained #-}
maybeFoldAllConstrained
:: forall c rs record
. ( V.RPureConstrained (FG.ConstrainedField c) rs
, V.RPureConstrained V.KnownField rs
, V.RApply rs
, FG.EndoFieldFoldsToRecordFolds rs record Maybe
, MG.IsoRec rs record Maybe
)
=> (forall a . c a => FL.Fold (Maybe a) (Maybe a))
-> FL.Fold (record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
maybeFoldAllConstrained :: (forall a. c a => Fold (Maybe a) (Maybe a))
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
maybeFoldAllConstrained = forall (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
(RPureConstrained (ConstrainedField c) rs,
RPureConstrained KnownField rs, RApply rs,
EndoFieldFoldsToRecordFolds rs record f, IsoRec rs record f,
Applicative f) =>
(forall a. c a => Fold (f a) (f a))
-> Fold (record (f :. ElField) rs) (record (f :. ElField) rs)
forall (c :: * -> Constraint) (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
(RPureConstrained (ConstrainedField c) rs,
RPureConstrained KnownField rs, RApply rs,
EndoFieldFoldsToRecordFolds rs record f, IsoRec rs record f,
Applicative f) =>
(forall a. c a => Fold (f a) (f a))
-> Fold (record (f :. ElField) rs) (record (f :. ElField) rs)
FG.functorFoldAllConstrained @c
{-# INLINABLE maybeFoldAllConstrained #-}
foldAllMonoid
:: forall g rs record
. ( V.RPureConstrained (FG.ConstrainedField (MonoidalField g)) rs
, V.RPureConstrained V.KnownField rs
, V.RApply rs
, FG.EndoFieldFoldsToRecordFolds rs record Maybe
, MG.IsoRec rs record Maybe
)
=> FL.Fold (record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
foldAllMonoid :: Fold (record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
foldAllMonoid = (forall x. Maybe x -> Maybe x)
-> Fold
(record (Maybe :. ElField) rs) (record (Maybe :. ElField) rs)
forall (g :: * -> *) (rs :: [(Symbol, *)])
(record :: ((Symbol, *) -> *) -> [(Symbol, *)] -> *) (f :: * -> *).
(RPureConstrained (ConstrainedField (MonoidalField g)) rs,
RPureConstrained KnownField rs, RApply rs,
EndoFieldFoldsToRecordFolds rs record f, IsoRec rs record f,
Applicative f) =>
(forall a. f a -> Maybe a)
-> Fold (record (f :. ElField) rs) (record (f :. ElField) rs)
FG.foldAllMonoid @g forall a. a -> a
forall x. Maybe x -> Maybe x
id
{-# INLINABLE foldAllMonoid #-}