magistraleinformaticanetworking:spm:skelfsem
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
Entrambe le parti precedenti la revisioneRevisione precedenteProssima revisione | Revisione precedente | ||
magistraleinformaticanetworking:spm:skelfsem [31/03/2011 alle 08:25 (14 anni fa)] – Marco Danelutto | magistraleinformaticanetworking:spm:skelfsem [31/03/2011 alle 08:36 (14 anni fa)] (versione attuale) – [Source code] Marco Danelutto | ||
---|---|---|---|
Linea 1: | Linea 1: | ||
===== OCAML functional semantics for the sample skeleton set ===== | ===== OCAML functional semantics for the sample skeleton set ===== | ||
- | We define here the functional semantics for the sample skeleton set presented in SPM lessons by means of Ocaml functions. [[http:// | + | We define here the functional semantics for the sample skeleton set presented in SPM lessons by means of Ocaml functions. |
- | < | + | * [[http:// |
+ | * [[ocamlfs1|This page]] hosts the preliminary version of functional semantics given in the lesson of March 23 | ||
+ | |||
+ | ==== Source code ==== | ||
+ | < | ||
(** this file hosts the functions defining the functional semantics | (** this file hosts the functions defining the functional semantics | ||
of typical algorithmic skeletons | of typical algorithmic skeletons | ||
Linea 248: | Linea 252: | ||
end;; | end;; | ||
+ | </ | ||
+ | |||
+ | <code ocaml Img.ml> | ||
+ | (** Sample usage of Skeleton functional semantics | ||
+ | We use a dummy Image format (array of array of pixels) | ||
+ | and we define some functions over images to be used in | ||
+ | pipeline stages. | ||
+ | Functions are defined as map s *) | ||
+ | |||
+ | open Skeleton | ||
+ | |||
+ | (** the type of black and white pixels: levels of gray *) | ||
+ | type bw_pixel = BWP of int;; | ||
+ | (** the type of black and white images: 2 dim array of pixels *) | ||
+ | type bw_image = BWImage of bw_pixel array array;; | ||
+ | |||
+ | (** the type of color pixels: RGB *) | ||
+ | type col_pixel = CP of int * int * int;; | ||
+ | (** the type of color images: 2 dim array of pixels *) | ||
+ | type col_image = Image of col_pixel array array;; | ||
+ | |||
+ | (** sample image definition *) | ||
+ | let p1 = CP(127, | ||
+ | let p2 = CP(0, | ||
+ | let p3 = CP(0, | ||
+ | let p4 = CP(64, | ||
+ | let a = Image [| [| p1; p1; p1; p2; p2 |] ; | ||
+ | [| p2; p3; p3; p4; p1 |] ; | ||
+ | [| p1; p1; p1; p1; p1 |] ; | ||
+ | [| p1; p2; p2; p2; p1 |] |];; | ||
+ | |||
+ | (** changes color pixels to black and white *) | ||
+ | let col_to_bw x = | ||
+ | match x with | ||
+ | CP(r,g,b) -> BWP(r+g+b);; | ||
+ | |||
+ | (** kind of smooth pixel *) | ||
+ | let average = | ||
+ | function | ||
+ | CP(x,y,z) -> | ||
+ | let n = ((x+y+z)/3) in | ||
+ | CP(n,n,n);; | ||
+ | |||
+ | (** invert colors *) | ||
+ | let invert = function | ||
+ | CP(r,g,b) -> CP(255-r, 255-g, 255-b);; | ||
+ | |||
+ | |||
+ | (** kind of saturation *) | ||
+ | let sq = function | ||
+ | CP(r,g,b) -> let msq x = ((x*x) mod 256) in | ||
+ | CP((msq r), (msq g), (msq b));; | ||
+ | |||
+ | (** generic stage: takes a pixel processing function (Color to Color) and | ||
+ | returns the stage working on the whole array of arrays | ||
+ | @param f the function to be applied *) | ||
+ | let stage f = | ||
+ | function | ||
+ | Image x -> Image ( (map (map f)) x);; | ||
+ | |||
+ | (** sample stage definitions *) | ||
+ | let stage_average = stage average;; | ||
+ | let stage_sq = stage sq;; | ||
+ | |||
+ | (** color to black and white stage: this could not be defined through stage function | ||
+ | as the type of the output image changes ... *) | ||
+ | let c_to_bw_stage = function | ||
+ | Image x -> BWImage((map (map col_to_bw)) x);; | ||
+ | |||
+ | (** sample main program *) | ||
+ | let main = | ||
+ | pipe | ||
+ | (pipe (stage invert) (stage average)) | ||
+ | c_to_bw_stage ;; | ||
</ | </ |
magistraleinformaticanetworking/spm/skelfsem.1301559947.txt.gz · Ultima modifica: 31/03/2011 alle 08:25 (14 anni fa) da Marco Danelutto