-
Notifications
You must be signed in to change notification settings - Fork 47
/
Hell.hs
1244 lines (1112 loc) · 49.8 KB
/
Hell.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Welcome to Hell
--
-- Haskell as a scripting language!
--
-- Special thanks to Stephanie Weirich, whose type-safe typechecker
-- this is built upon, and for the Type.Reflection module, which has
-- made some of this more ergonomic.
{-# LANGUAGE ExistentialQuantification, TypeApplications, BlockArguments, NamedFieldPuns #-}
{-# LANGUAGE GADTs, PolyKinds, TupleSections, StandaloneDeriving, Rank2Types, FlexibleContexts #-}
{-# LANGUAGE ViewPatterns, LambdaCase, ScopedTypeVariables, PatternSynonyms, TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings, MultiWayIf, DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
module Main (main) where
-- All modules tend to be imported qualified by their last component,
-- e.g. 'Data.Graph' becomes 'Graph', and are then exposed to the Hell
-- guest language as such.
import Data.Void
import Data.Foldable
import qualified Language.Haskell.TH as TH
import qualified Language.Haskell.TH.Syntax as TH
import Language.Haskell.TH (Q)
import qualified Data.Graph as Graph
import qualified Data.Eq as Eq
import qualified Data.Either as Either
import qualified Data.Ord as Ord
import qualified Control.Concurrent as Concurrent
import qualified System.Timeout as Timeout
import qualified Data.Bool as Bool
import qualified Data.Map as Map
import qualified Data.List as List
import qualified Data.Set as Set
import qualified Text.Show as Show
import qualified Data.Function as Function
import qualified Data.Generics.Schemes as SYB
import qualified Type.Reflection as Type
import qualified Data.Maybe as Maybe
import qualified Language.Haskell.Exts as HSE
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Builder as ByteString hiding (writeFile)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified System.IO as IO
import qualified UnliftIO.Async as Async
import qualified System.Directory as Dir
import qualified Options.Applicative as Options
-- Things used within the host language.
import Data.Traversable
import Data.Bifunctor
import System.Process.Typed as Process
import Control.Monad.State.Strict
import Control.Monad.Reader
import System.Environment
import Data.Map (Map)
import Data.Set (Set)
import Data.Text (Text)
import Data.ByteString (ByteString)
import GHC.Types
import Type.Reflection (SomeTypeRep(..), TypeRep, typeRepKind, typeRep, pattern TypeRep)
-- Testing support
import Test.Hspec
------------------------------------------------------------------------------
-- Main entry point
data Command
= Run FilePath
| Check FilePath
| Version
| Exec String
main :: IO ()
main = dispatch =<< Options.execParser opts
where
opts = Options.info (commandParser Options.<**> Options.helper)
( Options.fullDesc
<> Options.progDesc "Runs and typechecks Hell scripts"
<> Options.header "hell - A Haskell-driven scripting language" )
commandParser :: Options.Parser Command
commandParser =
Options.asum [
Run <$> Options.strArgument (Options.metavar "FILE" <> Options.help "Run the given .hell file"),
Check <$> Options.strOption (Options.long "check" <> Options.metavar "FILE" <> Options.help "Typecheck the given .hell file"),
Version <$ Options.flag () () (Options.long "version" <> Options.help "Print the version"),
Exec <$> Options.strOption (Options.long "exec" <> Options.help "Execute the given expression" <> Options.metavar "EXPR")
]
dispatch :: Command -> IO ()
dispatch Version = putStrLn "2024-04-12"
dispatch (Run filePath) = do
result <- parseFile filePath
case result of
Left e -> error $ e
Right binds
| anyCycles binds -> error "Cyclic bindings are not supported!"
| otherwise ->
case desugarAll binds of
Left err -> error $ "Error desugaring! " ++ show err
Right terms ->
case lookup "main" terms of
Nothing -> error "No main declaration!"
Just main' ->
case inferExp mempty main' of
Left err -> error $ "Error inferring! " ++ show err
Right uterm ->
case check uterm Nil of
Left err -> error $ "Type checker error: " ++ show err
Right (Typed t ex) ->
case Type.eqTypeRep (typeRepKind t) (typeRep @Type) of
Nothing -> error $ "Kind error, that's nowhere near an IO ()!"
Just Type.HRefl ->
case Type.eqTypeRep t (typeRep @(IO ())) of
Just Type.HRefl ->
let action :: IO () = eval () ex
in action
Nothing -> error $ "Type isn't IO (), but: " ++ show t
dispatch (Check filePath) = do
result <- parseFile filePath
case result of
Left e -> error $ e
Right binds
| anyCycles binds -> error "Cyclic bindings are not supported!"
| otherwise ->
case desugarAll binds of
Left err -> error $ "Error desugaring! " ++ show err
Right terms ->
case lookup "main" terms of
Nothing -> error "No main declaration!"
Just main' ->
case inferExp mempty main' of
Left err -> error $ "Error inferring! " ++ show err
Right uterm ->
case check uterm Nil of
Left err -> error $ "Type checker error: " ++ show err
Right (Typed t _ex) ->
case Type.eqTypeRep (typeRepKind t) (typeRep @Type) of
Nothing -> error $ "Kind error, that's nowhere near an IO ()!"
Just Type.HRefl ->
case Type.eqTypeRep t (typeRep @(IO ())) of
Just Type.HRefl -> pure ()
Nothing -> error $ "Type isn't IO (), but: " ++ show t
dispatch (Exec string) = do
case HSE.parseExpWithMode HSE.defaultParseMode { HSE.extensions = HSE.extensions HSE.defaultParseMode ++ [HSE.EnableExtension HSE.PatternSignatures, HSE.EnableExtension HSE.BlockArguments, HSE.EnableExtension HSE.TypeApplications] } string of
HSE.ParseFailed _ e -> error $ e
HSE.ParseOk e ->
case desugarExp mempty e of
Left err -> error $ "Error desugaring! " ++ show err
Right uterm ->
case inferExp mempty uterm of
Left err -> error $ "Type inferer error! " ++ show err
Right iterm ->
case check iterm Nil of
Left err -> error $ "Type checker error: " ++ show err
Right (Typed t ex) ->
case Type.eqTypeRep (typeRepKind t) (typeRep @Type) of
Nothing -> error $ "Kind error, that's nowhere near an IO ()!"
Just Type.HRefl ->
case Type.eqTypeRep t (typeRep @(IO ())) of
Just Type.HRefl ->
let action :: IO () = eval () ex
in action
Nothing -> error $ "Type isn't IO (), but: " ++ show t
--------------------------------------------------------------------------------
-- Get declarations from the module
parseModule :: HSE.Module HSE.SrcSpanInfo -> HSE.ParseResult [(String, HSE.Exp HSE.SrcSpanInfo)]
parseModule (HSE.Module _ Nothing [] [] decls) =
traverse parseDecl decls
where
parseDecl (HSE.PatBind _ (HSE.PVar _ (HSE.Ident _ string)) (HSE.UnGuardedRhs _ exp') Nothing) =
pure (string, exp')
parseDecl _ = fail "Can't parse that!"
parseModule _ = fail "Module headers aren't supported."
--------------------------------------------------------------------------------
-- Typed AST support
--
-- We define a well-typed, well-indexed GADT AST which can be evaluated directly.
data Term g t where
Var :: Var g t -> Term g t
Lam :: TypeRep (a :: Type) -> Term (g, a) b -> Term g (a -> b)
App :: Term g (s -> t) -> Term g s -> Term g t
Lit :: a -> Term g a
data Var g t where
ZVar :: (t -> a) -> Var (h, t) a
SVar :: Var h t -> Var (h, s) t
--------------------------------------------------------------------------------
-- Evaluator
--
-- This is the entire evaluator. Type-safe and total.
eval :: env -> Term env t -> t
eval env (Var v) = lookp v env
eval env (Lam _ e) = \x -> eval (env, x) e
eval env (App e1 e2) = (eval env e1) (eval env e2)
eval _env (Lit a) = a
-- Type-safe, total lookup. The final @slot@ determines which slot of
-- a given tuple to pick out.
lookp :: Var env t -> env -> t
lookp (ZVar slot) (_, x) = slot x
lookp (SVar v) (env, _) = lookp v env
--------------------------------------------------------------------------------
-- The "untyped" AST
--
-- This is the AST that is not interpreted, and is just
-- type-checked. The HSE AST is desugared into this one.
data UTerm t
= UVar t String
| ULam t Binding (Maybe SomeStarType) (UTerm t)
| UApp t (UTerm t) (UTerm t)
-- IRep below: The variables are poly types, they aren't metavars,
-- and need to be instantiated.
| UForall t [SomeStarType] Forall [TH.Uniq] (IRep TH.Uniq) [t]
deriving (Traversable, Functor, Foldable)
typeOf :: UTerm t -> t
typeOf = \case
UVar t _ -> t
ULam t _ _ _ -> t
UApp t _ _ -> t
UForall t _ _ _ _ _ -> t
data Binding = Singleton String | Tuple [String]
data Forall where
NoClass :: (forall (a :: Type). TypeRep a -> Forall) -> Forall
OrdEqShow :: (forall (a :: Type). (Ord a, Eq a, Show a) => TypeRep a -> Forall) -> Forall
Monadic :: (forall (m :: Type -> Type). (Monad m) => TypeRep m -> Forall) -> Forall
Final :: (forall g. Typed (Term g)) -> Forall
lit :: Type.Typeable a => a -> UTerm ()
lit l = UForall () [] (Final (Typed (Type.typeOf l) (Lit l))) [] (fromSomeStarType (SomeStarType (Type.typeOf l))) []
data SomeStarType = forall (a :: Type). SomeStarType (TypeRep a)
deriving instance Show SomeStarType
instance Eq SomeStarType where
SomeStarType x == SomeStarType y = Type.SomeTypeRep x == Type.SomeTypeRep y
pattern StarTypeRep t <- (toStarType -> Just (SomeStarType t)) where
StarTypeRep t = SomeTypeRep t
toStarType :: SomeTypeRep -> Maybe SomeStarType
toStarType (SomeTypeRep t) = do
Type.HRefl <- Type.eqTypeRep (typeRepKind t) (typeRep @Type)
pure $ SomeStarType t
--------------------------------------------------------------------------------
-- The type checker
data Typed (thing :: Type -> Type) = forall ty. Typed (TypeRep (ty :: Type)) (thing ty)
data TypeCheckError
= NotInScope String
| TupleTypeMismatch
| TypeCheckMismatch
| TupleTypeTooBig
| TypeOfApplicandIsNotFunction
| LambdaIsNotAFunBug
| InferredCheckedDisagreeBug
| LambdaMustBeStarBug
deriving (Show)
typed :: Type.Typeable a => a -> Typed (Term g)
typed l = Typed (Type.typeOf l) (Lit l)
-- The type environment and lookup
data TyEnv g where
Nil :: TyEnv g
Cons :: Binding -> TypeRep (t :: Type) -> TyEnv h -> TyEnv (h, t)
-- The top-level checker used by the main function.
check :: (UTerm SomeTypeRep) -> TyEnv () -> Either TypeCheckError (Typed (Term ()))
check = tc
-- Type check a term given an environment of names.
tc :: (UTerm SomeTypeRep) -> TyEnv g -> Either TypeCheckError (Typed (Term g))
tc (UVar _ v) env = do
Typed ty v' <- lookupVar v env
pure $ Typed ty (Var v')
tc (ULam (StarTypeRep lam_ty) s _ body) env =
case lam_ty of
Type.Fun bndr_ty' _ |
Just Type.HRefl <- Type.eqTypeRep (typeRepKind bndr_ty') (typeRep @Type) ->
case tc body (Cons s bndr_ty' env) of
Left e -> Left e
Right (Typed body_ty' body') ->
let checked_ty = Type.Fun bndr_ty' body_ty'
in
case Type.eqTypeRep checked_ty lam_ty of
Just Type.HRefl -> Right $ Typed lam_ty (Lam bndr_ty' body')
Nothing -> Left InferredCheckedDisagreeBug
_ -> Left LambdaIsNotAFunBug
tc (ULam (SomeTypeRep{}) _ _ _) _ =
Left LambdaMustBeStarBug
tc (UApp _ e1 e2) env =
case tc e1 env of
Left e -> Left e
Right (Typed (Type.Fun bndr_ty body_ty) e1') ->
case tc e2 env of
Left e -> Left e
Right (Typed arg_ty e2') ->
case Type.eqTypeRep arg_ty bndr_ty of
Nothing ->
-- error $ "Type error: " ++ show arg_ty ++ " vs " ++ show bndr_ty
Left TypeCheckMismatch
Just (Type.HRefl) ->
let kind = typeRepKind body_ty
in
case Type.eqTypeRep kind (typeRep @Type) of
Just Type.HRefl -> Right $ Typed body_ty (App e1' e2')
_ -> Left TypeCheckMismatch
Right{} -> Left TypeOfApplicandIsNotFunction
-- Polytyped terms, must be, syntactically, fully-saturated
tc (UForall _ _ fall _ _ reps0) _env = go reps0 fall where
go :: [SomeTypeRep] -> Forall -> Either TypeCheckError (Typed (Term g))
go [] (Final typed') = pure typed'
go (StarTypeRep rep:reps) (NoClass f) = go reps (f rep)
go (StarTypeRep rep:reps) (OrdEqShow f) =
if
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @Int) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @Bool) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @Char) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @Text) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @ByteString) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @ExitCode) -> go reps (f rep)
| otherwise -> error $ "type doesn't have enough instances " ++ show rep
go (SomeTypeRep rep:reps) (Monadic f) =
if
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @IO) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @Maybe) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @[]) -> go reps (f rep)
| Type.App either' _ <- rep,
Just Type.HRefl <- Type.eqTypeRep either' (typeRep @Either) -> go reps (f rep)
| otherwise -> error $ "type doesn't have enough instances " ++ show rep
go _ _ = error "forall type arguments mismatch."
-- Make a well-typed literal - e.g. @lit Text.length@ - which can be
-- embedded in the untyped AST.
lookupVar :: String -> TyEnv g -> Either TypeCheckError (Typed (Var g))
lookupVar str Nil = Left $ NotInScope str
lookupVar v (Cons (Singleton s) ty e)
| v == s = pure $ Typed ty (ZVar id)
| otherwise = do
Typed ty' v' <- lookupVar v e
pure $ Typed ty' (SVar v')
lookupVar v (Cons (Tuple ss) ty e)
| Just i <- lookup v $ zip ss [0 :: Int ..] =
case ty of
Type.App (Type.App tup x) y
| Just Type.HRefl <- Type.eqTypeRep tup (typeRep @(,)) ->
case i of
0 -> pure $ Typed x $ ZVar \(a,_) -> a
1 -> pure $ Typed y $ ZVar \(_,b) -> b
_ -> Left TupleTypeMismatch
Type.App (Type.App (Type.App tup x) y) z
| Just Type.HRefl <- Type.eqTypeRep tup (typeRep @(,,)) ->
case i of
0 -> pure $ Typed x $ ZVar \(a,_,_) -> a
1 -> pure $ Typed y $ ZVar \(_,b,_) -> b
2 -> pure $ Typed z $ ZVar \(_,_,c) -> c
_ -> Left TupleTypeMismatch
Type.App (Type.App (Type.App (Type.App tup x) y) z) z'
| Just Type.HRefl <- Type.eqTypeRep tup (typeRep @(,,,)) ->
case i of
0 -> pure $ Typed x $ ZVar \(a,_,_,_) -> a
1 -> pure $ Typed y $ ZVar \(_,b,_,_) -> b
2 -> pure $ Typed z $ ZVar \(_,_,c,_) -> c
3 -> pure $ Typed z' $ ZVar \(_,_,_,d) -> d
_ -> Left TupleTypeMismatch
_ -> Left TupleTypeTooBig
| otherwise = do
Typed ty' v' <- lookupVar v e
pure $ Typed ty' (SVar v')
--------------------------------------------------------------------------------
-- Desugar expressions
data DesugarError = InvalidConstructor String | InvalidVariable String | UnknownType String | UnsupportedSyntax String | BadParameterSyntax String | KindError | BadDoNotation | TupleTooBig | UnsupportedLiteral
deriving (Show, Eq)
nestedTyApps :: HSE.Exp HSE.SrcSpanInfo -> Maybe (HSE.QName HSE.SrcSpanInfo, [HSE.Type HSE.SrcSpanInfo])
nestedTyApps = go [] where
go acc (HSE.App _ (HSE.Var _ qname) (HSE.TypeApp _ ty)) = pure (qname, ty:acc)
go acc (HSE.App _ e (HSE.TypeApp _ ty)) = go (ty:acc) e
go _ _ = Nothing
desugarExp :: Map String (UTerm ()) -> HSE.Exp HSE.SrcSpanInfo ->
Either DesugarError (UTerm ())
desugarExp globals = go where
go = \case
HSE.Paren _ x -> go x
HSE.If _ i t e ->
(\e' t' i' -> UApp () (UApp () (UApp () bool' e') t') i')
<$> go e <*> go t <*> go i
HSE.Tuple _ HSE.Boxed xs -> do
xs' <- traverse go xs
pure $ foldl (UApp ()) (tuple' (length xs)) xs'
HSE.List _ xs -> do
xs' <- traverse go xs
pure $ foldr (\x y -> UApp () (UApp () cons' x) y) nil' xs'
HSE.Lit _ lit' -> case lit' of
HSE.Char _ char _ -> pure $ lit char
HSE.String _ string _ -> pure $ lit $ Text.pack string
HSE.Int _ int _ -> pure $ lit (fromIntegral int :: Int)
_ -> Left $ UnsupportedLiteral
app@HSE.App{} | Just (qname, tys) <- nestedTyApps app -> do
reps <- traverse desugarType tys
desugarQName globals qname reps
HSE.Var _ qname ->
desugarQName globals qname []
HSE.App _ f x -> UApp () <$> go f <*> go x
HSE.InfixApp _ x (HSE.QVarOp l f) y -> UApp () <$> (UApp () <$> go (HSE.Var l f) <*> go x) <*> go y
HSE.Lambda _ pats e -> do
args <- traverse desugarArg pats
e' <- go e
pure $ foldr (\(name,ty) inner -> ULam () name ty inner) e' args
HSE.Con _ qname ->
desugarQName mempty qname []
HSE.Do _ stmts -> do
let loop f [HSE.Qualifier _ e] = f <$> go e
loop f (s:ss) = do
case s of
HSE.Generator _ pat e -> do
(s', rep) <- desugarArg pat
m <- go e
loop (f . (\f' -> UApp () (UApp () bind' m) (ULam () s' rep f'))) ss
HSE.LetStmt _ (HSE.BDecls _ [HSE.PatBind _ pat (HSE.UnGuardedRhs _ e) Nothing]) -> do
(s', rep) <- desugarArg pat
value <- go e
loop (f . (\f' -> UApp () (ULam () s' rep f') value)) ss
HSE.Qualifier _ e -> do
e' <- go e
loop (f . UApp () (UApp () then' e')) ss
_ -> Left BadDoNotation
loop _ _ = Left BadDoNotation
loop id stmts
e -> Left $ UnsupportedSyntax $ show e
desugarQName :: Map String (UTerm ()) -> HSE.QName HSE.SrcSpanInfo -> [SomeStarType] -> Either DesugarError (UTerm ())
desugarQName globals qname [] =
case qname of
HSE.UnQual _ (HSE.Ident _ string) -> pure $ UVar () string
HSE.Qual _ (HSE.ModuleName _ "Main") (HSE.Ident _ string)
| Just uterm <- Map.lookup string globals ->
pure uterm
HSE.Qual _ (HSE.ModuleName _ prefix) (HSE.Ident _ string)
| Just uterm <- Map.lookup (prefix ++ "." ++ string) supportedLits ->
pure $ uterm
HSE.UnQual _ (HSE.Symbol _ string)
| Just uterm <- Map.lookup string supportedLits ->
pure $ uterm
_ -> desugarPolyQName globals qname []
desugarQName globals qname treps = desugarPolyQName globals qname treps
desugarPolyQName :: Show l => p -> HSE.QName l -> [SomeStarType] -> Either DesugarError (UTerm ())
desugarPolyQName _ qname treps =
case qname of
HSE.Qual _ (HSE.ModuleName _ prefix) (HSE.Ident _ string)
| Just (forall', vars, irep) <- Map.lookup (prefix ++ "." ++ string) polyLits -> do
pure (UForall () treps forall' vars irep [])
HSE.UnQual _ (HSE.Symbol _ string)
| Just (forall', vars, irep) <- Map.lookup string polyLits -> do
pure (UForall () treps forall' vars irep [])
_ -> Left $ InvalidVariable $ show qname
desugarArg :: HSE.Pat HSE.SrcSpanInfo -> Either DesugarError (Binding, Maybe SomeStarType)
desugarArg (HSE.PatTypeSig _ (HSE.PVar _ (HSE.Ident _ i)) typ) =
fmap (Singleton i,) (fmap Just (desugarType typ))
desugarArg (HSE.PatTypeSig _ (HSE.PTuple _ HSE.Boxed idents) typ)
| Just idents' <- traverse desugarIdent idents =
fmap (Tuple idents',) (fmap Just (desugarType typ))
desugarArg (HSE.PVar _ (HSE.Ident _ i)) =
pure (Singleton i,Nothing)
desugarArg (HSE.PTuple _ HSE.Boxed idents)
| Just idents' <- traverse desugarIdent idents =
pure (Tuple idents',Nothing)
desugarArg (HSE.PParen _ p) = desugarArg p
desugarArg p = Left $ BadParameterSyntax $ show p
desugarIdent :: HSE.Pat HSE.SrcSpanInfo -> Maybe String
desugarIdent (HSE.PVar _ (HSE.Ident _ s)) = Just s
desugarIdent _ = Nothing
--------------------------------------------------------------------------------
-- Desugar types
desugarType :: HSE.Type HSE.SrcSpanInfo -> Either DesugarError SomeStarType
desugarType t = do
someRep <- go t
case someRep of
StarTypeRep t' -> pure (SomeStarType t')
_ -> Left KindError
where
go :: HSE.Type HSE.SrcSpanInfo -> Either DesugarError SomeTypeRep
go = \case
HSE.TyTuple _ HSE.Boxed types -> do
tys <- traverse go types
case tys of
[StarTypeRep a,StarTypeRep b] ->
pure $ StarTypeRep (Type.App (Type.App (typeRep @(,)) a) b)
[StarTypeRep a,StarTypeRep b, StarTypeRep c] ->
pure $ StarTypeRep (Type.App (Type.App (Type.App (typeRep @(,,)) a) b) c)
[StarTypeRep a,StarTypeRep b, StarTypeRep c, StarTypeRep d] ->
pure $ StarTypeRep (Type.App (Type.App (Type.App (Type.App (typeRep @(,,,)) a) b) c) d)
_ -> Left TupleTooBig
HSE.TyParen _ x -> go x
HSE.TyCon _ (HSE.UnQual _ (HSE.Ident _ name))
| Just rep <- Map.lookup name supportedTypeConstructors -> pure rep
HSE.TyCon _ (HSE.Special _ HSE.UnitCon{}) -> pure $ StarTypeRep $ typeRep @()
HSE.TyList _ inner -> do
rep <- go inner
case rep of
StarTypeRep t' -> pure $ StarTypeRep $ Type.App (typeRep @[]) t'
_ -> Left KindError
HSE.TyFun _ a b -> do
a' <- go a
b' <- go b
case (a', b') of
(StarTypeRep aRep, StarTypeRep bRep) ->
pure $ StarTypeRep (Type.Fun aRep bRep)
_ -> Left KindError
HSE.TyApp _ f a -> do
f' <- go f
a' <- go a
case applyTypes f' a' of
Just someTypeRep -> pure someTypeRep
_ -> Left KindError
t' -> Left $ UnknownType $ show t'
-- | Supports up to 3-ary type functions, but not more.
applyTypes :: SomeTypeRep -> SomeTypeRep -> Maybe SomeTypeRep
applyTypes (SomeTypeRep f) (SomeTypeRep a) = do
Type.HRefl <- Type.eqTypeRep (typeRepKind a) (typeRep @Type)
if
| Just Type.HRefl <- Type.eqTypeRep (typeRepKind f) (typeRep @(Type -> Type)) ->
pure $ SomeTypeRep $ Type.App f a
| Just Type.HRefl <- Type.eqTypeRep (typeRepKind f) (typeRep @(Type -> Type -> Type)) ->
pure $ SomeTypeRep $ Type.App f a
| Just Type.HRefl <- Type.eqTypeRep (typeRepKind f) (typeRep @(Type -> Type -> Type -> Type)) ->
pure $ SomeTypeRep $ Type.App f a
| Just Type.HRefl <- Type.eqTypeRep (typeRepKind f) (typeRep @(Type -> Type -> Type -> Type -> Type)) ->
pure $ SomeTypeRep $ Type.App f a
| otherwise -> Nothing
desugarTypeSpec :: Spec
desugarTypeSpec = do
it "desugarType" $ do
shouldBe (try "Bool") (Right (SomeStarType $ typeRep @Bool))
shouldBe (try "Int") (Right (SomeStarType $ typeRep @Int))
shouldBe (try "Bool -> Int") (Right (SomeStarType $ typeRep @(Bool -> Int)))
shouldBe (try "()") (Right (SomeStarType $ typeRep @()))
shouldBe (try "[Int]") (Right (SomeStarType $ typeRep @[Int]))
where try e = case fmap (desugarType) $ HSE.parseType e of
HSE.ParseOk r -> r
_ -> error "Parse failed."
--------------------------------------------------------------------------------
-- Desugar all bindings
desugarAll :: [(String, HSE.Exp HSE.SrcSpanInfo)] -> Either DesugarError [(String, UTerm ())]
desugarAll = flip evalStateT Map.empty . traverse go . Graph.flattenSCCs . stronglyConnected where
go :: (String, HSE.Exp HSE.SrcSpanInfo) -> StateT (Map String (UTerm ())) (Either DesugarError) (String, UTerm ())
go (name, expr) = do
globals <- get
uterm <- lift $ desugarExp globals expr
modify' $ Map.insert name uterm
pure (name, uterm)
--------------------------------------------------------------------------------
-- Infer
data InferError =
UnifyError UnifyError | ZonkError ZonkError | ElabError ElaborateError
deriving Show
-- | Note: All types in the input are free of metavars. There is an
-- intermediate phase in which there are metavars, but then they're
-- all eliminated. By the type system, the output contains only
-- determinate types.
inferExp ::
Map String (UTerm SomeTypeRep) ->
UTerm () ->
Either InferError (UTerm SomeTypeRep)
inferExp _ uterm =
case elaborate uterm of
Left elabError -> Left $ ElabError elabError
Right (iterm, equalities) ->
case unify equalities of
Left unifyError -> Left $ UnifyError unifyError
Right subs ->
case traverse (zonkToStarType subs) iterm of
Left zonkError -> Left $ ZonkError $ zonkError
Right sterm -> pure sterm
-- | Zonk a type and then convert it to a type: t :: *
zonkToStarType :: Map IMetaVar (IRep IMetaVar) -> IRep IMetaVar -> Either ZonkError SomeTypeRep
zonkToStarType subs irep = do
zonked <- zonk (substitute subs irep)
toSomeTypeRep zonked
--------------------------------------------------------------------------------
-- Occurs check
anyCycles :: [(String, HSE.Exp HSE.SrcSpanInfo)] -> Bool
anyCycles =
any isCycle .
stronglyConnected
where
isCycle = \case
Graph.CyclicSCC{} -> True
_ -> False
stronglyConnected :: [(String, HSE.Exp HSE.SrcSpanInfo)] -> [Graph.SCC (String, HSE.Exp HSE.SrcSpanInfo)]
stronglyConnected =
Graph.stronglyConnComp .
map \thing@(name, e) -> (thing, name, freeVariables e)
anyCyclesSpec :: Spec
anyCyclesSpec = do
it "anyCycles" do
shouldBe (try [("foo","\\z -> x * Z.y"), ("bar","\\z -> Main.bar * Z.y")]) True
shouldBe (try [("foo","\\z -> Main.bar * Z.y"), ("bar","\\z -> Main.foo * Z.y")]) True
shouldBe (try [("foo","\\z -> x * Z.y"), ("bar","\\z -> Main.mu * Z.y")]) False
shouldBe (try [("foo","\\z -> x * Z.y"), ("bar","\\z -> Main.foo * Z.y")]) False
where
try named =
case traverse (\(n, e) -> (n, ) <$> HSE.parseExp e) named of
HSE.ParseOk decls -> anyCycles decls
_ -> error "Parse failed."
--------------------------------------------------------------------------------
-- Get free variables of an HSE expression
freeVariables :: HSE.Exp HSE.SrcSpanInfo -> [String]
freeVariables =
Maybe.mapMaybe unpack .
SYB.listify (const True :: HSE.QName HSE.SrcSpanInfo -> Bool)
where
unpack = \case
HSE.Qual _ (HSE.ModuleName _ "Main") (HSE.Ident _ name) -> pure name
_ -> Nothing
freeVariablesSpec :: Spec
freeVariablesSpec = do
it "freeVariables" $ shouldBe (try "\\z -> Main.x * Z.y") ["x"]
where try e = case fmap freeVariables $ HSE.parseExp e of
HSE.ParseOk names -> names
_ -> error "Parse failed."
--------------------------------------------------------------------------------
-- Supported type constructors
supportedTypeConstructors :: Map String SomeTypeRep
supportedTypeConstructors = Map.fromList [
("Bool", SomeTypeRep $ typeRep @Bool),
("Int", SomeTypeRep $ typeRep @Int),
("Char", SomeTypeRep $ typeRep @Char),
("Text", SomeTypeRep $ typeRep @Text),
("ByteString", SomeTypeRep $ typeRep @ByteString),
("ExitCode", SomeTypeRep $ typeRep @ExitCode),
("Maybe", SomeTypeRep $ typeRep @Maybe),
("Either", SomeTypeRep $ typeRep @Either),
("IO", SomeTypeRep $ typeRep @IO),
("ProcessConfig", SomeTypeRep $ typeRep @ProcessConfig)
]
--------------------------------------------------------------------------------
-- Support primitives
supportedLits :: Map String (UTerm ())
supportedLits = Map.fromList [
-- Text I/O
("Text.putStrLn", lit t_putStrLn),
("Text.hPutStr", lit t_hPutStr),
("Text.putStr", lit t_putStr),
("Text.getLine", lit t_getLine),
("Text.writeFile", lit t_writeFile),
("Text.readFile", lit t_readFile),
("Text.appendFile", lit t_appendFile),
("Text.readProcess", lit t_readProcess),
("Text.readProcess_", lit t_readProcess_),
("Text.readProcessStdout_", lit t_readProcessStdout_),
("Text.setStdin", lit t_setStdin),
-- Text operations
("Text.eq", lit ((==) @Text)),
("Text.length", lit Text.length),
("Text.concat", lit Text.concat),
("Text.breakOn", lit Text.breakOn),
("Text.lines", lit Text.lines),
("Text.words", lit Text.words),
("Text.unlines", lit Text.unlines),
("Text.unwords", lit Text.unwords),
("Text.intercalate", lit Text.intercalate),
("Text.reverse", lit Text.reverse),
("Text.toLower", lit Text.toLower),
("Text.toUpper", lit Text.toUpper),
-- Needs Char operations.
-- ("Text.any", lit Text.any),
-- ("Text.all", lit Text.all),
-- ("Text.filter", lit Text.filter),
("Text.take", lit Text.take),
("Text.splitOn", lit Text.splitOn),
("Text.takeEnd", lit Text.takeEnd),
("Text.drop", lit Text.drop),
("Text.stripPrefix", lit Text.stripPrefix),
("Text.stripSuffix", lit Text.stripSuffix),
("Text.isSuffixOf", lit Text.isSuffixOf),
("Text.isPrefixOf", lit Text.isPrefixOf),
("Text.dropEnd", lit Text.dropEnd),
("Text.strip", lit Text.strip),
("Text.replace", lit Text.replace),
("Text.isPrefixOf", lit Text.isPrefixOf),
("Text.isSuffixOf", lit Text.isSuffixOf),
("Text.isInfixOf", lit Text.isInfixOf),
-- Int operations
("Int.show", lit (Text.pack . show @Int)),
("Int.eq", lit ((==) @Int)),
("Int.plus", lit ((+) @Int)),
("Int.subtract", lit (subtract @Int)),
-- Bytes I/O
("ByteString.hGet", lit ByteString.hGet),
("ByteString.hPutStr", lit ByteString.hPutStr),
("ByteString.readProcess", lit b_readProcess),
("ByteString.readProcess_", lit b_readProcess_),
("ByteString.readProcessStdout_", lit b_readProcessStdout_),
-- Handles, buffering
("IO.stdout", lit IO.stdout),
("IO.stderr", lit IO.stderr),
("IO.stdin", lit IO.stdin),
("IO.hSetBuffering", lit IO.hSetBuffering),
("IO.NoBuffering", lit IO.NoBuffering),
("IO.LineBuffering", lit IO.LineBuffering),
("IO.BlockBuffering", lit IO.BlockBuffering),
-- Concurrent stuff
("Concurrent.threadDelay", lit Concurrent.threadDelay),
-- Bool
("Bool.True", lit Bool.True),
("Bool.False", lit Bool.False),
("Bool.not", lit Bool.not),
-- Get arguments
("Environment.getArgs", lit $ fmap (map Text.pack) getArgs),
("Environment.getEnvironment", lit $ fmap (map (bimap Text.pack Text.pack)) getEnvironment),
("Environment.getEnv", lit $ fmap Text.pack . getEnv . Text.unpack),
-- Current directory
("Directory.createDirectoryIfMissing", lit (\b f -> Dir.createDirectoryIfMissing b (Text.unpack f))),
("Directory.createDirectory", lit (Dir.createDirectory . Text.unpack)),
("Directory.getCurrentDirectory", lit (fmap Text.pack Dir.getCurrentDirectory)),
("Directory.listDirectory", lit (fmap (fmap Text.pack) . Dir.listDirectory . Text.unpack)),
("Directory.setCurrentDirectory", lit (Dir.setCurrentDirectory . Text.unpack)),
("Directory.renameFile", lit (\x y -> Dir.renameFile (Text.unpack x) (Text.unpack y))),
("Directory.copyFile", lit (\x y -> Dir.copyFile (Text.unpack x) (Text.unpack y))),
-- Process
("Process.proc", lit $ \n xs -> proc (Text.unpack n) (map Text.unpack xs)),
("Process.setEnv", lit $ Process.setEnv @() @() @() . map (bimap Text.unpack Text.unpack)),
("Process.runProcess", lit $ runProcess @IO @() @() @()),
("Process.runProcess_", lit $ runProcess_ @IO @() @() @()),
-- Lists
("List.and", lit (List.and @[])),
("List.or", lit (List.or @[]))
]
--------------------------------------------------------------------------------
-- Derive prims TH
polyLits :: Map String (Forall, [TH.Uniq], IRep TH.Uniq)
polyLits = Map.fromList
$(let
-- Derive well-typed primitive forms.
derivePrims :: Q TH.Exp -> Q TH.Exp
derivePrims m = do
e <- m
case e of
TH.DoE Nothing binds -> do
TH.listE $ map makePrim binds
_ -> error $ "Expected plain do-notation, but got: " ++ show e
nameUnique (TH.Name _ (TH.NameU i)) = i
nameUnique _ = error "Bad TH problem in nameUnique."
toTy :: TH.Type -> Q TH.Exp
toTy = \case
TH.AppT (TH.AppT TH.ArrowT f) x -> [| IFun $(toTy f) $(toTy x) |]
TH.AppT f x -> [| IApp $(toTy f) $(toTy x) |]
TH.ConT name -> [| ICon (SomeTypeRep $(TH.appTypeE (TH.varE 'typeRep) (TH.conT name))) |]
TH.VarT a -> [| IVar $(TH.litE $ TH.IntegerL $ nameUnique a) |]
TH.ListT -> [| ICon (SomeTypeRep (typeRep @[])) |]
TH.TupleT 2 -> [| ICon (SomeTypeRep (typeRep @(,))) |]
TH.TupleT 3 -> [| ICon (SomeTypeRep (typeRep @(,,))) |]
TH.TupleT 4 -> [| ICon (SomeTypeRep (typeRep @(,,,))) |]
TH.TupleT 0 -> [| ICon (SomeTypeRep (typeRep @())) |]
t -> error $ "Uexpected type shape: " ++ show t
-- Make a well-typed primitive form. Expects a very strict format.
makePrim :: TH.Stmt -> Q TH.Exp
makePrim (TH.NoBindS (TH.SigE (TH.AppE (TH.LitE (TH.StringL string)) expr)
(TH.ForallT vars constraints typ))) =
let constrained = foldl getConstraint mempty constraints
vars0 = map (\case
(TH.PlainTV v TH.SpecifiedSpec) -> TH.litE $ TH.IntegerL $ nameUnique v
_ -> error "The type variable isn't what I expected.")
vars
ordEqShow = Set.fromList [''Ord, ''Eq, ''Show]
monadics = Set.fromList [''Functor, ''Applicative, ''Monad]
builder =
foldr
(\case
(TH.PlainTV v TH.SpecifiedSpec) -> \rest ->
TH.appE
(TH.conE (case Map.lookup v constrained of
Nothing -> 'NoClass
Just constraints'
| Set.isSubsetOf constraints' ordEqShow -> 'OrdEqShow
| Set.isSubsetOf constraints' monadics -> 'Monadic
_ -> error "I'm not sure what to do with this variable."))
(TH.lamE [pure $ TH.ConP 'TypeRep [TH.VarT v] []]
rest)
_ -> error "Did not expect this type of variable!")
[| Final $ typed $(TH.sigE (pure expr) (pure typ)) |]
vars
in [| (string, ($builder, $(TH.listE vars0), $(toTy typ))) |]
makePrim e = error $ "Should be of the form \"Some.name\" The.name :: T\ngot: " ++ show e
-- Just tells us whether a given variable is constrained by a
-- type-class or not.
getConstraint m (TH.AppT (TH.ConT cls') (TH.VarT v)) =
Map.insertWith Set.union v (Set.singleton cls') m
getConstraint _ _ = error "Bad constraint!"
in
derivePrims [| do
-- Operators
"$" (Function.$) :: forall a b. (a -> b) -> a -> b
"." (Function..) :: forall a b c. (b -> c) -> (a -> b) -> a -> c
-- Monad
"Monad.bind" (Prelude.>>=) :: forall m a b. Monad m => m a -> (a -> m b) -> m b
"Monad.then" (Prelude.>>) :: forall m a b. Monad m => m a -> m b -> m b
"Monad.return" return :: forall a m. Monad m => a -> m a
-- Monadic operations
"Monad.mapM_" mapM_ :: forall a m. Monad m => (a -> m ()) -> [a] -> m ()
"Monad.forM_" forM_ :: forall a m. Monad m => [a] -> (a -> m ()) -> m ()
"Monad.mapM" mapM :: forall a b m. Monad m => (a -> m b) -> [a] -> m [b]
"Monad.forM" forM :: forall a b m. Monad m => [a] -> (a -> m b) -> m [b]
"Monad.when" when :: forall m. Monad m => Bool -> m () -> m ()
-- IO
"IO.mapM_" mapM_ :: forall a. (a -> IO ()) -> [a] -> IO ()
"IO.forM_" forM_ :: forall a. [a] -> (a -> IO ()) -> IO ()
"IO.pure" pure :: forall a. a -> IO a
"IO.print" (t_putStrLn . Text.pack . Show.show) :: forall a. Show a => a -> IO ()
"Timeout.timeout" Timeout.timeout :: forall a. Int -> IO a -> IO (Maybe a)
-- Show
"Show.show" (Text.pack . Show.show) :: forall a. Show a => a -> Text
-- Eq/Ord
"Eq.eq" (Eq.==) :: forall a. Eq a => a -> a -> Bool
"Ord.lt" (Ord.<) :: forall a. Ord a => a -> a -> Bool
"Ord.gt" (Ord.>) :: forall a. Ord a => a -> a -> Bool
-- Tuples
"Tuple.(,)" (,) :: forall a b. a -> b -> (a,b)
"Tuple.(,)" (,) :: forall a b. a -> b -> (a,b)
"Tuple.(,,)" (,,) :: forall a b c. a -> b -> c -> (a,b,c)
"Tuple.(,,,)" (,,,) :: forall a b c d. a -> b -> c -> d -> (a,b,c,d)
-- Exceptions
"Error.error" (error . Text.unpack) :: forall a. Text -> a
-- Bool
"Bool.bool" Bool.bool :: forall a. a -> a -> Bool -> a
-- Function
"Function.id" Function.id :: forall a. a -> a
"Function.fix" Function.fix :: forall a. (a -> a) -> a
-- Lists
"List.cons" (:) :: forall a. a -> [a] -> [a]
"List.nil" [] :: forall a. [a]
"List.length" List.length :: forall a. [a] -> Int
"List.concat" List.concat :: forall a. [[a]] -> [a]
"List.drop" List.drop :: forall a. Int -> [a] -> [a]
"List.take" List.take :: forall a. Int -> [a] -> [a]
"List.map" List.map :: forall a b. (a -> b) -> [a] -> [b]
"List.lookup" List.lookup :: forall a b. Eq a => a -> [(a,b)] -> Maybe b
"List.sort" List.sort :: forall a. Ord a => [a] -> [a]
"List.reverse" List.reverse :: forall a. [a] -> [a]
"List.sortOn" List.sortOn :: forall a b. Ord b => (a -> b) -> [a] -> [a]
-- Maybe
"Maybe.maybe" Maybe.maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
"Maybe.Nothing" Maybe.Nothing :: forall a. Maybe a
"Maybe.Just" Maybe.Just :: forall a. a -> Maybe a
"Maybe.listToMaybe" Maybe.listToMaybe :: forall a. [a] -> Maybe a
-- Either
"Either.either" Either.either :: forall a b x. (a -> x) -> (b -> x) -> Either a b -> x
"Either.Left" Either.Left :: forall a b. a -> Either a b
"Either.Right" Either.Right :: forall a b. b -> Either a b
-- Async
"Async.concurrently" Async.concurrently :: forall a b. IO a -> IO b -> IO (a,b)
"Async.race" Async.race :: forall a b. IO a -> IO b -> IO (Either a b)
"Async.pooledMapConcurrently_" Async.pooledMapConcurrently_ :: forall a. (a -> IO ()) -> [a] -> IO ()
"Async.pooledForConcurrently_" Async.pooledForConcurrently_ :: forall a. [a] -> (a -> IO ()) -> IO ()
"Async.pooledMapConcurrently" Async.pooledMapConcurrently :: forall a b. (a -> IO b) -> [a] -> IO [b]
"Async.pooledForConcurrently" Async.pooledForConcurrently :: forall a b. [a] -> (a -> IO b) -> IO [b]
|])
--------------------------------------------------------------------------------
-- Internal-use only, used by the desugarer
cons' :: UTerm ()
cons' = unsafeGetForall "List.cons"
nil' :: UTerm ()
nil' = unsafeGetForall "List.nil"
bool' :: UTerm ()
bool' = unsafeGetForall "Bool.bool"
then' :: UTerm ()
then' = unsafeGetForall "Monad.then"
bind' :: UTerm ()
bind' = unsafeGetForall "Monad.bind"
tuple' :: Int -> UTerm ()
tuple' 0 = unsafeGetForall "Tuple.()"
tuple' 2 = unsafeGetForall "Tuple.(,)"
tuple' 3 = unsafeGetForall "Tuple.(,,)"
tuple' 4 = unsafeGetForall "Tuple.(,,,)"
tuple' _ = error "Bad compile-time lookup for tuple'."
unsafeGetForall :: String -> UTerm ()
unsafeGetForall key = Maybe.fromMaybe (error $ "Bad compile-time lookup for " ++ key) $ do
(forall', vars, irep) <- Map.lookup key polyLits
pure (UForall () [] forall' vars irep [])
--------------------------------------------------------------------------------
-- UTF-8 specific operations without all the environment gubbins
--
-- Much better than what Data.Text.IO provides
t_setStdin :: Text -> ProcessConfig () () () -> ProcessConfig () () ()
t_setStdin text = setStdin (byteStringInput (L.fromStrict (Text.encodeUtf8 text)))
t_readProcess :: ProcessConfig () () () -> IO (ExitCode, Text, Text)
t_readProcess c = do
(code, out, err) <- b_readProcess c
pure (code, Text.decodeUtf8 out, Text.decodeUtf8 err)
t_readProcess_ :: ProcessConfig () () () -> IO (Text, Text)
t_readProcess_ c = do
(out, err) <- b_readProcess_ c
pure (Text.decodeUtf8 out, Text.decodeUtf8 err)
t_readProcessStdout_ :: ProcessConfig () () () -> IO Text
t_readProcessStdout_ c = do
out <- b_readProcessStdout_ c
pure (Text.decodeUtf8 out)
t_putStrLn :: Text -> IO ()
t_putStrLn = ByteString.hPutBuilder IO.stdout . (<>"\n") . ByteString.byteString . Text.encodeUtf8
t_hPutStr :: IO.Handle -> Text -> IO ()
t_hPutStr h = ByteString.hPutBuilder h . ByteString.byteString . Text.encodeUtf8
t_putStr :: Text -> IO ()
t_putStr = t_hPutStr IO.stdout
t_getLine :: IO Text
t_getLine = fmap Text.decodeUtf8 ByteString.getLine
t_writeFile :: Text -> Text -> IO ()
t_writeFile fp t = ByteString.writeFile (Text.unpack fp) (Text.encodeUtf8 t)
t_appendFile :: Text -> Text -> IO ()
t_appendFile fp t = ByteString.appendFile (Text.unpack fp) (Text.encodeUtf8 t)
t_readFile :: Text -> IO Text
t_readFile fp = fmap Text.decodeUtf8 (ByteString.readFile (Text.unpack fp))
--------------------------------------------------------------------------------
-- ByteString operations
b_readProcess :: ProcessConfig () () () -> IO (ExitCode, ByteString, ByteString)
b_readProcess c = do
(code, out, err) <- readProcess c
pure (code, L.toStrict out, L.toStrict err)
b_readProcess_ :: ProcessConfig () () () -> IO (ByteString, ByteString)