(*
 * Copyright (c) 2001-2002 Stefan Kral
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *)

open NonDetMonad
open List

let mapPairM f (a,b) = 
  f a >>= fun a' ->
    f b >>= fun b' ->
      unitM (a',b')

let mapTripleM f (a,b,c) = 
  f a >>= fun a' ->
    f b >>= fun b' ->
      f c >>= fun c' ->
	unitM (a',b',c')

let mapM f xs =
  let rec mapM' bag = function
    | []    -> unitM (rev bag) 
    | x::xs -> f x >>= fun x' ->
		 mapM' (x'::bag) xs
  in mapM' [] xs

let consM x xs = unitM (x::xs)

(* memberM' x xs === memberM (x::xs) *)
let rec memberM' elem = function
  | []    -> unitM elem
  | x::xs -> (fun _ -> unitM elem) ||| (fun _ -> memberM' x xs)

let memberM = function
  | []    -> failM
  | x::xs -> memberM' x xs

(* selectM' x xs === selectM (x::xs) *)
let rec selectM' elem = function
  | [] -> 
      unitM (elem,[])
  | x::xs as xxs -> 
      (fun _ -> unitM (elem,xxs)) |||
      (fun _ -> selectM' x xs >>= fun (z,zs) -> unitM (z,elem::zs))

let selectM = function
  | []    -> failM
  | x::xs -> selectM' x xs

(* uses boolean test predicate *)
(* ==> kick: use list preds for that! *)
let rec selectFirstM p = function
  | []    	   -> failM
  | x::xs when p x -> unitM (x,xs) 
  | x::xs 	   -> selectFirstM p xs >>= fun (z,zs) -> unitM (z,x::zs)

(* uses boolean test predicate *)
let deleteFirstM p xs = selectFirstM p xs >>= fun (_,zs) -> unitM zs
(* ==> kick: use list preds for that! *)

let rec permutationM = function
  | []         -> unitM []
  | _::_ as xs -> selectM xs >>= fun (z,zs) -> permutationM zs >>= consM z

let rec forallM p = function
  | []    -> unitM ()
  | x::xs -> p x >>= fun _ -> forallM p xs 

let existsM p xs = memberM xs >>= p

let rec betweenM' i0 i1 i =
  if i1 <= i then
    (fun _ -> unitM i0) ||| (fun _ -> betweenM' i1 (succ i1) i)
  else
    unitM i0

let betweenM i0 i =
  if i0 <= i then
    betweenM' i0 (succ i0) i
  else 
    failM

let disjM xs = memberM xs >>= fun f -> f ()

let optionToValueM = function 
  | None   -> failM 
  | Some x -> unitM x

let rec iterM f = function
  | []    -> unitM ()
  | x::xs -> f x >>= fun _ -> iterM f xs

let rec iterirevM f i = function
  | []    -> unitM ()
  | x::xs -> iterirevM f (succ i) xs >>= fun _ -> f i x

let rec suffixM = function
  | [] 		 -> unitM []
  | _::xs as xxs -> (fun _ -> unitM xxs) ||| (fun _ -> suffixM xs)

