Skip to content

Instantly share code, notes, and snippets.

@jeaye
Created June 25, 2017 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeaye/bece34943b4508c789bde770ed168a56 to your computer and use it in GitHub Desktop.
Save jeaye/bece34943b4508c789bde770ed168a56 to your computer and use it in GitHub Desktop.
(ns logic.playground
(:require [clojure.core.logic :as l]))
(def types [:int :float :string])
(def fns {:foo [{:args [:int :float]
:ret :string}
{:args [:float :int]
:ret :string}
{:args [:string]
:ret :string}]
:bar [{:args []
:ret :int}]})
(def definition {:args {:x :int
:y :float
:s :string}
:body [{:fn :foo
:args [:x :y]}
{:fn :bar
:args []}]})
(defn typeo [lvar]
(l/membero lvar types))
(defn valid-callo [defin allowed-fns call]
(let [overloads (-> call :fn fns)
arg-types (map (:args defin) (:args call))]
; Each call argument is of a valid type.
(l/everyg typeo arg-types)
; Each call matches an overload.
(l/membero arg-types (map :args overloads))))
(defn check-definition [defin allowed-fns]
(l/run*
[r]
; Each argument is of a valid type.
(l/everyg typeo (-> defin :args vals))
; All of the calls in the body are valid.
(l/everyg #(valid-callo defin allowed-fns %) (:body defin))
; Good
(l/== r true)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment