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

* [Caml-list] converting a list to a Stream
  2001-05-12  5:52 [Caml-list] recursive variants Miles Egan
@ 2001-05-12 15:13 ` 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:35 ` Sven LUTHER
  2 siblings, 2 replies; 7+ messages in thread
From: Terrence Brannon @ 2001-05-12 15:13 UTC (permalink / raw)
  To: caml-list

How does one convert a list to a stream?

# let c = [1; 2; 3 ];;
val c : int list = [1; 2; 3]
# Stream_of_list c;;
Characters 0-16:
Unbound constructor Stream_of_list
# stream_of_list c;;
Characters 0-14:
Unbound value stream_of_list

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


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

* Re: [Caml-list] recursive variants
       [not found] ` <3AFCFB23.CB503721@tsc.uc3m.es>
@ 2001-05-12 15:28   ` Miles Egan
  0 siblings, 0 replies; 7+ messages in thread
From: Miles Egan @ 2001-05-12 15:28 UTC (permalink / raw)
  To: caml-list

On Sat, May 12, 2001 at 10:58:11AM +0200, Francisco Valverde Albacete wrote:
> You don't need a recursive type for this:
> 
> type word = Word of string
> type word_list = word list
> type phrase = unit -> word list
> type phrase_list = phrase list

I tried this also, but I couldn't figure out how to type the list that contains
both (phrase_list * phrase_list) and (phrase_list * word_list) without grouping
them in a single common type.  The compiler still rejects the grammar list
because its elements are not homogenous.

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


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

* Re: [Caml-list] converting a list to a Stream
  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
  1 sibling, 0 replies; 7+ messages in thread
From: Sylvain Pogodalla @ 2001-05-12 15:31 UTC (permalink / raw)
  To: Terrence Brannon, caml-list

Terrence Brannon wrote:
> 
> How does one convert a list to a stream?
> 
> # let c = [1; 2; 3 ];;
> val c : int list = [1; 2; 3]
> # Stream_of_list c;;
> Characters 0-16:
> Unbound constructor Stream_of_list
> # stream_of_list c;;
> Characters 0-14:
> Unbound value stream_of_list

To use a function defined for a module, you call it with the syntax :

Module.function arg

Here, you have :
# let l=[1; 2; 3 ];;
val l : int list = [1; 2; 3]
# Stream.of_list l;;
- : int Stream.t = <abstr>

Or, if you don't want to have to prefix the function with the module
name, you can open it :
# open Stream;;
# let l=[1; 2; 3 ];;
val l : int list = [1; 2; 3]
# of_list l;;

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


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

* Re: [Caml-list] converting a list to a Stream
  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
  1 sibling, 0 replies; 7+ messages in thread
From: Didier Le Botlan @ 2001-05-12 15:35 UTC (permalink / raw)
  To: Terrence Brannon; +Cc: caml-list

Terrence Brannon wrote:

> How does one convert a list to a stream?

Try function of_list in Stream module : "Stream.of_list"
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


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

* Re: [Caml-list] recursive variants
  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
       [not found] ` <3AFCFB23.CB503721@tsc.uc3m.es>
@ 2001-05-12 15:35 ` Sven LUTHER
  2001-05-12 21:49   ` Miles Egan
  2 siblings, 1 reply; 7+ messages in thread
From: Sven LUTHER @ 2001-05-12 15:35 UTC (permalink / raw)
  To: Miles Egan; +Cc: caml-list

On Fri, May 11, 2001 at 10:52:51PM -0700, Miles Egan wrote:
> type grammar_element =
>   Word of string
> | Wordlist of Word list
> | Phrase of unit -> Wordlist
> | Phraselist of Phrase list

Err, Phrase and such are not types, but values, you could do somethign like :

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

or more probably :

type word = string
type wordlist = string list
type phrase = unit -> wordlist
type phraselist = phrase list

and then :

type grammar_element =
| Word of word
| Wordlist of wordlist
| Phrase of phrase
| Phraselist of phraselist

or something such.

beware, non of this is tested, but you should be able to find somethign
working along those lines.

Hope this helps, ...

Friendly,

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


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

* Re: [Caml-list] recursive variants
  2001-05-12 15:35 ` Sven LUTHER
@ 2001-05-12 21:49   ` Miles Egan
  0 siblings, 0 replies; 7+ messages in thread
From: Miles Egan @ 2001-05-12 21:49 UTC (permalink / raw)
  To: caml-list

On Sat, May 12, 2001 at 05:35:53PM +0200, Sven LUTHER wrote:
> type word = string
> type wordlist = string list
> type phrase = unit -> wordlist
> type phraselist = phrase list
> 
> and then :
> 
> type grammar_element =
> | Word of word
> | Wordlist of wordlist
> | Phrase of phrase
> | Phraselist of phraselist

I finally got this to work:

(*****************************************************)
type grammar_element =
  Word of string
| Term of (unit -> string)
| Nonterm of (unit -> grammar_element list)
| Terms of grammar_element list

let adj () =
  "big"

let prep () =
  "to"

let article () =
  "the"

let noun () =
  "man"

let verb () =
  "hit"

let noun_phrase () =
  [Term article; Term noun]

let verb_phrase () =
  [Term verb; Nonterm noun_phrase]

let sentence () =
  [Nonterm noun_phrase; Nonterm verb_phrase]

let rec generate part =
  match part with
  Terms p -> Terms (List.map generate p)
| Nonterm p -> generate (Terms (p ()))
| Term p -> Word (p ())
| Word p -> Word p

let _ =
  let s = generate (Nonterm sentence) in
  let rec printelem e =
    match e with
    Word w -> print_string (w ^ " ")
  | Term t -> print_string "term "
  | Nonterm t -> print_string "nonterm "
  | Terms t -> List.iter printelem t
  in
  match s with Terms t -> List.iter printelem t
(*****************************************************)

The trick was making sure that generate is of type (grammar_element ->
grammar_element) and defining a grammar_element such that a grammar_element list
is also a grammar_element list.  

This seems a bit cumbersome.  Is there a cleaner way to do this?  How do people
generally handle heterogeneous lists in ocaml?

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