comparison src/fftw-3.3.8/genfft/monads.ml @ 167:bd3cc4d1df30

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 19 Nov 2019 14:52:55 +0000
parents
children
comparison
equal deleted inserted replaced
166:cbd6d7e562c7 167:bd3cc4d1df30
1 (*
2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
3 * Copyright (c) 2003, 2007-14 Matteo Frigo
4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 *)
21
22 (*************************************************************
23 * Monads
24 *************************************************************)
25
26 (*
27 * Phil Wadler has many well written papers about monads. See
28 * http://cm.bell-labs.com/cm/cs/who/wadler/
29 *)
30 (* vanilla state monad *)
31 module StateMonad = struct
32 let returnM x = fun s -> (x, s)
33
34 let (>>=) = fun m k ->
35 fun s ->
36 let (a', s') = m s
37 in let (a'', s'') = k a' s'
38 in (a'', s'')
39
40 let (>>) = fun m k ->
41 m >>= fun _ -> k
42
43 let rec mapM f = function
44 [] -> returnM []
45 | a :: b ->
46 f a >>= fun a' ->
47 mapM f b >>= fun b' ->
48 returnM (a' :: b')
49
50 let runM m x initial_state =
51 let (a, _) = m x initial_state
52 in a
53
54 let fetchState =
55 fun s -> s, s
56
57 let storeState newState =
58 fun _ -> (), newState
59 end
60
61 (* monad with built-in memoizing capabilities *)
62 module MemoMonad =
63 struct
64 open StateMonad
65
66 let memoizing lookupM insertM f k =
67 lookupM k >>= fun vMaybe ->
68 match vMaybe with
69 Some value -> returnM value
70 | None ->
71 f k >>= fun value ->
72 insertM k value >> returnM value
73
74 let runM initial_state m x = StateMonad.runM m x initial_state
75 end