Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: checker@d6.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] How to read three integers from a text-file...  ?
Date: Thu, 02 May 2002 17:48:08 +0900	[thread overview]
Message-ID: <20020502174808V.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20020426211632T.garrigue@kurims.kyoto-u.ac.jp>

> Exercise to the interested reader:
> Define a function which, given a string and a concrete representation
> of its format type, checks whether the string conforms to the type.
> To make it more useful, the concrete representation used should
> include the format type in its type, using phantom types.
> 
> val check_format : ('a, 'b) format_type -> string -> bool
> val make_format : ('a, 'b) format_type -> string -> ('a, 'c, 'b) format
> 
> # make_format (arr string (arr int (arr int null))) "Hello %s, it's %d:%d.\n";;
> val fmt : (string -> int -> int -> 'a, 'b, 'a) format = <abstr>

Since nobody answered to the challenge, here is a solution (only
partial, qualifiers are not supported).

Syntax slightly differs.

# open Checkfmt;;
# check_printf (apply (int null)) "Price is %a or %i.\n"
    (fun c -> Printf.fprintf c "%f") 1.2 3;;
Price is 1.200000 or 3.
- : unit = ()

Here is the code. Note that I use Obj.magic only once.

module Checkfmt : sig
  type ('a, 'b, 'c) format_type
  val null : ('a, 'b, 'a) format_type
  val int : ('a, 'b, 'c) format_type -> (int -> 'a, 'b, 'c) format_type
  val string :
      ('a, 'b, 'c) format_type -> (string -> 'a, 'b, 'c) format_type
  val char : ('a, 'b, 'c) format_type -> (char -> 'a, 'b, 'c) format_type
  val float : ('a, 'b, 'c) format_type -> (float -> 'a, 'b, 'c) format_type
  val bool : ('a, 'b, 'c) format_type -> (bool -> 'a, 'b, 'c) format_type
  val apply :
      ('a, 'b, 'c) format_type ->
      (('b -> 'd -> 'c) -> 'd -> 'a, 'b, 'c) format_type
  val output :
      ('a, 'b, 'c) format_type -> (('b -> 'c) -> 'a, 'b, 'c) format_type
  val check_format : ('a, 'b, 'c) format_type -> string -> bool
  val make_format : ('a, 'b, 'c) format_type -> string -> ('a, 'b, 'c) format
  val check_printf : ('a, out_channel, unit) format_type -> string -> 'a
end = struct
type ('a,'b, 'c) format_type =
    Null
  | Int of ('a, 'b, 'c) format_type
  | String of ('a, 'b, 'c) format_type
  | Char of ('a, 'b, 'c) format_type
  | Float of ('a, 'b, 'c) format_type
  | Bool of ('a, 'b, 'c) format_type
  | Apply of ('a, 'b, 'c) format_type
  | Output of ('a, 'b, 'c) format_type

let null : ('a, 'b, 'a) format_type = Null
let int (x : ('a, 'b, 'c) format_type) =
  (Int x :> (int -> 'a, 'b, 'c) format_type)
let string (x : ('a, 'b, 'c) format_type) =
  (String x :> (string -> 'a, 'b, 'c) format_type)
let char (x : ('a, 'b, 'c) format_type) =
  (Char x :> (char -> 'a, 'b, 'c) format_type)
let float (x : ('a, 'b, 'c) format_type) =
  (Float x :> (float -> 'a, 'b, 'c) format_type)
let bool (x : ('a, 'b, 'c) format_type) =
  (Bool x :> (bool -> 'a, 'b, 'c) format_type)
let apply (x : ('a, 'b, 'c) format_type) =
  (Apply x :> (('b -> 'd -> 'c) -> 'd -> 'a, 'b, 'c) format_type)
let output (x : ('a, 'b, 'c) format_type) =
  (Apply x :> (('b -> 'c) -> 'a, 'b, 'c) format_type)

let check_format fmtype fmt =
  let rec check pos fmtype =
    if pos >= String.length fmt then fmtype = Null else
    try
      let pos = String.index_from fmt pos '%' + 1 in
      if pos >= String.length fmt then invalid_arg "check_format";
      let c = fmt.[pos] in
      match c, fmtype with
        '%', _ -> check (pos+1) fmtype
      | ('d'|'i'|'u'|'x'|'X'|'o'), Int fmtype -> check (pos+1) fmtype
      | 's', String fmtype -> check (pos+1) fmtype
      | 'c', Char fmtype -> check (pos+1) fmtype
      | ('f'|'e'|'E'|'g'|'G'), Float fmtype -> check (pos+1) fmtype
      | 'b', Bool fmtype -> check (pos+1) fmtype
      | 'a', Apply fmtype -> check (pos+1) fmtype
      | 't', Output fmtype -> check (pos+1) fmtype
      | _ -> false
    with Not_found ->
      fmtype = Null
  in
  check 0 fmtype

let make_format (fmtype : ('a,'b,'c) format_type) fmt =
  if check_format fmtype fmt then (Obj.magic fmt : ('a,'b,'c) format)
  else failwith "make_format"

let check_printf fmtype fmt =
  Printf.printf (make_format fmtype fmt)
end
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-05-02  8:48 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-23 10:41 Jacek Chrzaszcz
2002-04-24 10:44 ` Stefano Lanzavecchia
2002-04-24 18:46   ` Tomasz Zielonka
2002-04-24 11:16 ` Jacques Garrigue
2002-04-24 13:40   ` Tomasz Zielonka
2002-04-25  5:30   ` pervasives (was: Re: [Caml-list] How to read three integers from a text-file... ?) Chris Hecker
2002-04-25  6:33     ` Tomasz Zielonka
2002-04-25 17:54       ` Chris Hecker
2002-04-27  4:43         ` John Max Skaller
2002-04-27 16:02           ` [Caml-list] input_line (Re: pervasives) Lauri Alanko
2002-04-30 12:07             ` [Caml-list] input_line Xavier Leroy
2002-05-03  0:13               ` Lauri Alanko
2002-05-03 11:27                 ` Florian Hars
2002-04-24 21:23 ` [Caml-list] How to read three integers from a text-file... ? Tomasz Zielonka
2002-04-25  1:51   ` John Max Skaller
2002-04-25  8:55   ` Daniel de Rauglaudre
2002-04-25 11:19     ` Markus Mottl
2002-04-25 11:33       ` Jérôme Marant
2002-04-25 11:43         ` Markus Mottl
2002-04-25 17:56         ` Chris Hecker
2002-04-25 20:52           ` John Prevost
2002-04-25 23:32           ` Jacques Garrigue
2002-04-26  7:25             ` Jérôme Marant
2002-04-26 12:16           ` Jacques Garrigue
2002-05-02  8:48             ` Jacques Garrigue [this message]
2002-04-26  1:39         ` Daniel de Rauglaudre
2002-04-29  6:44   ` Francois Pottier
2002-04-30 11:07     ` Dave Berry
2002-04-30 12:20       ` Francois Pottier
2002-04-30 13:54         ` T. Kurt Bond
2002-05-03 22:12         ` Dave Berry
2002-04-30 14:42       ` Jocelyn Sérot
2002-05-02  7:34         ` [Caml-list] Extensible tuple types Francois Pottier
2002-05-02  9:42           ` Alain Frisch
2002-05-02 11:03             ` Francois Pottier
     [not found]       ` <6ECF4649-5C48-11D6-AC27-0003934491C2@lasmea.univ-bpclermon t.fr>
2002-05-03 21:58         ` [Caml-list] How to read three integers from a text-file... ? Dave Berry
2002-05-06  0:53           ` Eray Ozkural
2002-05-06  6:40           ` Florian Hars
2002-04-30 23:30     ` [Caml-list] Danvy "Functional Unparsing" style output in OCaml [was: How to read three integers from a text-file... ?] T. Kurt Bond
2002-05-13 14:11       ` [Caml-list] RE: Danvy "Functional Unparsing" style output in OCaml T. Kurt Bond
2002-05-13 19:59         ` [Caml-list] "Functional Unparsing" benchmark results links fixed [Was: Danvy "Functional Unparsing" style output in OCaml] T. Kurt Bond

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=20020502174808V.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.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