%{^
#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"

dataviewtype container = Container of (resource1, resource1)
typedef callback = (container) -<fun1> void
extern fun do_something(f: callback, arg: container):void = "mac#do_something"

fun mycallback(c: container): void = let
  val ~Container (r,r2) = c
  val () = resource_use(r);
  val () = resource_use(r2);
  val () = resource_free(r);
  val () = resource_free(r2);
in
  ()
end
 
implement main() = let
  val r = resource_new();
  val r2 = resource_new();

  val c = Container(r, r2)

  val () = do_something(mycallback, c)
in
  ()
end