Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8810031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:01:27+00:00 2026-06-14T03:01:27+00:00

I’m new to haskell and I’m attempting to write my first haskell C library.

  • 0

I’m new to haskell and I’m attempting to write my first haskell C library. This is my first time using the Foreign.C module. I’m getting lost in examples and have become stuck. This is what I have come up with so far:

{-# LANGUAGE ForeignFunctionInterface #-}

module Grep where

import GHC.Ptr
import Foreign.C.String
import Data.List (isInfixOf)

grep :: CString -> CString -> CString
grep i s = do
    ii <- peekCString i
    ss <- peekCString s
    g <- newCString (isInfixOf ii ss)
    g 

foreign export ccall grep :: CString -> CString -> CString

I get the following error:

PS C:\Users\GGuy\Source\haskell> ghc -c -O grep.hs

grep.hs:11:9:
  No instance for (Monad Ptr)
    arising from a do statement
  Possible fix: add an instance declaration for (Monad Ptr)
  In a stmt of a 'do' block: ii <- (peekCString i)
  In the expression:
    do { ii <- (peekCString i);
      ss <- peekCString s;
      g <- newCString (isInfixOf ii ss);
      g }
  In an equation for `grep':
    grep i s
      = do { ii <- (peekCString i);
             ss <- peekCString s;
             g <- newCString (isInfixOf ii ss);
             .... }

grep.hs:11:16:
  Couldn't match expected type `Ptr t0' with actual type `IO String'
  In the return type of a call of `peekCString'
  In a stmt of a 'do' block: ii <- (peekCString i)
  In the expression:
    do { ii <- (peekCString i);
       ss <- peekCString s;
       g <- newCString (isInfixOf ii ss);
       g }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:01:28+00:00Added an answer on June 14, 2026 at 3:01 am

    Now that the fix

    grep :: CString -> CString -> IO Bool
    grep i s = do
        ii <- peekCString i
        ss <- peekCString s
        return (isInfixOf ii ss)
    
    foreign export ccall grep :: CString -> CString -> IO Bool
    

    is clarified, let us find out how the error messages come to be:

    grep :: CString -> CString -> CString
    grep i s = do
        ii <- peekCString i
        ss <- peekCString s
        g <- newCString (isInfixOf ii ss)
        g
    

    The declared result type of grep is CString, which is a type synonym for Ptr CChar. The right hand side of the definition is a do-block (with more than one statement), so the result type must have the form m a for some Monad m and some type a.

    The declared result type Ptr CChar matches the form m a – with m = Ptr, a = CChar – and so what remains is to find/verify the Monad instance of the type constructor Ptr. There is none in scope, hence

    grep.hs:11:9:
      No instance for (Monad Ptr)              -- quite
        arising from a do statement            -- right
      Possible fix: add an instance declaration for (Monad Ptr)    -- Umm, no, not really
    

    the first reported error.

    The second error comes from analysing the contents of the do-block. The compiler could have stopped type-checking upon encountering the first error, but didn’t. Now the further type checking continues under the assumption that Ptr were a Monad. So in that do-block, every expression on the right of a <- (which, after desugaring becomes the first argument of a (>>=) :: Monad m => m a -> (a -> mb) -> m b), must have type Ptr sometype. But peekCString :: CString -> IO String, thus

    grep.hs:11:16:
      Couldn't match expected type `Ptr t0' with actual type `IO String'
      In the return type of a call of `peekCString'
      In a stmt of a 'do' block: ii <- (peekCString i)
      In the expression:
        do { ii <- (peekCString i);
           ss <- peekCString s;
           g <- newCString (isInfixOf ii ss);
           g }
    

    If the compiler continued, it would give the same type error for the second peekCString line, and finally a

    Couldn't match type `Bool' with `[Char]'
    Expected type: String
      Actual type: Bool
    In the first argument of `newCString', namely ...
    

    for the wrongly typed argument of newCString (plus another IO – Ptr mismatch).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.