<commit_before>{-# LANGUAGE MultiParamTypeClasses,  
             FlexibleInstances, FlexibleContexts,
             GADTs, 
             TypeFamilies #-} 

{- <NAME> 2012

   Notes:
    2013-01-08: Removed number-of-blocks field from Distribs
    2012-12-10: Drastically shortened. 
-}

module Obsidian.Array  where

import Obsidian.Exp 
import Obsidian.Types
import Obsidian.Globs
import Obsidian.Program


import Data.List
import Data.Word

---------------------------------------------------------------------------
-- A value that can not be used in further computations
--------------------------------------------------------------------------- 
data Final a = Final {cheat :: a} -- cheat should not be exposed. 


---------------------------------------------------------------------------
-- Global result array. 
---------------------------------------------------------------------------

data GlobPush a =
  GlobPush (( a -> Exp Word32 -> TProgram ()) -> GProgram ())

--data GlobPush2 a =
--  GlobPush2 Word32
--        ((a -> Exp Word32 -> Exp Word32 -> TProgram ()) ->
--         GProgram ())


-- Conversions between kinds of global push arrays 
--globView :: GlobPush2 a -> GlobPush a
--globView (GlobPush2 n pushf) = GlobPush n pushf'
-- where
--    pushf' wf = pushf $ \a bix tix -> wf a (bix * fromIntegral n + tix)

--blockView :: GlobPush a -> GlobPush2 a
--blockView (GlobPush n pushf) = GlobPush2 n pushf'
--  where
--    pushf' wf = pushf $ \a gix -> wf a (gix `div` fromIntegral n)
--                                       (gix `mod` fromIntegral n) 


---------------------------------------------------------------------------
-- Experiment
---------------------------------------------------------------------------
data GlobPull a = GlobPull (Exp Word32 -> a)


-- Takes a block id and gives you what that block computes. 
data DistPull a = DistPull (Exp Word32 -> BProgram a)

undist :: DistPull (Pull a) -> GlobPush a
undist (DistPull bixf) =
  GlobPush $ \wf ->
  ForAllBlocks $ \bix ->
  do
    pully <- bixf bix
    let n = len pully 
    ForAll (Just n) $ \tix ->
      wf (pully ! (bix * fromIntegral n + tix))
         (bix * fromIntegral n + tix) 
                          

-- replaces Distrib ? 
-- data GlobPull2 a = GlobPull2 Word32 (Exp Word32 -> Exp Word32 -> a)

-- Create global pull arrays 
undefinedGlobal = GlobPull $ \gix -> undefined
namedGlobal name = GlobPull $ \gix -> index name gix


--sizedGlobal2 bs = GlobPull2 bs $ \bix tix -> undefined
--namedGlobal2 name bs = GlobPull2 bs
--                       $ \gix tix -> index name (gix * fromIntegral bs + tix) 

---------------------------------------------------------------------------
-- Push and Pull arrays
---------------------------------------------------------------------------
data Push a = Push Word32
                   ((a -> Exp Word32 -> TProgram ()) -> BProgram ())   

data Pull a = Pull {pullLen :: Word32, 
                    pullFun :: Exp Word32 -> a}
<commit_msg>fixed bug in undist. Still not sure if DistPull is the way to go<commit_after>{-# LANGUAGE MultiParamTypeClasses,  
             FlexibleInstances, FlexibleContexts,
             GADTs, 
             TypeFamilies #-} 

{- <NAME> 2012

   Notes:
    2013-01-08: Removed number-of-blocks field from Distribs
    2012-12-10: Drastically shortened. 
-}

module Obsidian.Array  where

import Obsidian.Exp 
import Obsidian.Types
import Obsidian.Globs
import Obsidian.Program


import Data.List
import Data.Word

---------------------------------------------------------------------------
-- A value that can not be used in further computations
--------------------------------------------------------------------------- 
data Final a = Final {cheat :: a} -- cheat should not be exposed. 


---------------------------------------------------------------------------
-- Global result array. 
---------------------------------------------------------------------------

data GlobPush a =
  GlobPush (( a -> Exp Word32 -> TProgram ()) -> GProgram ())

--data GlobPush2 a =
--  GlobPush2 Word32
--        ((a -> Exp Word32 -> Exp Word32 -> TProgram ()) ->
--         GProgram ())


-- Conversions between kinds of global push arrays 
--globView :: GlobPush2 a -> GlobPush a
--globView (GlobPush2 n pushf) = GlobPush n pushf'
-- where
--    pushf' wf = pushf $ \a bix tix -> wf a (bix * fromIntegral n + tix)

--blockView :: GlobPush a -> GlobPush2 a
--blockView (GlobPush n pushf) = GlobPush2 n pushf'
--  where
--    pushf' wf = pushf $ \a gix -> wf a (gix `div` fromIntegral n)
--                                       (gix `mod` fromIntegral n) 


---------------------------------------------------------------------------
-- Experiment
---------------------------------------------------------------------------
data GlobPull a = GlobPull (Exp Word32 -> a)


-- Takes a block id and gives you what that block computes. 
data DistPull a = DistPull (Exp Word32 -> BProgram a)

undist :: DistPull (Pull a) -> GlobPush a
undist (DistPull bixf) =
  GlobPush $ \wf ->
  ForAllBlocks $ \bix ->
  do
    pully <- bixf bix
    let n = len pully 
    ForAll (Just n) $ \tix ->
      wf (pully ! tix)
         (bix * fromIntegral n + tix) 
                          

-- replaces Distrib ? 
-- data GlobPull2 a = GlobPull2 Word32 (Exp Word32 -> Exp Word32 -> a)

-- Create global pull arrays 
undefinedGlobal = GlobPull $ \gix -> undefined
namedGlobal name = GlobPull $ \gix -> index name gix


--sizedGlobal2 bs = GlobPull2 bs $ \bix tix -> undefined
--namedGlobal2 name bs = GlobPull2 bs
--                       $ \gix tix -> index name (gix * fromIntegral bs + tix) 

---------------------------------------------------------------------------
-- Push and Pull arrays
---------------------------------------------------------------------------
data Push a = Push Word32
                   ((a -> Exp Word32 -> TProgram ()) -> BProgram ())   

data Pull a = Pull {pullLen :: Word32, 
                    pullFun :: Exp Word32 -> a}

{'fraction_non_alphanumeric': 0.28958749168330006, 'fraction_numerical': 0.01929474384564205, 'mean_word_length': 1.1849563953488371, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1153284', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1613, 'n_words': 554}