caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Problem with universal functions in a module
Date: Wed, 7 Jan 2015 16:30:09 +0100	[thread overview]
Message-ID: <20150107153009.GD17784@frosties> (raw)
In-Reply-To: <20150107135012.GA17784@frosties>

Somehow the example code got lost:

module M = struct
  type _ t = Int : int -> int t | Float : float -> float t
  let print : type a . a t -> unit = function
    | Int i -> Printf.printf "%d\n" i
    | Float f -> Printf.printf "%f\n" f
  (* should be abstract *)
  type box = Box : 'a t -> box
  let box t = Box t

  (*
  let call_unboxed fn (Box t) = fn t
  Error: This expression has type a#0 t but an expression was expected of type
         a#0 t
       The type constructor a#0 would escape its scope
  *)

  (* should be abstract *)
  type 'a helper = { fn : 'b . 'b t -> 'a }

  (*
  let helper fn = { fn }
  Error: This field value has type 'c t -> 'd which is less general than
         'b. 'b t -> 'a
  *)

  let call helper (Box t) = helper.fn t
end  

open M
let t = [box (Int 1); box (Float 2.0)]  

(* Works but is not possible if box is abstract *)
let () = List.iter (function Box x -> print x) t

(* Use conversion helper to call unboxed values with a function *)
let h = { fn = print }
let () = List.iter (call h) t

MfG
	Goswin

  reply	other threads:[~2015-01-07 15:30 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-07 13:50 Goswin von Brederlow
2015-01-07 15:30 ` Goswin von Brederlow [this message]
2015-01-07 17:26 ` Jeremy Yallop
2015-01-08  9:45   ` Ben Millwood
2015-01-08 15:21     ` Goswin von Brederlow
2015-01-08 16:25       ` Leo White
2015-01-09  1:02         ` Jacques Garrigue
2015-01-10 18:02           ` Goswin von Brederlow
2015-01-10 17:52         ` Goswin von Brederlow
2015-01-10 18:49           ` Leo White
2015-01-12 14:28             ` Goswin von Brederlow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150107153009.GD17784@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).