Skip to content

Commit e0c5b8b

Browse files
committed
convert let bindings to function applications for run time
letsToApp completed, but not wired in working lambdaLift fixing formatting hlint formatting
1 parent d483671 commit e0c5b8b

14 files changed

Lines changed: 1078 additions & 220 deletions

app/Repl.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ import System.Exit (exitSuccess)
2828
import qualified System.IO.Strict as Strict
2929
import Telomare
3030
import Telomare.Eval (EvalError (..), compileUnitTestNoAbort)
31-
import Telomare.Parser (TelomareParser,
32-
parseAssignment,
33-
parseLongExpr, parsePrelude)
34-
import Telomare.Possible (evalPartial', deferB)
35-
import Telomare.PossibleData (CompiledExpr, setEnvB, pairB, zeroB)
31+
import Telomare.Parser (TelomareParser, parseAssignment, parseLongExpr,
32+
parsePrelude)
33+
import Telomare.Possible (deferB, evalPartial')
34+
import Telomare.PossibleData (CompiledExpr, pairB, setEnvB, zeroB)
3635
import Telomare.Resolver (process)
3736
import Telomare.RunTime (fastInterpretEval, simpleEval)
3837
import Telomare.TypeChecker (inferType)
@@ -119,7 +118,7 @@ printLastExpr eval bindings = do
119118
case compile' =<< process (DummyLoc :< LetUPF bindings' upt) of
120119
Left err -> putStrLn err
121120
Right iexpr' -> case eval iexpr' of
122-
Left e -> putStrLn $ "error: " <> show e
121+
Left e -> putStrLn $ "error: " <> show e
123122
Right expr' -> print . PrettyIExpr $ expr'
124123
case res of
125124
Left err -> print err

src/Debug.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ hPutStrLn file "test"
1616
-- hFlush file
1717
hClose file
1818
-}
19-
data DebugState = DebugState Int
19+
newtype DebugState = DebugState Int
2020

