source: sizechecking/branches/macs/Lambda.hs

Last change on this file was 27, checked in by gobi, 11 years ago

new files

File size: 1.9 KB
Line 
1{-# LANGUAGE TypeFamilies, GADTs, Rank2Types, TemplateHaskell #-}
2
3module Lambda where
4
5import qualified Data.Supply as S
6import qualified Data.Char as C
7import Data.Lens.Light 
8
9{-
10 - Lambda calculus beagyazas
11 -}
12class Lambda l where
13    lam :: (l a -> l b) -> l (a -> b)
14    app :: l (a -> b) -> l a -> l b
15    lit :: Int -> l Int
16
17{-
18 - eval interpreter
19 -}
20newtype Q a = Q { unQ :: a }
21instance Lambda Q where
22    lit = Q
23    lam a = Q (unQ.a.Q)
24    app a b = Q $ unQ a (unQ b)
25
26eval :: Q a -> a
27eval = unQ
28
29{-
30- show interpreter
31-}
32showVar :: Int -> String -> String
33showVar x = if x>28 
34    then showVar (x `div` 29) . showChar (C.chr $ C.ord 'a' + (x `mod` 29))
35    else showChar $ C.chr $ C.ord 'a' + x
36
37class SContext a where
38  supply :: Lens a (S.Supply Int)
39  prec :: Lens a Int
40
41newtype S ctx a = S { unS :: ctx -> ShowS }
42
43instance SContext ctx => Lambda (S ctx) where
44    lit a = S (\ctx -> showsPrec (getL prec ctx) a)
45    app (S fun) (S arg) = S (\ctx ->
46        let (s1, s2) = S.split2 (getL supply ctx)
47            p = getL prec ctx
48        in showParen (p>6) $ fun (updateCtx s1 6 ctx) . showChar ' ' . arg (updateCtx s2 7 ctx))
49    lam fun = S (\ctx -> 
50        let (s1, s2) = S.split2 (getL supply ctx)
51            v        = S.supplyValue s1
52            p        = getL prec ctx
53            showV = S $ \ctx -> showVar v
54        in showParen (p>0) $ showChar 'λ' . showVar v . showChar '.' . unS (fun showV) (updateCtx s2 0 ctx))
55
56updateCtx :: SContext ctx => S.Supply Int -> Int -> ctx -> ctx
57updateCtx s p = setL supply s . setL prec p
58
59data SData = SData { _getSDataSupply :: S.Supply Int, _getSDataPrec :: Int }
60$(makeLens ''SData)
61
62instance SContext SData where
63  supply = getSDataSupply
64  prec = getSDataPrec
65
66ast :: S SData a -> IO ShowS
67ast a = do
68    s <- S.newSupply 0 (+1)
69    return $ unS a $ SData s 0
70
71printAst :: S SData a -> IO ()
72printAst l = ast l >>= (\s -> putStrLn $ s "")
73
Note: See TracBrowser for help on using the repository browser.