{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.Datatype
( Datatype(..)
, typeName, typeRules, typeMkRules, typeMkModifiers, typeMkCanonicalForm
, getTypeAxiom, getTypeRule
, DatatypeVal(..)
, getDTMod
, getDTRel
, tvalMkCanonicalForm
, DatatypeMap(..)
, DatatypeRel(..), DatatypeRelFn, DatatypeRelPr
, altArgs
, UnaryFnDescr, UnaryFnTable, UnaryFnApply, unaryFnApp
, BinaryFnDescr, BinaryFnTable, BinaryFnApply, binaryFnApp
, BinMaybeFnDescr, BinMaybeFnTable, BinMaybeFnApply, binMaybeFnApp
, ListFnDescr, ListFnTable, ListFnApply, listFnApp
, DatatypeMod(..), ModifierFn
, ApplyModifier
, nullDatatypeMod
, makeVmod11inv, makeVmod11
, makeVmod21inv, makeVmod21
, makeVmod20
, makeVmod22
, makeVmodN1
, DatatypeSub(..)
)
where
import Swish.Namespace (ScopedName)
import Swish.Rule (Formula(..), Rule(..))
import Swish.Ruleset (Ruleset(..))
import Swish.Ruleset (getRulesetAxiom, getRulesetRule)
import Swish.VarBinding (VarBinding(..), VarBindingModify(..), OpenVarBindingModify)
import Swish.VarBinding (addVarBinding, nullVarBindingModify)
import Swish.RDF.Vocabulary (swishName)
import Swish.Utils.ListHelpers (flist)
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 808)
import Control.Applicative ((<$>))
#endif
import Data.Maybe (isJust, catMaybes)
import qualified Data.Map as M
import qualified Data.Text as T
data Datatype ex lb vn = forall vt . Datatype (DatatypeVal ex vt lb vn)
typeName :: Datatype ex lb vn -> ScopedName
typeName :: forall ex lb vn. Datatype ex lb vn -> ScopedName
typeName (Datatype DatatypeVal ex vt lb vn
dtv) = DatatypeVal ex vt lb vn -> ScopedName
forall ex vt lb vn. DatatypeVal ex vt lb vn -> ScopedName
tvalName DatatypeVal ex vt lb vn
dtv
typeRules :: Datatype ex lb vn -> Ruleset ex
typeRules :: forall ex lb vn. Datatype ex lb vn -> Ruleset ex
typeRules (Datatype DatatypeVal ex vt lb vn
dtv) = DatatypeVal ex vt lb vn -> Ruleset ex
forall ex vt lb vn. DatatypeVal ex vt lb vn -> Ruleset ex
tvalRules DatatypeVal ex vt lb vn
dtv
typeMkRules :: Datatype ex lb vn -> ex -> [Rule ex]
typeMkRules :: forall ex lb vn. Datatype ex lb vn -> ex -> [Rule ex]
typeMkRules (Datatype DatatypeVal ex vt lb vn
dtv) = DatatypeVal ex vt lb vn -> ex -> [Rule ex]
forall ex vt lb vn. DatatypeVal ex vt lb vn -> ex -> [Rule ex]
tvalMkRules DatatypeVal ex vt lb vn
dtv
typeMkModifiers :: Datatype ex lb vn -> [OpenVarBindingModify lb vn]
typeMkModifiers :: forall ex lb vn. Datatype ex lb vn -> [OpenVarBindingModify lb vn]
typeMkModifiers (Datatype DatatypeVal ex vt lb vn
dtv) = DatatypeVal ex vt lb vn -> [OpenVarBindingModify lb vn]
forall ex vt lb vn.
DatatypeVal ex vt lb vn -> [OpenVarBindingModify lb vn]
tvalMkMods DatatypeVal ex vt lb vn
dtv
getTypeAxiom :: ScopedName -> Datatype ex lb vn -> Maybe (Formula ex)
getTypeAxiom :: forall ex lb vn.
ScopedName -> Datatype ex lb vn -> Maybe (Formula ex)
getTypeAxiom ScopedName
nam Datatype ex lb vn
dt = ScopedName -> Ruleset ex -> Maybe (Formula ex)
forall ex. ScopedName -> Ruleset ex -> Maybe (Formula ex)
getRulesetAxiom ScopedName
nam (Datatype ex lb vn -> Ruleset ex
forall ex lb vn. Datatype ex lb vn -> Ruleset ex
typeRules Datatype ex lb vn
dt)
getTypeRule :: ScopedName -> Datatype ex lb vn -> Maybe (Rule ex)
getTypeRule :: forall ex lb vn. ScopedName -> Datatype ex lb vn -> Maybe (Rule ex)
getTypeRule ScopedName
nam Datatype ex lb vn
dt = ScopedName -> Ruleset ex -> Maybe (Rule ex)
forall ex. ScopedName -> Ruleset ex -> Maybe (Rule ex)
getRulesetRule ScopedName
nam (Datatype ex lb vn -> Ruleset ex
forall ex lb vn. Datatype ex lb vn -> Ruleset ex
typeRules Datatype ex lb vn
dt)
typeMkCanonicalForm :: Datatype ex lb vn -> T.Text -> Maybe T.Text
typeMkCanonicalForm :: forall ex lb vn. Datatype ex lb vn -> Text -> Maybe Text
typeMkCanonicalForm (Datatype DatatypeVal ex vt lb vn
dtv) = DatatypeVal ex vt lb vn -> Text -> Maybe Text
forall ex vt lb vn. DatatypeVal ex vt lb vn -> Text -> Maybe Text
tvalMkCanonicalForm DatatypeVal ex vt lb vn
dtv
data DatatypeVal ex vt lb vn = DatatypeVal
{ forall ex vt lb vn. DatatypeVal ex vt lb vn -> ScopedName
tvalName :: ScopedName
, forall ex vt lb vn. DatatypeVal ex vt lb vn -> Ruleset ex
tvalRules :: Ruleset ex
, forall ex vt lb vn. DatatypeVal ex vt lb vn -> ex -> [Rule ex]
tvalMkRules :: ex -> [Rule ex]
, forall ex vt lb vn.
DatatypeVal ex vt lb vn -> [OpenVarBindingModify lb vn]
tvalMkMods :: [OpenVarBindingModify lb vn]
, forall ex vt lb vn. DatatypeVal ex vt lb vn -> DatatypeMap vt
tvalMap :: DatatypeMap vt
, forall ex vt lb vn. DatatypeVal ex vt lb vn -> [DatatypeRel vt]
tvalRel :: [DatatypeRel vt]
, forall ex vt lb vn.
DatatypeVal ex vt lb vn -> [DatatypeMod vt lb vn]
tvalMod :: [DatatypeMod vt lb vn]
}
getDTRel ::
ScopedName -> DatatypeVal ex vt lb vn -> Maybe (DatatypeRel vt)
getDTRel :: forall ex vt lb vn.
ScopedName -> DatatypeVal ex vt lb vn -> Maybe (DatatypeRel vt)
getDTRel ScopedName
nam DatatypeVal ex vt lb vn
dtv =
let m :: Map ScopedName (DatatypeRel vt)
m = [(ScopedName, DatatypeRel vt)] -> Map ScopedName (DatatypeRel vt)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(ScopedName, DatatypeRel vt)] -> Map ScopedName (DatatypeRel vt))
-> [(ScopedName, DatatypeRel vt)]
-> Map ScopedName (DatatypeRel vt)
forall a b. (a -> b) -> a -> b
$ (DatatypeRel vt -> (ScopedName, DatatypeRel vt))
-> [DatatypeRel vt] -> [(ScopedName, DatatypeRel vt)]
forall a b. (a -> b) -> [a] -> [b]
map (\DatatypeRel vt
n -> (DatatypeRel vt -> ScopedName
forall vt. DatatypeRel vt -> ScopedName
dtRelName DatatypeRel vt
n, DatatypeRel vt
n)) (DatatypeVal ex vt lb vn -> [DatatypeRel vt]
forall ex vt lb vn. DatatypeVal ex vt lb vn -> [DatatypeRel vt]
tvalRel DatatypeVal ex vt lb vn
dtv)
in ScopedName
-> Map ScopedName (DatatypeRel vt) -> Maybe (DatatypeRel vt)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ScopedName
nam Map ScopedName (DatatypeRel vt)
m
getDTMod ::
ScopedName -> DatatypeVal ex vt lb vn -> Maybe (DatatypeMod vt lb vn)
getDTMod :: forall ex vt lb vn.
ScopedName
-> DatatypeVal ex vt lb vn -> Maybe (DatatypeMod vt lb vn)
getDTMod ScopedName
nam DatatypeVal ex vt lb vn
dtv =
let m :: Map ScopedName (DatatypeMod vt lb vn)
m = [(ScopedName, DatatypeMod vt lb vn)]
-> Map ScopedName (DatatypeMod vt lb vn)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(ScopedName, DatatypeMod vt lb vn)]
-> Map ScopedName (DatatypeMod vt lb vn))
-> [(ScopedName, DatatypeMod vt lb vn)]
-> Map ScopedName (DatatypeMod vt lb vn)
forall a b. (a -> b) -> a -> b
$ (DatatypeMod vt lb vn -> (ScopedName, DatatypeMod vt lb vn))
-> [DatatypeMod vt lb vn] -> [(ScopedName, DatatypeMod vt lb vn)]
forall a b. (a -> b) -> [a] -> [b]
map (\DatatypeMod vt lb vn
n -> (DatatypeMod vt lb vn -> ScopedName
forall vt lb vn. DatatypeMod vt lb vn -> ScopedName
dmName DatatypeMod vt lb vn
n, DatatypeMod vt lb vn
n)) (DatatypeVal ex vt lb vn -> [DatatypeMod vt lb vn]
forall ex vt lb vn.
DatatypeVal ex vt lb vn -> [DatatypeMod vt lb vn]
tvalMod DatatypeVal ex vt lb vn
dtv)
in ScopedName
-> Map ScopedName (DatatypeMod vt lb vn)
-> Maybe (DatatypeMod vt lb vn)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ScopedName
nam Map ScopedName (DatatypeMod vt lb vn)
m
tvalMkCanonicalForm :: DatatypeVal ex vt lb vn -> T.Text -> Maybe T.Text
tvalMkCanonicalForm :: forall ex vt lb vn. DatatypeVal ex vt lb vn -> Text -> Maybe Text
tvalMkCanonicalForm DatatypeVal ex vt lb vn
dtv Text
str = Maybe Text
can
where
dtmap :: DatatypeMap vt
dtmap = DatatypeVal ex vt lb vn -> DatatypeMap vt
forall ex vt lb vn. DatatypeVal ex vt lb vn -> DatatypeMap vt
tvalMap DatatypeVal ex vt lb vn
dtv
val :: Maybe vt
val = DatatypeMap vt -> Text -> Maybe vt
forall vt. DatatypeMap vt -> Text -> Maybe vt
mapL2V DatatypeMap vt
dtmap Text
str
can :: Maybe Text
can = DatatypeMap vt -> vt -> Maybe Text
forall vt. DatatypeMap vt -> vt -> Maybe Text
mapV2L DatatypeMap vt
dtmap (vt -> Maybe Text) -> Maybe vt -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe vt
val
data DatatypeMap vt = DatatypeMap
{ forall vt. DatatypeMap vt -> Text -> Maybe vt
mapL2V :: T.Text -> Maybe vt
, forall vt. DatatypeMap vt -> vt -> Maybe Text
mapV2L :: vt -> Maybe T.Text
}
type DatatypeRelFn vt = [Maybe vt] -> Maybe [[vt]]
type DatatypeRelPr vt = [vt] -> Bool
data DatatypeRel vt = DatatypeRel
{ forall vt. DatatypeRel vt -> ScopedName
dtRelName :: ScopedName
, forall vt. DatatypeRel vt -> DatatypeRelFn vt
dtRelFunc :: DatatypeRelFn vt
}
type ModifierFn vn = [vn] -> [vn]
type ApplyModifier lb vn =
ScopedName -> [ModifierFn vn] -> OpenVarBindingModify lb vn
data DatatypeMod vt lb vn = DatatypeMod
{ forall vt lb vn. DatatypeMod vt lb vn -> ScopedName
dmName :: ScopedName
, forall vt lb vn. DatatypeMod vt lb vn -> [ModifierFn vt]
dmModf :: [ModifierFn vt]
, forall vt lb vn. DatatypeMod vt lb vn -> ApplyModifier lb vn
dmAppf :: ApplyModifier lb vn
}
nullDatatypeMod :: DatatypeMod vt lb vn
nullDatatypeMod :: forall vt lb vn. DatatypeMod vt lb vn
nullDatatypeMod = DatatypeMod
{ dmName :: ScopedName
dmName = LName -> ScopedName
swishName LName
"nullDatatypeMod"
, dmModf :: [ModifierFn vt]
dmModf = []
, dmAppf :: ApplyModifier lb vn
dmAppf = ApplyModifier lb vn
forall {p} {a} {b}. ScopedName -> p -> [a] -> VarBindingModify a b
nullAppf
}
where
nullAppf :: ScopedName -> p -> [a] -> VarBindingModify a b
nullAppf ScopedName
nam p
_ [a]
lbs = ([a] -> VarBindingModify a b
forall a b. OpenVarBindingModify a b
nullVarBindingModify [a]
lbs) { vbmName = nam }
makeVmod11inv :: (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod11inv :: forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod11inv ScopedName
nam [ModifierFn vn
f0,ModifierFn vn
f1,ModifierFn vn
f2] lbs :: [lb]
lbs@(~[lb
lb1,lb
lb2]) = VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
nam
, vbmApply :: [VarBinding lb vn] -> [VarBinding lb vn]
vbmApply = (VarBinding lb vn -> [VarBinding lb vn])
-> [VarBinding lb vn] -> [VarBinding lb vn]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap VarBinding lb vn -> [VarBinding lb vn]
app1
, vbmVocab :: [lb]
vbmVocab = [lb]
lbs
, vbmUsage :: [[lb]]
vbmUsage = [[],[lb
lb1],[lb
lb2]]
}
where
app1 :: VarBinding lb vn -> [VarBinding lb vn]
app1 VarBinding lb vn
vbind = [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 ((lb -> Maybe vn) -> [lb] -> [Maybe vn]
forall a b. (a -> b) -> [a] -> [b]
map (VarBinding lb vn -> lb -> Maybe vn
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding lb vn
vbind) [lb]
lbs) VarBinding lb vn
vbind
app2 :: [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 [Just vn
v1,Just vn
v2] VarBinding lb vn
vbind = [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv (ModifierFn vn
f0 [vn
v1,vn
v2]) VarBinding lb vn
vbind
app2 [Maybe vn
Nothing,Just vn
v2] VarBinding lb vn
vbind = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb1 (ModifierFn vn
f1 [vn
v2]) VarBinding lb vn
vbind
app2 [Just vn
v1,Maybe vn
Nothing] VarBinding lb vn
vbind = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb2 (ModifierFn vn
f2 [vn
v1]) VarBinding lb vn
vbind
app2 [Maybe vn]
_ VarBinding lb vn
_ = []
makeVmod11inv ScopedName
_ [ModifierFn vn]
_ [lb]
_ =
[Char] -> VarBindingModify lb vn
forall a. HasCallStack => [Char] -> a
error [Char]
"makeVmod11inv: requires 3 functions and 2 labels"
makeVmod11 :: (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod11 :: forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod11 ScopedName
nam [ModifierFn vn
f0,ModifierFn vn
f1] lbs :: [lb]
lbs@(~[lb
lb1,lb
_]) = VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
nam
, vbmApply :: [VarBinding lb vn] -> [VarBinding lb vn]
vbmApply = (VarBinding lb vn -> [VarBinding lb vn])
-> [VarBinding lb vn] -> [VarBinding lb vn]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap VarBinding lb vn -> [VarBinding lb vn]
app1
, vbmVocab :: [lb]
vbmVocab = [lb]
lbs
, vbmUsage :: [[lb]]
vbmUsage = [[],[lb
lb1]]
}
where
app1 :: VarBinding lb vn -> [VarBinding lb vn]
app1 VarBinding lb vn
vbind = [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 ((lb -> Maybe vn) -> [lb] -> [Maybe vn]
forall a b. (a -> b) -> [a] -> [b]
map (VarBinding lb vn -> lb -> Maybe vn
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding lb vn
vbind) [lb]
lbs) VarBinding lb vn
vbind
app2 :: [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 [Just vn
v1,Just vn
v2] VarBinding lb vn
vbind = [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv (ModifierFn vn
f0 [vn
v1,vn
v2]) VarBinding lb vn
vbind
app2 [Maybe vn
Nothing,Just vn
v2] VarBinding lb vn
vbind = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb1 (ModifierFn vn
f1 [vn
v2]) VarBinding lb vn
vbind
app2 [Maybe vn]
_ VarBinding lb vn
_ = []
makeVmod11 ScopedName
_ [ModifierFn vn]
_ [lb]
_ =
[Char] -> VarBindingModify lb vn
forall a. HasCallStack => [Char] -> a
error [Char]
"makeVmod11: requires 2 functions and 2 labels"
makeVmod21inv :: (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21inv :: forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21inv ScopedName
nam [ModifierFn vn
f0,ModifierFn vn
f1,ModifierFn vn
f2,ModifierFn vn
f3] lbs :: [lb]
lbs@(~[lb
lb1,lb
lb2,lb
lb3]) = VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
nam
, vbmApply :: [VarBinding lb vn] -> [VarBinding lb vn]
vbmApply = (VarBinding lb vn -> [VarBinding lb vn])
-> [VarBinding lb vn] -> [VarBinding lb vn]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap VarBinding lb vn -> [VarBinding lb vn]
app1
, vbmVocab :: [lb]
vbmVocab = [lb]
lbs
, vbmUsage :: [[lb]]
vbmUsage = [[],[lb
lb1],[lb
lb2],[lb
lb3]]
}
where
app1 :: VarBinding lb vn -> [VarBinding lb vn]
app1 VarBinding lb vn
vbind = [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 ((lb -> Maybe vn) -> [lb] -> [Maybe vn]
forall a b. (a -> b) -> [a] -> [b]
map (VarBinding lb vn -> lb -> Maybe vn
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding lb vn
vbind) [lb]
lbs) VarBinding lb vn
vbind
app2 :: [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 [Just vn
v1,Just vn
v2,Just vn
v3] VarBinding lb vn
vbind = [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv (ModifierFn vn
f0 [vn
v1,vn
v2,vn
v3]) VarBinding lb vn
vbind
app2 [Maybe vn
Nothing,Just vn
v2,Just vn
v3] VarBinding lb vn
vbind = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb1 (ModifierFn vn
f1 [vn
v2,vn
v3]) VarBinding lb vn
vbind
app2 [Just vn
v1,Maybe vn
Nothing,Just vn
v3] VarBinding lb vn
vbind = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb2 (ModifierFn vn
f2 [vn
v1,vn
v3]) VarBinding lb vn
vbind
app2 [Just vn
v1,Just vn
v2,Maybe vn
Nothing] VarBinding lb vn
vbind = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb3 (ModifierFn vn
f3 [vn
v1,vn
v2]) VarBinding lb vn
vbind
app2 [Maybe vn]
_ VarBinding lb vn
_ = []
makeVmod21inv ScopedName
_ [ModifierFn vn]
_ [lb]
_ =
[Char] -> VarBindingModify lb vn
forall a. HasCallStack => [Char] -> a
error [Char]
"makeVmod21inv: requires 4 functions and 3 labels"
makeVmod21 :: (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21 :: forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21 ScopedName
nam [ModifierFn vn
f0,ModifierFn vn
f1] lbs :: [lb]
lbs@(~[lb
lb1,lb
_,lb
_]) = VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
nam
, vbmApply :: [VarBinding lb vn] -> [VarBinding lb vn]
vbmApply = (VarBinding lb vn -> [VarBinding lb vn])
-> [VarBinding lb vn] -> [VarBinding lb vn]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap VarBinding lb vn -> [VarBinding lb vn]
app1
, vbmVocab :: [lb]
vbmVocab = [lb]
lbs
, vbmUsage :: [[lb]]
vbmUsage = [[],[lb
lb1]]
}
where
app1 :: VarBinding lb vn -> [VarBinding lb vn]
app1 VarBinding lb vn
vbind = [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 ((lb -> Maybe vn) -> [lb] -> [Maybe vn]
forall a b. (a -> b) -> [a] -> [b]
map (VarBinding lb vn -> lb -> Maybe vn
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding lb vn
vbind) [lb]
lbs) VarBinding lb vn
vbind
app2 :: [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 [Just vn
v1,Just vn
v2,Just vn
v3] VarBinding lb vn
vbind = [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv (ModifierFn vn
f0 [vn
v1,vn
v2,vn
v3]) VarBinding lb vn
vbind
app2 [Maybe vn
Nothing,Just vn
v2,Just vn
v3] VarBinding lb vn
vbind = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb1 (ModifierFn vn
f1 [vn
v2,vn
v3]) VarBinding lb vn
vbind
app2 [Maybe vn]
_ VarBinding lb vn
_ = []
makeVmod21 ScopedName
_ [ModifierFn vn]
_ [lb]
_ =
[Char] -> VarBindingModify lb vn
forall a. HasCallStack => [Char] -> a
error [Char]
"makeVmod21: requires 2 functions and 3 labels"
makeVmod20 :: (Eq lb, Show lb, Eq vn, Show vn) => ApplyModifier lb vn
makeVmod20 :: forall lb vn.
(Eq lb, Show lb, Eq vn, Show vn) =>
ApplyModifier lb vn
makeVmod20 ScopedName
nam [ModifierFn vn
f0] lbs :: [lb]
lbs@(~[lb
_,lb
_]) = VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
nam
, vbmApply :: [VarBinding lb vn] -> [VarBinding lb vn]
vbmApply = (VarBinding lb vn -> [VarBinding lb vn])
-> [VarBinding lb vn] -> [VarBinding lb vn]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap VarBinding lb vn -> [VarBinding lb vn]
app1
, vbmVocab :: [lb]
vbmVocab = [lb]
lbs
, vbmUsage :: [[lb]]
vbmUsage = [[]]
}
where
app1 :: VarBinding lb vn -> [VarBinding lb vn]
app1 VarBinding lb vn
vbind = [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall {varBinding :: * -> * -> *} {lb}.
[Maybe vn] -> varBinding lb vn -> [varBinding lb vn]
app2 ((lb -> Maybe vn) -> [lb] -> [Maybe vn]
forall a b. (a -> b) -> [a] -> [b]
map (VarBinding lb vn -> lb -> Maybe vn
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding lb vn
vbind) [lb]
lbs) VarBinding lb vn
vbind
app2 :: [Maybe vn] -> varBinding lb vn -> [varBinding lb vn]
app2 [Just vn
v1,Just vn
v2] varBinding lb vn
vbind = [vn] -> varBinding lb vn -> [varBinding lb vn]
forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv (ModifierFn vn
f0 [vn
v1,vn
v2]) varBinding lb vn
vbind
app2 [Maybe vn]
_ varBinding lb vn
_ = []
makeVmod20 ScopedName
_ [ModifierFn vn]
_ [lb]
_ =
[Char] -> VarBindingModify lb vn
forall a. HasCallStack => [Char] -> a
error [Char]
"makeVmod20: requires 1 function and 2 labels"
makeVmod22 :: (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod22 :: forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod22 ScopedName
nam [ModifierFn vn
f0,ModifierFn vn
f1] lbs :: [lb]
lbs@(~[lb
lb1,lb
lb2,lb
_,lb
_]) = VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
nam
, vbmApply :: [VarBinding lb vn] -> [VarBinding lb vn]
vbmApply = (VarBinding lb vn -> [VarBinding lb vn])
-> [VarBinding lb vn] -> [VarBinding lb vn]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap VarBinding lb vn -> [VarBinding lb vn]
app1
, vbmVocab :: [lb]
vbmVocab = [lb]
lbs
, vbmUsage :: [[lb]]
vbmUsage = [[],[lb
lb1,lb
lb2]]
}
where
app1 :: VarBinding lb vn -> [VarBinding lb vn]
app1 VarBinding lb vn
vbind = [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 ((lb -> Maybe vn) -> [lb] -> [Maybe vn]
forall a b. (a -> b) -> [a] -> [b]
map (VarBinding lb vn -> lb -> Maybe vn
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding lb vn
vbind) [lb]
lbs) VarBinding lb vn
vbind
app2 :: [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 [Just vn
v1,Just vn
v2,Just vn
v3,Just vn
v4] VarBinding lb vn
vbind =
[vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv (ModifierFn vn
f0 [vn
v1,vn
v2,vn
v3,vn
v4]) VarBinding lb vn
vbind
app2 [Maybe vn
Nothing,Maybe vn
Nothing,Just vn
v3,Just vn
v4] VarBinding lb vn
vbind =
lb -> lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv2 lb
lb1 lb
lb2 (ModifierFn vn
f1 [vn
v3,vn
v4]) VarBinding lb vn
vbind
app2 [Maybe vn]
_ VarBinding lb vn
_ = []
makeVmod22 ScopedName
_ [ModifierFn vn]
_ [lb]
_ =
[Char] -> VarBindingModify lb vn
forall a. HasCallStack => [Char] -> a
error [Char]
"makeVmod22: requires 2 functions and 4 labels"
makeVmodN1 :: (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmodN1 :: forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmodN1 ScopedName
nam [ModifierFn vn
f0,ModifierFn vn
f1] lbs :: [lb]
lbs@(~(lb
lb1:[lb]
_)) = VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
nam
, vbmApply :: [VarBinding lb vn] -> [VarBinding lb vn]
vbmApply = (VarBinding lb vn -> [VarBinding lb vn])
-> [VarBinding lb vn] -> [VarBinding lb vn]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap VarBinding lb vn -> [VarBinding lb vn]
app1
, vbmVocab :: [lb]
vbmVocab = [lb]
lbs
, vbmUsage :: [[lb]]
vbmUsage = [[],[lb
lb1]]
}
where
app1 :: VarBinding lb vn -> [VarBinding lb vn]
app1 VarBinding lb vn
vbind = [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 ((lb -> Maybe vn) -> [lb] -> [Maybe vn]
forall a b. (a -> b) -> [a] -> [b]
map (VarBinding lb vn -> lb -> Maybe vn
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding lb vn
vbind) [lb]
lbs) VarBinding lb vn
vbind
app2 :: [Maybe vn] -> VarBinding lb vn -> [VarBinding lb vn]
app2 vs :: [Maybe vn]
vs@(Maybe vn
v1:[Maybe vn]
_) VarBinding lb vn
vbind
| Maybe vn -> Bool
forall a. Maybe a -> Bool
isJust Maybe vn
v1 Bool -> Bool -> Bool
&& Bool
isJustvs = [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv (ModifierFn vn
f0 [vn]
jvs) VarBinding lb vn
vbind
| Bool
isJustvs = lb -> [vn] -> VarBinding lb vn -> [VarBinding lb vn]
forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb1 (ModifierFn vn
f1 [vn]
jvs) VarBinding lb vn
vbind
| Bool
otherwise = []
where
isJustvs :: Bool
isJustvs = (Maybe vn -> Bool) -> [Maybe vn] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Maybe vn -> Bool
forall a. Maybe a -> Bool
isJust [Maybe vn]
vs
jvs :: [vn]
jvs = [Maybe vn] -> [vn]
forall a. [Maybe a] -> [a]
catMaybes [Maybe vn]
vs
app2 [Maybe vn]
_ VarBinding lb vn
_ = [Char] -> [VarBinding lb vn]
forall a. HasCallStack => [Char] -> a
error [Char]
"app2 sent empty list"
makeVmodN1 ScopedName
_ [ModifierFn vn]
_ [lb]
_ =
[Char] -> VarBindingModify lb vn
forall a. HasCallStack => [Char] -> a
error [Char]
"makeVmodN1: requires 2 functions and at 1 or more labels"
addv :: (Ord lb, Ord vt)
=> lb -> [vt] -> VarBinding lb vt
-> [VarBinding lb vt]
addv :: forall lb vt.
(Ord lb, Ord vt) =>
lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv lb
lb [vt
val] VarBinding lb vt
vbind = [lb -> vt -> VarBinding lb vt -> VarBinding lb vt
forall a b.
(Ord a, Ord b) =>
a -> b -> VarBinding a b -> VarBinding a b
addVarBinding lb
lb vt
val VarBinding lb vt
vbind]
addv lb
_ [vt]
_ VarBinding lb vt
_ = []
addv2 :: (Ord lb, Ord vt)
=> lb -> lb -> [vt] -> VarBinding lb vt
-> [VarBinding lb vt]
addv2 :: forall lb vt.
(Ord lb, Ord vt) =>
lb -> lb -> [vt] -> VarBinding lb vt -> [VarBinding lb vt]
addv2 lb
lb1 lb
lb2 [vt
val1,vt
val2] VarBinding lb vt
vbind = [lb -> vt -> VarBinding lb vt -> VarBinding lb vt
forall a b.
(Ord a, Ord b) =>
a -> b -> VarBinding a b -> VarBinding a b
addVarBinding lb
lb1 vt
val1 (VarBinding lb vt -> VarBinding lb vt)
-> VarBinding lb vt -> VarBinding lb vt
forall a b. (a -> b) -> a -> b
$
lb -> vt -> VarBinding lb vt -> VarBinding lb vt
forall a b.
(Ord a, Ord b) =>
a -> b -> VarBinding a b -> VarBinding a b
addVarBinding lb
lb2 vt
val2 VarBinding lb vt
vbind]
addv2 lb
_ lb
_ [vt]
_ VarBinding lb vt
_ = []
selv :: [vt] -> varBinding lb vt -> [varBinding lb vt]
selv :: forall vt (varBinding :: * -> * -> *) lb.
[vt] -> varBinding lb vt -> [varBinding lb vt]
selv [] varBinding lb vt
_ = []
selv [vt]
_ varBinding lb vt
vbind = [varBinding lb vt
vbind]
altArgs ::
(Eq vt)
=> DatatypeRelPr vt
-> [(vt->Bool,[b])]
-> ((vt->Bool)->b->[Maybe vt]->Maybe [vt])
-> DatatypeRelFn vt
altArgs :: forall vt b.
Eq vt =>
DatatypeRelPr vt
-> [(vt -> Bool, [b])]
-> ((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt])
-> DatatypeRelFn vt
altArgs DatatypeRelPr vt
pr [(vt -> Bool, [b])]
fnss (vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt]
apfn [Maybe vt]
args = Maybe [[vt]] -> Maybe [[vt]]
forall {t :: * -> *}.
Foldable t =>
Maybe (t [vt]) -> Maybe (t [vt])
cvals4 Maybe [[vt]]
cvals3
where
cvals1 :: [Maybe [vt]]
cvals1 = [[Maybe vt] -> Maybe [vt]] -> [Maybe vt] -> [Maybe [vt]]
forall a b. [a -> b] -> a -> [b]
flist (((vt -> Bool, [b]) -> [Maybe vt] -> Maybe [vt])
-> [(vt -> Bool, [b])] -> [[Maybe vt] -> Maybe [vt]]
forall a b. (a -> b) -> [a] -> [b]
map (((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt])
-> (vt -> Bool, [b]) -> [Maybe vt] -> Maybe [vt]
forall vt b.
((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt])
-> (vt -> Bool, [b]) -> [Maybe vt] -> Maybe [vt]
applyFdescToTuple (vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt]
apfn) [(vt -> Bool, [b])]
fnss) [Maybe vt]
args
cvals2 :: Maybe [[vt]]
cvals2 = [Maybe [vt]] -> Maybe [[vt]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence ([Maybe [vt]] -> Maybe [[vt]]) -> [Maybe [vt]] -> Maybe [[vt]]
forall a b. (a -> b) -> a -> b
$ [vt -> Bool] -> [Maybe vt] -> [Maybe [vt]] -> [Maybe [vt]]
forall a.
Eq a =>
[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
mergeTupleVals (((vt -> Bool, [b]) -> vt -> Bool)
-> [(vt -> Bool, [b])] -> [vt -> Bool]
forall a b. (a -> b) -> [a] -> [b]
map (vt -> Bool, [b]) -> vt -> Bool
forall a b. (a, b) -> a
fst [(vt -> Bool, [b])]
fnss) [Maybe vt]
args [Maybe [vt]]
cvals1
cvals3 :: Maybe [[vt]]
cvals3 = ([[vt]] -> [[vt]]) -> Maybe [[vt]] -> Maybe [[vt]]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[vt]] -> [[vt]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence Maybe [[vt]]
cvals2
cvals4 :: Maybe (t [vt]) -> Maybe (t [vt])
cvals4 Maybe (t [vt])
Nothing = Maybe (t [vt])
forall a. Maybe a
Nothing
cvals4 cvs :: Maybe (t [vt])
cvs@(Just t [vt]
ts) = if DatatypeRelPr vt -> t [vt] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all DatatypeRelPr vt
pr t [vt]
ts then Maybe (t [vt])
cvs else Maybe (t [vt])
forall a. Maybe a
Nothing
applyFdescToTuple ::
((vt->Bool)->b->[Maybe vt]->Maybe [vt]) -> (vt->Bool,[b]) -> [Maybe vt]
-> Maybe [vt]
applyFdescToTuple :: forall vt b.
((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt])
-> (vt -> Bool, [b]) -> [Maybe vt] -> Maybe [vt]
applyFdescToTuple (vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt]
apfn (vt -> Bool
p,[b]
fns) [Maybe vt]
args =
[[vt]] -> [vt]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[vt]] -> [vt]) -> Maybe [[vt]] -> Maybe [vt]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Maybe [vt]] -> Maybe [[vt]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [Maybe [vt]]
cvals
where
cvals :: [Maybe [vt]]
cvals = [[Maybe vt] -> Maybe [vt]] -> [Maybe vt] -> [Maybe [vt]]
forall a b. [a -> b] -> a -> [b]
flist ((b -> [Maybe vt] -> Maybe [vt])
-> [b] -> [[Maybe vt] -> Maybe [vt]]
forall a b. (a -> b) -> [a] -> [b]
map ((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt]
apfn vt -> Bool
p) [b]
fns) [Maybe vt]
args
mergeTupleVals :: (Eq a) => [a->Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
mergeTupleVals :: forall a.
Eq a =>
[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
mergeTupleVals [a -> Bool]
_ [Maybe a]
_ (Maybe [a]
Nothing:[Maybe [a]]
_) = [Maybe [a]
forall a. Maybe a
Nothing]
mergeTupleVals (a -> Bool
_:[a -> Bool]
ps) (Maybe a
Nothing:[Maybe a]
a1s) (Just [a]
a2s:[Maybe [a]]
a2ss)
= [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
a2sMaybe [a] -> [Maybe [a]] -> [Maybe [a]]
forall a. a -> [a] -> [a]
:[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
forall a.
Eq a =>
[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
mergeTupleVals [a -> Bool]
ps [Maybe a]
a1s [Maybe [a]]
a2ss
mergeTupleVals (a -> Bool
p:[a -> Bool]
ps) (Just a
a1:[Maybe a]
a1s) (Just []:[Maybe [a]]
a2ss)
| a -> Bool
p a
a1 = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a
a1]Maybe [a] -> [Maybe [a]] -> [Maybe [a]]
forall a. a -> [a] -> [a]
:[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
forall a.
Eq a =>
[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
mergeTupleVals [a -> Bool]
ps [Maybe a]
a1s [Maybe [a]]
a2ss
| Bool
otherwise = [Maybe [a]
forall a. Maybe a
Nothing]
mergeTupleVals (a -> Bool
p:[a -> Bool]
ps) (Just a
a1:[Maybe a]
a1s) (Just [a]
a2s:[Maybe [a]]
a2ss)
| a -> Bool
p a
a1 Bool -> Bool -> Bool
&& a -> [a] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem a
a1 [a]
a2s = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a
a1]Maybe [a] -> [Maybe [a]] -> [Maybe [a]]
forall a. a -> [a] -> [a]
:[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
forall a.
Eq a =>
[a -> Bool] -> [Maybe a] -> [Maybe [a]] -> [Maybe [a]]
mergeTupleVals [a -> Bool]
ps [Maybe a]
a1s [Maybe [a]]
a2ss
| Bool
otherwise = [Maybe [a]
forall a. Maybe a
Nothing]
mergeTupleVals [a -> Bool]
_ [] [Maybe [a]]
_ = []
mergeTupleVals [a -> Bool]
_ [Maybe a]
_ [Maybe [a]]
_ = [Maybe [a]
forall a. Maybe a
Nothing]
type UnaryFnDescr a = (a->a,Int)
type UnaryFnTable a = [(a->Bool,[UnaryFnDescr a])]
type UnaryFnApply a = (a->Bool) -> UnaryFnDescr a -> [Maybe a] -> Maybe [a]
unaryFnApp :: UnaryFnApply a
unaryFnApp :: forall a. UnaryFnApply a
unaryFnApp a -> Bool
p (a -> a
f1,Int
n) [Maybe a]
args = Maybe a -> Maybe [a]
apf ([Maybe a]
args [Maybe a] -> Int -> Maybe a
forall a. HasCallStack => [a] -> Int -> a
!! Int
n)
where
apf :: Maybe a -> Maybe [a]
apf (Just a
a) = if a -> Bool
p a
r then [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a
r] else Maybe [a]
forall a. Maybe a
Nothing where r :: a
r = a -> a
f1 a
a
apf Maybe a
Nothing = [a] -> Maybe [a]
forall a. a -> Maybe a
Just []
type BinaryFnDescr a = (a -> a -> a, Int, Int)
type BinaryFnTable a = [(a -> Bool, [BinaryFnDescr a])]
type BinaryFnApply a =
(a -> Bool) -> BinaryFnDescr a -> [Maybe a] -> Maybe [a]
binaryFnApp :: BinaryFnApply a
binaryFnApp :: forall a. BinaryFnApply a
binaryFnApp a -> Bool
p (a -> a -> a
f,Int
n1,Int
n2) [Maybe a]
args = Maybe a -> Maybe a -> Maybe [a]
apf ([Maybe a]
args [Maybe a] -> Int -> Maybe a
forall a. HasCallStack => [a] -> Int -> a
!! Int
n1) ([Maybe a]
args [Maybe a] -> Int -> Maybe a
forall a. HasCallStack => [a] -> Int -> a
!! Int
n2)
where
apf :: Maybe a -> Maybe a -> Maybe [a]
apf (Just a
a1) (Just a
a2) = if a -> Bool
p a
r then [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a
r] else Maybe [a]
forall a. Maybe a
Nothing
where r :: a
r = a -> a -> a
f a
a1 a
a2
apf Maybe a
_ Maybe a
_ = [a] -> Maybe [a]
forall a. a -> Maybe a
Just []
type BinMaybeFnDescr a = (a -> a ->Maybe [a], Int, Int)
type BinMaybeFnTable a = [(a -> Bool, [BinMaybeFnDescr a])]
type BinMaybeFnApply a =
(a -> Bool) -> BinMaybeFnDescr a -> [Maybe a] -> Maybe [a]
binMaybeFnApp :: BinMaybeFnApply a
binMaybeFnApp :: forall a. BinMaybeFnApply a
binMaybeFnApp a -> Bool
p (a -> a -> Maybe [a]
f,Int
n1,Int
n2) [Maybe a]
args = Maybe a -> Maybe a -> Maybe [a]
apf ([Maybe a]
args [Maybe a] -> Int -> Maybe a
forall a. HasCallStack => [a] -> Int -> a
!! Int
n1) ([Maybe a]
args [Maybe a] -> Int -> Maybe a
forall a. HasCallStack => [a] -> Int -> a
!! Int
n2)
where
apf :: Maybe a -> Maybe a -> Maybe [a]
apf (Just a
a1) (Just a
a2) = if Maybe [a] -> Bool
forall {t :: * -> *}. Foldable t => Maybe (t a) -> Bool
pm Maybe [a]
r then Maybe [a]
r else Maybe [a]
forall a. Maybe a
Nothing
where
r :: Maybe [a]
r = a -> a -> Maybe [a]
f a
a1 a
a2
pm :: Maybe (t a) -> Bool
pm Maybe (t a)
Nothing = Bool
False
pm (Just t a
x) = (a -> Bool) -> t a -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all a -> Bool
p t a
x
apf Maybe a
_ Maybe a
_ = [a] -> Maybe [a]
forall a. a -> Maybe a
Just []
type ListFnDescr a = (a -> a -> a, a, a -> a -> a, Int)
type ListFnTable a = [(a -> Bool, [ListFnDescr a])]
type ListFnApply a = (a -> Bool) -> ListFnDescr a -> [Maybe a] -> Maybe [a]
listFnApp :: ListFnApply a
listFnApp :: forall a. ListFnApply a
listFnApp a -> Bool
p (a -> a -> a
f,a
z,a -> a -> a
g,Int
n) (Maybe a
a0:[Maybe a]
args)
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 =
Maybe [a] -> Maybe [a]
app (Maybe [a] -> Maybe [a]) -> Maybe [a] -> Maybe [a]
forall a b. (a -> b) -> a -> b
$ (Maybe a -> Maybe [a] -> Maybe [a])
-> Maybe [a] -> [Maybe a] -> Maybe [a]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((a -> a -> a) -> Maybe a -> Maybe [a] -> Maybe [a]
forall a. (a -> a -> a) -> Maybe a -> Maybe [a] -> Maybe [a]
apf a -> a -> a
f) ([a] -> Maybe [a]
forall a. a -> Maybe a
Just [a
z]) [Maybe a]
args
| Bool
otherwise =
Maybe [a] -> Maybe [a]
app (Maybe [a] -> Maybe [a]) -> Maybe [a] -> Maybe [a]
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> Maybe a -> Maybe [a] -> Maybe [a]
forall a. (a -> a -> a) -> Maybe a -> Maybe [a] -> Maybe [a]
apf a -> a -> a
g Maybe a
a0 ((Maybe a -> Maybe [a] -> Maybe [a])
-> Maybe [a] -> [Maybe a] -> Maybe [a]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((a -> a -> a) -> Maybe a -> Maybe [a] -> Maybe [a]
forall a. (a -> a -> a) -> Maybe a -> Maybe [a] -> Maybe [a]
apf a -> a -> a
f) ([a] -> Maybe [a]
forall a. a -> Maybe a
Just [a
z]) ([Maybe a]
args [Maybe a] -> Int -> [Maybe a]
forall a. [a] -> Int -> [a]
`deleteIndex` (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)))
where
apf :: (a->a->a) -> Maybe a -> Maybe [a] -> Maybe [a]
apf :: forall a. (a -> a -> a) -> Maybe a -> Maybe [a] -> Maybe [a]
apf a -> a -> a
fn (Just a
a1) (Just [a
a2]) = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a -> a -> a
fn a
a1 a
a2]
apf a -> a -> a
_ Maybe a
_ Maybe [a]
_ = [a] -> Maybe [a]
forall a. a -> Maybe a
Just []
app :: Maybe [a] -> Maybe [a]
app Maybe [a]
Nothing = Maybe [a]
forall a. Maybe a
Nothing
app r :: Maybe [a]
r@(Just [a
a]) = if a -> Bool
p a
a then Maybe [a]
r else Maybe [a]
forall a. Maybe a
Nothing
app Maybe [a]
_ = [a] -> Maybe [a]
forall a. a -> Maybe a
Just []
listFnApp a -> Bool
_ (a -> a -> a, a, a -> a -> a, Int)
_ [] = [Char] -> Maybe [a]
forall a. HasCallStack => [Char] -> a
error [Char]
"listFnApp called with an empty list"
deleteIndex :: [a] -> Int -> [a]
deleteIndex :: forall a. [a] -> Int -> [a]
deleteIndex [] Int
_ = []
deleteIndex xxs :: [a]
xxs@(a
x:[a]
xs) Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = [a]
xxs
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = [a]
xs
| Bool
otherwise = a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a] -> Int -> [a]
forall a. [a] -> Int -> [a]
deleteIndex [a]
xs (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
data DatatypeSub ex lb vn supvt subvt = DatatypeSub
{ forall ex lb vn supvt subvt.
DatatypeSub ex lb vn supvt subvt -> DatatypeVal ex supvt lb vn
trelSup :: DatatypeVal ex supvt lb vn
, forall ex lb vn supvt subvt.
DatatypeSub ex lb vn supvt subvt -> DatatypeVal ex subvt lb vn
trelSub :: DatatypeVal ex subvt lb vn
, forall ex lb vn supvt subvt.
DatatypeSub ex lb vn supvt subvt -> subvt -> supvt
trelToSup :: subvt -> supvt
, forall ex lb vn supvt subvt.
DatatypeSub ex lb vn supvt subvt -> supvt -> Maybe subvt
trelToSub :: supvt -> Maybe subvt
}