packages feed

phino-0.0.148 / src / Tau.hs

{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Tau (seedTaus, freshTau, tausOf) whereimport ASTimport Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef, writeIORef)import Data.Set (Set)import qualified Data.Set as Setimport Data.Text (Text)import qualified Data.Text as Timport GHC.IO (unsafePerformIO)taus :: IORef (Set Text, Int){-# NOINLINE taus #-}taus = unsafePerformIO (newIORef (Set.empty, 0))seedTaus :: Expression -> IO ()seedTaus expr = writeIORef taus (exprLabels expr, 0)freshTau :: IO TextfreshTau = atomicModifyIORef' taus advance  where    advance (taken, cursor) =      let (minted, idx) = mint taken cursor       in ((Set.insert minted taken, idx + 1), minted)tausOf :: Int -> IO (IO Text)tausOf entry = do  (taken, _) <- readIORef taus  own <- newIORef (taken, 0)  pure (atomicModifyIORef' own advance)  where    advance :: (Set Text, Int) -> ((Set Text, Int), Text)    advance (taken, cursor) =      let (minted, idx) = mint' (T.pack ("a🌵" <> show entry <> "-")) taken cursor       in ((Set.insert minted taken, idx + 1), minted)mint :: Set Text -> Int -> (Text, Int)mint = mint' "a🌵"mint' :: Text -> Set Text -> Int -> (Text, Int)mint' stem taken idx  | name `Set.member` taken = mint' stem taken (idx + 1)  | otherwise = (name, idx)  where    name :: Text    name = stem <> T.pack (show idx)exprLabels :: Expression -> Set TextexprLabels (ExFormation bds) = Set.unions (map bindingLabels bds)exprLabels (ExApplication expr arg) = exprLabels expr <> argumentLabels argexprLabels (ExDispatch expr attr) = exprLabels expr <> attrLabel attrexprLabels (ExPhiMeet _ _ expr) = exprLabels exprexprLabels (ExPhiAgain _ _ expr) = exprLabels exprexprLabels _ = Set.emptybindingLabels :: Binding -> Set TextbindingLabels (BiTau attr expr) = attrLabel attr <> exprLabels exprbindingLabels (BiVoid attr) = attrLabel attrbindingLabels _ = Set.emptyargumentLabels :: Argument -> Set TextargumentLabels (ArTau attr expr) = attrLabel attr <> exprLabels exprargumentLabels (ArAlpha _ expr) = exprLabels exprattrLabel :: Attribute -> Set TextattrLabel (AtLabel label) = Set.singleton labelattrLabel _ = Set.empty