Created
December 29, 2024 17:43
-
-
Save VictorTaelin/6ae92fc10e28471fd6cc627b5ae9642c to your computer and use it in GitHub Desktop.
wtf gpt-4o
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You're a coding assistant. | |
USER INPUTS | |
1. A CODEBASE, split in chunks. | |
2. A GOAL to fulfill. | |
3. A TARGET chunk. | |
INSTRUCTIONS | |
1. Read the CODEBASE. | |
2. Read the GOAL. | |
3. Read the TARGET chunk. | |
4. Determine if fulfilling the GOAL requires modifying the TARGET chunk. | |
5. If yes, also determine which other chunks are relevant to that GOAL. | |
6. Answer with a valid JSON, as follows: | |
If the TARGET must be modified: | |
{"modify":true,"relevant":number[]} | |
Otherwise: | |
{"modify":false} | |
EXAMPLE | |
Given the following CODEBASE: | |
math.js | |
#0: | |
function add(a,b) { | |
... | |
#1: | |
function sub(a,b) { | |
... | |
#2: | |
function mul(a,b) { | |
... | |
#3: | |
function div(a,b) { | |
return a / b; | |
} | |
Given the following TARGET: | |
#3: | |
function div(a,b) { | |
return a / b; | |
} | |
Given the following GOAL: | |
always return integers | |
You must answer with: | |
{"modify":true, "expand": [0,2,4]} | |
Reasoning: | |
The focused chunk is the chunk 3 (the 'div' function). Its code needs to be | |
modified directly to use 'Math.floor', thus, we set "modify" to "true". Since | |
that's the case, we also include a list of relevant chunks to expand. | |
WHAT IS A CHUNK? | |
- A 'chunk' is defined as a sequence of non-empty lines. | |
- The user will give you a 'chunked' view of their codebase. | |
- Chunks are displayed as \`#chunk_id: chunk_code\`. | |
- For brevity, some chunks will be *shortened* (using "..."). | |
- One chunk will be the TARGET. That's the one you must examine. | |
GUIDE: HOW TO DECIDE IF TARGET CHUNK MUST BE MODIFIED? | |
The TARGET chunk must be modified when, and only when, fulfilling the GOAL | |
requires directly changing its source code. If the chunk is unrelated to the | |
GOAL, just return '{"edit":false}'. If the chunk is related to the GOAL, but | |
does not need to be directly modified, then, just return '{"edit":false}'. | |
GUIDE: HOW TO DECIDE WHICH OTHER CHUNKS ARE RELEVANT? | |
A chunk is relevant when it is a used inside the TARGET chunk, or when it is | |
similar in some way. For example, if the TARGET chunk is V2_add, then, a V3_add | |
is relevant, because the code is similar, and can be handy to fulfill the goal. | |
IMPORTANT | |
DO NOT INCLUDE EXPLANATORY TEXT AROUND THE JSON, OTHERWISE IT WILL NOT PARSE. | |
CODEBASE: | |
Collapse.hs: | |
#0: | |
module HVML.Collapse where... | |
#1: | |
import Control.Monad (ap, forM, forM_)... | |
#2: | |
-- The Collapse Monad... | |
#3: | |
-- A bit-string... | |
data Bin... | |
#4: | |
-- A Collapse is a tree of superposed values... | |
data Collapse a = CSup Word64 (Collapse a) (Collapse a) | CVal a | CEra... | |
#5: | |
-- fork :: Collapse a -> IntMap (Bin -> Bin) -> Collapse b... | |
bind :: Collapse a -> (a -> Collapse b) -> Collapse b... | |
#6: | |
-- Mutates an element at given index in a list... | |
mut :: Word64 -> (a -> a) -> [a] -> [a]... | |
#7: | |
instance Functor Collapse where... | |
#8: | |
instance Applicative Collapse where... | |
#9: | |
instance Monad Collapse where... | |
#10: | |
-- Dup Collapser... | |
#11: | |
collapseDupsAt :: IM.IntMap [Int] -> ReduceAt -> Book -> Loc -> HVM Core... | |
#12: | |
collapseDupsAt state@(paths) reduceAt book host = unsafeInterleaveIO $ do... | |
#13: | |
ERA -> do... | |
#14: | |
LET -> do... | |
#15: | |
LAM -> do... | |
#16: | |
APP -> do... | |
#17: | |
SUP -> do... | |
#18: | |
VAR -> do... | |
#19: | |
DP0 -> do... | |
#20: | |
DP1 -> do... | |
#21: | |
CTR -> do... | |
#22: | |
MAT -> do... | |
#23: | |
W32 -> do... | |
#24: | |
CHR -> do... | |
#25: | |
OPX -> do... | |
#26: | |
OPY -> do... | |
#27: | |
REF -> do... | |
#28: | |
-- exitFailure... | |
tag -> do... | |
#29: | |
-- Sup Collapser... | |
#30: | |
collapseSups :: Book -> Core -> Collapse Core... | |
#31: | |
collapseSups book core = case core of... | |
#32: | |
Var name -> do... | |
#33: | |
Ref name fid args -> do... | |
#34: | |
Lam name body -> do... | |
#35: | |
App fun arg -> do... | |
#36: | |
Dup lab x y val body -> do... | |
#37: | |
Ctr cid fields -> do... | |
#38: | |
Mat val mov css -> do... | |
#39: | |
U32 val -> do... | |
#40: | |
Chr val -> do... | |
#41: | |
Op2 op x y -> do... | |
#42: | |
Let mode name val body -> do... | |
#43: | |
Era -> do... | |
#44: | |
Sup lab tm0 tm1 -> do... | |
#45: | |
-- Tree Collapser... | |
#46: | |
-- namesRef <- newIORef MS.empty... | |
doCollapseAt :: ReduceAt -> Book -> Loc -> HVM (Collapse Core)... | |
#47: | |
-- Priority Queue... | |
#48: | |
data PQ a... | |
#49: | |
pqUnion :: PQ a -> PQ a -> PQ a... | |
#50: | |
pqPop :: PQ a -> Maybe ((Word64, a), PQ a)... | |
#51: | |
pqPut :: (Word64,a) -> PQ a -> PQ a... | |
#52: | |
-- Simple Queue... | |
#53: | |
data SQ a = SQ [a] [a]... | |
#54: | |
sqPop :: SQ a -> Maybe (a, SQ a)... | |
#55: | |
sqPut :: a -> SQ a -> SQ a... | |
#56: | |
-- Flattener... | |
#57: | |
flattenDFS :: Collapse a -> [a]... | |
#58: | |
flattenBFS :: Collapse a -> [a]... | |
#59: | |
flattenPQ :: Collapse a -> [a]... | |
#60: | |
flatten :: Collapse a -> [a]... | |
#61: | |
-- Flat Collapser... | |
#62: | |
doCollapseFlatAt :: ReduceAt -> Book -> Loc -> HVM [Core]... | |
Compile.hs: | |
#63: | |
#64: | |
module HVML.Compile where... | |
#65: | |
import Control.Monad (forM_, forM, foldM, when)... | |
#66: | |
-- Compilation... | |
#67: | |
data CompileState = CompileState... | |
#68: | |
type Compile = State CompileState... | |
#69: | |
compile :: Book -> Word64 -> String... | |
#70: | |
-- Compiles a function using either Fast-Mode or Full-Mode... | |
compileWith :: (Book -> Word64 -> Core -> Bool -> [(Bool,String)] -> Compile ()) -> Book -> Word64 -> String... | |
#71: | |
emit :: String -> Compile ()... | |
#72: | |
tabInc :: Compile ()... | |
#73: | |
tabDec :: Compile ()... | |
#74: | |
bind :: String -> String -> Compile ()... | |
#75: | |
fresh :: String -> Compile String... | |
#76: | |
-- Full Compiler... | |
#77: | |
compileFull :: Book -> Word64 -> Core -> Bool -> [(Bool,String)] -> Compile ()... | |
#78: | |
compileFullVar :: String -> String -> Compile String... | |
#79: | |
compileFullCore :: Book -> Word64 -> Core -> String -> Compile String... | |
#80: | |
compileFullCore book fid Era _ = do... | |
#81: | |
compileFullCore book fid (Var name) host = do... | |
#82: | |
-- emit $ "set(" ++ letNam ++ " + 0, term_new(SUB, 0, 0));"... | |
compileFullCore book fid (Let mode var val bod) host = do... | |
#83: | |
-- emit $ "set(" ++ lamNam ++ " + 0, term_new(SUB, 0, 0));"... | |
compileFullCore book fid (Lam var bod) host = do... | |
#84: | |
compileFullCore book fid (App fun arg) host = do... | |
#85: | |
compileFullCore book fid (Sup lab tm0 tm1) host = do... | |
#86: | |
compileFullCore book fid (Dup lab dp0 dp1 val bod) host = do... | |
#87: | |
compileFullCore book fid (Ctr cid fds) host = do... | |
#88: | |
-- Create a chain of lambdas for fields and moved vars... | |
compileFullCore book fid tm@(Mat val mov css) host = do... | |
#89: | |
compileFullCore book fid (U32 val) _ =... | |
#90: | |
compileFullCore book fid (Chr val) _ =... | |
#91: | |
compileFullCore book fid (Op2 opr nu0 nu1) host = do... | |
#92: | |
compileFullCore book fid (Ref rNam rFid rArg) host = do... | |
#93: | |
-- Fast Compiler... | |
#94: | |
-- Compiles a function using Fast-Mode... | |
compileFast :: Book -> Word64 -> Core -> Bool -> [(Bool,String)] -> Compile ()... | |
#95: | |
-- Compiles a fast function's argument list... | |
compileFastArgs :: Book -> Word64 -> Core -> [String] -> MS.Map Int [String] -> Compile ()... | |
#96: | |
-- Compiles a fast function body (pattern-matching)... | |
compileFastBody :: Book -> Word64 -> Core -> [String] -> Bool -> Int -> MS.Map Int [String] -> Compile ()... | |
#97: | |
-- Numeric Pattern-Matching... | |
if isNumeric then do... | |
#98: | |
-- Constructor Pattern-Matching (with IfLet)... | |
else do... | |
#99: | |
-- Constructor Pattern-Matching (without IfLet)... | |
else do... | |
#100: | |
compileFastUndo book fid term ctx itr reuse... | |
#101: | |
compileFastBody book fid term@(Dup lab dp0 dp1 val bod) ctx stop itr reuse = do... | |
#102: | |
compileFastBody book fid term@(Let mode var val bod) ctx stop itr reuse = do... | |
#103: | |
compileFastBody book fid term@(Ref fNam fFid fArg) ctx stop itr reuse | fFid == fid = do... | |
#104: | |
compileFastBody book fid term ctx stop itr reuse = do... | |
#105: | |
-- Falls back from fast mode to full mode... | |
compileFastUndo :: Book -> Word64 -> Core -> [String] -> Int -> MS.Map Int [String] -> Compile ()... | |
#106: | |
-- Completes a fast mode call... | |
compileFastSave :: Book -> Word64 -> Core -> [String] -> Int -> MS.Map Int [String] -> Compile ()... | |
#107: | |
-- Helper function to allocate nodes with reuse... | |
compileFastAlloc :: Int -> MS.Map Int [String] -> Compile String... | |
#108: | |
-- Compiles a core term in fast mode... | |
compileFastCore :: Book -> Word64 -> Core -> MS.Map Int [String] -> Compile String... | |
#109: | |
compileFastCore book fid Era reuse = ... | |
#110: | |
compileFastCore book fid (Let mode var val bod) reuse = do... | |
#111: | |
compileFastCore book fid (Var name) reuse = do... | |
#112: | |
-- emit $ "set(" ++ lamNam ++ " + 0, term_new(SUB, 0, 0));"... | |
compileFastCore book fid (Lam var bod) reuse = do... | |
#113: | |
compileFastCore book fid (App fun arg) reuse = do... | |
#114: | |
compileFastCore book fid (Sup lab tm0 tm1) reuse = do... | |
#115: | |
compileFastCore book fid (Dup lab dp0 dp1 val bod) reuse = do... | |
#116: | |
compileFastCore book fid (Ctr cid fds) reuse = do... | |
#117: | |
compileFastCore book fid tm@(Mat val mov css) reuse = do... | |
#118: | |
compileFastCore book fid (U32 val) reuse =... | |
#119: | |
compileFastCore book fid (Chr val) reuse =... | |
#120: | |
compileFastCore book fid (Op2 opr nu0 nu1) reuse = do... | |
#121: | |
compileFastCore book fid (Ref rNam rFid rArg) reuse = do... | |
#122: | |
-- Inline Dynamic SUP... | |
if rNam == "SUP" then do... | |
#123: | |
-- Inline Dynamic DUP... | |
else if rNam == "DUP" && (case rArg of [_, _, Lam _ (Lam _ _)] -> True ; _ -> False) then do... | |
#124: | |
-- Create REF node... | |
else do... | |
#125: | |
-- Compiles a variable in fast mode... | |
compileFastVar :: String -> Compile String... | |
#126: | |
-- Compiles a function using Fast-Mode... | |
compileSlow :: Book -> Word64 -> Core -> Bool -> [(Bool,String)] -> Compile ()... | |
Extract.hs: | |
#127: | |
#128: | |
module HVML.Extract where... | |
#129: | |
import Control.Monad (foldM)... | |
#130: | |
extractCoreAt :: IORef IS.IntSet -> ReduceAt -> Book -> Loc -> HVM Core... | |
#131: | |
extractCoreAt dupsRef reduceAt book host = unsafeInterleaveIO $ do... | |
#132: | |
ERA -> do... | |
#133: | |
LET -> do... | |
#134: | |
LAM -> do... | |
#135: | |
APP -> do... | |
#136: | |
SUP -> do... | |
#137: | |
VAR -> do... | |
#138: | |
DP0 -> do... | |
#139: | |
DP1 -> do... | |
#140: | |
CTR -> do... | |
#141: | |
MAT -> do... | |
#142: | |
W32 -> do... | |
#143: | |
CHR -> do... | |
#144: | |
OPX -> do... | |
#145: | |
OPY -> do... | |
#146: | |
REF -> do... | |
#147: | |
_ -> do... | |
#148: | |
-- return $ doLiftDups core... | |
doExtractCoreAt :: ReduceAt -> Book -> Loc -> HVM Core... | |
#149: | |
-- Lifting Dups... | |
#150: | |
liftDups :: Core -> (Core, Core -> Core)... | |
#151: | |
liftDups (Var nam) =... | |
#152: | |
liftDups (Ref nam fid arg) =... | |
#153: | |
liftDups Era =... | |
#154: | |
liftDups (Lam str bod) =... | |
#155: | |
liftDups (App fun arg) =... | |
#156: | |
liftDups (Sup lab tm0 tm1) =... | |
#157: | |
liftDups (Dup lab dp0 dp1 val bod) =... | |
#158: | |
liftDups (Ctr cid fds) =... | |
#159: | |
liftDups (Mat val mov css) =... | |
#160: | |
liftDups (U32 val) =... | |
#161: | |
liftDups (Chr val) =... | |
#162: | |
liftDups (Op2 opr nm0 nm1) =... | |
#163: | |
liftDups (Let mod nam val bod) =... | |
#164: | |
liftDupsList :: [Core] -> ([Core], Core -> Core)... | |
#165: | |
liftDupsList [] = ... | |
#166: | |
liftDupsList (x:xs) =... | |
#167: | |
liftDupsMov :: [(String, Core)] -> ([(String, Core)], Core -> Core)... | |
#168: | |
liftDupsMov [] = ... | |
#169: | |
liftDupsMov ((k,v):xs) =... | |
#170: | |
liftDupsCss :: [(String, [String], Core)] -> ([(String, [String], Core)], Core -> Core)... | |
#171: | |
liftDupsCss [] = ... | |
#172: | |
liftDupsCss ((c,fs,b):xs) =... | |
#173: | |
-- hack to print expr before dups... | |
doLiftDups :: Core -> Core... | |
Inject.hs: | |
#174: | |
#175: | |
module HVML.Inject where... | |
#176: | |
import Control.Monad (foldM, when, forM_)... | |
#177: | |
type InjectM a = StateT InjectState HVM a... | |
#178: | |
data InjectState = InjectState... | |
#179: | |
emptyState :: InjectState... | |
#180: | |
injectCore :: Book -> Core -> Loc -> InjectM ()... | |
#181: | |
injectCore _ Era loc = do... | |
#182: | |
injectCore _ (Var nam) loc = do... | |
#183: | |
injectCore book (Let mod nam val bod) loc = do... | |
#184: | |
-- lift $ set (lam + 0) (termNew _SUB_ 0 0)... | |
injectCore book (Lam vr0 bod) loc = do... | |
#185: | |
injectCore book (App fun arg) loc = do... | |
#186: | |
injectCore book (Sup lab tm0 tm1) loc = do... | |
#187: | |
-- lift $ set (dup + 0) (termNew _SUB_ 0 0)... | |
injectCore book (Dup lab dp0 dp1 val bod) loc = do... | |
#188: | |
-- lift $ set loc (termNew _REF_ 0 fid)... | |
injectCore book (Ref nam fid arg) loc = do... | |
#189: | |
injectCore book (Ctr cid fds) loc = do... | |
#190: | |
-- Allocate space for the Mat term... | |
injectCore book tm@(Mat val mov css) loc = do... | |
#191: | |
injectCore book (U32 val) loc = do... | |
#192: | |
injectCore book (Chr val) loc = do... | |
#193: | |
injectCore book (Op2 opr nm0 nm1) loc = do... | |
#194: | |
doInjectCoreAt :: Book -> Core -> Loc -> [(String,Term)] -> HVM Term... | |
Main.hs: | |
#195: | |
-- Type.hs:... | |
#196: | |
{-# LANGUAGE OverloadedStrings #-}... | |
#197: | |
module Main where... | |
#198: | |
import Control.Monad (when, forM_)... | |
#199: | |
runtime_c :: String... | |
#200: | |
-- Main... | |
#201: | |
data RunMode... | |
#202: | |
main :: IO ()... | |
#203: | |
printHelp :: IO (Either String ())... | |
#204: | |
-- CLI Commands... | |
#205: | |
-- Initialize the HVM... | |
cliRun :: FilePath -> Bool -> Bool -> RunMode -> Bool -> IO (Either String ())... | |
#206: | |
genMain :: Book -> String... | |
Parse.hs: | |
#207: | |
#208: | |
module HVML.Parse where... | |
#209: | |
import Control.Monad (foldM, forM)... | |
#210: | |
-- Core Parsers... | |
#211: | |
data ParserState = ParserState... | |
#212: | |
type ParserM = Parsec String ParserState... | |
#213: | |
parseCore :: ParserM Core... | |
#214: | |
'*' -> do... | |
#215: | |
'λ' -> do... | |
#216: | |
'(' -> do... | |
#217: | |
'@' -> do... | |
#218: | |
'&' -> do... | |
#219: | |
'!' -> do... | |
#220: | |
'&' -> do... | |
#221: | |
'!' -> do... | |
#222: | |
'^' -> do... | |
#223: | |
_ -> do... | |
#224: | |
'#' -> parseCtr... | |
#225: | |
'~' -> parseMat... | |
#226: | |
'[' -> parseLst... | |
#227: | |
'\'' -> parseChr... | |
#228: | |
'"' -> parseStr... | |
#229: | |
_ -> do... | |
#230: | |
parseRef :: ParserM Core... | |
#231: | |
parseCtr :: ParserM Core... | |
#232: | |
-- Parse mov (external variables)... | |
parseMat :: ParserM Core... | |
#233: | |
intoIfLetChain :: Core -> [(String, Core)] -> [(String, [String], Core)] -> String -> (String, [String], Core) -> Core... | |
#234: | |
parseOper :: Oper -> ParserM Core... | |
#235: | |
parseEscapedChar :: ParserM Char... | |
#236: | |
parseChr :: ParserM Core... | |
#237: | |
parseStr :: ParserM Core... | |
#238: | |
parseLst :: ParserM Core... | |
#239: | |
parseName :: ParserM String... | |
#240: | |
parseName1 :: ParserM String... | |
#241: | |
parseDef :: ParserM (String, ((Bool, [(Bool, String)]), Core))... | |
#242: | |
parseADT :: ParserM ()... | |
#243: | |
parseADTCtr :: ParserM (String, [String])... | |
#244: | |
parseBook :: ParserM [(String, ((Bool, [(Bool,String)]), Core))]... | |
#245: | |
doParseCore :: String -> IO Core... | |
#246: | |
doParseBook :: String -> IO Book ... | |
#247: | |
-- Helper Parsers... | |
#248: | |
consume :: String -> ParserM String... | |
#249: | |
closeWith :: String -> ParserM ()... | |
#250: | |
skip :: ParserM ()... | |
#251: | |
genFreshLabel :: ParserM Word64... | |
#252: | |
-- Adjusting... | |
#253: | |
createBook :: [(String, ((Bool,[(Bool,String)]), Core))] -> MS.Map String Word64 -> MS.Map String Int -> Book... | |
#254: | |
-- Adds the function id to Ref constructors... | |
setRefIds :: MS.Map String Word64 -> Core -> Core... | |
#255: | |
-- Collects all SUP/DUP labels used... | |
collectLabels :: Core -> MS.Map Word64 ()... | |
#256: | |
-- Gives unique names to lexically scoped vars, unless they start with '$'.... | |
lexify :: Core -> Core... | |
#257: | |
extend :: String -> String -> MS.Map String String -> State Int (MS.Map String String)... | |
#258: | |
go :: Core -> MS.Map String String -> State Int Core... | |
#259: | |
go term ctx = case term of... | |
#260: | |
Var nam -> ... | |
#261: | |
Ref nam fid arg -> do... | |
#262: | |
Let mod nam val bod -> do... | |
#263: | |
Lam nam bod -> do... | |
#264: | |
App fun arg -> do... | |
#265: | |
Sup lab tm0 tm1 -> do... | |
#266: | |
Dup lab dp0 dp1 val bod -> do... | |
#267: | |
Ctr cid fds -> do... | |
#268: | |
Mat val mov css -> do... | |
#269: | |
Op2 op nm0 nm1 -> do... | |
#270: | |
U32 n -> ... | |
#271: | |
Chr c ->... | |
#272: | |
Era -> ... | |
#273: | |
-- Errors... | |
#274: | |
-- Error handling... | |
extractExpectedTokens :: ParseError -> String... | |
#275: | |
showParseError :: String -> String -> ParseError -> IO ()... | |
Reduce.hs: | |
#276: | |
#277: | |
module HVML.Reduce where... | |
#278: | |
import Control.Monad (when, forM, forM_)... | |
#279: | |
reduceAt :: Bool -> ReduceAt... | |
#280: | |
reduceAt debug book host = do ... | |
#281: | |
-- putStrLn $ "---------------- CORE: "... | |
when debug $ do... | |
#282: | |
case tagT tag of... | |
#283: | |
LET -> do... | |
#284: | |
APP -> do... | |
#285: | |
MAT -> do... | |
#286: | |
OPX -> do... | |
#287: | |
OPY -> do... | |
#288: | |
DP0 -> do... | |
#289: | |
DP1 -> do... | |
#290: | |
VAR -> do... | |
#291: | |
REF -> do... | |
#292: | |
otherwise -> do... | |
#293: | |
where... | |
#294: | |
-- TODO: I disabled Fast Copy Optimization on interpreted mode because I... | |
reduceRefAt :: Book -> Loc -> HVM Term... | |
#295: | |
-- Primitive: Dynamic Dup `@DUP(lab val λdp0λdp1(bod))`... | |
reduceRefAt_DupF :: Book -> Loc -> Loc -> Word64 -> HVM Term ... | |
#296: | |
-- Primitive: Dynamic Sup `@SUP(lab tm0 tm1)`... | |
reduceRefAt_SupF :: Book -> Loc -> Loc -> Word64 -> HVM Term... | |
#297: | |
-- Primitive: Logger `@LOG(msg)`... | |
reduceRefAt_LogF :: Book -> Loc -> Loc -> Word64 -> HVM Term... | |
#298: | |
-- Primitive: Fresh `@FRESH`... | |
reduceRefAt_FreshF :: Book -> Loc -> Loc -> Word64 -> HVM Term... | |
#299: | |
reduceCAt :: Bool -> ReduceAt... | |
#300: | |
-- normalAtWith :: (Book -> Term -> HVM Term) -> Book -> Loc -> HVM Term... | |
#301: | |
-- normalAt :: Book -> Loc -> HVM Term... | |
#302: | |
-- normalCAt :: Book -> Loc -> HVM Term... | |
Runtime.c: | |
#303: | |
#304: | |
#include <stdatomic.h>... | |
#305: | |
typedef uint8_t Tag;... | |
#306: | |
// Runtime Types... | |
#307: | |
// Global State Type... | |
typedef struct {... | |
#308: | |
// Global State Value... | |
static State HVM = {... | |
#309: | |
// Constants... | |
#310: | |
#define DP0 0x00... | |
#311: | |
#define OP_ADD 0x00... | |
#312: | |
#define DUP_F 0xFFF... | |
#313: | |
#define LAZY 0x0... | |
#314: | |
#define VOID 0x00000000000000... | |
#315: | |
// Heap... | |
#316: | |
Loc get_len() {... | |
#317: | |
u64 get_itr() {... | |
#318: | |
u64 fresh() {... | |
#319: | |
void set_len(Loc value) {... | |
#320: | |
void set_itr(Loc value) {... | |
#321: | |
// Terms... | |
#322: | |
Term term_new(Tag tag, Lab lab, Loc loc) {... | |
#323: | |
Tag term_tag(Term x) {... | |
#324: | |
Lab term_lab(Term x) {... | |
#325: | |
Loc term_loc(Term x) {... | |
#326: | |
Tag term_get_bit(Term x) {... | |
#327: | |
Term term_set_bit(Term term) {... | |
#328: | |
Term term_rem_bit(Term term) {... | |
#329: | |
}... | |
#330: | |
// u12v2... | |
#331: | |
u64 u12v2_new(u64 x, u64 y) {... | |
#332: | |
u64 u12v2_x(u64 u12v2) {... | |
#333: | |
u64 u12v2_y(u64 u12v2) {... | |
#334: | |
// Atomics... | |
#335: | |
Term swap(Loc loc, Term term) {... | |
#336: | |
Term got(Loc loc) {... | |
#337: | |
void set(Loc loc, Term term) {... | |
#338: | |
void sub(Loc loc, Term term) {... | |
#339: | |
Term take(Loc loc) {... | |
#340: | |
// Allocation... | |
#341: | |
Loc alloc_node(Loc arity) {... | |
#342: | |
Loc inc_itr() {... | |
#343: | |
// Stringification... | |
#344: | |
void print_tag(Tag tag) {... | |
#345: | |
void print_term(Term term) {... | |
#346: | |
void print_term_ln(Term term) {... | |
#347: | |
void print_heap() {... | |
#348: | |
// Evaluation... | |
#349: | |
// @foo(&L{ax ay} b c ...)... | |
Term reduce_ref_sup(Term ref, u32 idx) {... | |
#350: | |
// @foo(a b c ...)... | |
Term reduce_ref(Term ref) {... | |
#351: | |
// ! x = val... | |
Term reduce_let(Term let, Term val) {... | |
#352: | |
// (* a)... | |
Term reduce_app_era(Term app, Term era) {... | |
#353: | |
// (λx(body) a)... | |
Term reduce_app_lam(Term app, Term lam) {... | |
#354: | |
// (&L{a b} c)... | |
Term reduce_app_sup(Term app, Term sup) {... | |
#355: | |
// (#{x y z ...} a)... | |
Term reduce_app_ctr(Term app, Term ctr) {... | |
#356: | |
// (123 a)... | |
Term reduce_app_w32(Term app, Term w32) {... | |
#357: | |
// ! &L{x y} = *... | |
Term reduce_dup_era(Term dup, Term era) {... | |
#358: | |
// ! &L{r s} = λx(f)... | |
Term reduce_dup_lam(Term dup, Term lam) {... | |
#359: | |
// ! &L{x y} = &R{a b}... | |
Term reduce_dup_sup(Term dup, Term sup) {... | |
#360: | |
// ! &L{x y} = #{a b c ...}... | |
Term reduce_dup_ctr(Term dup, Term ctr) {... | |
#361: | |
// ! &L{x y} = 123... | |
Term reduce_dup_w32(Term dup, Term w32) {... | |
#362: | |
// ! &L{x y} = @foo(a b c ...)... | |
Term reduce_dup_ref(Term dup, Term ref) {... | |
#363: | |
// ~ * {K0 K1 K2 ...} ... | |
Term reduce_mat_era(Term mat, Term era) {... | |
#364: | |
// ~ λx(x) {K0 K1 K2 ...}... | |
Term reduce_mat_lam(Term mat, Term lam) {... | |
#365: | |
// ~ &L{x y} {K0 K1 K2 ...}... | |
Term reduce_mat_sup(Term mat, Term sup) {... | |
#366: | |
// ~ #N{x y z ...} {K0 K1 K2 ...} ... | |
Term reduce_mat_ctr(Term mat, Term ctr) {... | |
#367: | |
// ~ num {K0 K1 K2 ... KN}... | |
Term reduce_mat_w32(Term mat, Term w32) {... | |
#368: | |
// <op(* b)... | |
Term reduce_opx_era(Term opx, Term era) {... | |
#369: | |
// <op(λx(B) y)... | |
Term reduce_opx_lam(Term opx, Term lam) {... | |
#370: | |
// <op(&L{x0 x1} y)... | |
Term reduce_opx_sup(Term opx, Term sup) {... | |
#371: | |
// <op(#{x0 x1 x2...} y)... | |
Term reduce_opx_ctr(Term opx, Term ctr) {... | |
#372: | |
// <op(x0 x1)... | |
Term reduce_opx_w32(Term opx, Term w32) {... | |
#373: | |
// >op(a *)... | |
Term reduce_opy_era(Term opy, Term era) {... | |
#374: | |
// >op(a λx(B))... | |
Term reduce_opy_lam(Term opy, Term era) {... | |
#375: | |
// >op(a &L{x y})... | |
Term reduce_opy_sup(Term opy, Term sup) {... | |
#376: | |
// >op(#{x y z ...} b)... | |
Term reduce_opy_ctr(Term opy, Term ctr) {... | |
#377: | |
// >op(x y)... | |
Term reduce_opy_w32(Term opy, Term w32) {... | |
#378: | |
Term reduce(Term term) {... | |
#379: | |
while (1) {... | |
#380: | |
//printf("NEXT "); print_term(term); printf("\n");... | |
Tag tag = term_tag(next);... | |
#381: | |
switch (tag) {... | |
#382: | |
case LET: {... | |
#383: | |
case APP: {... | |
#384: | |
case MAT: {... | |
#385: | |
case OPX: {... | |
#386: | |
case OPY: {... | |
#387: | |
case DP0: {... | |
#388: | |
case DP1: {... | |
#389: | |
case VAR: {... | |
#390: | |
case REF: {... | |
#391: | |
default: {... | |
#392: | |
if ((*spos) == stop) {... | |
#393: | |
case LET: {... | |
#394: | |
case APP: {... | |
#395: | |
case DP0:... | |
#396: | |
case MAT: {... | |
#397: | |
case OPX: {... | |
#398: | |
case OPY: {... | |
#399: | |
default: break;... | |
#400: | |
if ((*HVM.spos) == stop) {... | |
#401: | |
}... | |
#402: | |
Term reduce_at(Loc host) {... | |
#403: | |
Term normal(Term term) {... | |
#404: | |
case LAM: {... | |
#405: | |
case APP: {... | |
#406: | |
case SUP: {... | |
#407: | |
case DP0:... | |
#408: | |
case CTR: {... | |
#409: | |
case MAT: {... | |
#410: | |
default:... | |
#411: | |
}... | |
#412: | |
// Primitives... | |
#413: | |
// Primitive: Dynamic Sup `@SUP(lab tm0 tm1)`... | |
Term SUP_f(Term ref) {... | |
#414: | |
// Primitive: Dynamic Dup `@DUP(lab val λdp0λdp1(bod))`... | |
Term DUP_f(Term ref) {... | |
#415: | |
}... | |
#416: | |
Term LOG_f(Term ref) {... | |
#417: | |
Term FRESH_f(Term ref) {... | |
#418: | |
// Runtime Memory... | |
#419: | |
// FIXME: use mmap instead... | |
void hvm_init() {... | |
#420: | |
void hvm_free() {... | |
#421: | |
State* hvm_get_state() {... | |
#422: | |
void hvm_set_state(State* hvm) {... | |
#423: | |
//printf("defined %llu %p\n", fid, func);... | |
void hvm_define(u64 fid, Term (*func)()) {... | |
Show.hs: | |
#424: | |
#425: | |
module HVML.Show where... | |
#426: | |
import Control.Applicative ((<|>))... | |
#427: | |
-- Core Stringification... | |
#428: | |
showCore :: Core -> String... | |
#429: | |
coreToString :: Core -> String... | |
#430: | |
case pretty core of... | |
#431: | |
Var nam ->... | |
#432: | |
Era ->... | |
#433: | |
Lam vr0 bod ->... | |
#434: | |
App fun arg ->... | |
#435: | |
Sup lab tm0 tm1 ->... | |
#436: | |
Dup lab dp0 dp1 val bod ->... | |
#437: | |
Ref nam fid arg ->... | |
#438: | |
Ctr cid fds ->... | |
#439: | |
Mat val mov css ->... | |
#440: | |
U32 val ->... | |
#441: | |
Chr val ->... | |
#442: | |
Op2 opr nm0 nm1 ->... | |
#443: | |
Let mod nam val bod ->... | |
#444: | |
operToString :: Oper -> String... | |
#445: | |
modeToString LAZY = ""... | |
#446: | |
-- Runtime Stringification... | |
#447: | |
tagToString :: Tag -> String... | |
#448: | |
labToString :: Word64 -> String... | |
#449: | |
locToString :: Word64 -> String... | |
#450: | |
termToString :: Term -> String... | |
#451: | |
-- Pretty Renaming... | |
#452: | |
prettyRename :: Core -> Core... | |
#453: | |
go namesRef core = case core of... | |
#454: | |
Var name -> do... | |
#455: | |
Lam name body -> do... | |
#456: | |
Let mode name val body -> do... | |
#457: | |
App fun arg -> do... | |
#458: | |
Sup lab x y -> do... | |
#459: | |
Dup lab x y val body -> do... | |
#460: | |
Ctr cid args -> do... | |
#461: | |
Mat val mov css -> do... | |
#462: | |
Op2 op x y -> do... | |
#463: | |
Ref name fid args -> do... | |
#464: | |
other -> return other... | |
#465: | |
genName namesRef name = do... | |
#466: | |
genNameFromIndex n = go (n + 1) "" where... | |
#467: | |
-- Pretty Printers... | |
#468: | |
-- pretty core = prettyStr core... | |
pretty :: Core -> Maybe String... | |
#469: | |
prettyStr :: Core -> Maybe String... | |
#470: | |
prettyLst :: Core -> Maybe String... | |
#471: | |
-- Dumping... | |
#472: | |
dumpHeapRange :: Word64 -> Word64 -> HVM [(Word64, Term)]... | |
#473: | |
dumpHeap :: HVM ([(Word64, Term)], Word64)... | |
#474: | |
heapToString :: ([(Word64, Term)], Word64) -> String... | |
#475: | |
padLeft :: String -> Int -> Char -> String... | |
#476: | |
showHex :: Word64 -> String... | |
Type.hs: | |
#477: | |
module HVML.Type where... | |
#478: | |
import Data.Map.Strict as MS... | |
#479: | |
-- Core Types... | |
#480: | |
data Core | |
= Var String -- x | |
| Ref String Word64 [Core] -- @fn | |
| Era -- * | |
| Lam String Core -- λx(F) | |
| App Core Core -- (f x) | |
| Sup Word64 Core Core -- &L{a b} | |
| Dup Word64 String String Core Core -- ! &L{a b} = v body | |
| Ctr Word64 [Core] -- #Ctr{a b ...} | |
| Mat Core [(String,Core)] [(String,[String],Core)] -- ~ v { #A{a b ...}: ... #B{a b ...}: ... ... } | |
| U32 Word32 -- 123 | |
| Chr Char -- 'a' | |
| Op2 Oper Core Core -- (+ a b) | |
| Let Mode String Core Core -- ! x = v body | |
deriving (Show, Eq) | |
#481: | |
data Mode | |
= LAZY | |
| STRI | |
| PARA | |
deriving (Show, Eq, Enum) | |
#482: | |
data Oper | |
= OP_ADD | OP_SUB | OP_MUL | OP_DIV | |
| OP_MOD | OP_EQ | OP_NE | OP_LT | |
| OP_GT | OP_LTE | OP_GTE | OP_AND | |
| OP_OR | OP_XOR | OP_LSH | OP_RSH | |
deriving (Show, Eq, Enum) | |
#483: | |
-- A top-level function, including: | |
-- - copy: true when ref-copy mode is enabled | |
-- - args: a list of (isArgStrict, argName) pairs | |
-- - core: the function's body | |
-- Note: ref-copy improves C speed, but increases interaction count | |
type Func = ((Bool, [(Bool,String)]), Core) | |
#484: | |
-- NOTE: the new idToLabs field is a map from a function id to a set of all | |
-- DUP/SUP labels used in its body. note that, when a function uses either | |
-- HVM.SUP or HVM.DUP internally, this field is set to Nothing. this will be | |
-- used to apply the fast DUP-REF and REF-SUP interactions, when safe to do so | |
data Book = Book | |
{ idToFunc :: MS.Map Word64 Func | |
, idToName :: MS.Map Word64 String | |
, idToLabs :: MS.Map Word64 (MS.Map Word64 ()) | |
, nameToId :: MS.Map String Word64 | |
, ctrToAri :: MS.Map String Int | |
, ctrToCid :: MS.Map String Word64 | |
} deriving (Show, Eq) | |
#485: | |
-- Runtime Types... | |
#486: | |
type Tag = Word64 | |
type Lab = Word64 | |
type Loc = Word64 | |
type Term = Word64 | |
#487: | |
data TAG | |
= DP0 | |
| DP1 | |
| VAR | |
| ERA | |
| APP | |
| LAM | |
| SUP | |
| SUB | |
| REF | |
| LET | |
| CTR | |
| MAT | |
| W32 | |
| CHR | |
| OPX | |
| OPY | |
deriving (Eq, Show) | |
#488: | |
type HVM = IO | |
#489: | |
type ReduceAt = Book -> Loc -> HVM Term | |
#490: | |
-- C Functions... | |
#491: | |
foreign import ccall unsafe "Runtime.c hvm_init"... | |
#492: | |
-- Constants... | |
#493: | |
tagT :: Tag -> TAG | |
tagT 0x00 = DP0 | |
tagT 0x01 = DP1 | |
tagT 0x02 = VAR | |
tagT 0x03 = SUB | |
tagT 0x04 = REF | |
tagT 0x05 = LET | |
tagT 0x06 = APP | |
tagT 0x08 = MAT | |
tagT 0x09 = OPX | |
tagT 0x0A = OPY | |
tagT 0x0B = ERA | |
tagT 0x0C = LAM | |
tagT 0x0D = SUP | |
tagT 0x0F = CTR | |
tagT 0x10 = W32 | |
tagT 0x11 = CHR | |
tagT tag = error $ "unknown tag: " ++ show tag | |
#494: | |
_DP0_ :: Tag... | |
#495: | |
_DP1_ :: Tag... | |
#496: | |
_VAR_ :: Tag... | |
#497: | |
_SUB_ :: Tag... | |
#498: | |
_REF_ :: Tag... | |
#499: | |
_LET_ :: Tag... | |
#500: | |
_APP_ :: Tag... | |
#501: | |
_MAT_ :: Tag... | |
#502: | |
_OPX_ :: Tag... | |
#503: | |
_OPY_ :: Tag... | |
#504: | |
_ERA_ :: Tag... | |
#505: | |
_LAM_ :: Tag... | |
#506: | |
_SUP_ :: Tag... | |
#507: | |
_CTR_ :: Tag... | |
#508: | |
_W32_ :: Tag... | |
#509: | |
_CHR_ :: Tag... | |
#510: | |
modeT :: Lab -> Mode | |
modeT 0x00 = LAZY | |
modeT 0x01 = STRI | |
modeT 0x02 = PARA | |
modeT mode = error $ "unknown mode: " ++ show mode | |
#511: | |
-- Primitive Functions... | |
_DUP_F_ :: Lab... | |
#512: | |
_SUP_F_ :: Lab... | |
#513: | |
_LOG_F_ :: Lab... | |
#514: | |
_FRESH_F_ :: Lab... | |
#515: | |
primitives :: [(String, Lab)]... | |
#516: | |
-- Utils... | |
#517: | |
-- Getter function for maps... | |
mget map key =... | |
#518: | |
-- The if-let match stores its target ctr id... | |
ifLetLab :: Book -> Core -> Word64... | |
TARGET: | |
#458: | |
Sup lab x y -> do | |
x' <- go namesRef x | |
y' <- go namesRef y | |
return $ Sup lab x' y' | |
GOAL: | |
change the lambda syntax from 'λx body' to '@x body' | |
Now, read the CODEBASE and TARGET, and answer with a JSON including whether the | |
TARGET chunk needs to be edited, and, if so, which other chunks are relevant. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment