Skip to content

Commit

Permalink
qualified names
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Sep 13, 2024
1 parent c53cac6 commit dc34ae7
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/Juvix/Compiler/Backend/Isabelle/Translation/FromTyped.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ goModule onlyTypes infoTable Internal.Module {..} =
where
toIsabelleTheoryName :: Text -> Text
toIsabelleTheoryName name =
Text.intercalate "_" $ filter (/= "") $ T.splitOn "." name
quote . Text.intercalate "_" $ filter (/= "") $ T.splitOn "." name

isTypeDef :: Statement -> Bool
isTypeDef = \case
Expand Down Expand Up @@ -1219,8 +1219,25 @@ goModule onlyTypes infoTable Internal.Module {..} =
++ map (^. Internal.axiomInfoDef . Internal.axiomName . namePretty) (HashMap.elems (infoTable ^. Internal.infoAxioms))

quote :: Text -> Text
quote = quote' . Text.filter isLatin1 . Text.filter (isLetter .||. isDigit .||. (== '_') .||. (== '\''))
quote txt0
| Text.elem '.' txt0 = moduleName' <> "." <> idenName'
| otherwise = quote'' txt0
where
(moduleName, idenName) = Text.breakOnEnd "." txt0
moduleName' = toIsabelleTheoryName (removeLastDot moduleName)
idenName' = quote'' idenName

removeLastDot :: Text -> Text
removeLastDot txt
| Text.last txt == '.' = Text.init txt
| otherwise = txt

quote'' :: Text -> Text
quote'' =
quote'
. Text.filter isLatin1
. Text.filter (isLetter .||. isDigit .||. (== '_') .||. (== '\''))

quote' :: Text -> Text
quote' txt
| HashSet.member txt reservedNames = quote' (prime txt)
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Parser/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ instance ToGenericError StdinOrFileError where
genericError StdinOrFileError =
return
GenericError
{ _genericErrorLoc = singletonInterval (mkInitialLoc formatStdinPath),
{ _genericErrorLoc = singletonInterval (mkInitialLoc noFile),
_genericErrorMessage = prettyError "Neither JUVIX_FILE_OR_PROJECT nor --stdin option is chosen",
_genericErrorIntervals = []
}
Expand Down
6 changes: 6 additions & 0 deletions tests/positive/Isabelle/M/Main.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module M.Main;

import Stdlib.Prelude open;
import M.N open;

f (a : Nat) : Nat := g a;
5 changes: 5 additions & 0 deletions tests/positive/Isabelle/M/N.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module M.N;

import Stdlib.Prelude open;

g (a : Nat) : Nat := a + 1;
5 changes: 5 additions & 0 deletions tests/positive/Isabelle/N.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module N;

import Stdlib.Prelude open;

g (a : Nat) : Nat := a * 2;
5 changes: 4 additions & 1 deletion tests/positive/Isabelle/Program.juvix
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Program;

import Stdlib.Prelude open;
import N;
import M.N;
import M.Main as Main;

id0 : Nat -> Nat := id;

Expand All @@ -23,7 +26,7 @@ f (x y : Nat) : Nat -> Nat

g (x y : Nat) : Bool :=
if
| x == y := false
| x == f x (N.g y) (M.N.g (Main.f y)) := false
| else := true;

inc (x : Nat) : Nat := suc x;
Expand Down
9 changes: 9 additions & 0 deletions tests/positive/Isabelle/isabelle/M_Main.thy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
theory M_Main
imports Main
M_N
begin

fun f :: "nat \<Rightarrow> nat" where
"f a = (g a)"

end
8 changes: 8 additions & 0 deletions tests/positive/Isabelle/isabelle/M_N.thy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
theory M_N
imports Main
begin

fun g :: "nat \<Rightarrow> nat" where
"g a = (a + 1)"

end
8 changes: 8 additions & 0 deletions tests/positive/Isabelle/isabelle/N.thy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
theory N
imports Main
begin

fun g :: "nat \<Rightarrow> nat" where
"g a = (a * 2)"

end
5 changes: 4 additions & 1 deletion tests/positive/Isabelle/isabelle/Program.thy
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
theory Program
imports Main
N
M_N
M_Main
begin

definition id0 :: "nat \<Rightarrow> nat" where
Expand All @@ -25,7 +28,7 @@ fun f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat" where
"f x y z = ((z + 1) * x + y)"

fun g :: "nat \<Rightarrow> nat \<Rightarrow> bool" where
"g x y = (if x = y then False else True)"
"g x y = (if x = f x (N.g y) (M_N.g (M_Main.f y)) then False else True)"

fun inc :: "nat \<Rightarrow> nat" where
"inc x = (Suc x)"
Expand Down

0 comments on commit dc34ae7

Please sign in to comment.