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 7559801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:43:42+00:00 2026-05-30T12:43:42+00:00

I’ve been inspired by the recent Haskell blog activity 1 to try my hand

  • 0

I’ve been inspired by the recent Haskell blog activity1 to try my hand at writing a Forth-like DSL in Haskell. The approach I have taken is simultaneously straightforward and confusing:

{-# LANGUAGE TypeOperators, RankNTypes, ImpredicativeTypes #-}

-- a :~> b represents a "stack transformation"
--          from stack type "a" to stack type "b"
-- a :> b represents a "stack" where the top element is of type "b"
--          and the "rest" of the stack has type "a"
type s :~> s' = forall r. s -> (s' -> r) -> r
data a :> b = a :> b deriving Show
infixl 4 :>

For doing simple things, this works quite nicely:

start :: (() -> r) -> r
start f = f ()

end :: (() :> a) -> a
end (() :> a) = a

stack x f = f x
runF s = s end
_1 = liftS0 1
neg = liftS1 negate
add = liftS2 (+)

-- aka "push"
liftS0 :: a -> (s :~> (s :> a))
liftS0 a s = stack $ s :> a

liftS1 :: (a -> b) -> ((s :> a) :~> (s :> b))
liftS1 f (s :> a) = stack $ s :> f a

liftS2 :: (a -> b -> c) -> ((s :> a :> b) :~> (s :> c))
liftS2 f (s :> a :> b) = stack $ s :> f a b

Simple functions can trivially be transformed into their corresponding stack transformations. Some playing around yields pleasant results so far:

ghci> runF $ start _1 _1 neg add
0

The trouble comes when I try to extend this with higher-order functions.

-- this requires ImpredicativeTypes...not really sure what that means
-- also this implementation seems way too simple to be correct
-- though it does typecheck. I arrived at this after pouring over types
-- and finally eta-reducing the (s' -> r) function argument out of the equation
-- call (a :> f) h = f a h
call :: (s :> (s :~> s')) :~> s'
call (a :> f) = f a

call is supposed to transform a stack of the form (s :> (s :~> s')) to the form s, by essentially “applying” the transformation (held at the tip of the stack) to the “rest” of it. I imagine it should work like this:

ghci> runF $ start _1 (liftS0 neg) call
-1

But in actuality, it gives me a huge type mismatch error. What am I doing wrong? Can the “stack transformation” representation sufficiently handle higher-order functions, or do I need to adjust it?

1N.B. Unlike how these guys did it, instead of start push 1 push 2 add end, I want it to be runF $ start (push 1) (push 2) add, the idea being that maybe later I can work some typeclass magic to make the push implicit for certain literals.

  • https://github.com/leonidas/codeblog/blob/master/2012/2012-02-17-concatenative-haskell.md
  • https://gist.github.com/1847747
  • 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-05-30T12:43:44+00:00Added an answer on May 30, 2026 at 12:43 pm

    Your :~> type is not what you actually want (hence the ImpredicativeTypes). If you just remove type annotation from call then your last sample will work as expected. Another way to make it work is to use less fancy but more appropriate type with extra parameter:

    type Tran s s' r = s -> (s' -> r) -> r
    
    call :: Tran (s :> (Tran s s' r)) s' r
    call (a :> f) = f a
    

    But if what you are after is a nice DSL syntax and you are can tolerate OverlappingInstances then you can even pretty much get rid of liftSx functions:

    {-# LANGUAGE TypeOperators, MultiParamTypeClasses, TypeFamilies,
                 FlexibleInstances, FlexibleContexts,
                 UndecidableInstances, IncoherentInstances  #-}
    
    data a :> b = a :> b deriving Show
    infixl 4 :>
    
    
    class Stackable s o r where
        eval :: s -> o -> r
    
    
    data End = End
    
    instance (r1 ~ s) => Stackable s End r1 where
        eval s End = s
    
    
    instance (Stackable (s :> a) o r0, r ~ (o -> r0)) => Stackable s a r where
        eval s a = eval (s :> a)
    
    instance (a ~ b, Stackable s c r0, r ~ r0) => Stackable (s :> a) (b -> c) r where
        eval (s :> a) f = eval s (f a)
    
    -- Wrap in Box a function which should be just placed on stack without immediate application
    data Box a = Box a
    
    instance (Stackable (s :> a) o r0, r ~ (o -> r0)) => Stackable s (Box a) r where
        eval s (Box a) = eval (s :> a)
    
    
    runS :: (Stackable () a r) => a -> r
    runS a = eval () a
    
    -- tests
    t1 = runS 1 negate End
    t2 = runS 2 1 negate (+) End
    
    t3 = runS 1 (Box negate) ($) End
    t4 = runS [1..5] 0 (Box (+)) foldr End
    t5 = runS not True (flip ($)) End
    
    t6 = runS 1 (+) 2 (flip ($)) End
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests

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.