Skip to content

Instantly share code, notes, and snippets.

@trentgill
Created August 21, 2018 03:35
Show Gist options
  • Save trentgill/95b295d8eb6cc42e9636bc1b69f5fc32 to your computer and use it in GitHub Desktop.
Save trentgill/95b295d8eb6cc42e9636bc1b69f5fc32 to your computer and use it in GitHub Desktop.
Forth in haskell, reverse lookup via function
data FIWord = Imm
| Not
| NA deriving (Show, Eq)
type FDictEntry = (String, FIWord, FStackItem)
type FDict = [FDictEntry]
native_dict :: FDict
native_dict = [ (".S" ,Not, FFn fDOTESS )
, ("." ,Not, FFn fDOT )
]
data FStackItem = FFn (FState -> FState)
| FCFn [FStackItem]
| FNull deriving (Eq)
type FDataStack = [FStackItem]
data FState = FState
{ datastack :: FDataStack
, dictionary :: FDict
} deriving (Eq, Show)
-- current Show instance
instance Show FStackItem where
show (FFn _) = " <function>"
-- desired fancy show instance
fancyShow :: FDict -> FStackItem -> String
fancyShow d (FFn x) = getDictString $ matchDictByFn d x
getDictString :: FDictEntry -> String
getDictString (s,_,_) = s
matchDictByFn :: FDict -> FStackItem -> [FDictEntry]
matchDictByFn d (FFn ffn) = [ (name, imm, fn) | (name, imm, fn) <- d
, fn == ffn ] -- can't equate due to no Eq typeclass
@trentgill
Copy link
Author

gives error:
• No instance for (Eq (FState -> FState))
arising from the first field of ‘FFn’ (type ‘FState -> FState’)
(maybe you haven't applied a function to enough arguments?)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (Eq FStackItem)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment