On Mon, Apr 09, 2012 at 07:37:48AM +0200, Cedric Cellier wrote:
> > - Chung-chieh Shan and Oleg's "Lightweight Static Capabilities"
> > presents several examples of phantom types and a general design method
> > to use them to enhance program safety:
> >   http://okmij.org/ftp/papers/lightweight-static-capabilities.pdf
> 
> I attempted to read this one out of curiosity,
> and it raised a question: is there a way to write
> these unsafe functions (List.Unsafe.tail/head...) in pure OCaml?

It depends on what you call "pure OCaml".  Using the evil Obj module:

let head (l : 'a list) : 'a = Obj.obj (Obj.field (Obj.repr l) 0) ;;

which relies on the underlying representation (use at your own risks) and
generates a call to caml_array_unsafe_get:
    L1: const 0
        push
        acc 1
        ccall caml_array_unsafe_get, 2
        return 1

Similarly:
let tail (l : 'a list) : 'a list = Obj.obj (Obj.field (Obj.repr l) 1) ;;

Best,
-- 
Gabriel

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to