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

feat: Allow writing holes which are solved by BRAT #59

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fec6dc4
Add holes(!)
croyzor Nov 8, 2024
3c1f4ec
WIP: Merge better nat solving
croyzor Nov 8, 2024
0f7d13d
Revert `run` namespacing changes
croyzor Nov 8, 2024
a53435b
Add defines
croyzor Nov 26, 2024
fec3263
rename some variables
croyzor Nov 26, 2024
c8cb33f
drive-by: Give everything created with anext a label
croyzor Nov 27, 2024
5b2cb45
Replace define -> instantiate
croyzor Nov 27, 2024
ed531ae
Define targets instead of sources (fixes unified.brat)
croyzor Nov 27, 2024
c48da2c
fix: Dodgy demandSucc logic
croyzor Nov 27, 2024
95a4c2d
Define tgts to respective srcs in invertNatVal
croyzor Nov 27, 2024
37ef790
Allow hopes to be removed from the set
croyzor Nov 27, 2024
7626077
Merge remote-tracking branch 'origin/main' into holes
croyzor Nov 27, 2024
cebc93b
Cleanup Error.hs
croyzor Nov 27, 2024
c6e7c3c
Add golden file for unsolved hope error
croyzor Nov 27, 2024
4e776cc
XFAIL infer.brat because we don't compile `Pow`
croyzor Nov 27, 2024
541f35a
Apply lints
croyzor Nov 27, 2024
8ad05de
Revert "drive-by: Give everything created with anext a label"
croyzor Nov 27, 2024
3fe88aa
Merge remote-tracking branch 'origin/main' into holes
croyzor Nov 28, 2024
0cef0d3
drive-by: Give labels to more `next` calls
croyzor Nov 28, 2024
ef85987
Merge remote-tracking branch 'origin/main' into holes
croyzor Dec 3, 2024
c4ebd8c
[cleanup] Remove News datatype from this PR
croyzor Dec 3, 2024
991e0dc
Update brat/Brat/Checker/Monad.hs
croyzor Dec 3, 2024
f82f25c
refactor: Move Nat building code to Helpers
croyzor Dec 3, 2024
f75ce8c
refactor: Replace mkStaticNum with a call to buildNatVal
croyzor Dec 3, 2024
da08608
Revert changes to FC
croyzor Dec 3, 2024
575474e
Apply suggestions from code review
croyzor Dec 3, 2024
c6555b9
rename HopeSet -> Hopes
croyzor Dec 3, 2024
d077d4c
refactor: Make GHC enforce that Hopes are InPorts
croyzor Dec 3, 2024
741d896
refactor: Curry ANewHope
croyzor Dec 3, 2024
bb431ef
refactor: Expose only a simpler version of typeEq
croyzor Dec 3, 2024
b846a62
cosmetic: Get rid of unused variable
croyzor Dec 3, 2024
8bee0d0
cleanup: Get rid of outdated comments
croyzor Dec 3, 2024
631509e
Don't reexport nat building from SolveHoles
croyzor Dec 3, 2024
5a3ffd8
[broken] New example
croyzor Dec 3, 2024
7286b0c
refactor: Make better use of `toEnd` function
croyzor Dec 3, 2024
787baed
Fill in the missing case for demandSucc
croyzor Dec 3, 2024
ca5df29
fix: Add missing unelab cases
croyzor Dec 6, 2024
837eeed
lint: Redundant brackets
croyzor Dec 6, 2024
722e610
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Dec 9, 2024
da57ef6
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Dec 9, 2024
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
Prev Previous commit
Next Next commit
refactor: Move Nat building code to Helpers
  • Loading branch information
croyzor committed Dec 3, 2024
commit f82f25c5dfb0291a664db759e24cfcece42ab0fd
93 changes: 93 additions & 0 deletions brat/Brat/Checker/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,96 @@ buildConst :: SimpleTerm -> Val Z -> Checking Src
buildConst tm ty = do
(_, _, [(out,_)], _) <- next "const" (Const tm) (S0, Some (Zy :* S0)) R0 (RPr ("value", ty) R0)
croyzor marked this conversation as resolved.
Show resolved Hide resolved
pure out

buildNum :: Integer -> Checking Src
buildNum n = buildConst (Num (fromIntegral n)) TNat

-- Generate wiring to produce a dynamic instance of the numval argument
-- N.B. In these functions, we wire using Req, rather than the `wire` function
-- because we don't want it to do any extra evaluation.
buildNatVal :: NumVal (VVar Z) -> Checking Src
buildNatVal nv@(NumValue n gro) = case n of
0 -> buildGro gro
n -> do
nDangling <- buildNum n
((lhs,rhs),out) <- buildArithOp Add
src <- buildGro gro
req $ Wire (end nDangling, TNat, end lhs)
req $ Wire (end src, TNat, end rhs)
defineSrc out (VNum (nPlus n (nVar (VPar (toEnd src)))))
pure out
where
buildGro :: Fun00 (VVar Z) -> Checking Src
buildGro Constant0 = buildNum 0
buildGro (StrictMonoFun sm) = buildSM sm

