Skip to content

Instantly share code, notes, and snippets.

@martialboniou
Last active December 18, 2015 12:09
Show Gist options
  • Save martialboniou/5780904 to your computer and use it in GitHub Desktop.
Save martialboniou/5780904 to your computer and use it in GitHub Desktop.
ps-inline function to reduce KLambda code in terms of primitives (https://groups.google.com/forum/#!topic/Qilang/Jb9YR1pA_SA)
(define zip
X [] -> []
[] Y -> []
X Y -> (zip-help X Y []))
(define zip-help
X Y Acc -> (reverse Acc) where (or (empty? X) (empty? Y))
[X | Xs] [Y | Ys] Acc -> (zip-help Xs Ys [(@p X Y) | Acc]))
(define substitute
Old New List -> (map (/. X (if (cons? X) (substitute Old New X) (if (= X Old) New X))) List))
(define substitute-with-zip
[] List -> List
[(@p O N) | Rest] List -> (substitute-with-zip Rest (substitute O N List)))
(define function?
{A --> boolean}
X -> (not (= (arity X) -1)))
(define user-function?
F -> (and (function? F) (not (shen.sysfunc? F))))
(define kl-inline
Kl NewParams -> (let A (reverse Kl)
B (head (tail A))
C (head A)
(substitute-with-zip (zip B NewParams) C)))
(define walk-src
[Fn | Rest] Survey -> (let Kl (ps Fn)
Chase (map (/. X (walk-src X Survey)) Rest)
(if (empty? (intersection Kl Survey))
(walk-src (kl-inline Kl Chase) [Fn | Survey])
[Fn | Chase])) where (user-function? Fn)
[Fn | Rest] Survey -> (map (/. X (walk-src X Survey)) [Fn | Rest])
X _ -> X)
(defcc <kl-defun>
defun F A C := [defun F A (walk-src C [F])];)
(define ps-inline
F -> (compile <kl-defun> (ps F)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment