Permalink
Browse files

New examples...

  • Loading branch information...
1 parent 68cc043 commit 74fdab92dbc0c5a1a715cec54b16f8e249fe86d7 @leepike committed Jan 4, 2012
Showing with 264 additions and 0 deletions.
  1. +47 −0 Examples/Array.hs
  2. +109 −0 Examples/BadExtVars.hs
  3. +108 −0 Examples/Languages.hs
View
@@ -0,0 +1,47 @@
+--------------------------------------------------------------------------------
+-- Copyright © 2011 National Institute of Aerospace / Galois, Inc.
+--------------------------------------------------------------------------------
+
+-- | Example in sampling external arrays.
+
+{-# LANGUAGE RebindableSyntax #-}
+
+module Array ( array ) where
+
+import Language.Copilot hiding (cycle)
+import Data.List (cycle)
+--import qualified Copilot.Compile.C99 as C
+import qualified Copilot.Compile.SBV as S
+
+--------------------------------------------------------------------------------
+
+extArr :: Stream Word32
+extArr = externArray "arr1" arrIdx 5 (Just $ repeat [7,8,9,10,11])
+
+arrIdx :: Stream Word32
+arrIdx = [0] ++ (arrIdx + 1) `mod` 4
+
+arrIdx2 :: Stream Word8
+arrIdx2 = extern "idx2" (Just [0,0..])
+
+extArr2 :: Stream Word16 -> Stream Word32
+extArr2 idx = externArray "arr2" idx 4 (Just $ repeat [1,2,3,4])
+
+extArr3 :: Stream Word32
+extArr3 = extArr2 (cast $ externW8 "idx3" (Just $ cycle [0,1,2]))
+
+spec :: Spec
+spec = trigger "trigger" true [ arg extArr
+ , arg (extArr2 (cast arrIdx2))
+ , arg extArr3
+-- Throws an exception since the index is too big for the array!
+-- , arg (extArr2 5)
+ ]
+
+array :: IO ()
+array = do
+-- reify spec >>= C.compile C.defaultParams
+ interpret 10 spec
+ reify spec >>= S.compile S.defaultParams
+
+--------------------------------------------------------------------------------
View
@@ -0,0 +1,109 @@
+--------------------------------------------------------------------------------
+-- Copyright © 2011 National Institute of Aerospace / Galois, Inc.
+--------------------------------------------------------------------------------
+
+-- | Test bad typing on external variables is caught.
+
+{-# LANGUAGE RebindableSyntax #-}
+
+module BadExtVars ( badExtVars ) where
+
+import Language.Copilot
+
+--------------------------------------------------------------------------------
+
+x :: Stream Word8
+x = extern "x" (Just $ [0..])
+
+
+-- Should throw an error
+x' :: Stream Word16 -- Bad type!
+x' = extern "x" (Just $ [0..])
+
+spec0 :: Spec
+spec0 = trigger "trigger" true [ arg x
+ , arg x'
+ ]
+
+-- Should fail: "x" given two different types.
+interp0 :: IO ()
+interp0 = interpret 10 spec0
+
+--------------------------------------------------------------------------------
+
+y :: Stream Word16
+y = extern "y" (Just $ [0..])
+
+-- This is ok---indexes just different types. However, normally, the external
+-- array should be given exactly one environment---this will use a different
+-- array in different contexts.
+extArr0 :: Stream Word32
+extArr0 = externArray "arr" x 5 (Just $ repeat [7,8,9,10,11])
+extArr1 :: Stream Word32
+extArr1 = externArray "arr" y 5 (Just $ repeat [4,4,5,5,6])
+
+-- Both indexes grow to be too big for the array. The interpreter throws an
+-- error.
+spec1 :: Spec
+spec1 = trigger "trigger" true [ arg extArr0
+ , arg extArr1
+ ]
+
+interp1 :: IO ()
+interp1 = interpret 10 spec1
+
+--------------------------------------------------------------------------------
+
+-- Not Ok---saying "arr" is of a different type
+extArr2 :: Stream Word16
+extArr2 = externArray "arr" y 5 (Just $ repeat [7::Word16,8,9,10,11])
+
+spec2 :: Spec
+spec2 = trigger "trigger" true [ arg extArr2
+ , arg extArr1
+ ]
+
+-- Should fail---"arr" given two different types.
+interp2 :: IO ()
+interp2 = interpret 10 spec2
+
+--------------------------------------------------------------------------------
+
+-- Not Ok---different number of args.
+func0 :: Stream Bool
+func0 = externFun "func0" [funArg x] (Just $ x < 4)
+func1 :: Stream Bool
+func1 = externFun "func0" [funArg x, funArg y] (Just $ (cast x + y) > 10)
+
+spec3 :: Spec
+spec3 = trigger "trigger" true [ arg func0
+ , arg func1
+ ]
+
+-- Should fail---different number of args
+interp3 :: IO ()
+interp3 = interpret 10 spec3
+
+func2 :: Stream Bool
+func2 = externFun "func0" [funArg y] (Just $ y > 4)
+
+-- Should fail: func0 and func2 say that "func0" have different typed arguments.
+spec4 :: Spec
+spec4 = trigger "trigger" true [ arg func0, arg func2 ]
+
+interp4 :: IO ()
+interp4 =
+ interpret 10 spec4
+
+--------------------------------------------------------------------------------
+
+badExtVars :: IO ()
+badExtVars = do
+ interp0
+ interp1
+ interp2
+ interp3
+ interp4
+
+
+
View
@@ -0,0 +1,108 @@
+-- | Examples of parsing various languages. We'll assume input tokens come from
+-- an external variable. Assume the input doesn't given tokens outside the
+-- alphabet, and the result is always delayed by one w.r.t. the input stream.
+
+{-# LANGUAGE RebindableSyntax #-}
+
+module Languages where
+
+import Language.Copilot
+import qualified Prelude as P
+import Data.List (replicate)
+
+---------------------------------------------------------------------------------
+-- Regular expressions
+
+{-
+We'll build a Copilot program to accept the regular language over the alphabet
+{0,1} that contains an even number of 0s.
+-}
+
+reAccept :: Spec
+reAccept = do
+ observer "accept" accept
+ observer "string" string
+ where
+ accept :: Stream Bool
+ accept = [True] ++ if string == 0
+ then if accept then false
+ else true
+ else accept
+
+ -- Input tokens.
+ string :: Stream Word8
+ string = [0] ++ if string == 0 then 1 else 0
+
+-- interpret 10 reAccept
+
+---------------------------------------------------------------------------------
+
+---------------------------------------------------------------------------------
+-- Context-free Grammars
+
+{-
+This Copilot program recognizes <0^n 1^n>, for n >= 0.
+-}
+
+cfAccept :: Int -> Spec
+cfAccept n = do
+ observer "accept" accept
+ observer "string" string
+ where
+ accept :: Stream Bool
+ accept = if zerosSeen == 0
+ then true
+ else false
+
+ zerosSeen :: Stream Word64
+ zerosSeen = [0] ++ if string == 0
+ then zerosSeen + 1
+ else zerosSeen - 1
+
+ -- Input tokens.
+ string :: Stream Word8
+ string = replicate n 0 P.++ replicate n 1 ++ 0 -- don't care about part of
+ -- stream after ++
+
+-- interpret 40 (cfAccept 10)
+---------------------------------------------------------------------------------
+
+---------------------------------------------------------------------------------
+-- Context-sensitive Grammars
+
+{-
+This Copilot program recognizes <0^n 1^n 2^n>, for n >= 0.
+-}
+
+csAccept :: Int -> Spec
+csAccept n = do
+ observer "accept" accept
+ observer "string" string
+ where
+ accept :: Stream Bool
+ accept = if zerosSeen == 0 && onesSeen == 0
+ then true
+ else false
+
+ zerosSeen :: Stream Word64
+ zerosSeen = [0] ++ if string == 0
+ then zerosSeen + 1
+ else if string == 1
+ then zerosSeen - 1
+ else zerosSeen
+
+ onesSeen :: Stream Word64
+ onesSeen = [0] ++ if string == 1
+ then onesSeen + 1
+ else if string == 0
+ then onesSeen
+ else onesSeen - 1
+
+ -- Input tokens.
+ string :: Stream Word8
+ string = replicate n 0 P.++ replicate n 1 P.++ replicate n 2
+ ++ 0 -- don't care about part of
+ -- stream after ++
+
+-- interpret 40 (csAccept 5)
+---------------------------------------------------------------------------------

0 comments on commit 74fdab9

Please sign in to comment.