packages feed

code-conjure-0.4.0: eg/spec.hs

-- spec.hs: conjuring a function from a specification
--
-- Adapted from Colin Runciman's example "ListFuns"
import Conjure
import Prelude hiding (sum)


sumSpec :: Spec1 [Int] Int
sumSpec  =
  [ []      -= 0
  , [1,2]   -= 3
  , [3,4,5] -= 12
  ]

-- hoping for something like
-- sum xs  =  if null xs then 0 else head xs + sum (tail xs)

sumPrimitives :: [Prim]
sumPrimitives  =
  [ prim "null" (null :: [Int] -> Bool)
  , pr (0::Int)
  , prim "+"    ((+) :: Int -> Int -> Int)
  , prim "head" (head :: [Int] -> Int)
  , prim "tail" (tail :: [Int] -> [Int])
  ]


appSpec :: Spec2 [Int] [Int] [Int]
appSpec  =
  [ (,) []      [0,1]   -= [0,1]
  , (,) [2,3]   []      -= [2,3]
  , (,) [4,5,6] [7,8,9] -= [4,5,6,7,8,9]
  ]

-- hoping for something like
-- app xs ys = if null xs then ys else head xs : app (tail xs) ys

appPrimitives :: [Prim]
appPrimitives =
  [ prim "null" (null :: [Int] -> Bool)
  , prim ":"    ((:) :: Int -> [Int] -> [Int])
  , prim "head" (head :: [Int] -> Int)
  , prim "tail" (tail :: [Int] -> [Int])
  ]


main :: IO ()
main = do
  conjure1 "sum" sumSpec sumPrimitives
  conjure2 "++"  appSpec appPrimitives