Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] dynamically loading C functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: Re: [Caml-list] dynamically loading C functions

>I thought I could fix this by saying type 'a t = T of int | Tl of 'a t list, but that seems to constrain (@->) to  be 'a t -> 'a t -> 'a t and I lose the cruicial ('a -> 'b) t combinator (not sure if I'm using that word correctly)  and I get a type error.  Any idea how I could get it to nest?

Okay, I fixed this by introducing another type that didn't need to be parameterized (see below).  I guess I should have thought of that earlier.  Now my only problem is that I collapse int_t -> (int_t -> int_t) to int_t -> int_t -> int_t, which is correct for languages with currying, but C doesn't curry, so I need to keep those parens around to generate the right type.  Not sure how to do this, but I might be able to pull something fancy...gotta think about it.  Or, maybe I should left associate, which will make the return type last always, rather than first, and any time I come across a list on the right I should preserve it...

Chris

(* handles nested function types *)

module C:
    sig
      type 'a t
      val int_t   : int t
      val float_t : float t
      (* Begins with @ so it's right-associative :-) *)
      val (@->)   : 'a t -> 'b t -> ('a -> 'b) t
      val get_function : string -> 'a t -> 'a

      val print : 'a t -> unit
    end =
  struct
    type tt = T of int | Tl of tt list
    type 'a t = tt
    let int_t = T 1
    let float_t = T 2
    let (@->) a b = match a,b with 
      ((T _ as a),(T _ as b)) -> Tl [a;b]
    | ((T _ as a),Tl b) -> Tl (a :: b)
    | (((Tl _) as a),((Tl _) as b)) -> Tl [a;b]
    | (((Tl _) as a),(T _ as b)) -> Tl [a;b]
    let get_function s l = Obj.magic s (* just get it to compile *)
        
    let print el =
      let rec doprn el =
        match el with
          T a -> Printf.printf " %d " a
        | Tl a -> begin print_string " [ "; List.iter ~f:doprn a; print_string " ] " end
      in doprn el      
  end

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr