From 91e62c04f61893231348ded80305bf69551948ef Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 13 Apr 2024 20:15:39 +0200 Subject: [PATCH] debug utils --- lib/term.ml | 27 +++++++++++++++++++++++++++ lib/term.mli | 3 +++ lib/type.ml | 7 +++++++ lib/type.mli | 3 +++ lib/typeSubstitution.ml | 14 ++++++++++++++ lib/typeSubstitution.mli | 1 + 6 files changed, 55 insertions(+) diff --git a/lib/term.ml b/lib/term.ml index cf9f357..536c4ae 100644 --- a/lib/term.ml +++ b/lib/term.ml @@ -19,3 +19,30 @@ type t = | Fun of Identifier.t * t | App of t * t [@@deriving eq, ord, show] + +let rec string_of_term = function + | Var v -> "Var '" ^ v ^ "'" + | IntConst n -> "IntConst(" ^ string_of_int n ^ ")" + | Binop (a, b, c) -> + "Binop (" + ^ string_of_term a + ^ ", " + ^ (match b with + | Plus -> "+" + | Minus -> "-" + | Times -> "*" + | Div -> "/") + ^ string_of_term c + ^ ")" + | Pair (a, b) -> "Pair (" ^ string_of_term a ^ ", " ^ string_of_term b ^ ")" + | Proj (a, b) -> + "Proj (" + ^ (match a with + | First -> "fst" + | Second -> "snd") + ^ ", " + ^ string_of_term b + ^ ")" + | Fun (a, b) -> "Fun ('" ^ a ^ "', " ^ string_of_term b ^ ")" + | App (a, b) -> "App (" ^ string_of_term a ^ ", " ^ string_of_term b ^ ")" +;; diff --git a/lib/term.mli b/lib/term.mli index 8fad451..31df53b 100644 --- a/lib/term.mli +++ b/lib/term.mli @@ -26,3 +26,6 @@ type t = | Fun of Identifier.t * t | App of t * t [@@deriving eq, ord, show] + +(* Term to string *) +val string_of_term : t -> string diff --git a/lib/type.ml b/lib/type.ml index c3adb3d..acac45e 100644 --- a/lib/type.ml +++ b/lib/type.ml @@ -4,3 +4,10 @@ type t = | Product of t * t | Arrow of t * t [@@deriving eq, ord, show] + +let rec string_of_type = function + | Var v -> "Var '" ^ v ^ "'" + | Int -> "Int" + | Product (a, b) -> "Product(" ^ string_of_type a ^ ", " ^ string_of_type b ^ ")" + | Arrow (a, b) -> "Arrow(" ^ string_of_type a ^ ", " ^ string_of_type b ^ ")" +;; diff --git a/lib/type.mli b/lib/type.mli index c3adb3d..ba78485 100644 --- a/lib/type.mli +++ b/lib/type.mli @@ -4,3 +4,6 @@ type t = | Product of t * t | Arrow of t * t [@@deriving eq, ord, show] + +(* Type to string *) +val string_of_type : t -> string diff --git a/lib/typeSubstitution.ml b/lib/typeSubstitution.ml index 6d07155..f35a573 100644 --- a/lib/typeSubstitution.ml +++ b/lib/typeSubstitution.ml @@ -38,4 +38,18 @@ let compose s2 s1 = s2 ;; +let to_string map = + let rec ty_str = function + | Type.Var s -> "Var('" ^ s ^ "')" + | Type.Int -> "Int" + | Type.Product (a, b) -> "Product(" ^ ty_str a ^ ", " ^ ty_str b ^ ")" + | Type.Arrow (a, b) -> "Arrow(" ^ ty_str a ^ ", " ^ ty_str b ^ ")" + in + "{" + ^ (IdentifierMap.bindings map + |> List.map (fun (id, ty) -> Printf.sprintf "'%s' typed as %s" id (ty_str ty)) + |> String.concat "\n") + ^ "}" +;; + let find = IdentifierMap.find_opt diff --git a/lib/typeSubstitution.mli b/lib/typeSubstitution.mli index 7a7154f..f7d437e 100644 --- a/lib/typeSubstitution.mli +++ b/lib/typeSubstitution.mli @@ -7,3 +7,4 @@ val compose : t -> t -> t val empty : t val singleton : Identifier.t -> Type.t -> t val find : Identifier.t -> t -> Type.t option +val to_string : t -> string