packages feed

sifflet-2.3.0: tests/Testing/Unit/FunctionExamples.hs

-- | Example functions for testing simplification of expressions
-- (removing parentheses).

module Testing.Unit.FunctionExamples
    (f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14
    , f15, f16)

where

import Data.Number.Sifflet
import Language.Sifflet.Expr

fun :: String -> [String] -> Expr -> Function
fun fname fargs body =
    Function (Just fname) undefined undefined -- undef arg and ret types 
             (Compound fargs body)

sym :: String -> Expr
sym name = ESymbol (Symbol name)

int :: Integer -> Expr
int n = ENumber (Exact n)

-- | The type of a function with two arguments
type F2 = Expr -> Expr -> Expr

-- | Builds a function call with two arguments,
-- which will be converted to EOp expressions for 
-- Haskell or Python
call2 :: String -> F2
call2 name left right = ECall (Symbol name) [left, right]


plus, minus, times, eq, lt :: F2

plus = call2 "+"
minus = call2 "-"
times = call2 "*"
eq = call2 "=="
lt = call2 "<"

f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14 :: Function

f1 = fun "foo" [] (int 1)

f2 = fun "bar" ["a", "b", "c"]
     (plus (times (sym "a") (sym "b"))
           (times (sym "c") (int 2)))

f3 = fun "cup" ["a", "b", "c"]
     (times (plus (sym "a") (sym "b"))
            (plus (sym "c") (int 2)))

f4 = fun "dook" ["a", "b", "c"]
     (plus (sym "a")
           (times (sym "b") (sym "c")))

f5 = fun "egg" ["x", "y", "z"]
         (plus (plus (sym "x") (sym "y"))
               (EIf (lt (sym "x") (sym "y"))
                    (plus (sym "z") (int 7))
                    (plus (int 1)
                          (times (sym "z") (sym "z")))))

f6 = fun "hen" ["x", "y", "z"]
         (plus (plus (sym "x") (sym "y"))
               (plus (sym "z")
                     (EIf (lt (sym "x") (sym "y"))
                          (int 7)
                          (int 16))))

f7 = fun "fact" ["n"]
     (EIf (eq (sym "n") (int 0))
          (int 1)
          (times (sym "n") 
                 (ECall (Symbol "fact") [minus (sym "n") (int 1)])))

f8 = fun "glob" ["x", "y", "z"]
         (plus (sym "x")
               (plus (sym "y") (sym "z")))

f9 = fun "hack" ["x", "y", "z"]
         (minus (sym "x")
                (minus (sym "y") (sym "z")))

f10 = fun "icarus" ["x", "y", "z"]
          (minus (minus (sym "x") (sym "y"))
                 (sym "z"))

f11 = fun "jessie" ["x", "y", "z"]
           (plus (minus (sym "x") (sym "y"))
                 (sym "z"))

f12 = fun "kicks" ["x", "y", "z"]
          (plus (sym "x")
                 (minus (sym "y") (sym "z")))

f13 = fun "lazy" ["x", "y", "z"]
           (minus (plus (sym "x") (sym "y"))
                  (sym "z"))

f14 = fun "mental" ["x", "y", "z"]
           (minus (sym "x")
                  (plus (sym "y") (sym "z")))


-- A couple more for consing

cons :: F2
cons = call2 ":"

f15, f16 :: Function

f15 = fun "cons3" ["x", "y", "z"]
      (cons (sym "x")
            (cons (sym "y")
                  (cons (sym "z")
                        (EList []))))

f16 = fun "add_cons" ["x", "y", "z"]
      (cons (plus (sym "x") (sym "y"))
            (cons (sym "z") 
                  (EList [])))