Garciat
11/30/2015 - 2:50 AM

ffi.idr

module Main

%include C "stdlib.h"
%include C "string.h"
%include C "stdio.h"

-----

malloc : Int -> IO Int
malloc = foreign FFI_C "malloc" (Int -> IO Int)

memset : Int -> Int -> Int -> IO ()
memset = foreign FFI_C "memset" (Int -> Int -> Int -> IO ())

puts : String -> IO ()
puts = foreign FFI_C "puts" (String -> IO ())

puts_ptr : Int -> IO ()
puts_ptr = foreign FFI_C "puts" (Int -> IO ())

-----

instance Enum Char where
  toNat c   = toNat (ord c)
  fromNat n = chr (fromNat n)
  
  pred c = fromNat (pred (toNat c))

enumerate : List a -> List (Nat, a)
enumerate xs = zip [0..length xs] xs

-----

main : IO ()
main = do p <- malloc 50
          memset p 0 50
          for_ (enumerate ['a'..'z']) $ \ix => do
            let i = fst ix
            let x = snd ix
            memset (p + cast i) (cast x) 1
          puts_ptr p

-----