Skip to content

Commit 276c498

Browse files
committed
copilot-verifier demo from YouTube video
This is the code from a demo for a video that I recorded. I have found myself using it to introduce people to the code, so we ought to just merge this for future use.
1 parent c6eef4a commit 276c498

File tree

9 files changed

+385
-0
lines changed

9 files changed

+385
-0
lines changed

cabal.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
packages:
88
copilot-verifier/
9+
copilot-verifier-demo/
910

1011
deps/copilot/copilot/
1112
deps/copilot/copilot-core/

copilot-verifier-demo/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
demo1.c
2+
demo1.h
3+
demo1_types.h
4+
demo1-driver
5+
demo1-driver.exe
6+
7+
demo2.c
8+
demo2.h
9+
demo2_types.h
10+
demo2-driver
11+
demo2-driver.exe
12+
13+
demo3.c
14+
demo3.h
15+
demo3_types.h

copilot-verifier-demo/LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2021 Galois Inc.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions
6+
are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in
13+
the documentation and/or other materials provided with the
14+
distribution.
15+
16+
* Neither the name of Galois, Inc. nor the names of its contributors
17+
may be used to endorse or promote products derived from this
18+
software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21+
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24+
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Cabal-version: 2.2
2+
Name: copilot-verifier-demo
3+
Version: 0.0
4+
Author: Galois Inc.
5+
Maintainer: rscott@galois.com
6+
Copyright: (c) Galois, Inc 2022
7+
License: BSD-3-Clause
8+
License-file: LICENSE
9+
Build-type: Simple
10+
Category: Language
11+
Synopsis: Demo programs for copilot-verifier
12+
Description:
13+
14+
common bldflags
15+
ghc-options: -Wall
16+
-Werror=incomplete-patterns
17+
-Werror=missing-methods
18+
-Werror=overlapping-patterns
19+
-Wpartial-fields
20+
-Wincomplete-uni-patterns
21+
ghc-prof-options: -O2 -fprof-auto-top
22+
default-language: Haskell2010
23+
default-extensions:
24+
NondecreasingIndentation
25+
build-depends:
26+
base,
27+
copilot,
28+
copilot-c99,
29+
copilot-core,
30+
copilot-libraries,
31+
copilot-language,
32+
copilot-theorem,
33+
copilot-verifier
34+
35+
library
36+
import: bldflags
37+
38+
hs-source-dirs: src
39+
exposed-modules:
40+
Demo1
41+
Demo2
42+
Demo3
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include "demo1.h"
4+
5+
#define ITERATIONS 6
6+
7+
void even(uint32_t even_arg0) {
8+
printf("Even Fibonacci number: %d\n", even_arg0);
9+
}
10+
11+
void odd(uint32_t odd_arg0) {
12+
printf("Odd Fibonacci number: %d\n", odd_arg0);
13+
}
14+
15+
int main(void) {
16+
for (int i = 0; i < ITERATIONS; i++) {
17+
step();
18+
}
19+
return 0;
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include "demo2.h"
4+
5+
uint8_t temperature;
6+
7+
#define ITERATIONS 4
8+
uint8_t temperature_vals[ITERATIONS] = { 100, 120, 120, 120 };
9+
10+
void heaton(float heaton_arg0) {
11+
printf("Temperature below 18.0: %f\n", heaton_arg0);
12+
}
13+
14+
void heatoff(float heatoff_arg0) {
15+
printf("Temperature above 21.0: %f\n", heatoff_arg0);
16+
}
17+
18+
int main(void) {
19+
for (int i = 0; i < ITERATIONS; i++) {
20+
temperature = temperature_vals[i];
21+
step();
22+
}
23+
return 0;
24+
}

copilot-verifier-demo/src/Demo1.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- A simple example involving a stream of Fibonacci numbers.
2+
3+
{-# LANGUAGE RebindableSyntax #-}
4+
module Demo1 (main) where
5+
6+
import qualified Prelude as P ()
7+
8+
import Language.Copilot
9+
import Copilot.Compile.C99
10+
import Copilot.Verifier
11+
12+
fibs :: Stream Word32
13+
fibs = [1, 1] ++ (fibs + drop 1 fibs)
14+
15+
evenStream :: Stream Word32 -> Stream Bool
16+
evenStream n = (n `mod` constant 2) == constant 0
17+
18+
oddStream :: Stream Word32 -> Stream Bool
19+
oddStream n = not (evenStream n)
20+
21+
spec :: Spec
22+
spec = do
23+
trigger "even" (evenStream fibs) [arg fibs]
24+
trigger "odd" (oddStream fibs) [arg fibs]
25+
26+
main :: IO ()
27+
main = do
28+
interpret 6 spec
29+
30+
-- spec' <- reify spec
31+
-- compile "demo1" spec'
32+
-- verify mkDefaultCSettings [] "demo1" spec'

copilot-verifier-demo/src/Demo2.hs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-- This is a simple example with basic usage. It implements a simple home
2+
-- heating system: It heats when temp gets too low, and stops when it is high
3+
-- enough. It read temperature as a byte (range -50C to 100C) and translates
4+
-- this to Celcius.
5+
6+
{-# LANGUAGE RebindableSyntax #-}
7+
module Demo2 (main) where
8+
9+
import qualified Prelude as P ()
10+
11+
import Language.Copilot
12+
import Copilot.Compile.C99
13+
import Copilot.Verifier
14+
15+
-- External temperature as a byte, range of -50C to 100C
16+
temp :: Stream Word8
17+
temp = extern "temperature"
18+
Nothing
19+
-- (Just [100, 120, 120, 120])
20+
21+
-- Calculate temperature in Celcius.
22+
-- We need to cast the Word8 to a Float. Note that it is an unsafeCast, as there
23+
-- is no direct relation between Word8 and Float.
24+
ctemp :: Stream Float
25+
ctemp = (unsafeCast temp * constant (150.0/255.0)) - constant 50.0
26+
27+
-- Compute the sliding average of the last 5 temps
28+
-- (Here, 19.5 is the average of 18.0 and 21.0, the two temperature extremes
29+
-- that we check for in the spec.)
30+
avgTemp :: Stream Float
31+
avgTemp = (sum 5 (replicate 5 19.5 ++ ctemp)) / fromIntegral 5
32+
33+
spec :: Spec
34+
spec = do
35+
trigger "heaton" (avgTemp < 18.0) [arg avgTemp]
36+
trigger "heatoff" (avgTemp > 21.0) [arg avgTemp]
37+
38+
main :: IO ()
39+
main = do
40+
-- interpret 4 spec
41+
42+
spec' <- reify spec
43+
compile "demo2" spec'
44+
verify mkDefaultCSettings [] "demo2" spec'

0 commit comments

Comments
 (0)