From: Mikhail Mandrykin <mandrykin@ispras.ru>
To: caml-list@inria.fr, Reed Wilson <cedilla@gmail.com>
Cc: Leo White <leo@lpw25.net>, Jeremy Yallop <yallop@gmail.com>
Subject: Re: [Caml-list] Type generalization confusion
Date: Wed, 10 May 2017 16:29:06 +0300 [thread overview]
Message-ID: <99634372.gpbQ14KDAs@molnar> (raw)
In-Reply-To: <CALLFq5S5pFRtE2Rat5KAwgbmrXpfix5CxxhSFP_3u=CKF1rVtQ@mail.gmail.com>
On вторник, 9 мая 2017 г. 12:56:24 MSK Reed Wilson wrote:
> The current issue I'm actually facing is trying to create a lookup table
> for these templates, which only differ by the length of some of the fields.
> For example:
>
> let lookup_array = [| String (0, End); String (1, End) |]
> let lookup_fun i = String (i, End)
Since what's needed here is actually not a generalized array type
( 'a. ((string -> 'a, 'a) field array) ),
but the generalized type of the element
( ('a. (string -> 'a, 'a) field) array ),
this could be work-around by introducing an intermediate universal type:
type (_, _) field =
| Int8 : ('a, 'b) field -> ((int -> 'a),'b) field
| String : int * ('a, 'b) field -> ((string -> 'a), 'b) field
| End : ('b, 'b) field
type 'a lookup_param = { f : 'b. ('a -> 'b, 'b) field } [@@unboxed]
let lookup_array = [| { f = String (0, End) }; { f = String (1, End) } |]
let lookup i = match lookup_array.(i) with { f } -> f
let of_chan : ('a, 'b) field -> 'a -> in_channel -> 'b = fun _ _ _ -> assert
false
let x = of_chan (lookup 0) (fun s -> int_of_string s) stdin
let y = of_chan (lookup 0) (fun s -> s) stdin
[@@unboxed] should have allowed this without any penalty of using an
intermediate wrapper, but currently it fails:
Fatal error: exception File "bytecomp/typeopt.ml", line 97, characters 6-12:
Assertion failed
(Typeopt.classify doesn't support universal types).
But even without [@@unboxed] this at least allows to avoid allocation of
`String (i, End)` inside the loop.
--
Mikhail
prev parent reply other threads:[~2017-05-10 13:29 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-08 18:32 Reed Wilson
2017-05-09 3:49 ` Gabriel Scherer
2017-05-09 5:43 ` Reed Wilson
2017-05-09 8:54 ` Leo White
2017-05-09 19:56 ` Reed Wilson
2017-05-09 20:54 ` Jeremy Yallop
2017-05-10 18:14 ` Reed Wilson
2017-05-10 13:29 ` Mikhail Mandrykin [this message]
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=99634372.gpbQ14KDAs@molnar \
--to=mandrykin@ispras.ru \
--cc=caml-list@inria.fr \
--cc=cedilla@gmail.com \
--cc=leo@lpw25.net \
--cc=yallop@gmail.com \
/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