shrub/pkg/proto/lib/Dashboard.hs

131 lines
3.5 KiB
Haskell
Raw Normal View History

2019-09-24 02:10:24 +03:00
module Dashboard
( pattern FastAtom
, pattern FastHint
, Jet
, Dashboard (match)
, Freeboard
, Hashboard
, Fastboard
, Careboard
, runFree
, runHash
, runFast
, runCare
) where
2019-09-23 08:17:16 +03:00
import ClassyPrelude
import Control.Monad.State.Strict
import Noun
type Jet = Noun -> Noun
2019-09-24 02:10:24 +03:00
type JetName = Atom
2019-09-23 08:17:16 +03:00
type Hash = Int
2019-09-24 02:10:24 +03:00
pattern FastAtom = 9999
pattern FastHint id n =
C (A 11)
(C
(C (A FastAtom) (C (A 1) (A id)))
n)
-- | A context in which to run nock which supports jet lookup.
2019-09-23 08:17:16 +03:00
class Monad m => Dashboard m where
2019-09-24 02:10:24 +03:00
-- | Find the jet associated with the formula represented by the given noun,
-- if any.
2019-09-23 08:17:16 +03:00
match :: Noun -> m (Maybe Jet)
2019-09-24 02:10:24 +03:00
-- | A dashboard which doesn't jet.
2019-09-23 08:17:16 +03:00
newtype Freeboard a = Freeboard (Identity a)
deriving newtype Functor
deriving newtype Applicative
deriving newtype Monad
2019-09-24 02:10:24 +03:00
-- | A dashboard which looks for jets by formula hash
2019-09-23 08:17:16 +03:00
newtype Hashboard a = Hashboard (Identity a)
deriving newtype Functor
deriving newtype Applicative
deriving newtype Monad
2019-09-24 02:10:24 +03:00
-- | A dashboard which checks the head of formulas for "fast
-- hints" and uses the name contained in such a hint to look for jets.
newtype Fastboard a = Fastboard (Identity a)
2019-09-23 08:17:16 +03:00
deriving newtype Functor
deriving newtype Applicative
deriving newtype Monad
2019-09-24 02:10:24 +03:00
-- | A dashboard which uses both lookup strategies, checking for consistency
-- between them and that each fast hint is applied to a unique formula.
-- Violations of these principles are written to standard out.
newtype Careboard a = Careboard (StateT (HashMap JetName Noun) IO a)
2019-09-23 08:17:16 +03:00
deriving newtype Functor
deriving newtype Applicative
deriving newtype Monad
runFree :: Freeboard a -> a
runFree (Freeboard x) = runIdentity x
runHash :: Hashboard a -> a
runHash (Hashboard x) = runIdentity x
runFast :: Fastboard a -> a
2019-09-24 02:10:24 +03:00
runFast (Fastboard x) = runIdentity x
runCare :: Careboard a -> IO a
runCare (Careboard x) = evalStateT x mempty
2019-09-23 08:17:16 +03:00
instance Dashboard Freeboard where
2019-09-24 02:10:24 +03:00
match _ = Freeboard $ pure Nothing
2019-09-23 08:17:16 +03:00
instance Dashboard Hashboard where
2019-09-24 02:10:24 +03:00
match = Hashboard . pure . byHash . hash
instance Dashboard Fastboard where
match = Fastboard . \case
FastHint id n -> pure (byFast id)
_ -> pure Nothing
-- TODO maybe also detect hash collisions
instance Dashboard Careboard where
match = Careboard . \case
n@(FastHint nm _) -> case namely nm of
Just (h, j) -> do
when (h /= hash n) $
putStrLn ("careboard: jet " <> tshow nm <> " should have its hash "
<> "updated from " <> tshow h <> " to " <> tshow (hash n))
get <&> lookup nm >>= \case
Just n' ->
when (n' /= n) $
putStrLn ("careboard: jet hint " <> tshow nm <> " has been "
<> "detected on unequal formulae " <> tshow n
<> " and " <> tshow n' <> ", which is very bad")
Nothing -> modify' (insertMap nm n)
pure (Just j)
n -> pure $ byHash $ hash n
byFast :: JetName -> Maybe Jet
2019-09-23 08:17:16 +03:00
byFast = flip lookup fast
where
2019-09-24 02:10:24 +03:00
fast :: HashMap JetName Jet
fast = mapFromList $ map (\(n, _, j) -> (n, j)) jets
2019-09-23 08:17:16 +03:00
byHash :: Hash -> Maybe Jet
byHash = flip lookup hash
where
hash :: HashMap Hash Jet
2019-09-24 02:10:24 +03:00
hash = mapFromList $ map (\(_, h, j) -> (h, j)) jets
namely :: JetName -> Maybe (Hash, Jet)
namely = flip lookup fash
where
fash :: HashMap JetName (Hash, Jet)
fash = mapFromList $ map (\(n, h, j) -> (n, (h, j))) jets
2019-09-23 08:17:16 +03:00
2019-09-24 02:10:24 +03:00
type Entry = (JetName, Hash, Jet)
-- | Your jets here
2019-09-23 08:17:16 +03:00
jets :: [Entry]
jets =
2019-10-01 02:27:35 +03:00
[ (123, 0, \(A a) -> trace "jetting" $ A (a - 1))
2019-09-23 08:17:16 +03:00
]