Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Created February 25, 2018 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjoerdvisscher/8daf3085f60f62e1da872adf0d553f84 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/8daf3085f60f62e1da872adf0d553f84 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TemplateHaskell, TypeFamilies, DeriveFunctor, DeriveFoldable, DeriveTraversable, FlexibleInstances #-}
{-# LANGUAGE LambdaCase, FlexibleContexts, UndecidableInstances, TypeOperators, DataKinds, MultiParamTypeClasses #-}
module Sem where
import Data.Algebra
import Data.Constraint
import Data.Constraint.Class1
import Data.Functor.Free
class BaseSem a where
val :: Int -> a
add :: a -> a -> a
instance BaseSem Int where
val = id
add = (+)
class BaseSem a => AdvSem a where
mul :: a -> a -> a
instance AdvSem Int where
mul = (*)
deriveInstances ''BaseSem
deriveInstances ''AdvSem
instance HasSuperClasses BaseSem
instance HasSuperClasses AdvSem where
type SuperClasses AdvSem = AdvSem ': SuperClasses BaseSem
superClasses = Sub Dict
containsSelf = Sub Dict
eval :: Free AdvSem Int -> Int
eval = counit
test :: Free AdvSem String
test = mul (add (pure "a") (val 3)) (val 5)
main :: IO ()
main = print $ rightAdjunct lookupVar test
where
lookupVar :: String -> Int
lookupVar "a" = 2
lookupVar v = error $ "Unknown variable: " ++ v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment