git @ Cat's Eye Technologies Argyle / 3b2fdb6
"free" -> "leftfree" to try to reduce confusion. Chris Pressey 26 days ago
4 changed file(s) with 26 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
338338 ### `abt`
339339
340340 Quoted literal ABTs are allowed to have free variables. The `abt` form take a list of
341 variables that are considered free in the ABT expression. `quote` is actually a special
342 case of `abt` where the set of free variables is empty.
341 variables that are to be considered free in the ABT expression. We call these variables
342 "variables left free" to distinguish them from when we use the term "free variables" to
343 describe variables that we have discovered to have no bindings (a perhaps more traditional
344 usage of "free").
345
346 `quote` is actually a special case of `abt` where the set of variables left free is empty.
343347
344348 (abt (x) x)
345 ===> <ABT: Operator "free" [Binder "x" (Var "x")]>
346
347 ABTs with free variables can't be `eval`'ed.
349 ===> <ABT: Operator "leftfree" [Binder "x" (Var "x")]>
350
351 ABTs with variables left free can't be `eval`'ed.
348352
349353 (eval (abt (x) x))
350354 ???> Cannot eval
351355
352 The free variables declared don't actually have to appear.
356 The variables that we declare to have been left free, don't actually have to appear in
357 the expression.
353358
354359 (abt (x) 1)
355 ===> <ABT: Operator "free" [Binder "x" (Operator "literal" [Operator "1" []])]>
356
357 But an ABT with free variables that don't actually appear still can't be `eval`'ed.
360 ===> <ABT: Operator "leftfree" [Binder "x" (Operator "literal" [Operator "1" []])]>
361
362 But an ABT with variables left free still can't be `eval`'ed, even if those variables
363 don't actually appear in it.
358364
359365 (eval (abt (x) 1))
360366 ???> Cannot eval
9494 Operator "values" (map (\val -> performSubstitution val n k) vals)
9595 ]
9696
97 -- Handle free variables
98 Operator "free" [Binder var body] ->
97 -- Handle leftfree variables
98 Operator "leftfree" [Binder var body] ->
9999 if var == n
100100 then performSubstitution body n k -- This free var is being substituted
101 else Operator "free" [Binder var (performSubstitution body n k)]
101 else Operator "leftfree" [Binder var (performSubstitution body n k)]
102102
103103 -- Base cases
104104 Var x | x == n -> k
5151 scope <- ask
5252 return $ Right (Operator "literal" [Operator ("string:" ++ s) []], scope)
5353
54 Quote fvars body -> do
54 Quote leftFreeVars body -> do
5555 scope <- ask
56 -- Put the free variables in scope before converting the body
56 -- Put the left-free variables in scope before converting the body
5757 let scopeWithFreeVars = scope {
58 boundVars = Set.union (boundVars scope) (Set.fromList fvars)
58 boundVars = Set.union (boundVars scope) (Set.fromList leftFreeVars)
5959 }
6060 -- Convert the body with the extended scope
6161 case runReader (convertAST body) scopeWithFreeVars of
6464 let finalScope = scope {
6565 boundVars = boundVars scope, -- Don't persist the temporary free vars
6666 freeVars = Set.union (freeVars scope)
67 (Set.difference (freeVars bodyScope) (Set.fromList fvars))
67 (Set.difference (freeVars bodyScope) (Set.fromList leftFreeVars))
6868 }
69 abt = if fvars == []
69 abt = if leftFreeVars == []
7070 then Operator "literal" [bodyABT]
71 else Operator "literal" [Operator "free" [foldr Binder bodyABT fvars]]
71 else Operator "literal" [Operator "leftfree" [foldr Binder bodyABT leftFreeVars]]
7272 in return $ Right (abt, finalScope)
7373
7474 Op name args -> do
3030 env <- ask
3131 return $ VClosure param body env
3232
33 Operator "free" [body] ->
34 -- Regular evaluation only works on ABTs without "free" at the top
33 Operator "leftfree" [body] ->
34 -- Evaluation can only happen on ABTs without free variables
3535 throwError "Cannot evaluate ABT with free variables"
3636
3737 Operator "apply" (fn:args) -> do
157157 _ -> Left "subst: second argument must be a string"
158158 _ -> Left "subst: first argument must be an ABT")
159159 ]
160 where
161 -- Helper function to perform the substitution
162160
163161 -- | Add transformers to initial environment
164162 extendedInitialEnv :: Environment