Skip to content

Commit

Permalink
[compiler_data] update pretty printer of type_assignment
Browse files Browse the repository at this point in the history
- add pretty_raw allowing to print raw types, i.e. types with modes
  and functionality
  • Loading branch information
FissoreD committed Dec 16, 2024
1 parent 582810a commit 54952f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
19 changes: 15 additions & 4 deletions src/compiler/compiler_data.ml
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ module TypeAssignment = struct
| x -> x

open Format
let pretty f fmt tm =
let pretty ?(is_raw=false) f fmt tm =

let arrs = 0 in
let app = 1 in
Expand All @@ -348,14 +348,18 @@ module TypeAssignment = struct
| App _ -> app
| _ -> 2 in

let show_mode fmt m =
if is_raw then (Format.fprintf fmt "%a:" Mode.pretty m) else Format.fprintf fmt ""
in

let rec pretty fmt = function
| Prop Relation -> fprintf fmt "prop"
| Prop Function -> fprintf fmt "func"
| Prop Function -> fprintf fmt "%s" (if is_raw then "func" else "prop")
| Any -> fprintf fmt "any"
| Cons c -> F.pp fmt c
| App(f,x,xs) -> fprintf fmt "@[<hov 2>%a@ %a@]" F.pp f (Util.pplist (pretty_parens ~lvl:app) " ") (x::xs)
| Arr(m,NotVariadic,s,t) -> fprintf fmt "@[<hov 2>(*%a:*)%a ->@ %a@]" Mode.pretty m (pretty_parens ~lvl:arrs) s pretty t
| Arr(m,Variadic,s,t) -> fprintf fmt "(*%a:*)%a ..-> %a" Mode.pretty m (pretty_parens ~lvl:arrs) s pretty t
| Arr(m,NotVariadic,s,t) -> fprintf fmt "@[<hov 2>%a%a ->@ %a@]" show_mode m (pretty_parens ~lvl:arrs) s pretty t
| Arr(m,Variadic,s,t) -> fprintf fmt "%a%a ..-> %a" show_mode m (pretty_parens ~lvl:arrs) s pretty t
| UVar m -> f fmt pretty m
(* | UVar m -> MutableOnce.pretty fmt m *)
and pretty_parens ~lvl fmt = function
Expand All @@ -366,9 +370,16 @@ module TypeAssignment = struct
in
pretty fmt tm

let pretty_raw fmt = pretty ~is_raw:true fmt
let pretty fmt = pretty ~is_raw:false fmt


let pretty_mut_once =
pretty (fun fmt f t -> if MutableOnce.is_set t then f fmt (deref t) else MutableOnce.pretty fmt t)

let pretty_mut_once_raw =
pretty_raw (fun fmt f t -> if MutableOnce.is_set t then f fmt (deref t) else MutableOnce.pretty fmt t)

let pretty_ft =
pretty (fun fmt _ (t:F.t) -> F.pp fmt t)

Expand Down
19 changes: 5 additions & 14 deletions src/compiler/test_compiler_data.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
let pp_ta t s =
let open Elpi_compiler.Compiler_data in
let s' = Format.asprintf "@[%a@]" TypeAssignment.pretty_mut_once t in
let s' = Format.asprintf "@[%a@]" TypeAssignment.pretty_mut_once_raw t in
if s <> s' then begin
Format.eprintf "Unexpected print: %a\nactual: %a\nreference: %s\n"
TypeAssignment.pp (Val t) TypeAssignment.pretty_mut_once t s;
TypeAssignment.pp (Val t) TypeAssignment.pretty_mut_once_raw t s;
exit 1
end
;;
Expand All @@ -27,24 +27,15 @@ let list x = (App(F.from_string "list",x,[]))
let int = Cons (F.from_string "int")
let arr s t = Arr(Output,NotVariadic,s,t)

(* let () = pp_ta (Prop Relation) "prop";;
let () = pp_ta (Prop Function) "fprop";;
let () = pp_ta (Prop Relation) "prop";;
let () = pp_ta (Prop Function) "func";;
let () = pp_ta (list int) "list int";;
let () = pp_ta (list (list int)) "list (list int)";;
let () = pp_ta (arr (list int) int) "o:list int -> int";;
let () = pp_ta (arr (arr int int) int) "o:(o:int -> int) -> int";;
let () = pp_ta (arr int (arr int int)) "o:int -> o:int -> int";;
let () = pp_ta (arr int (arr (list int) int)) "o:int -> o:list int -> int";;
let () = pp_ta (list (arr int int)) "list (o:int -> int)";; *)
let () = pp_ta (Prop Relation) "prop";;
let () = pp_ta (Prop Function) "prop";;
let () = pp_ta (list int) "list int";;
let () = pp_ta (list (list int)) "list (list int)";;
let () = pp_ta (arr (list int) int) "list int -> int";;
let () = pp_ta (arr (arr int int) int) "(int -> int) -> int";;
let () = pp_ta (arr int (arr int int)) "int -> int -> int";;
let () = pp_ta (arr int (arr (list int) int)) "int -> list int -> int";;
let () = pp_ta (list (arr int int)) "list (int -> int)";;
let () = pp_ta (list (arr int int)) "list (o:int -> int)";;

open ScopedTerm

Expand Down

0 comments on commit 54952f0

Please sign in to comment.