2121
debugState :: IORef DebugState
2222
{-# NOINLINE debugState #-}

src/PrettyPrint.hs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ module PrettyPrint where
66
import Control.Monad.State (State)
77
import Data.Map (Map)
88
import Naturals (NExpr (..), NExprs (..), NResult)
9-
import Telomare (FragExpr (..), FragExprF (..), FragExprUR (..),
9+
import Telomare (DataType (..), FragExpr (..), FragExprF (..), FragExprUR (..),
1010
FragExprURSansAnnotation (FragExprURSA, unFragExprURSA),
11-
FragIndex (..), IExpr (..), LocTag, PartialType (..),
12-
RecursionSimulationPieces (..), IExprF (..), DataType (..),
13-
Term3 (..), Term4 (..), forget, forgetAnnotationFragExprUR,
14-
indentWithOneChild', indentWithTwoChildren', rootFrag,
15-
Pattern (..), UnprocessedParsedTerm (..),
16-
UnprocessedParsedTermF (..), isNum, g2i
17-
)
11+
FragIndex (..), IExpr (..), IExprF (..), LamType (..), LocTag,
12+
ParserTermF (..), PartialType (..), Pattern (..),
13+
RecursionSimulationPieces (..), Term1, Term3 (..), Term4 (..),
14+
UnprocessedParsedTerm (..), UnprocessedParsedTermF (..),
15+
forget, forgetAnnotationFragExprUR, g2i, indentWithChildren',
16+
indentWithOneChild', indentWithTwoChildren', isNum, rootFrag)
1817

1918
import qualified Control.Comonad.Trans.Cofree as CofreeT (CofreeF (..))
2019
import qualified Control.Monad.State as State
@@ -180,6 +179,24 @@ instance PrettyPrintable Term4 where
180179
TraceFragF -> pure "T"
181180
AuxFragF _ -> error "prettyPrint term4 - should be no auxfrag here"
182181

182+
instance {-# OVERLAPPING #-} PrettyPrintable Term1 where
183+
showP = cata fa where
184+
fa (_ CofreeT.:< x) = case x of
185+
TZeroF -> pure "0"
186+
TPairF a b -> indentWithTwoChildren' "(" a b
187+
TVarF v -> pure v
188+
TAppF c i -> indentWithTwoChildren' "($)" c i
189+
TCheckF cf i -> indentWithTwoChildren' ":" cf i
190+
TITEF i t e -> indentWithChildren' "ITE" [i,t,e]
191+
TLeftF x -> indentWithOneChild' "L" x
192+
TRightF x -> indentWithOneChild' "R" x
193+
TTraceF x -> indentWithOneChild' "T" x
194+
THashF x -> indentWithOneChild' "#" x
195+
TChurchF n -> pure $ "$" <> show n
196+
TLamF (Open v) x -> indentWithOneChild' ("\\" <> v) x
197+
TLamF (Closed v) x -> indentWithOneChild' ("[\\" <> v) x
198+
TLimitedRecursionF t r b -> indentWithChildren' "TRB" [t,r,b]
199+
183200
showTypeDebugInfo :: TypeDebugInfo -> String
184201
showTypeDebugInfo (TypeDebugInfo (Term3 m) lookup rootType) =
185202
let termMap = forgetAnnotationFragExprUR <$> m

src/Telomare.hs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ import Data.Functor.Classes (Eq1 (..), Eq2 (..), Show1 (..), Show2 (..), eq1)
3030
import Data.Functor.Foldable (Base, Corecursive (embed),
3131
Recursive (cata, project))
3232
import Data.Functor.Foldable.TH (MakeBaseFunctor (makeBaseFunctor))
33+
import Data.GenValidity (GenValid)
3334
import Data.Map (Map)
3435
import qualified Data.Map as Map
3536
import Data.Ord.Deriving (deriveOrd1)
3637
import qualified Data.Set as Set
38+
import Data.Validity (Validity)
3739
import Data.Void (Void)
3840
import Debug.Trace (trace, traceShow, traceShowId)
3941
import GHC.Generics (Generic)
4042
import Text.Show.Deriving (deriveShow1)
41-
import Data.Validity (Validity)
42-
import Data.GenValidity (GenValid)
4343

4444
{- top level TODO list
4545
- change AbortFrag form to something more convenient
@@ -126,12 +126,20 @@ data LamType l
126126
| Closed l
127127
deriving (Eq, Show, Ord)
128128

129+
{-
130+
data FunRef v r
131+
= NamedRef v
132+
| ExplicitRef r
133+
deriving (Eq, Show, Ord, Functor, Foldable, Traversable)
134+
-}
135+
129136
-- | Parser AST
130137
data ParserTerm l v
131138
= TZero
132139
| TPair (ParserTerm l v) (ParserTerm l v)
133140
| TVar v
134141
| TApp (ParserTerm l v) (ParserTerm l v)
142+
-- | TPartApp (ParserTerm l v) (ParserTerm l v)
135143
| TCheck (ParserTerm l v) (ParserTerm l v)
136144
| TITE (ParserTerm l v) (ParserTerm l v) (ParserTerm l v)
137145
| TLeft (ParserTerm l v)
@@ -140,8 +148,11 @@ data ParserTerm l v
140148
| THash (ParserTerm l v)
141149
| TChurch Int
142150
| TLam (LamType l) (ParserTerm l v)
151+
-- | TLam [ParserTerm l v] (ParserTerm l v)
152+
-- | TLam l [FunRef v (ParserTerm l v)] (ParserTerm l v)
143153
| TLimitedRecursion (ParserTerm l v) (ParserTerm l v) (ParserTerm l v)
144154
deriving (Eq, Ord, Functor, Foldable, Traversable)
155+
-- deriving (Eq, Ord)
145156
makeBaseFunctor ''ParserTerm -- Functorial version ParserTermF
146157
deriving instance (Show l, Show v, Show a) => Show (ParserTermF l v a)
147158
deriving instance (Show l, Show v) => Show1 (ParserTermF l v)
@@ -152,6 +163,7 @@ instance Eq l => Eq2 (ParserTermF l) where
152163
liftEq2 eqv eqa (TPairF x1 y1) (TPairF x2 y2) = eqa x1 x2 && eqa y1 y2
153164
liftEq2 eqv eqa (TVarF v1) (TVarF v2) = eqv v1 v2
154165
liftEq2 eqv eqa (TAppF x1 y1) (TAppF x2 y2) = eqa x1 x2 && eqa y1 y2
166+
-- liftEq2 eqv eqa (TPartAppF x1 y1) (TPartAppF x2 y2) = eqa x1 x2 && eqa y1 y2
155167
liftEq2 eqv eqa (TCheckF x1 y1) (TCheckF x2 y2) = eqa x1 x2 && eqa y1 y2
156168
liftEq2 eqv eqa (TITEF c1 t1 e1) (TITEF c2 t2 e2) = eqa c1 c2 && eqa t1 t2 && eqa e1 e2
157169
liftEq2 eqv eqa (TLeftF x1) (TLeftF x2) = eqa x1 x2
@@ -160,6 +172,7 @@ instance Eq l => Eq2 (ParserTermF l) where
160172
liftEq2 eqv eqa (THashF x1) (THashF x2) = eqa x1 x2
161173
liftEq2 eqv eqa (TChurchF n1) (TChurchF n2) = n1 == n2
162174
liftEq2 eqv eqa (TLamF l1 x1) (TLamF l2 x2) = l1 == l2 && eqa x1 x2
175+
-- liftEq2 eqv eqa (TLamF v1 x1) (TLamF v2 x2) = and (zipWith eqa v1 v2) && eqa x1 x2
163176
liftEq2 eqv eqa (TLimitedRecursionF a1 b1 c1) (TLimitedRecursionF a2 b2 c2) =
164177
eqa a1 a2 && eqa b1 b2 && eqa c1 c2
165178
liftEq2 _ _ _ _ = False

0 commit comments

Comments
 (0)