From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id HAA01381; Sat, 12 May 2001 07:53:00 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA01379 for ; Sat, 12 May 2001 07:52:59 +0200 (MET DST) Received: from www.invert.com (invert.com [209.164.21.15]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f4C5qvb19803 for ; Sat, 12 May 2001 07:52:57 +0200 (MET DST) Received: (from miles@localhost) by www.invert.com (8.10.1/8.10.1AA) id f4C5qpH50783 for caml-list@inria.fr; Fri, 11 May 2001 22:52:51 -0700 (PDT) (envelope-from miles) Date: Fri, 11 May 2001 22:52:51 -0700 From: Miles Egan To: caml-list@inria.fr Subject: [Caml-list] recursive variants Message-ID: <20010511225250.A50306@caddr.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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