caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] recursive variants
@ 2001-05-12  5:52 Miles Egan
  2001-05-12 15:13 ` [Caml-list] converting a list to a Stream Terrence Brannon
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Miles Egan @ 2001-05-12  5:52 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2001-05-13 21:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-12  5:52 [Caml-list] recursive variants Miles Egan
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

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).