buildSM :: StrictMono (VVar Z) -> Checking Src
buildSM (StrictMono k mono) = do
-- Calculate 2^k as `factor`
two <- buildNum 2
kDangling <- buildNum k
((lhs,rhs),factor) <- buildArithOp Pow
req $ Wire (end two, TNat, end lhs)
req $ Wire (end kDangling, TNat, end rhs)
-- Multiply mono by 2^k
((lhs,rhs),out) <- buildArithOp Mul
monoDangling <- buildMono mono
req $ Wire (end factor, TNat, end lhs)
req $ Wire (end monoDangling, TNat, end rhs)
defineSrc out (VNum (n2PowTimes k (nVar (VPar (toEnd monoDangling)))))
pure out

buildMono :: Monotone (VVar Z) -> Checking Src
buildMono (Linear (VPar (ExEnd e))) = pure $ NamedPort e "numval"
buildMono (Full sm) = do
-- Calculate 2^n as `outPlus1`
two <- buildNum 2
dangling <- buildSM sm
((lhs,rhs),outPlus1) <- buildArithOp Pow
req $ Wire (end two, TNat, end lhs)
req $ Wire (end dangling, TNat, end rhs)
-- Then subtract 1
one <- buildNum 1
((lhs,rhs),out) <- buildArithOp Sub
req $ Wire (end outPlus1, TNat, end lhs)
req $ Wire (end one, TNat, end rhs)
defineSrc out (VNum (nFull (nVar (VPar (toEnd dangling)))))
pure out
buildMono _ = err . InternalError $ "Trying to build a non-closed nat value: " ++ show nv

invertNatVal :: NumVal (VVar Z) -> Checking Tgt
invertNatVal (NumValue up gro) = case up of
0 -> invertGro gro
_ -> do
((lhs,rhs),out) <- buildArithOp Sub
upSrc <- buildNum up
req $ Wire (end upSrc, TNat, end rhs)
tgt <- invertGro gro
req $ Wire (end out, TNat, end tgt)
defineTgt tgt (VNum (nVar (VPar (toEnd out))))
defineTgt lhs (VNum (nPlus up (nVar (VPar (toEnd tgt)))))
pure lhs
where
invertGro Constant0 = error "Invariant violated: the numval arg to invertNatVal should contain a variable"
invertGro (StrictMonoFun sm) = invertSM sm

invertSM (StrictMono k mono) = case k of
0 -> invertMono mono
_ -> do
divisor <- buildNum (2 ^ k)
((lhs,rhs),out) <- buildArithOp Div
tgt <- invertMono mono
req $ Wire (end out, TNat, end tgt)
req $ Wire (end divisor, TNat, end rhs)
defineTgt tgt (VNum (nVar (VPar (toEnd out))))
defineTgt lhs (VNum (n2PowTimes k (nVar (VPar (toEnd tgt)))))
pure lhs

invertMono (Linear (VPar (InEnd e))) = pure (NamedPort e "numval")
invertMono (Full sm) = do
(_, [(llufTgt,_)], [(llufSrc,_)], _) <- next "luff" (Prim ("BRAT","lluf")) (S0, Some (Zy :* S0)) (REx ("n", Nat) R0) (REx ("n", Nat) R0)
tgt <- invertSM sm
req $ Wire (end llufSrc, TNat, end tgt)
defineTgt tgt (VNum (nVar (VPar (toEnd llufSrc))))
defineTgt llufTgt (VNum (nFull (nVar (VPar (toEnd tgt)))))
pure llufTgt
99 changes: 1 addition & 98 deletions brat/Brat/Checker/SolveHoles.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ module Brat.Checker.SolveHoles (typeEq, buildNatVal, buildNum, invertNatVal) whe

import Brat.Checker.Monad
import Brat.Checker.Types (kindForMode)
import Brat.Checker.Helpers (buildArithOp, buildConst, defineSrc, defineTgt, next)
import Brat.Checker.Helpers (buildConst, buildNatVal, buildNum, invertNatVal)
import Brat.Error (ErrorMsg(..))
import Brat.Eval
import Brat.Graph (NodeType(..))
import Brat.Syntax.Common
import Brat.Syntax.Port (ToEnd(..))
import Brat.Syntax.Simple (SimpleTerm(..))
import Brat.Syntax.Value
import Control.Monad.Freer
Expand Down Expand Up @@ -154,98 +152,3 @@ typeEqRigid tm lvkz (TypeFor _ []) (VSum m0 rs0) (VSum m1 rs1)
where
eqVariant (Some r0, Some r1) = throwLeft (snd <$> typeEqRow m0 tm lvkz r0 r1)
typeEqRigid tm _ _ v0 v1 = err $ TypeMismatch tm (show v0) (show v1)

