Skip to content

Commit

Permalink
I am very suspicious about how well this works...
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed Dec 5, 2023
1 parent a77c9df commit eba4527
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 104 deletions.
Binary file modified build/Prelude.class
Binary file not shown.
Binary file added build/Test.class
Binary file not shown.
24 changes: 24 additions & 0 deletions build/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Elara.Prim;
import elara.IO;
import elara.Prelude;

//
// Decompiled by Procyon v0.5.36
//

public class Test
{
public static IO main;

static {
main = Prim.println(Prim.toString(fact(5)));
}

public static void main(String[] var0) {
main.run();
}

public static Integer fact(Integer var0) {
return Prim.eq(var0, Integer.valueOf(0)) ? 1 : Prim.times(var0, fact(Prim.minus(var0, 1)));
}
}
48 changes: 24 additions & 24 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

diagnose = {
extraBuildDepends = [
pkgs.haskellPackages.megaparsec_9_5_0
pkgs.haskellPackages.megaparsec_9_6_1
];
cabalFlags.megaparsec-compat = true;
jailbreak = true;
Expand All @@ -69,8 +69,6 @@
h2jvm = {
# Skip the tests due to conflicting base version
check = false;


};

ghcid = {
Expand Down
1 change: 0 additions & 1 deletion result

This file was deleted.

18 changes: 9 additions & 9 deletions src/Elara/AST/Generic/Instances/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Elara.AST.StripLocation
import Elara.Data.Pretty
import Prelude hiding (group)

deriving instance (Pretty (ASTLocate ast (BinaryOperator' ast))) => Pretty (BinaryOperator ast)
deriving instance Pretty (ASTLocate ast (BinaryOperator' ast)) => Pretty (BinaryOperator ast)

instance
( Pretty (CleanupLocated (ASTLocate' ast (Select "SymOp" ast)))
Expand All @@ -26,16 +26,15 @@ instance
pretty (SymOp op) = pretty op
pretty (Infixed op) = "`" <> pretty op <> "`"

deriving instance (Pretty (ASTLocate ast (Type' ast))) => Pretty (Type ast)
deriving instance Pretty (ASTLocate ast (Type' ast)) => Pretty (Type ast)

instance
( Pretty (ASTLocate ast (Declaration' ast))
) =>
Pretty (ASTLocate ast (Declaration' ast)) =>
Pretty (Declaration ast)
where
pretty (Declaration ldb) = pretty ldb

data UnknownPretty = forall a. (Pretty a) => UnknownPretty a
data UnknownPretty = forall a. Pretty a => UnknownPretty a

instance Pretty UnknownPretty where
pretty (UnknownPretty a) = pretty a
Expand Down Expand Up @@ -68,8 +67,10 @@ instance
-- The converting of values to a 'Maybe' is handled by the 'ToMaybe' class.
prettyDB n (Value e@(Expr (_, t)) _ t') =
let typeOfE =
UnknownPretty <$> (toMaybe t :: Maybe exprType) -- Prioritise the type in the expression
<|> UnknownPretty <$> (toMaybe t' :: Maybe valueType) -- Otherwise, use the type in the declaration
UnknownPretty
<$> (toMaybe t :: Maybe exprType) -- Prioritise the type in the expression
<|> UnknownPretty
<$> (toMaybe t' :: Maybe valueType) -- Otherwise, use the type in the declaration
in prettyValueDeclaration n e typeOfE
prettyDB n (TypeDeclaration vars t) = prettyTypeDeclaration n vars t

Expand Down Expand Up @@ -247,8 +248,7 @@ instance

prettyPattern ::
forall ast.
(_) =>
(?contextFree :: Bool) =>
(?contextFree :: Bool, _) =>
Pattern' ast ->
Doc AnsiStyle
prettyPattern (VarPattern v) = pretty v
Expand Down
16 changes: 8 additions & 8 deletions src/Elara/AST/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ prettyStringExpr = dquotes . pretty
prettyCharExpr :: Char -> Doc AnsiStyle
prettyCharExpr = squotes . escapeChar

prettyLambdaExpr :: (?contextFree :: Bool) => (Pretty a, Pretty b) => [a] -> b -> Doc AnsiStyle
prettyLambdaExpr :: (?contextFree :: Bool, Pretty a, Pretty b) => [a] -> b -> Doc AnsiStyle
prettyLambdaExpr args body = parens (if ?contextFree then prettyCTFLambdaExpr else prettyLambdaExpr')
where
prettyCTFLambdaExpr =
Expand Down Expand Up @@ -73,7 +73,7 @@ prettyBinaryOperatorExpr e1 o e2 =
<+> parensIf (shouldParen e2) (pretty e2)
)

prettyTupleExpr :: (Pretty a) => NonEmpty a -> Doc AnsiStyle
prettyTupleExpr :: Pretty a => NonEmpty a -> Doc AnsiStyle
prettyTupleExpr l = parens (hsep (punctuate "," (pretty <$> toList l)))

prettyMatchExpr :: (Pretty a1, Pretty a2, Foldable t, ?contextFree :: Bool) => a1 -> t a2 -> Doc AnsiStyle
Expand All @@ -98,13 +98,13 @@ prettyLetInExpr v ps e1 e2 =
<+> "in"
<+> blockParensIf (?contextFree && shouldBrace e2) (pretty e2)

shouldBrace :: forall astK (ast :: astK). (RUnlocate ast) => Expr ast -> Bool
shouldBrace :: forall astK (ast :: astK). RUnlocate ast => Expr ast -> Bool
shouldBrace x = case (x ^. _Unwrapped . _1 . to (rUnlocate @astK @ast)) :: Expr' ast of
Block _ -> False
Let{} -> True
_ -> False

shouldParen :: forall astK (ast :: astK). (RUnlocate ast) => Expr ast -> Bool
shouldParen :: forall astK (ast :: astK). RUnlocate ast => Expr ast -> Bool
shouldParen x = case (x ^. _Unwrapped . _1 . to (rUnlocate @astK @ast)) :: Expr' ast of
Block _ -> True
Let{} -> True
Expand All @@ -126,10 +126,10 @@ prettyBlockExpr b = do
separator = if ?contextFree then "; " else flatAlt "" "; "
arrange = if ?contextFree then identity else group . align

arrange (encloseSep' open close separator (pretty <$> toList b))
arrange (encloseSep' ?contextFree open close separator (pretty <$> toList b))

encloseSep' :: (?contextFree :: Bool) => Doc AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle -> [Doc AnsiStyle] -> Doc AnsiStyle
encloseSep' = if ?contextFree then encloseSepUnarranged else encloseSep
encloseSep' :: Bool -> Doc AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle -> [Doc AnsiStyle] -> Doc AnsiStyle
encloseSep' contextFree = if contextFree then encloseSepUnarranged else encloseSep
where
encloseSepUnarranged :: Doc AnsiStyle -> Doc AnsiStyle -> Doc AnsiStyle -> [Doc AnsiStyle] -> Doc AnsiStyle
encloseSepUnarranged open close _ [] = open <> close
Expand All @@ -141,7 +141,7 @@ prettyConstructorPattern c p = parens (pretty c <+> hsep (pretty <$> p))
prettyList :: (Pretty a, ?contextFree :: Bool) => [a] -> Doc AnsiStyle
prettyList l =
if ?contextFree
then encloseSep' "[ " " ]" ", " (pretty <$> l)
then encloseSep' ?contextFree "[ " " ]" ", " (pretty <$> l)
else list (pretty <$> l)

prettyConsPattern :: (Pretty a1, Pretty a2) => a1 -> a2 -> Doc AnsiStyle
Expand Down
3 changes: 2 additions & 1 deletion src/Elara/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ instance Plated (Expr b) where
TyLam t e -> TyLam t <$> f e
Let b e -> (Let b <$> f e)
Match e b as -> Match <$> f e <*> pure b <*> traverse (traverse3 f) as
where traverse3 f (a, b, c) = ((,,) a b <$> f c)
where
traverse3 f (a, b, c) = ((,,) a b <$> f c)

type CoreExpr = Expr Var

Expand Down
27 changes: 14 additions & 13 deletions src/Elara/Core/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Elara.Data.Pretty
import Elara.Prim.Core (listCon)
import Prelude hiding (Alt)

class PrettyVar v where
class Pretty v => PrettyVar v where
prettyVar ::
-- | With type
Bool ->
Expand All @@ -24,6 +24,10 @@ class PrettyVar v where

prettyVarArg :: v -> Doc AnsiStyle

instance Pretty Var where
pretty :: Var -> Doc AnsiStyle
pretty = prettyVar True True

instance PrettyVar Var where
prettyVar withType withParens = \case
TyVar tv -> prettyTypeVariable withType tv
Expand All @@ -43,13 +47,10 @@ instance PrettyVar Type where
TyVarTy (TypeVariable tv _) -> parens ("@" <> pretty tv)
v -> prettyVar True True v

instance (PrettyVar v, Show v) => Pretty (Expr v) where
instance {-# OVERLAPS #-} PrettyVar v => Pretty (Expr v) where
pretty = prettyExpr

instance {-# OVERLAPPABLE #-} PrettyVar v => Pretty v where
pretty = prettyVar True True

prettyTLLam :: (PrettyVar v1, PrettyVar v2, Show v2) => v1 -> Expr v2 -> Doc AnsiStyle
prettyTLLam :: (PrettyVar v1, PrettyVar v2) => v1 -> Expr v2 -> Doc AnsiStyle
prettyTLLam b e@(Lam _ _) = "\\" <+> prettyVarArg b <+> prettyLam e
prettyTLLam b e = "\\" <+> prettyVarArg b <+> "->" <+> prettyExpr e

Expand All @@ -58,7 +59,7 @@ prettyLam (Lam b e@(Lam _ _)) = prettyVarArg b <+> prettyLam e
prettyLam (Lam b e) = prettyVarArg b <+> "->" <+> prettyLam e
prettyLam e = pretty e

prettyExpr :: (Pretty (Expr v), PrettyVar v, Show v, HasCallStack) => Expr v -> Doc AnsiStyle
prettyExpr :: (Pretty (Expr v), PrettyVar v, HasCallStack) => Expr v -> Doc AnsiStyle
prettyExpr (Lam b e) = prettyTLLam b e
prettyExpr (TyLam b e) = prettyTLLam b e
prettyExpr (Let bindings e) = "let" <+> prettyVdefg bindings <+> "in" <+> prettyExpr e
Expand All @@ -69,30 +70,30 @@ prettyExpr (Match e of' alts) =
]
prettyExpr other = prettyExpr1 other

prettyExpr1 :: (Pretty (Expr v), PrettyVar v, Show v, HasCallStack) => Expr v -> Doc AnsiStyle
prettyExpr1 :: (Pretty (Expr v), PrettyVar v) => Expr v -> Doc AnsiStyle
prettyExpr1 (TyApp f t) = prettyExpr1 f <+> "@" <> prettyTy2 t
prettyExpr1 (App f x) = prettyExpr1 f <+> prettyExpr2 x
prettyExpr1 e = prettyExpr2 e

prettyExpr2 :: (Pretty (Expr v), PrettyVar v, Show v, HasCallStack) => Expr v -> Doc AnsiStyle
prettyExpr2 :: (Pretty (Expr v), PrettyVar v) => Expr v -> Doc AnsiStyle
prettyExpr2 (Var v) = prettyVar False False v
prettyExpr2 (Lit l) = pretty l
prettyExpr2 e = parens (prettyExpr e)

prettyVdefg :: (PrettyVar v, Pretty (Expr v), Show v) => Bind v -> Doc AnsiStyle
prettyVdefg :: (PrettyVar v, Pretty (Expr v)) => Bind v -> Doc AnsiStyle
prettyVdefg (Recursive bindings) = "Rec" <> let ?contextFree = True in prettyBlockExpr (prettyVdef <$> bindings)
prettyVdefg (NonRecursive b) = prettyVdef b

prettyVdef :: (PrettyVar v, Pretty (Expr v), Show v) => (v, Expr v) -> Doc AnsiStyle
prettyVdef :: (PrettyVar v, Pretty (Expr v)) => (v, Expr v) -> Doc AnsiStyle
prettyVdef (v, e) = vsep [prettyVar True False v, indent indentDepth ("=" <+> prettyExpr e)]

prettyVBind :: PrettyVar v => v -> Doc AnsiStyle
prettyVBind = prettyVar True True

prettyAlts :: (PrettyVar v, Show v) => [Alt v] -> Doc AnsiStyle
prettyAlts :: PrettyVar v => [Alt v] -> Doc AnsiStyle
prettyAlts alts = let ?contextFree = True in prettyBlockExpr (prettyAlt <$> alts)
where
prettyAlt (con, vars, e) = pretty con <+> hsep (prettyVarArg <$> vars) <+> "->" <+> prettyExpr e
prettyAlt (con, vars, e) = pretty @AltCon con <+> hsep (prettyVarArg <$> vars) <+> "->" <+> prettyExpr e

instance Pretty Literal where
pretty :: Literal -> Doc AnsiStyle
Expand Down
4 changes: 2 additions & 2 deletions src/Elara/Data/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ instance (Pretty a, Pretty b, Pretty c) => Pretty (a, b, c) where
instance (Pretty a, Pretty b, Pretty c, Pretty d) => Pretty (a, b, c, d) where
pretty (a, b, c, d) = tupled [pretty a, pretty b, pretty c, pretty d]

-- instance {-# OVERLAPPABLE #-} (PP.Pretty a) => Pretty a where
-- pretty = PP.pretty
instance {-# OVERLAPS #-} PP.Pretty a => Pretty a where
pretty = PP.pretty

escapeChar :: IsString s => Char -> s
escapeChar c = case c of
Expand Down
Loading

0 comments on commit eba4527

Please sign in to comment.