%{^
#include <stdio.h>

void* resource_new();
void resource_free(void* r);
void do_something(void (*f)(void* r), void* arg);
void resource_use(void* r);
%}

%{
void* resource_new() { return ATS_MALLOC(10); }
void resource_use(void* r) { printf("using: %p\n", r); }
void resource_free(void* r) { ATS_FREE(r); }
void do_something(void (*f)(void*), void* arg) { printf("a\n"); f(arg); printf("b\n"); }
%}

absviewtype resource (l:addr)
viewtypedef resource1 = [l:addr | l > null] resource l
extern fun resource_new():resource1 = "mac#resource_new"
extern fun resource_use(r: !resource1):void = "mac#resource_use"
extern fun resource_free(r: resource1):void = "mac#resource_free"
typedef callback = (!resource1) -<fun1> void
extern fun do_something(f: callback, arg: !resource1):void = "mac#do_something"

implement main() = let
  val r = resource_new();
  val () = do_something(lam (r) => resource_use(r), r) 
  val () = resource_free(r);
in
  ()
end