Skip to content

Instantly share code, notes, and snippets.

@cr1901
Last active August 29, 2015 14:21
Show Gist options
  • Save cr1901/fbf6cb49cb0eeb6ba949 to your computer and use it in GitHub Desktop.
Save cr1901/fbf6cb49cb0eeb6ba949 to your computer and use it in GitHub Desktop.
Ivory to executable sample with Makefile
#include <stdio.h>
#include <stdlib.h>
#include "fib_tutorial.h"
int main(int argc, char * argv[])
{
if(argc < 2)
{
printf("Please provide an input number!\n");
return -1;
}
printf("fib(%d) is %d\n", atoi(argv[1]), fib_loop(atoi(argv[1])));
return 0;
}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
import Ivory.Language
import qualified Ivory.Compile.C.CmdlineFrontend as C (compile)
fib_loop :: Def ('[Ix 1000] :-> Uint32)
fib_loop = proc "fib_loop" $ \ n -> body $ do
a <- local (ival 0)
b <- local (ival 1)
n `times` \ _ -> do
a' <- deref a
b' <- deref b
store a b'
store b (a' + b')
result <- deref a
ret result
fib_tutorial_module :: Module
fib_tutorial_module = package "fib_tutorial" $ do
incl fib_loop
main :: IO ()
main = C.compile [ fib_tutorial_module ]
HC=ghc
CFLAGS=-std=c99 -DIVORY_TEST
.c.o:
$(CC) $(CFLAGS) -c -o $@ $*.c
fib.out: fib_tutorial.o fib_main.o
$(CC) $(LDFLAGS) -o fib.out fib_main.o fib_tutorial.o
fib_main.o: fib_tutorial.c fib_tutorial.h ivory.h ivory_asserts.h
fib_tutorial.o: fib_tutorial.c fib_tutorial.h ivory.h ivory_asserts.h
fib_tutorial.c: fibonacci
./fibonacci
fibonacci:
$(HC) fibonacci.hs
clean:
$(RM) *.o fib.out fibonacci *.h *.hi fib_tutorial.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment