Strumenti Utente

Strumenti Sito


magistraleinformaticanetworking:spm:ocamlfs1

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisione Revisione precedente
Prossima revisione
Revisione precedente
Prossima revisione Entrambe le parti successive la revisione
magistraleinformaticanetworking:spm:ocamlfs1 [23/03/2011 alle 16:07 (13 anni fa)]
Marco Danelutto
magistraleinformaticanetworking:spm:ocamlfs1 [23/03/2011 alle 16:22 (13 anni fa)]
Marco Danelutto
Linea 3: Linea 3:
 This is an summary of the lesson given on March 23rd.  This is an summary of the lesson given on March 23rd. 
  
-We represent the functional semantics of skeletons with higher order functions. We use [[http://caml.inria.fr/ocaml/|Ocaml]] for the examples (see the [[http://caml.inria.fr/pub/docs/manual-ocaml/index.html|Ocaml online manual]] for the syntax. +We represent the functional semantics of skeletons with higher order functions. We use [[http://caml.inria.fr/ocaml/|Ocaml]] for the examples (see the [[http://caml.inria.fr/pub/docs/manual-ocaml/index.html|Ocaml online manual]] for the syntax)
  
 Assuming that //data streams// are represented with Ocaml lists (just for the moment), a **farm** skeleton may be defined as follows:  Assuming that //data streams// are represented with Ocaml lists (just for the moment), a **farm** skeleton may be defined as follows: 
Linea 30: Linea 30:
 # (farm sq) [1;2;3;4;5];; # (farm sq) [1;2;3;4;5];;
 - : int list = [1; 4; 9; 16; 25] - : int list = [1; 4; 9; 16; 25]
 +
 +</code>
 +
 +Suppose we also use lists to represent arrays. The map skeleton can be modelled through the **List.map** function, available as primitive in Ocaml and having the obvious type: 
 +<code>
 +# List.map;;
 +- : ('a -> 'b) -> 'a list -> 'b list = <fun>
 +
 +</code>
 +Therefore we can look at the type of skeleton composition. As an example, a farm having a map worker computing sq on all the elements of the matrix (which is actually a vector represented as a list, in our simple assumption) has type: 
 +<code>
 +# farm (List.map sq);;
 +- : int list list -> int list list = <fun>
 +
 +</code>
 +and in fact, giving the composite skeleton a stream (a list, in our assumption) of arrays (vectors represented as a stream, in our assumptions), we get the correct result: all items of the stream are processed in such a way all the subitems of the arrays are squared.
 +<code>
 +# let array1 = [1;2];;
 +val array1 : int list = [1; 2]
 +# let array2 = [3;4];;
 +val array2 : int list = [3; 4]
 +# let stream = [array1;array2];;
 +val stream : int list list = [[1; 2]; [3; 4]]
 +# farm (List.map sq) stream;;
 +- : int list list = [[1; 4]; [9; 16]]
 +
 +</code>
 +
 +This is a little bit ambiguous due to the usage of lists to represent both streams and arrays. 
 +Ocaml provides arrays as primitive types actually. Arrays are denoted similarly to lists using particular brackets:
 +<code>
 +# [|1;2|];;
 +- : int array = [|1; 2|]
 +
 +</code>
 +As for lists, a map working on arrays is pre-defined: 
 +<code>
 +# Array.map;;
 +- : ('a -> 'b) -> 'a array -> 'b array = <fun>
  
 </code> </code>
  
magistraleinformaticanetworking/spm/ocamlfs1.txt · Ultima modifica: 23/03/2011 alle 16:31 (13 anni fa) da Marco Danelutto