wire :: (Src, Val Z, Tgt) -> Checking ()
wire (src, ty, tgt) = req $ Wire (end src, ty, end tgt)

buildNum :: Integer -> Checking Src
buildNum n = buildConst (Num (fromIntegral n)) TNat


-- Generate wiring to produce a dynamic instance of the numval argument
buildNatVal :: NumVal (VVar Z) -> Checking Src
buildNatVal nv@(NumValue n gro) = case n of
0 -> buildGro gro
n -> do
nDangling <- buildNum n
((lhs,rhs),out) <- buildArithOp Add
src <- buildGro gro
wire (nDangling, TNat, lhs)
wire (src, TNat, rhs)
defineSrc out (VNum (nPlus n (nVar (VPar (toEnd src)))))
pure out
where
buildGro :: Fun00 (VVar Z) -> Checking Src
buildGro Constant0 = buildNum 0
buildGro (StrictMonoFun sm) = buildSM sm

buildSM :: StrictMono (VVar Z) -> Checking Src
buildSM (StrictMono k mono) = do
-- Calculate 2^k as `factor`
two <- buildNum 2
kDangling <- buildNum k
((lhs,rhs),factor) <- buildArithOp Pow
wire (two, TNat, lhs)
wire (kDangling, TNat, rhs)
-- Multiply mono by 2^k
((lhs,rhs),out) <- buildArithOp Mul
monoDangling <- buildMono mono
wire (factor, TNat, lhs)
wire (monoDangling, TNat, rhs)
defineSrc out (VNum (n2PowTimes k (nVar (VPar (toEnd monoDangling)))))
pure out

buildMono :: Monotone (VVar Z) -> Checking Src
buildMono (Linear (VPar (ExEnd e))) = pure $ NamedPort e "numval"
buildMono (Full sm) = do
-- Calculate 2^n as `outPlus1`
two <- buildNum 2
dangling <- buildSM sm
((lhs,rhs),outPlus1) <- buildArithOp Pow
wire (two, TNat, lhs)
wire (dangling, TNat, rhs)
-- Then subtract 1
one <- buildNum 1
((lhs,rhs),out) <- buildArithOp Sub
wire (outPlus1, TNat, lhs)
wire (one, TNat, rhs)
defineSrc out (VNum (nFull (nVar (VPar (toEnd dangling)))))
pure out
buildMono _ = err . InternalError $ "Trying to build a non-closed nat value: " ++ show nv

invertNatVal :: NumVal (VVar Z) -> Checking Tgt
invertNatVal (NumValue up gro) = case up of
0 -> invertGro gro
_ -> do
((lhs,rhs),out) <- buildArithOp Sub
upSrc <- buildNum up
wire (upSrc, TNat, rhs)
tgt <- invertGro gro
wire (out, TNat, tgt)
defineTgt tgt (VNum (nVar (VPar (toEnd out))))
defineTgt lhs (VNum (nPlus up (nVar (VPar (toEnd tgt)))))
pure lhs
where
invertGro Constant0 = error "Invariant violated: the numval arg to invertNatVal should contain a variable"
invertGro (StrictMonoFun sm) = invertSM sm

invertSM (StrictMono k mono) = case k of
0 -> invertMono mono
_ -> do
divisor <- buildNum (2 ^ k)
((lhs,rhs),out) <- buildArithOp Div
tgt <- invertMono mono
wire (out, TNat, tgt)
wire (divisor, TNat, rhs)
defineTgt tgt (VNum (nVar (VPar (toEnd out))))
defineTgt lhs (VNum (n2PowTimes k (nVar (VPar (toEnd tgt)))))
pure lhs

invertMono (Linear (VPar (InEnd e))) = pure (NamedPort e "numval")
invertMono (Full sm) = do
(_, [(llufTgt,_)], [(llufSrc,_)], _) <- next "luff" (Prim ("BRAT","lluf")) (S0, Some (Zy :* S0)) (REx ("n", Nat) R0) (REx ("n", Nat) R0)
tgt <- invertSM sm
wire (llufSrc, TNat, tgt)
defineTgt tgt (VNum (nVar (VPar (toEnd llufSrc))))
defineTgt llufTgt (VNum (nFull (nVar (VPar (toEnd tgt)))))
pure llufTgt
1 change: 0 additions & 1 deletion brat/Brat/Checker/SolvePatterns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Brat.Checker.SolvePatterns (argProblems, argProblemsWithLeftovers, solve)

import Brat.Checker.Monad
import Brat.Checker.Helpers
import Brat.Checker.SolveHoles (buildNatVal, invertNatVal)
import Brat.Checker.Types (EndType(..))
import Brat.Constructors
import Brat.Constructors.Patterns
Expand Down