[Assorted-commits] SF.net SVN: assorted: [263] sandbox/trunk/src
Brought to you by:
yangzhang
|
From: <yan...@us...> - 2008-01-20 06:41:46
|
Revision: 263
http://assorted.svn.sourceforge.net/assorted/?rev=263&view=rev
Author: yangzhang
Date: 2008-01-19 22:41:51 -0800 (Sat, 19 Jan 2008)
Log Message:
-----------
added stuff to sandbox
Added Paths:
-----------
sandbox/trunk/src/bash/zombie-processes/
sandbox/trunk/src/bash/zombie-processes/README
sandbox/trunk/src/bash/zombie-processes/child
sandbox/trunk/src/bash/zombie-processes/parent
sandbox/trunk/src/hs/PiCalc.hs
sandbox/trunk/src/hs/compilation/
sandbox/trunk/src/hs/compilation/HelloWorld.hs
sandbox/trunk/src/hs/compilation/Makefile
sandbox/trunk/src/tex/code-figure.tex
Added: sandbox/trunk/src/bash/zombie-processes/README
===================================================================
--- sandbox/trunk/src/bash/zombie-processes/README (rev 0)
+++ sandbox/trunk/src/bash/zombie-processes/README 2008-01-20 06:41:51 UTC (rev 263)
@@ -0,0 +1,5 @@
+Run `parent &` which forks `child` then becomes `sleep`. Keep an eye on `ps f`.
+
+`child` exits first, but since the `sleep` in `parent` does not query for its
+status (via `wait` or `waitpid`), `child` is left hanging as a `zombie` (until
+`parent` exits)
Added: sandbox/trunk/src/bash/zombie-processes/child
===================================================================
--- sandbox/trunk/src/bash/zombie-processes/child (rev 0)
+++ sandbox/trunk/src/bash/zombie-processes/child 2008-01-20 06:41:51 UTC (rev 263)
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+exec sleep 5
Property changes on: sandbox/trunk/src/bash/zombie-processes/child
___________________________________________________________________
Name: svn:executable
+ *
Added: sandbox/trunk/src/bash/zombie-processes/parent
===================================================================
--- sandbox/trunk/src/bash/zombie-processes/parent (rev 0)
+++ sandbox/trunk/src/bash/zombie-processes/parent 2008-01-20 06:41:51 UTC (rev 263)
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+./child &
+exec sleep 10
Property changes on: sandbox/trunk/src/bash/zombie-processes/parent
___________________________________________________________________
Name: svn:executable
+ *
Added: sandbox/trunk/src/hs/PiCalc.hs
===================================================================
--- sandbox/trunk/src/hs/PiCalc.hs (rev 0)
+++ sandbox/trunk/src/hs/PiCalc.hs 2008-01-20 06:41:51 UTC (rev 263)
@@ -0,0 +1,224 @@
+{-# OPTIONS -fglasgow-exts #-}
+module Main (main) where
+
+{-
+I didn't write this. Someone on #haskell dug up an old pi calc impl of
+theirs.
+-}
+
+import Control.Concurrent hiding (forkIO)
+import Control.Concurrent.MVar
+import Control.Concurrent.Chan
+import Control.Exception
+import Prelude hiding (putStrLn,(||),catch)
+import qualified Control.Concurrent (forkIO)
+import qualified Prelude (putStrLn)
+import System.IO.Unsafe (unsafePerformIO)
+import Data.Dynamic
+import Control.Monad (liftM)
+
+main = return ()
+
+globalLock :: MVar ()
+globalLock = unsafePerformIO $ newMVar ()
+
+putStrLn :: String -> IO ()
+putStrLn s = do
+ takeMVar globalLock
+ threadDelay 100
+ Prelude.putStrLn s
+ putMVar globalLock ()
+
+children :: MVar [MVar ()]
+children = unsafePerformIO (newMVar [])
+
+forkIO io = do
+ mvar <- newEmptyMVar
+ tID <- Control.Concurrent.forkIO (io `finally` putMVar mvar ())
+ childs <- takeMVar children
+ putMVar children (mvar:childs)
+ return tID
+
+waitForChildren = do
+ (mvar:mvars) <- takeMVar children
+ putMVar children mvars
+ takeMVar mvar
+ waitForChildren
+
+type Channel = Chan Dynamic
+
+{-# NOINLINE chanTyCon #-}
+chanTyCon = mkTyCon "Chan"
+
+{-# NOINLINE rationalTyCon #-}
+rationalTyCon = mkTyCon "Rational"
+
+instance Typeable Channel where
+ typeOf _ = mkAppTy chanTyCon [typeOf (undefined :: Dynamic)]
+
+instance Typeable Rational where
+ typeOf _ = mkAppTy rationalTyCon []
+
+instance Eq Dynamic where
+ a == b | Just (a :: Integer) <- fromDynamic a, Just b <- fromDynamic b = a == b
+ | Just (a :: Rational) <- fromDynamic a, Just b <- fromDynamic b = a == b
+ | otherwise = False
+
+instance Num Dynamic where
+ a + b = toDyn $ fromD a + (fromD b :: Integer)
+ a - b = toDyn $ fromD a - (fromD b :: Integer)
+ a * b = toDyn $ fromD a * (fromD b :: Integer)
+ negate a = toDyn $ negate (fromD a :: Integer)
+ signum a = toDyn $ signum (fromD a :: Integer)
+ abs a = toDyn $ abs (fromD a :: Integer)
+ fromInteger = toDyn
+
+instance Fractional Dynamic where
+ a / b = toDyn $ fromD a / (fromD b :: Rational)
+ recip a = toDyn $ recip (fromD a :: Rational)
+ fromRational = toDyn
+
+q :: Typeable a => a -> Dynamic
+q = toDyn
+
+
+p :: Typeable a => Dynamic -> a
+p = fromD
+
+(&) :: Typeable a => a -> [Dynamic] -> [Dynamic]
+infixr 9 &
+x & xs = toDyn x : xs
+
+fromD :: Typeable a => Dynamic -> a
+fromD d = case fromDynamic d of
+ Just a -> a
+ Nothing -> error ("type error: "++show d)
+
+newPrim :: ([Dynamic] -> IO a) -> IO Dynamic
+newPrim prim = do
+ chan <- newChan
+ forkIO (getChanContents chan >>= mapM_ (prim . fromD))
+ return (toDyn chan)
+
+newBinPrim :: (Typeable a, Typeable b, Typeable c) =>
+ (a -> b -> c) -> IO Dynamic
+newBinPrim (op :: a -> b -> c) = do
+ chan <- newChan
+ forkIO (getChanContents chan >>= mapM_ (prim . fromD))
+ return (toDyn chan)
+ where prim [a, b, out] = do
+ let x = (fromD a :: a) `op` (fromD b :: b) :: c
+ out![toDyn x]
+
+startOpsServices = do
+ add <- newBinPrim ((+) :: Integer -> Integer -> Integer)
+ sub <- newBinPrim ((-) :: Integer -> Integer -> Integer)
+ mul <- newBinPrim ((*) :: Integer -> Integer -> Integer)
+ div <- newBinPrim (div :: Integer -> Integer -> Integer)
+ return (add,sub,mul,div)
+
+startPrintService :: IO Dynamic -- print
+startPrintService = newPrim (\[s] -> putStrLn (fromD s))
+
+startPrintIntegerService :: Dynamic -> IO Dynamic -- printInteger
+startPrintIntegerService print
+ = liftM head $ new $ \printInteger -> printInteger?* \[i] -> print![q (show (p i :: Integer))]
+
+startEitherServices :: IO [Dynamic] -- [either,left,right]
+startEitherServices = new $ \either left right ->
+ (either?* \[l,r,lr] -> lr![l,r]) ||
+ (left?* \[v,out] -> new $ \l' -> (l'?* \[l,r] -> l![v]) || out![l']) ||
+ (right?* \[v,out] -> new $ \r' -> (r'?* \[l,r] -> r![v]) || out![r'])
+
+startRefServices :: IO [Dynamic] -- [newRef,emptyRef]
+startRefServices = new $ \newRef emptyRef ->
+ (newRef?* \[v,out] -> new $ \r ack ->
+ (r? \m@[get,put] -> put![v,ack] || ack? \[] -> out!m) ||
+ emptyRef![r]) ||
+ (emptyRef?* \[out] -> new $ \get put cell ->
+ (get?* \[out] -> cell? \[v] -> cell![v] || out![v]) ||
+ (put? \[v,ack] -> cell![v] || ack![] ||
+ (put?* \[v,ack] -> cell? \_ -> cell![v] || ack![])) ||
+ out![get,put])
+
+captureChannels :: (Dynamic -> IO a) -> IO [Dynamic]
+captureChannels p = do
+ chan <- newChan
+ forkIO (p (toDyn chan) >> return ())
+ cs <- readChan chan
+ return (fromD cs)
+
+-- P | Q
+-- use like p || q
+infixr 0 ||
+(||) :: IO a -> IO b -> IO ()
+p || q = forkIO (q >> return()) >> p >> return ()
+-- p || q = forkIO (p >> return()) >> forkIO (q >> return()) >> return ()
+
+-- new chan
+-- use like new $ \chan -> ...
+-- new :: (Channel -> IO a) -> IO Channel
+-- new p = do chan <- newChan; p chan; return chan
+
+--------------------------------------------------------------------------------
+{-
+
+ --Tips--
+
+ Functional Processes
+
+ let (x,y) = F(a,b) in P ==>
+ new $ \r -> (f![a,b,q r]) || (r? \[x,y] -> P)
+ e.g.
+ let x = add (a,b) in p![x]
+ new $ \r -> (add![a,b,r]) || (r? \[x] -> p![x])
+ or simplifying (tail-calling of sorts)
+ add![a,b,p]
+
+ Encoding Recursion
+
+ def X(a) = P;Y(b) = Q in W ==>
+ new $ \x y -> (x?* \[a] -> P) || (y?* \[b] -> Q) || W
+ e.g.
+ def inc(x) = printInteger![x] >> inc(x+1) in inc(0)
+ new $ \inc -> (inc?* \[x] -> printInteger![x] >> add![1,x,q inc]) ||
+ (inc![0])
+
+ "Higher-Order Processes"
+
+ x![P] || x? \[P] -> Q
+ (new $ \w -> (x![w]) || (w? \x -> P)) || (x? \[w] -> Q)
+ e.g.
+ x![new $ \y -> y? \x -> print!x] || (x? \[z] -> p z![q "foo"])
+ (new $ \y -> x![y] || y? \x -> print!x) || (x? \[z] -> p z[q "foo"]
+-}
+
+class NewChannel a where
+ new :: a -> IO [Dynamic]
+
+instance NewChannel (IO a) where
+ new p = forkIO (p >> return ()) >> return []
+
+instance (NewChannel a) => NewChannel (Dynamic -> a) where
+ new p = do chan <- newChan; chans <- new (p (toDyn (chan :: Channel))); return (toDyn chan:chans)
+
+-- f![arg1 arg2]
+-- use like f![q arg1, q arg2]
+infix 8 !
+(!) :: Dynamic -> [Dynamic] -> IO ()
+chan ! x = writeChan (fromD chan) (toDyn x)
+
+-- f?x = e
+-- use like chan? \x -> e
+infix 8 ?
+(?) :: Dynamic -> ([Dynamic] -> IO a) -> IO ()
+chan ? p = do v <- readChan (fromD chan); p (fromD v); return ()
+
+-- f?*x = e
+-- use like chan?* \x -> e
+infix 8 ?*
+(?*) :: Dynamic -> ([Dynamic] -> IO a) -> IO ()
+chan ?* p = getChanContents (fromD chan) >>= mapM_ (\x -> forkIO (p (fromD x) >> return()))
+
+--------------------------------------------------------------------------------
+
Added: sandbox/trunk/src/hs/compilation/HelloWorld.hs
===================================================================
--- sandbox/trunk/src/hs/compilation/HelloWorld.hs (rev 0)
+++ sandbox/trunk/src/hs/compilation/HelloWorld.hs 2008-01-20 06:41:51 UTC (rev 263)
@@ -0,0 +1,2 @@
+module Main where
+main = putStrLn "hello world!"
Added: sandbox/trunk/src/hs/compilation/Makefile
===================================================================
--- sandbox/trunk/src/hs/compilation/Makefile (rev 0)
+++ sandbox/trunk/src/hs/compilation/Makefile 2008-01-20 06:41:51 UTC (rev 263)
@@ -0,0 +1,14 @@
+all: HelloWorld
+ ghc -ddump-ds HelloWorld.hs
+
+HelloWorld: HelloWorld.hs
+ ghc -o HelloWorld HelloWorld.hs
+
+HelloWorld.s: HelloWorld.hs
+ ghc -S -o HelloWorld.s HelloWorld.hs
+
+HelloWorld.hc: HelloWorld.hs
+ ghc -C -o HelloWorld.hc HelloWorld.hs
+
+clean:
+ rm *.hi *.o *.out *.s *.hc HelloWorld
Added: sandbox/trunk/src/tex/code-figure.tex
===================================================================
--- sandbox/trunk/src/tex/code-figure.tex (rev 0)
+++ sandbox/trunk/src/tex/code-figure.tex 2008-01-20 06:41:51 UTC (rev 263)
@@ -0,0 +1,198 @@
+\documentclass{article}
+
+\usepackage{algorithm}
+\usepackage{amsmath}
+\usepackage{algorithmicx,algpseudocode}
+\usepackage{fancyvrb}
+\usepackage{listings}
+\usepackage{setspace}
+
+\newcommand{\proc}[1]{{\textsc{#1}}}
+\newcommand{\var}[1]{{\emph{#1}}}
+
+\begin{document}
+
+\subsection{Fancy Verbs}
+
+A demo of using fancy verbs.
+
+\SaveVerb{x}=another=
+This is a \Verb|fancy_vrb|. Here's \UseVerb{x}.
+
+\DefineShortVerb{\|}
+The function |camera_global_prioritization|.
+
+Fancier features:
+\begin{Verbatim}[frame=single, commandchars=\\\{\},
+ codes={\catcode`$=3 \catcode`^=7} %$
+]
+Simple math is fine, $ \frac{1}{\sqrt{z}}, but math becomes quirky!$
+\end{Verbatim}
+
+
+\begin{SaveVerbatim}{sql}
+SELECT a, b
+EVERY n [SECONDS]
+BUFFER IN buffername
+\end{SaveVerbatim}
+
+\UseVerbatim{sql}
+
+\begin{Verbatim}
+select * from r where a = 0;
+\end{Verbatim}
+
+%% The following don't work, despite the indirections of saving verbs
+%% and saving boxes.
+%%
+%% \fbox{\UseVerbatim{sql}}
+%%
+%% \newsavebox{\mybox}
+%%
+%% \begin{lrbox}{\mybox}
+%% \UseVerbatim{sql}
+%% \end{lrbox}
+%%
+%% \fbox{\usebox{\mybox}}
+
+\setbox0=\vbox{
+%\begin{minipage}
+\begin{verbatim}
+verbatim stuff
+\end{verbatim}
+%\end{minipage}
+}
+
+\fbox{\box0}
+
+\subsection{Math}
+
+Minipages in math:
+\begin{align}
+x = \begin{minipage}{1in}
+I'm in a minipage.
+\end{minipage}
+\end{align}
+
+\subsection{Algorithms}
+
+|algorithmicx| obsoletes |algorithmic|, and is the one I currently
+default to. Another worth considering is |algorithm2e|.
+
+See example figures.
+
+\begin{figure}
+\caption{Pseudocode for a global prioritization scheme.}
+\label{f:global}
+\fbox{
+\begin{minipage}{\textwidth}
+\begin{algorithmic}[1]
+\Procedure{CameraGlobalPrioritization}{\var{query points},
+ \var{summary grids}}
+\ForAll{$g \in \var{summary grids}$}
+\State $\var{scores}[g] \leftarrow
+\begin{cases}
+ 1, & \begin{minipage}{3in}
+ $\forall q \in \var{query points}$, no car previously
+ answered $q$ and $\proc{Distance}(\var{g.center}, q) < \var{threshold}$
+ \end{minipage} \\
+ 0, & \text{otherwise}
+\end{cases}$
+\EndFor \\
+\Return \var{summary grids}
+\EndProcedure
+\end{algorithmic}
+\end{minipage}
+}
+\end{figure}
+
+\begin{figure}
+\fbox{
+\begin{minipage}{\textwidth}
+\begin{algorithmic}[1]
+\Procedure{Bisect}{\var{tuples}}
+
+\State $\var{segs} \leftarrow \text{empty priority queue of segments,
+ ordered by segment length}$
+
+\State push \proc{New-Segment}(\var{tuples} sorted by time) onto
+\var{segs}
+
+\While{\proc{Not-Empty}(\var{segs})}
+
+\State $\var{seg} \leftarrow \proc{Pop-Min}(\var{segs})$
+
+\State add \proc{Midpoint}(\var{seg}) to output buffer
+
+\If{$\proc{Num-Tuples}(\var{seg}) > 1$}
+
+\State push \proc{Left}(\var{seg}) and \proc{Right}(\var{seg}) onto
+\var{segs}
+
+\EndIf
+\EndWhile
+\EndProcedure
+\end{algorithmic}
+\end{minipage}
+}
+\caption{Pseudocode for \texttt{bisect} delivery function.}
+\label{f:bisect}
+\end{figure}
+
+\subsection{Listings}
+
+I don't know how to change the font to something less ugly, escape to
+TeX, etc.
+
+\lstset{language=sql}
+
+\begin{lstlisting}
+SELECT * FROM r WHERE a = 0;
+\end{lstlisting}
+
+%\DeclareOption{singlespace}{
+% \typeout{Single spaced}
+% \def\mystretch{1}}
+%
+\newcommand{\sqlquery}[1]{{
+ % \setlength\baselinestretch{1.0}
+ \begin{samepage}
+ % \setlength\parskip{.1\baselineskip}
+ % \small
+ %
+ % \renewcommand\baselinestretch{1.0}
+ \hspace{0in}\parbox{\textwidth}{
+ \begin{singlespace}
+ \setlength\parindent{-.2in}
+ \texttt{#1}
+ \end{singlespace}
+ }
+ % \normalsize
+ % \renewcommand\baselinestretch{1.0}
+ \end{samepage}
+}}
+
+\sqlquery{
+SELECT * \\
+FROM my\_table \\
+WHERE ;
+}
+
+\subsection{Simple demo}
+
+This figure seems to work, but you better not uncomment the fbox!
+
+\begin{figure}
+% \fbox{
+\begin{minipage}{\textwidth}
+\begin{Verbatim}[frame=single, commandchars=\\\{\},
+ codes={\catcode`$=3\catcode`^=7}]
+hello, world! $ \forall x$
+\end{Verbatim}
+\end{minipage}
+% }
+\caption{Pseudocode for a global prioritization scheme.}
+\label{f:global}
+\end{figure}
+
+\end{document}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|