Last active
October 1, 2024 16:47
-
-
Save hirrolot/cf823202155a5d552b67d4e2c168ee42 to your computer and use it in GitHub Desktop.
Nondeterministic substitution by matrix transposition
This file contains hidden or 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
type term = Var of string | Call of string * term array | |
[@@deriving show { with_path = false }] | |
type possibilities = term array [@@deriving show] | |
let demux ~(x : string) ~(values : term array) : term -> possibilities = | |
let rec go = function | |
| Var y when x = y -> values | |
| Var _ as t -> Array.map (fun _ -> t) values | |
| Call (op, args) -> | |
let matrix = | |
Array.( | |
init (length values) (fun _i -> | |
Call (op, make (length args) (Var "?")))) | |
in | |
(* Transpose the nondeterministic matrix. *) | |
for i = 0 to Array.length args - 1 do | |
let values = go args.(i) in | |
for j = 0 to Array.length values - 1 do | |
let[@warning "-8"] (Call (_op, args)) = matrix.(j) in | |
args.(i) <- values.(j) | |
done | |
done; | |
matrix | |
in | |
go | |
(* [|(Call ("f", [|(Var "a"); (Call ("g", [|(Var "y"); (Var "z")|]))|])); | |
(Call ("f", [|(Var "b"); (Call ("g", [|(Var "y"); (Var "z")|]))|])); | |
(Call ("f", [|(Var "c"); (Call ("g", [|(Var "y"); (Var "z")|]))|]))|] *) | |
let () = | |
let t = Call ("f", [| Var "x"; Call ("g", [| Var "y"; Var "z" |]) |]) in | |
t | |
|> demux ~x:"x" ~values:[| Var "a"; Var "b"; Var "c" |] | |
|> show_possibilities |> print_endline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment