oops, i already implemented an applicative

This commit is contained in:
vegowotenks 2025-07-11 17:22:33 +02:00
commit a577bddd7f
15 changed files with 321 additions and 0 deletions

View file

@ -0,0 +1,6 @@
module Language.Java.Classfile () where
import Language.Java.Classfile.Version (Version)
data Classfile = Classfile
{ version :: Version
}

View file

@ -0,0 +1,39 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE InstanceSigs #-}
module Language.Java.Classfile.Extract (Extract()) where
import Data.ByteString.Lazy (ByteString)
data Extract a = Extract (Continuation a)
type Continuation a = ByteString -> Reply a
data Reply a
= Done ByteString a -- rest, result
| Fail
deriving (Functor)
instance Functor Extract where
fmap :: (a -> b) -> Extract a -> Extract b
fmap f (Extract cont) = Extract $ \ input -> f <$> cont input
instance Applicative Extract where
pure :: a -> Extract a
pure x = Extract $ \ rest -> Done rest x
(<*>) :: Extract (a -> b) -> Extract a -> Extract b
(<*>) (Extract computeF) (Extract computeX) = Extract $ \ input -> case computeF input of
Done rest f -> case computeX rest of
Done rest' x -> Done rest' (f x)
Fail -> Fail
Fail -> Fail
{- It seems I cannot define a lawful monad instance
instance Monad Extract where
(>>=) :: Extract a -> (a -> Extract b) -> Extract b
(>>=) (Extract computeA) f = Extract $ \ input -> case computeA input of
Done rest a -> _
Fail -> Fail
-}

View file

@ -0,0 +1,9 @@
module M () where
class Extractable a where
extract :: Extract a
instance Extractable Word8 where
extract :: Extract Word8
extract = _

View file

@ -0,0 +1,8 @@
module Language.Java.Classfile.Version (Version(..)) where
import Data.Word (Word16)
data Version = Version
{ minor :: Word16
, major :: Word16
}