Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support functions in instance parameters #2385

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Juvix/Compiler/Internal/Data/InstanceInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Juvix.Prelude
data InstanceParam
= InstanceParamVar VarName
| InstanceParamApp InstanceApp
| InstanceParamFun InstanceFun
| InstanceParamHole Hole
| InstanceParamMeta VarName
deriving stock (Eq)
Expand All @@ -21,6 +22,14 @@ data InstanceApp = InstanceApp
}
deriving stock (Eq)

data InstanceFun = InstanceFun
{ _instanceFunLeft :: InstanceParam,
_instanceFunRight :: InstanceParam,
-- | The original expression from which this InstanceFun was created
_instanceFunExpression :: Expression
}
deriving stock (Eq)

data InstanceInfo = InstanceInfo
{ _instanceInfoInductive :: InductiveName,
_instanceInfoParams :: [InstanceParam],
Expand All @@ -38,6 +47,7 @@ newtype InstanceTable = InstanceTable
}

makeLenses ''InstanceApp
makeLenses ''InstanceFun
makeLenses ''InstanceInfo
makeLenses ''InstanceTable

Expand Down Expand Up @@ -68,8 +78,10 @@ paramToExpression :: InstanceParam -> Expression
paramToExpression = \case
InstanceParamVar v ->
ExpressionIden (IdenVar v)
InstanceParamApp (InstanceApp {..}) ->
InstanceParamApp InstanceApp {..} ->
_instanceAppExpression
InstanceParamFun InstanceFun {..} ->
_instanceFunExpression
InstanceParamHole h -> ExpressionHole h
InstanceParamMeta {} ->
impossible
Expand Down Expand Up @@ -118,6 +130,17 @@ paramFromExpression metaVars e = case e of
}
_ ->
Nothing
ExpressionFunction Function {..}
| _functionLeft ^. paramImplicit == Explicit -> do
l <- paramFromExpression metaVars (_functionLeft ^. paramType)
r <- paramFromExpression metaVars _functionRight
return $
InstanceParamFun
InstanceFun
{ _instanceFunLeft = l,
_instanceFunRight = r,
_instanceFunExpression = e
}
_ ->
Nothing

Expand Down Expand Up @@ -146,3 +169,5 @@ checkNoMeta = \case
InstanceParamMeta {} -> False
InstanceParamHole {} -> True
InstanceParamApp InstanceApp {..} -> all checkNoMeta _instanceAppArgs
InstanceParamFun InstanceFun {..} ->
checkNoMeta _instanceFunLeft && checkNoMeta _instanceFunRight
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ lookupInstance' canFillHoles tab name params = do
(InstanceParamApp app1, InstanceParamApp app2)
| app1 ^. instanceAppHead == app2 ^. instanceAppHead -> do
and <$> sequence (zipWithExact goMatch (app1 ^. instanceAppArgs) (app2 ^. instanceAppArgs))
(InstanceParamFun fun1, InstanceParamFun fun2) -> do
l <- goMatch (fun1 ^. instanceFunLeft) (fun2 ^. instanceFunLeft)
r <- goMatch (fun1 ^. instanceFunRight) (fun2 ^. instanceFunRight)
return $ l && r
(InstanceParamVar {}, _) -> return False
(InstanceParamApp {}, _) -> return False
(InstanceParamFun {}, _) -> return False

lookupInstance ::
forall r.
Expand Down
1 change: 1 addition & 0 deletions tests/Compilation/positive/out/test061.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ abba
abba!
abba3
a :: b :: c :: d :: nil
\{ true := false | false := true }
13 changes: 12 additions & 1 deletion tests/Compilation/positive/test061.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ f'4 {A} {{_ : Show A}} : A → String := Show.show;

f5 {A} {{Show A}} (n : String) (a : A) : String := n ++str Show.show a;

showBoolFun (f : Bool → Bool) : String :=
let
b1 : Bool := f true;
b2 : Bool := f false;
in
"\\{ true := " ++str Show.show b1 ++str " | false := " ++str Show.show b2 ++str " }";

instance
showBoolFunI : Show (Bool → Bool) := mkShow (show := showBoolFun);

main : IO :=
printStringLn (Show.show true) >>
printStringLn (f false) >>
Expand All @@ -63,4 +73,5 @@ main : IO :=
printStringLn (f'3 "abba") >>
printStringLn (f'3 {{M := mkShow (λ{x := x ++str "!"})}} "abba") >>
printStringLn (f5 "abba" 3) >>
printStringLn (Show.show ["a"; "b"; "c"; "d"]);
printStringLn (Show.show ["a"; "b"; "c"; "d"]) >>
printStringLn (Show.show (λ{x := not x}));