File: Counter.hs

package info (click to toggle)
haskell-copilot 3.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 140 kB
  • sloc: haskell: 452; makefile: 6
file content (32 lines) | stat: -rw-r--r-- 758 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- Copyright © 2019 National Institute of Aerospace / Galois, Inc.

-- | Example showing an implementation of a resettable counter.

{-# LANGUAGE RebindableSyntax #-}

module Main where

import Language.Copilot
import Copilot.Compile.C99

-- A resettable counter
counter :: Stream Bool -> Stream Bool -> Stream Int32
counter inc reset = cnt
  where
    cnt = if reset then 0
          else if inc then z + 1
               else z
    z = [0] ++ cnt

-- Counter that resets when it reaches 256
bytecounter :: Stream Int32
bytecounter = counter true reset
  where
    reset = counter true false `mod` 256 == 0

spec :: Spec
spec = trigger "counter" true [arg $ bytecounter]

main :: IO ()
-- main = interpret 1280 spec
main = reify spec >>= compile "counter"