caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Miles Egan <miles@caddr.com>
To: caml-list@inria.fr
Subject: [Caml-list] recursive variants
Date: Fri, 11 May 2001 22:52:51 -0700	[thread overview]
Message-ID: <20010511225250.A50306@caddr.com> (raw)

I'm trying to translate a simple random sentence generator from lisp to ocaml
and I'm having a bit of trouble with the types.  Here's what I have so far:

(****************************************************)
type grammar_element =
  Word of string
| Wordlist of string list
| Phrase of (unit -> string list)
| Phraselist of (unit -> string list) list

let split str =
  Str.split (Str.regexp "[ \t]") str

let random_elt choices =
  (*Choose an element from a list at random.*)
  List.nth choices (Random.int (List.length choices))

let one_of set =
  (*Pick one element of set, and make a list of it.*)
  [random_elt set]

let pick_word str =
  one_of (split str)

let adj () =
  pick_word "big little blue green adiabatic"

let prep () =
  pick_word "to in by with on"

let article () =
  pick_word "the a"

let noun () =
  pick_word "man ball woman table"

let noun_phrase () =
  List.append (article ()) (noun ())

let verb () =
  pick_word "hit took saw liked"

let verb_phrase () =
  List.append (verb ()) (noun_phrase ())

let sentence () =
  List.append (noun_phrase ()) (verb_phrase ())    

let simple_grammar = 
  (* A grammar for a trivial subset of English. *)
  [ (Phrase sentence, Phraselist [noun_phrase; verb_phrase]);
    (Phrase noun_phrase, Phraselist [article; noun]);
    (Phrase verb_phrase, Phraselist [verb; noun_phrase]);
    (Phrase article, Wordlist (split "the a"));
    (Phrase noun, Wordlist (split "man ball woman table"));
    (Phrase verb, Wordlist (split "hit took saw liked")); ]

let rewrites category =
  (* Return a list of the possible rewrites for this category. *)
  List.find (fun (a,b) -> a = category) simple_grammar

let rec generate phrase =
  (* Generate a random sentence or phrase *)
  match phrase with 
    Phraselist p -> List.map generate p
  | Phrase p -> (one_of (rewrites p)) ()
  | Wordlist p -> one_of p
  | Word p -> p
(****************************************************)

This generates an error in 'generate' because List.map is called on the matched
Phraselist p, which isn't a grammar_element, but a (unit -> string list) list.

What I want to do is this:

type grammar_element =
  Word of string
| Wordlist of Word list
| Phrase of unit -> Wordlist
| Phraselist of Phrase list

But this doesn't seem to be legal.  I suppose this is pretty basic stuff, but
I'm stuck.  Any suggestions?

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


             reply	other threads:[~2001-05-12  5:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-12  5:52 Miles Egan [this message]
2001-05-12 15:13 ` [Caml-list] converting a list to a Stream Terrence Brannon
2001-05-12 15:31   ` Sylvain Pogodalla
2001-05-12 15:35   ` Didier Le Botlan
     [not found] ` <3AFCFB23.CB503721@tsc.uc3m.es>
2001-05-12 15:28   ` [Caml-list] recursive variants Miles Egan
2001-05-12 15:35 ` Sven LUTHER
2001-05-12 21:49   ` Miles Egan

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=20010511225250.A50306@caddr.com \
    --to=miles@caddr.com \
    --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).