about summary refs log tree commit diff
path: root/emacs/.emacs.d/wpc/iso.el
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-01-30T16·00+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-01-30T16·00+0000
commit578ed1ba98510058cf48f897a9bf4e3391684120 (patch)
tree5eadabd3d232151c75ac2424eaa8771ab25e927e /emacs/.emacs.d/wpc/iso.el
parent3684adf23f847114e1beeaab162398628ba571aa (diff)
Move move .emacs.d out of configs/shared
Moving all of my Emacs-related files into their own directory at the root of
this repository.
Diffstat (limited to 'emacs/.emacs.d/wpc/iso.el')
-rw-r--r--emacs/.emacs.d/wpc/iso.el95
1 files changed, 95 insertions, 0 deletions
diff --git a/emacs/.emacs.d/wpc/iso.el b/emacs/.emacs.d/wpc/iso.el
new file mode 100644
index 000000000000..c9ce4a48fc71
--- /dev/null
+++ b/emacs/.emacs.d/wpc/iso.el
@@ -0,0 +1,95 @@
+;;; iso.el --- Isomorphisms in Elisp -*- lexical-binding: t -*-
+;; Author: William Carroll <wpcarro@gmail.com>
+
+;;; Commentary:
+;; Providing basic isomorphisms to improve code quality.
+
+;;; Code:
+
+(require 'dotted)
+(require 'tuple)
+(require 'symbol)
+(require 'string)
+(require 'list)
+(require 'alist)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Library
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(cl-defstruct iso to from x)
+
+(defconst iso/whitelist
+  '((dotted . tuple)
+    (symbol . string))
+  "Alist representing supported isomorphisms.")
+
+(defconst iso/vertices
+  (list/concat (alist/keys iso/whitelist)
+               (alist/values iso/whitelist))
+  "List of all of the vertices in the iso graph.")
+
+(defun iso/classify (x)
+  "Return type of X."
+  (cond
+   ((string/instance? x) 'string)
+   ((symbol/instance? x) 'symbol)
+   ((dotted/instance? x) 'dotted)
+   ((tuple/instance? x)  'tuple)))
+
+(cl-defun iso/exists? (to from)
+  "Return t if an isomorphism of TO to FROM exists."
+  ;; TODO: All of this can be improved modelling this with a graph.
+  (cond
+   ;; to -> from
+   ((list/contains? to (alist/keys iso/whitelist))
+    (list/contains? from (alist/values iso/whitelist)))
+   ;; from -> to
+   ((list/contains? from (alist/keys iso/whitelist))
+    (list/contains? to (alist/values iso/whitelist)))
+   ;; doesn't exist
+   (t nil)))
+
+(progn
+  (prelude/assert
+   (iso/exists? 'symbol 'string))
+  (prelude/assert
+   (iso/exists? 'dotted 'tuple))
+  (prelude/refute
+   (iso/exists? 'dotted 'symbol))
+  (prelude/refute
+   (iso/exists? 'symbol 'list)))
+
+;; TODO: Model this as a graph.
+(defconst iso/morphisms
+  '((string .
+            '(symbol #')
+     ))
+  (list (:from 'string :to 'symbol :fn #'intern)
+        (:from 'symbol :to 'string :fn #'symbol-name)
+        )
+  "")
+
+(defun iso/to (f x)
+  "Apply F to X's to."
+  (->> x
+       iso-to))
+
+(->> (iso/new "william" :to 'symbol)
+     (iso/as-to #'symbol-name)
+     )
+
+(cl-defun iso/new (x &key to)
+  "Create a new isomorphism of X mapping to TO."
+  (let ((from (iso/classify x)))
+    (prelude/assert (iso/exists? to from))
+    (make-iso :from from
+              :to to
+              :x x)))
+
+(macros/comment
+ (iso/new "william" :to 'symbol)
+ (iso/new '(one . two) :to 'tuple))
+
+(provide 'iso)
+;;; iso.el ends here