Giter Site home page Giter Site logo

coq2hs-cpp-verify's Introduction

coq2hs-cpp-verify

Proof of concept test bench to code compare functions implemented in Haskell and C++.

Please visit the technical documentation.

requires haskell stack >= 1.9.8 and (gcc >= 5.0 or clang >= 3.8)

Quick Start

build

stack build

run sample application calling both Haskell and C++ functions

stack run

run property based tests and unit tests comparing results from Haskell and C++ functions

stack test

re-build documentation (stored in ./docs)

stack haddock

load application in GHCI and run functions interactively

stack ghci  --ghci-options='-fobject-code -O0'
> HsLib.isTriple 3 4 5
> True
> InlineCPP.isTriple 3 4 5
> 1

load a single file in GHCI, for example InlineCPP

stack ghci src/InlineCPP.hs --ghci-options='-fobject-code -O0'
> isTriple 3 4 5
> 1

Concept

Extract a Haskell function (hsFunction) from a formal Coq proof that represents the specification of an existing C++ function (cppFunction). The extracted Haskell function by definition will be verified as correct. This test bench represents an example of how to use Haskell, inline-c-cpp and the property based testing tool QuickCheck to verify that hsFunction and cppFunction produce identical results for the same inputs.

Simple Example

Haskell function

module HsLib ( square )

-- square a number
square :: Num a => a -> a
square x = x * x

C++ function

namespace cn {
    // square a number
    int square(int a) {
        return a * a;
    }
}

Haskell function calling cn::square as inline-cpp

module InlineCPP ( square )

-- inline call to c++ 
square :: CInt -> IO CInt
square x = [C.exp| int { cn::square($(int x)) } |]

Haskell QuickCheck test

module Main where

import           Control.Monad
import           Foreign.C.Types
import           Test.Hspec
import           Test.QuickCheck
import           Test.QuickCheck.Monadic
import qualified HsLib                         as HS
import qualified InlineCPP                     as CPP

-- quickCheck property comparing the c++ and Haskell results
prop_square :: Property
prop_square = forAll arbitrary $ \n -> monadicIO $ do
    nn <- run $ CPP.square (n :: CInt)
    assert $ nn == HS.square n

-- run test 10,000 times with random integers
main :: IO ()
main =
    hspec
        $ describe "Haskell checking CPP"
        $ it "quickcheck: HS.square a == CPP.square a"
        $ quickCheck (withMaxSuccess 10000 prop_square)

Run Test

$ stack test
Haskell checking CPP
+++ OK, passed 10000 tests.
  quickcheck: HS.square a == CPP.square a

coq2hs-cpp-verify's People

Contributors

roy-crippen avatar roycrippen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.