From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 11CB8BB81 for ; Sun, 27 Nov 2005 07:24:35 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jAR6OYad025894 for ; Sun, 27 Nov 2005 07:24:34 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA13921 for ; Sun, 27 Nov 2005 07:24:33 +0100 (MET) Received: from mail.rsise.anu.edu.au (mail.rsise.anu.edu.au [150.203.208.4]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jAR6OTKl025887 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Sun, 27 Nov 2005 07:24:32 +0100 Received: from localhost (localhost [127.0.0.1]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 8BA6373A87 for ; Sun, 27 Nov 2005 17:24:24 +1100 (EST) Received: from mail.rsise.anu.edu.au ([150.203.208.4]) by localhost (mail [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 30797-02 for ; Sun, 27 Nov 2005 17:24:24 +1100 (EST) Received: from pulp.rsise.anu.edu.au (pulp.rsise.anu.edu.au [150.203.208.49]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 71C6873A02 for ; Sun, 27 Nov 2005 17:24:13 +1100 (EST) Received: by pulp.rsise.anu.edu.au (Postfix, from userid 1560) id 782C525B284; Sun, 27 Nov 2005 17:24:11 +1100 (EST) Date: Sun, 27 Nov 2005 17:24:11 +1100 From: Pietro Abate To: ocaml ml Subject: backtracking monad Message-ID: <20051127062411.GA17290@pulp.anu.edu.au> Mail-Followup-To: Pietro Abate , ocaml ml MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Operating-System: GNU/Linux X-Organization: Research School of Information Science and Engineering (Australian National University) User-Agent: Mutt/1.5.11 X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at mail.rsise.anu.edu.au X-Miltered: at concorde with ID 43895122.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 4389511D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; anu:01 backtracking:01 haskell:01 ocaml:01 haskell:01 syntax:01 monads:01 monads:01 syntax:01 confuses:01 backtracking:01 ocaml:01 struct:01 struct:01 lazy:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 hi all, Can anyone help me to 'translate' this piece of haskell code [1] in ocaml ? I'm trying to learn how to program in monad style, but since I don't know haskell syntax (and almost all examples about monads are in haskell), I have some problems to understand what's going on here... I've got the general idea about monads, but the haskell syntax confuses me. Moreover I've also a general question about monads... I've the sensation that monads implementations are not tail-recursive. Is there a big overhead in using monads Vs Continuations (CPS) ? Where can I find a collections of basic monads (state, backtracking, continuation, etc) written in ocaml ? thanks, :) p ------------------- (* backtracking monad *) type MBt a = M [a] x `bindBt` f = x `bindM` \vs -> foldr orelseBt failBt (map f vs) liftBt :: M a -> MBt a liftBt m = m `bindM` \v -> unitM [v] infixl 5 `apM` apM :: M (a -> b) -> M a -> M b fm `apM` fx = fm `bindM` \f -> fx `bindM` \x -> unitM (f x) orelseBt :: MBt a -> MBt a -> MBt a x `orelseBt` y = unitM (++) `apM` x `apM` y failBt :: MBt a failBt = unitM [] ------------------- this is my (failed) attempt ... (* state monad *) module M = struct type ('x, 'a) t = ('a -> 'x) -> 'x let bind m f x = m (fun a -> f a x) let return x = fun s -> x, s end (* backtracking monad *) module BtM = struct (* this list should be a lazy list *) type ('x ,'a) t = ('a, 'x list) M.t let return x = [x] let fail = return [] let orelse = ??? let bind m x f = List.fold_left orelse fail (List.map f x) end (* recursive mondad ?? *) ------------------- (* recursive mondad ?? *) type MRBt a = M (Ans a) data Ans a = Ans a (MRBt a) | NoAns x `bindRBt` f = x `bindM` \y -> case y of NoAns -> failRBt Ans v x' -> f v `orelseRBt` (x' `bindRBt` f) liftRBt m = m `bindM` \v -> unitM (Ans v failRBt) failRBt = unitM NoAns x `orelseRBt` y = x `bindM` \z -> case z of NoAns -> y Ans v x' -> unitM (Ans v (x' `orelseRBt` y)) ------------------- [1] http://www.math.chalmers.se/~augustss/AFP/monads.html -- ++ Blog: http://blog.rsise.anu.edu.au/?q=pietro ++ ++ "All great truths begin as blasphemies." -George Bernard Shaw ++ Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html